| 1 | /* |
| 2 | * multipoll.c |
| 3 | * |
| 4 | * Copyright (C) 2010 - Pierre-Marc Fournier (pierre-marc dot fournier at polymtl dot ca) |
| 5 | * |
| 6 | * This library is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU Lesser General Public |
| 8 | * License as published by the Free Software Foundation; either |
| 9 | * version 2.1 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * This library is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | * Lesser General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU Lesser General Public |
| 17 | * License along with this library; if not, write to the Free Software |
| 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 19 | */ |
| 20 | |
| 21 | #include <poll.h> |
| 22 | #include <stdlib.h> |
| 23 | #include "multipoll.h" |
| 24 | #include "usterr.h" |
| 25 | |
| 26 | #define INITIAL_N_AVAIL 16 |
| 27 | |
| 28 | int multipoll_init(struct mpentries *ent) |
| 29 | { |
| 30 | ent->n_used = 0; |
| 31 | ent->n_avail = INITIAL_N_AVAIL; |
| 32 | |
| 33 | ent->pollfds = (struct pollfd *) malloc(sizeof(struct pollfd) * 16); |
| 34 | ent->extras = (struct pollfd_extra *) malloc(sizeof(struct pollfd_extra) * 16); |
| 35 | |
| 36 | return 0; |
| 37 | } |
| 38 | |
| 39 | int multipoll_destroy(struct mpentries *ent) |
| 40 | { |
| 41 | int i; |
| 42 | |
| 43 | for(i=0; i<ent->n_used; i++) { |
| 44 | if(ent->extras[i].destroy_priv) { |
| 45 | ent->extras[i].destroy_priv(ent->extras[i].priv); |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | free(ent->pollfds); |
| 50 | free(ent->extras); |
| 51 | |
| 52 | return 0; |
| 53 | } |
| 54 | |
| 55 | int multipoll_add(struct mpentries *ent, int fd, short events, int (*func)(void *priv, int fd, short events), void *priv, int (*destroy_priv)(void *)) |
| 56 | { |
| 57 | int cur; |
| 58 | |
| 59 | if(ent->n_used == ent->n_avail) { |
| 60 | ent->n_avail *= 2; |
| 61 | ent->pollfds = (struct pollfd *) realloc(ent->pollfds, sizeof(struct pollfd) * ent->n_avail); |
| 62 | ent->extras = (struct pollfd_extra *) realloc(ent->extras, sizeof(struct pollfd_extra) * ent->n_avail); |
| 63 | } |
| 64 | |
| 65 | cur = ent->n_used; |
| 66 | ent->n_used++; |
| 67 | |
| 68 | ent->pollfds[cur].fd = fd; |
| 69 | ent->pollfds[cur].events = events; |
| 70 | ent->extras[cur].func = func; |
| 71 | ent->extras[cur].priv = priv; |
| 72 | ent->extras[cur].destroy_priv = destroy_priv; |
| 73 | |
| 74 | return 0; |
| 75 | } |
| 76 | |
| 77 | int multipoll_poll(struct mpentries *ent, int timeout) |
| 78 | { |
| 79 | int result; |
| 80 | int i; |
| 81 | |
| 82 | result = poll(ent->pollfds, ent->n_used, timeout); |
| 83 | if(result == -1) { |
| 84 | PERROR("poll"); |
| 85 | return -1; |
| 86 | } |
| 87 | |
| 88 | for(i=0; i<ent->n_used; i++) { |
| 89 | if(ent->pollfds[i].revents) { |
| 90 | ent->extras[i].func(ent->extras[i].priv, ent->pollfds[i].fd, ent->pollfds[i].revents); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | return 0; |
| 95 | } |