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 "fdutil.h" // catta_set_nonblock
37 #include "internal.h" // closesocket
40 CattaSimplePoll *simple_poll;
46 CattaWatchCallback callback;
49 CATTA_LLIST_FIELDS(CattaWatch, watches);
53 CattaSimplePoll *simple_poll;
57 struct timeval expiry;
59 CattaTimeoutCallback callback;
62 CATTA_LLIST_FIELDS(CattaTimeout, timeouts);
65 struct CattaSimplePoll {
67 CattaPollFunc poll_func;
68 void *poll_func_userdata;
70 struct pollfd* pollfds;
71 int n_pollfds, max_pollfds, rebuild_pollfds;
73 int watch_req_cleanup, timeout_req_cleanup;
78 CATTA_LLIST_HEAD(CattaWatch, watches);
79 CATTA_LLIST_HEAD(CattaTimeout, timeouts);
99 void catta_simple_poll_wakeup(CattaSimplePoll *s) {
103 (void)writepipe(s->wakeup_pipe[1], &c, sizeof(c));
104 s->wakeup_issued = 1;
107 static void clear_wakeup(CattaSimplePoll *s) {
108 char c[10]; /* Read ten at a time */
110 if (!s->wakeup_issued)
113 s->wakeup_issued = 0;
115 printf("(clear wake-up"); fflush(stdout); // XXX
118 ioctl(s->wakeup_pipe[0], FIONREAD, &n);
119 printf(" %d", n); fflush(stdout); // XXX
120 if (readpipe(s->wakeup_pipe[0], c, sizeof(c)) != sizeof(c))
123 printf(")\n"); // XXX
126 static CattaWatch* watch_new(const CattaPoll *api, int fd, CattaWatchEvent event, CattaWatchCallback callback, void *userdata) {
137 if (!(w = catta_new(CattaWatch, 1)))
140 /* If there is a background thread running the poll() for us, tell it to exit the poll() */
141 catta_simple_poll_wakeup(s);
147 w->pollfd.events = event;
148 w->pollfd.revents = 0;
150 w->callback = callback;
151 w->userdata = userdata;
154 s->rebuild_pollfds = 1;
156 CATTA_LLIST_PREPEND(CattaWatch, watches, s->watches, w);
162 static void watch_update(CattaWatch *w, CattaWatchEvent events) {
166 /* If there is a background thread running the poll() for us, tell it to exit the poll() */
167 catta_simple_poll_wakeup(w->simple_poll);
169 w->pollfd.events = events;
172 assert(w->simple_poll);
173 w->simple_poll->pollfds[w->idx] = w->pollfd;
175 w->simple_poll->rebuild_pollfds = 1;
178 static CattaWatchEvent watch_get_events(CattaWatch *w) {
182 if (w->idx != -1 && w->simple_poll->events_valid)
183 return w->simple_poll->pollfds[w->idx].revents;
188 static void remove_pollfd(CattaWatch *w) {
194 w->simple_poll->rebuild_pollfds = 1;
197 static void watch_free(CattaWatch *w) {
202 /* If there is a background thread running the poll() for us, tell it to exit the poll() */
203 catta_simple_poll_wakeup(w->simple_poll);
208 w->simple_poll->n_watches --;
209 w->simple_poll->watch_req_cleanup = 1;
212 static void destroy_watch(CattaWatch *w) {
216 CATTA_LLIST_REMOVE(CattaWatch, watches, w->simple_poll->watches, w);
219 w->simple_poll->n_watches --;
224 static void cleanup_watches(CattaSimplePoll *s, int all) {
225 CattaWatch *w, *next;
228 for (w = s->watches; w; w = next) {
229 next = w->watches_next;
235 s->timeout_req_cleanup = 0;
238 static CattaTimeout* timeout_new(const CattaPoll *api, const struct timeval *tv, CattaTimeoutCallback callback, void *userdata) {
248 if (!(t = catta_new(CattaTimeout, 1)))
251 /* If there is a background thread running the poll() for us, tell it to exit the poll() */
252 catta_simple_poll_wakeup(s);
257 if ((t->enabled = !!tv))
260 t->callback = callback;
261 t->userdata = userdata;
263 CATTA_LLIST_PREPEND(CattaTimeout, timeouts, s->timeouts, t);
267 static void timeout_update(CattaTimeout *t, const struct timeval *tv) {
271 /* If there is a background thread running the poll() for us, tell it to exit the poll() */
272 catta_simple_poll_wakeup(t->simple_poll);
274 if ((t->enabled = !!tv))
278 static void timeout_free(CattaTimeout *t) {
282 /* If there is a background thread running the poll() for us, tell it to exit the poll() */
283 catta_simple_poll_wakeup(t->simple_poll);
286 t->simple_poll->timeout_req_cleanup = 1;
290 static void destroy_timeout(CattaTimeout *t) {
293 CATTA_LLIST_REMOVE(CattaTimeout, timeouts, t->simple_poll->timeouts, t);
298 static void cleanup_timeouts(CattaSimplePoll *s, int all) {
299 CattaTimeout *t, *next;
302 for (t = s->timeouts; t; t = next) {
303 next = t->timeouts_next;
309 s->timeout_req_cleanup = 0;
312 CattaSimplePoll *catta_simple_poll_new(void) {
315 if (!(s = catta_new(CattaSimplePoll, 1)))
318 winsock_init(); // on Windows, pipe uses sockets; no-op on other platforms
319 if (pipe(s->wakeup_pipe) < 0) {
325 catta_set_nonblock(s->wakeup_pipe[0]);
326 catta_set_nonblock(s->wakeup_pipe[1]);
330 s->api.watch_new = watch_new;
331 s->api.watch_free = watch_free;
332 s->api.watch_update = watch_update;
333 s->api.watch_get_events = watch_get_events;
335 s->api.timeout_new = timeout_new;
336 s->api.timeout_free = timeout_free;
337 s->api.timeout_update = timeout_update;
340 s->max_pollfds = s->n_pollfds = 0;
341 s->rebuild_pollfds = 1;
346 s->watch_req_cleanup = 0;
347 s->timeout_req_cleanup = 0;
349 s->prepared_timeout = 0;
351 s->state = STATE_INIT;
353 s->wakeup_issued = 0;
355 catta_simple_poll_set_func(s, NULL, NULL);
357 CATTA_LLIST_HEAD_INIT(CattaWatch, s->watches);
358 CATTA_LLIST_HEAD_INIT(CattaTimeout, s->timeouts);
363 void catta_simple_poll_free(CattaSimplePoll *s) {
366 cleanup_timeouts(s, 1);
367 cleanup_watches(s, 1);
368 assert(s->n_watches == 0);
370 catta_free(s->pollfds);
372 if (s->wakeup_pipe[0] >= 0)
373 closepipe(s->wakeup_pipe[0]);
375 if (s->wakeup_pipe[1] >= 0)
376 closepipe(s->wakeup_pipe[1]);
379 winsock_exit(); // match the winsock_init in catta_simple_poll_new
382 static int rebuild(CattaSimplePoll *s) {
388 if (s->n_watches+1 > s->max_pollfds) {
391 s->max_pollfds = s->n_watches + 10;
393 if (!(n = catta_realloc(s->pollfds, sizeof(struct pollfd) * s->max_pollfds)))
400 s->pollfds[0].fd = s->wakeup_pipe[0];
401 s->pollfds[0].events = POLLIN;
402 s->pollfds[0].revents = 0;
406 for (w = s->watches; w; w = w->watches_next) {
411 assert(w->idx < s->max_pollfds);
412 s->pollfds[w->idx = idx++] = w->pollfd;
417 s->rebuild_pollfds = 0;
422 static CattaTimeout* find_next_timeout(CattaSimplePoll *s) {
423 CattaTimeout *t, *n = NULL;
426 for (t = s->timeouts; t; t = t->timeouts_next) {
428 if (t->dead || !t->enabled)
431 if (!n || catta_timeval_compare(&t->expiry, &n->expiry) < 0)
438 static void timeout_callback(CattaTimeout *t) {
444 t->callback(t, t->userdata);
447 int catta_simple_poll_prepare(CattaSimplePoll *s, int timeout) {
448 CattaTimeout *next_timeout;
451 assert(s->state == STATE_INIT || s->state == STATE_DISPATCHED || s->state == STATE_FAILURE);
452 s->state = STATE_PREPARING;
454 /* Clear pending wakeup requests */
457 /* Cleanup things first */
458 if (s->watch_req_cleanup)
459 cleanup_watches(s, 0);
461 if (s->timeout_req_cleanup)
462 cleanup_timeouts(s, 0);
464 /* Check whether a quit was requested */
466 s->state = STATE_QUIT;
470 /* Do we need to rebuild our array of pollfds? */
471 if (s->rebuild_pollfds)
472 if (rebuild(s) < 0) {
473 s->state = STATE_FAILURE;
477 /* Calculate the wakeup time */
478 if ((next_timeout = find_next_timeout(s))) {
483 if (next_timeout->expiry.tv_sec == 0 &&
484 next_timeout->expiry.tv_usec == 0) {
486 /* Just a shortcut so that we don't need to call gettimeofday() */
491 gettimeofday(&now, NULL);
492 usec = catta_timeval_diff(&next_timeout->expiry, &now);
495 /* Timeout elapsed */
501 /* Calculate sleep time. We add 1ms because otherwise we'd
502 * wake up too early most of the time */
503 t = (int) (usec / 1000) + 1;
505 if (timeout < 0 || timeout > t)
510 s->prepared_timeout = timeout;
511 s->state = STATE_PREPARED;
515 int catta_simple_poll_run(CattaSimplePoll *s) {
517 assert(s->state == STATE_PREPARED || s->state == STATE_FAILURE);
519 s->state = STATE_RUNNING;
526 printf("(poll %d...", s->n_pollfds);
529 if (s->poll_func(s->pollfds, s->n_pollfds, s->prepared_timeout, s->poll_func_userdata) < 0) {
531 if (errno == EINTR) {
532 printf(" interrupted)\n"); // XXX
536 s->state = STATE_FAILURE;
537 printf(" FAIL)\n"); // XXX
544 /* The poll events are now valid again */
548 s->state = STATE_RAN;
552 int catta_simple_poll_dispatch(CattaSimplePoll *s) {
553 CattaTimeout *next_timeout;
557 assert(s->state == STATE_RAN);
558 s->state = STATE_DISPATCHING;
562 int i, nready=0, nwatches=0;
563 for(w=s->watches; w; w=w->watches_next)
565 for(i=0; i<s->n_pollfds; i++)
566 if(s->pollfds[i].revents)
568 if(nready > 0 && s->pollfds[i].revents)
570 printf(" %d ready, %d watches)\n", nready, nwatches);
573 /* We execute only one callback in every iteration */
575 /* Check whether the wakeup time has been reached now */
576 if ((next_timeout = find_next_timeout(s))) {
578 if (next_timeout->expiry.tv_sec == 0 && next_timeout->expiry.tv_usec == 0) {
580 /* Just a shortcut so that we don't need to call gettimeofday() */
581 timeout_callback(next_timeout);
585 if (catta_age(&next_timeout->expiry) >= 0) {
587 /* Timeout elapsed */
588 timeout_callback(next_timeout);
593 /* Look for some kind of I/O event */
594 for (w = s->watches; w; w = w->watches_next) {
600 assert(w->idx < s->n_pollfds);
602 if (s->pollfds[w->idx].revents != 0) {
603 w->callback(w, w->pollfd.fd, s->pollfds[w->idx].revents, w->userdata);
610 s->state = STATE_DISPATCHED;
614 int catta_simple_poll_iterate(CattaSimplePoll *s, int timeout) {
617 if ((r = catta_simple_poll_prepare(s, timeout)) != 0)
620 if ((r = catta_simple_poll_run(s)) != 0)
623 if ((r = catta_simple_poll_dispatch(s)) != 0)
629 void catta_simple_poll_quit(CattaSimplePoll *s) {
634 /* If there is a background thread running the poll() for us, tell it to exit the poll() */
635 catta_simple_poll_wakeup(s);
638 const CattaPoll* catta_simple_poll_get(CattaSimplePoll *s) {
644 static int system_poll(struct pollfd *ufds, unsigned int nfds, int timeout, CATTA_GCC_UNUSED void *userdata) {
645 return poll(ufds, nfds, timeout);
648 void catta_simple_poll_set_func(CattaSimplePoll *s, CattaPollFunc func, void *userdata) {
651 s->poll_func = func ? func : system_poll;
652 s->poll_func_userdata = func ? userdata : NULL;
654 /* If there is a background thread running the poll() for us, tell it to exit the poll() */
655 catta_simple_poll_wakeup(s);
658 int catta_simple_poll_loop(CattaSimplePoll *s) {
664 if ((r = catta_simple_poll_iterate(s, -1)) != 0)
665 if (r >= 0 || errno != EINTR)