2 This file is part of catta.
4 catta is free software; you can redistribute it and/or modify it
5 under the terms of the GNU Lesser General Public License as
6 published by the Free Software Foundation; either version 2.1 of the
7 License, or (at your option) any later version.
9 catta is distributed in the hope that it will be useful, but WITHOUT
10 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
12 Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with catta; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
32 #include <catta/llist.h>
33 #include <catta/malloc.h>
34 #include <catta/timeval.h>
35 #include <catta/simple-watch.h>
36 #include <catta/log.h>
37 #include "fdutil.h" // catta_set_nonblock
38 #include "internal.h" // closesocket
41 CattaSimplePoll *simple_poll;
47 CattaWatchCallback callback;
50 CATTA_LLIST_FIELDS(CattaWatch, watches);
54 CattaSimplePoll *simple_poll;
58 struct timeval expiry;
60 CattaTimeoutCallback callback;
63 CATTA_LLIST_FIELDS(CattaTimeout, timeouts);
66 struct CattaSimplePoll {
68 CattaPollFunc poll_func;
69 void *poll_func_userdata;
71 struct pollfd* pollfds;
72 int n_pollfds, max_pollfds, rebuild_pollfds;
74 int watch_req_cleanup, timeout_req_cleanup;
79 CATTA_LLIST_HEAD(CattaWatch, watches);
80 CATTA_LLIST_HEAD(CattaTimeout, timeouts);
100 void catta_simple_poll_wakeup(CattaSimplePoll *s) {
104 (void)writepipe(s->wakeup_pipe[1], &c, sizeof(c));
105 s->wakeup_issued = 1;
108 static void clear_wakeup(CattaSimplePoll *s) {
109 char c[10]; /* Read ten at a time */
111 if (!s->wakeup_issued)
114 s->wakeup_issued = 0;
116 printf("(clear wake-up"); fflush(stdout); // XXX
119 ioctl(s->wakeup_pipe[0], FIONREAD, &n);
120 printf(" %d", n); fflush(stdout); // XXX
121 if (readpipe(s->wakeup_pipe[0], c, sizeof(c)) != sizeof(c))
124 printf(")\n"); // XXX
127 static CattaWatch* watch_new(const CattaPoll *api, int fd, CattaWatchEvent event, CattaWatchCallback callback, void *userdata) {
138 if (!(w = catta_new(CattaWatch, 1)))
141 /* If there is a background thread running the poll() for us, tell it to exit the poll() */
142 catta_simple_poll_wakeup(s);
148 w->pollfd.events = event;
149 w->pollfd.revents = 0;
151 w->callback = callback;
152 w->userdata = userdata;
155 s->rebuild_pollfds = 1;
157 CATTA_LLIST_PREPEND(CattaWatch, watches, s->watches, w);
163 static void watch_update(CattaWatch *w, CattaWatchEvent events) {
167 /* If there is a background thread running the poll() for us, tell it to exit the poll() */
168 catta_simple_poll_wakeup(w->simple_poll);
170 w->pollfd.events = events;
173 assert(w->simple_poll);
174 w->simple_poll->pollfds[w->idx] = w->pollfd;
176 w->simple_poll->rebuild_pollfds = 1;
179 static CattaWatchEvent watch_get_events(CattaWatch *w) {
183 if (w->idx != -1 && w->simple_poll->events_valid)
184 return w->simple_poll->pollfds[w->idx].revents;
189 static void remove_pollfd(CattaWatch *w) {
195 w->simple_poll->rebuild_pollfds = 1;
198 static void watch_free(CattaWatch *w) {
203 /* If there is a background thread running the poll() for us, tell it to exit the poll() */
204 catta_simple_poll_wakeup(w->simple_poll);
209 w->simple_poll->n_watches --;
210 w->simple_poll->watch_req_cleanup = 1;
213 static void destroy_watch(CattaWatch *w) {
217 CATTA_LLIST_REMOVE(CattaWatch, watches, w->simple_poll->watches, w);
220 w->simple_poll->n_watches --;
225 static void cleanup_watches(CattaSimplePoll *s, int all) {
226 CattaWatch *w, *next;
229 for (w = s->watches; w; w = next) {
230 next = w->watches_next;
236 s->timeout_req_cleanup = 0;
239 static CattaTimeout* timeout_new(const CattaPoll *api, const struct timeval *tv, CattaTimeoutCallback callback, void *userdata) {
249 if (!(t = catta_new(CattaTimeout, 1)))
252 /* If there is a background thread running the poll() for us, tell it to exit the poll() */
253 catta_simple_poll_wakeup(s);
258 if ((t->enabled = !!tv))
261 t->callback = callback;
262 t->userdata = userdata;
264 CATTA_LLIST_PREPEND(CattaTimeout, timeouts, s->timeouts, t);
268 static void timeout_update(CattaTimeout *t, const struct timeval *tv) {
272 /* If there is a background thread running the poll() for us, tell it to exit the poll() */
273 catta_simple_poll_wakeup(t->simple_poll);
275 if ((t->enabled = !!tv))
279 static void timeout_free(CattaTimeout *t) {
283 /* If there is a background thread running the poll() for us, tell it to exit the poll() */
284 catta_simple_poll_wakeup(t->simple_poll);
287 t->simple_poll->timeout_req_cleanup = 1;
291 static void destroy_timeout(CattaTimeout *t) {
294 CATTA_LLIST_REMOVE(CattaTimeout, timeouts, t->simple_poll->timeouts, t);
299 static void cleanup_timeouts(CattaSimplePoll *s, int all) {
300 CattaTimeout *t, *next;
303 for (t = s->timeouts; t; t = next) {
304 next = t->timeouts_next;
310 s->timeout_req_cleanup = 0;
313 CattaSimplePoll *catta_simple_poll_new(void) {
316 if (!(s = catta_new(CattaSimplePoll, 1)))
319 winsock_init(); // on Windows, pipe uses sockets; no-op on other platforms
320 if (pipe(s->wakeup_pipe) < 0) {
321 catta_log_error(__FILE__": pipe() failed: %s", errnostrsocket());
325 if (catta_set_nonblock(s->wakeup_pipe[0]) < 0 ||
326 catta_set_nonblock(s->wakeup_pipe[1]) < 0)
328 catta_log_error(__FILE__": O_NONBLOCK failed: %s", errnostrsocket());
334 s->api.watch_new = watch_new;
335 s->api.watch_free = watch_free;
336 s->api.watch_update = watch_update;
337 s->api.watch_get_events = watch_get_events;
339 s->api.timeout_new = timeout_new;
340 s->api.timeout_free = timeout_free;
341 s->api.timeout_update = timeout_update;
344 s->max_pollfds = s->n_pollfds = 0;
345 s->rebuild_pollfds = 1;
350 s->watch_req_cleanup = 0;
351 s->timeout_req_cleanup = 0;
353 s->prepared_timeout = 0;
355 s->state = STATE_INIT;
357 s->wakeup_issued = 0;
359 catta_simple_poll_set_func(s, NULL, NULL);
361 CATTA_LLIST_HEAD_INIT(CattaWatch, s->watches);
362 CATTA_LLIST_HEAD_INIT(CattaTimeout, s->timeouts);
372 void catta_simple_poll_free(CattaSimplePoll *s) {
375 cleanup_timeouts(s, 1);
376 cleanup_watches(s, 1);
377 assert(s->n_watches == 0);
379 catta_free(s->pollfds);
381 if (s->wakeup_pipe[0] >= 0)
382 closepipe(s->wakeup_pipe[0]);
384 if (s->wakeup_pipe[1] >= 0)
385 closepipe(s->wakeup_pipe[1]);
388 winsock_exit(); // match the winsock_init in catta_simple_poll_new
391 static int rebuild(CattaSimplePoll *s) {
397 if (s->n_watches+1 > s->max_pollfds) {
400 s->max_pollfds = s->n_watches + 10;
402 if (!(n = catta_realloc(s->pollfds, sizeof(struct pollfd) * s->max_pollfds)))
409 s->pollfds[0].fd = s->wakeup_pipe[0];
410 s->pollfds[0].events = POLLIN;
411 s->pollfds[0].revents = 0;
415 for (w = s->watches; w; w = w->watches_next) {
420 assert(w->idx < s->max_pollfds);
421 s->pollfds[w->idx = idx++] = w->pollfd;
426 s->rebuild_pollfds = 0;
431 static CattaTimeout* find_next_timeout(CattaSimplePoll *s) {
432 CattaTimeout *t, *n = NULL;
435 for (t = s->timeouts; t; t = t->timeouts_next) {
437 if (t->dead || !t->enabled)
440 if (!n || catta_timeval_compare(&t->expiry, &n->expiry) < 0)
447 static void timeout_callback(CattaTimeout *t) {
453 t->callback(t, t->userdata);
456 int catta_simple_poll_prepare(CattaSimplePoll *s, int timeout) {
457 CattaTimeout *next_timeout;
460 assert(s->state == STATE_INIT || s->state == STATE_DISPATCHED || s->state == STATE_FAILURE);
461 s->state = STATE_PREPARING;
463 /* Clear pending wakeup requests */
466 /* Cleanup things first */
467 if (s->watch_req_cleanup)
468 cleanup_watches(s, 0);
470 if (s->timeout_req_cleanup)
471 cleanup_timeouts(s, 0);
473 /* Check whether a quit was requested */
475 s->state = STATE_QUIT;
479 /* Do we need to rebuild our array of pollfds? */
480 if (s->rebuild_pollfds)
481 if (rebuild(s) < 0) {
482 s->state = STATE_FAILURE;
486 /* Calculate the wakeup time */
487 if ((next_timeout = find_next_timeout(s))) {
492 if (next_timeout->expiry.tv_sec == 0 &&
493 next_timeout->expiry.tv_usec == 0) {
495 /* Just a shortcut so that we don't need to call gettimeofday() */
500 gettimeofday(&now, NULL);
501 usec = catta_timeval_diff(&next_timeout->expiry, &now);
504 /* Timeout elapsed */
510 /* Calculate sleep time. We add 1ms because otherwise we'd
511 * wake up too early most of the time */
512 t = (int) (usec / 1000) + 1;
514 if (timeout < 0 || timeout > t)
519 s->prepared_timeout = timeout;
520 s->state = STATE_PREPARED;
524 int catta_simple_poll_run(CattaSimplePoll *s) {
526 assert(s->state == STATE_PREPARED || s->state == STATE_FAILURE);
528 s->state = STATE_RUNNING;
535 printf("(poll %d...", s->n_pollfds);
538 if (s->poll_func(s->pollfds, s->n_pollfds, s->prepared_timeout, s->poll_func_userdata) < 0) {
540 if (errno == EINTR) {
541 printf(" interrupted)\n"); // XXX
545 s->state = STATE_FAILURE;
546 printf(" FAIL)\n"); // XXX
553 /* The poll events are now valid again */
557 s->state = STATE_RAN;
561 int catta_simple_poll_dispatch(CattaSimplePoll *s) {
562 CattaTimeout *next_timeout;
566 assert(s->state == STATE_RAN);
567 s->state = STATE_DISPATCHING;
571 int i, nready=0, nwatches=0;
572 for(w=s->watches; w; w=w->watches_next)
574 for(i=0; i<s->n_pollfds; i++)
575 if(s->pollfds[i].revents)
577 if(nready > 0 && s->pollfds[i].revents)
579 printf(" %d ready, %d watches)\n", nready, nwatches);
582 /* We execute only one callback in every iteration */
584 /* Check whether the wakeup time has been reached now */
585 if ((next_timeout = find_next_timeout(s))) {
587 if (next_timeout->expiry.tv_sec == 0 && next_timeout->expiry.tv_usec == 0) {
589 /* Just a shortcut so that we don't need to call gettimeofday() */
590 timeout_callback(next_timeout);
594 if (catta_age(&next_timeout->expiry) >= 0) {
596 /* Timeout elapsed */
597 timeout_callback(next_timeout);
602 /* Look for some kind of I/O event */
603 for (w = s->watches; w; w = w->watches_next) {
609 assert(w->idx < s->n_pollfds);
611 if (s->pollfds[w->idx].revents != 0) {
612 w->callback(w, w->pollfd.fd, s->pollfds[w->idx].revents, w->userdata);
619 s->state = STATE_DISPATCHED;
623 int catta_simple_poll_iterate(CattaSimplePoll *s, int timeout) {
626 if ((r = catta_simple_poll_prepare(s, timeout)) != 0)
629 if ((r = catta_simple_poll_run(s)) != 0)
632 if ((r = catta_simple_poll_dispatch(s)) != 0)
638 void catta_simple_poll_quit(CattaSimplePoll *s) {
643 /* If there is a background thread running the poll() for us, tell it to exit the poll() */
644 catta_simple_poll_wakeup(s);
647 const CattaPoll* catta_simple_poll_get(CattaSimplePoll *s) {
653 static int system_poll(struct pollfd *ufds, unsigned int nfds, int timeout, CATTA_GCC_UNUSED void *userdata) {
654 return poll(ufds, nfds, timeout);
657 void catta_simple_poll_set_func(CattaSimplePoll *s, CattaPollFunc func, void *userdata) {
660 s->poll_func = func ? func : system_poll;
661 s->poll_func_userdata = func ? userdata : NULL;
663 /* If there is a background thread running the poll() for us, tell it to exit the poll() */
664 catta_simple_poll_wakeup(s);
667 int catta_simple_poll_loop(CattaSimplePoll *s) {
673 if ((r = catta_simple_poll_iterate(s, -1)) != 0)
674 if (r >= 0 || errno != EINTR)