6 #include <talloc/talloc.h>
17 static struct waiter *waiters;
20 struct waiter *waiter_register(int fd, int events,
21 waiter_cb callback, void *arg)
23 struct waiter *waiter;
27 waiters = talloc_realloc(NULL, waiters, struct waiter, n_waiters);
28 waiter = &waiters[n_waiters - 1];
31 waiter->events = events;
32 waiter->callback = callback;
38 void waiter_remove(struct waiter *waiter)
43 assert(i >= 0 && i < n_waiters);
46 memmove(&waiters[i], &waiters[i+1],
47 (n_waiters - i) * sizeof(waiters[0]));
49 waiters = talloc_realloc(NULL, waiters, struct waiter, n_waiters);
54 static struct pollfd *pollfds;
58 if (n_waiters != n_pollfds) {
59 pollfds = talloc_realloc(NULL, pollfds,
60 struct pollfd, n_waiters);
61 n_pollfds = n_waiters;
64 for (i = 0; i < n_waiters; i++) {
65 pollfds[i].fd = waiters[i].fd;
66 pollfds[i].events = waiters[i].events;
67 pollfds[i].revents = 0;
70 rc = poll(pollfds, n_waiters, -1);
75 for (i = 0; i < n_waiters; i++) {
76 if (pollfds[i].revents) {
77 rc = waiters[i].callback(waiters[i].arg);
80 waiter_remove(&waiters[i]);