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;
117 if (readpipe(s->wakeup_pipe[0], c, sizeof(c)) != sizeof(c))
122 static CattaWatch* watch_new(const CattaPoll *api, int fd, CattaWatchEvent event, CattaWatchCallback callback, void *userdata) {
133 if (!(w = catta_new(CattaWatch, 1)))
136 /* If there is a background thread running the poll() for us, tell it to exit the poll() */
137 catta_simple_poll_wakeup(s);
143 w->pollfd.events = event;
144 w->pollfd.revents = 0;
146 w->callback = callback;
147 w->userdata = userdata;
150 s->rebuild_pollfds = 1;
152 CATTA_LLIST_PREPEND(CattaWatch, watches, s->watches, w);
158 static void watch_update(CattaWatch *w, CattaWatchEvent events) {
162 /* If there is a background thread running the poll() for us, tell it to exit the poll() */
163 catta_simple_poll_wakeup(w->simple_poll);
165 w->pollfd.events = events;
168 assert(w->simple_poll);
169 w->simple_poll->pollfds[w->idx] = w->pollfd;
171 w->simple_poll->rebuild_pollfds = 1;
174 static CattaWatchEvent watch_get_events(CattaWatch *w) {
178 if (w->idx != -1 && w->simple_poll->events_valid)
179 return w->simple_poll->pollfds[w->idx].revents;
184 static void remove_pollfd(CattaWatch *w) {
190 w->simple_poll->rebuild_pollfds = 1;
193 static void watch_free(CattaWatch *w) {
198 /* If there is a background thread running the poll() for us, tell it to exit the poll() */
199 catta_simple_poll_wakeup(w->simple_poll);
204 w->simple_poll->n_watches --;
205 w->simple_poll->watch_req_cleanup = 1;
208 static void destroy_watch(CattaWatch *w) {
212 CATTA_LLIST_REMOVE(CattaWatch, watches, w->simple_poll->watches, w);
215 w->simple_poll->n_watches --;
220 static void cleanup_watches(CattaSimplePoll *s, int all) {
221 CattaWatch *w, *next;
224 for (w = s->watches; w; w = next) {
225 next = w->watches_next;
231 s->timeout_req_cleanup = 0;
234 static CattaTimeout* timeout_new(const CattaPoll *api, const struct timeval *tv, CattaTimeoutCallback callback, void *userdata) {
244 if (!(t = catta_new(CattaTimeout, 1)))
247 /* If there is a background thread running the poll() for us, tell it to exit the poll() */
248 catta_simple_poll_wakeup(s);
253 if ((t->enabled = !!tv))
256 t->callback = callback;
257 t->userdata = userdata;
259 CATTA_LLIST_PREPEND(CattaTimeout, timeouts, s->timeouts, t);
263 static void timeout_update(CattaTimeout *t, const struct timeval *tv) {
267 /* If there is a background thread running the poll() for us, tell it to exit the poll() */
268 catta_simple_poll_wakeup(t->simple_poll);
270 if ((t->enabled = !!tv))
274 static void timeout_free(CattaTimeout *t) {
278 /* If there is a background thread running the poll() for us, tell it to exit the poll() */
279 catta_simple_poll_wakeup(t->simple_poll);
282 t->simple_poll->timeout_req_cleanup = 1;
286 static void destroy_timeout(CattaTimeout *t) {
289 CATTA_LLIST_REMOVE(CattaTimeout, timeouts, t->simple_poll->timeouts, t);
294 static void cleanup_timeouts(CattaSimplePoll *s, int all) {
295 CattaTimeout *t, *next;
298 for (t = s->timeouts; t; t = next) {
299 next = t->timeouts_next;
305 s->timeout_req_cleanup = 0;
308 CattaSimplePoll *catta_simple_poll_new(void) {
311 if (!(s = catta_new(CattaSimplePoll, 1)))
314 winsock_init(); // on Windows, pipe uses sockets; no-op on other platforms
315 if (pipe(s->wakeup_pipe) < 0) {
316 catta_log_error(__FILE__": pipe() failed: %s", errnostrsocket());
320 if (catta_set_nonblock(s->wakeup_pipe[0]) < 0 ||
321 catta_set_nonblock(s->wakeup_pipe[1]) < 0)
323 catta_log_error(__FILE__": O_NONBLOCK failed: %s", errnostrsocket());
329 s->api.watch_new = watch_new;
330 s->api.watch_free = watch_free;
331 s->api.watch_update = watch_update;
332 s->api.watch_get_events = watch_get_events;
334 s->api.timeout_new = timeout_new;
335 s->api.timeout_free = timeout_free;
336 s->api.timeout_update = timeout_update;
339 s->max_pollfds = s->n_pollfds = 0;
340 s->rebuild_pollfds = 1;
345 s->watch_req_cleanup = 0;
346 s->timeout_req_cleanup = 0;
348 s->prepared_timeout = 0;
350 s->state = STATE_INIT;
352 s->wakeup_issued = 0;
354 catta_simple_poll_set_func(s, NULL, NULL);
356 CATTA_LLIST_HEAD_INIT(CattaWatch, s->watches);
357 CATTA_LLIST_HEAD_INIT(CattaTimeout, s->timeouts);
367 void catta_simple_poll_free(CattaSimplePoll *s) {
370 cleanup_timeouts(s, 1);
371 cleanup_watches(s, 1);
372 assert(s->n_watches == 0);
374 catta_free(s->pollfds);
376 if (s->wakeup_pipe[0] >= 0)
377 closepipe(s->wakeup_pipe[0]);
379 if (s->wakeup_pipe[1] >= 0)
380 closepipe(s->wakeup_pipe[1]);
383 winsock_exit(); // match the winsock_init in catta_simple_poll_new
386 static int rebuild(CattaSimplePoll *s) {
392 if (s->n_watches+1 > s->max_pollfds) {
395 s->max_pollfds = s->n_watches + 10;
397 if (!(n = catta_realloc(s->pollfds, sizeof(struct pollfd) * s->max_pollfds)))
404 s->pollfds[0].fd = s->wakeup_pipe[0];
405 s->pollfds[0].events = POLLIN;
406 s->pollfds[0].revents = 0;
410 for (w = s->watches; w; w = w->watches_next) {
415 assert(w->idx < s->max_pollfds);
416 s->pollfds[w->idx = idx++] = w->pollfd;
421 s->rebuild_pollfds = 0;
426 static CattaTimeout* find_next_timeout(CattaSimplePoll *s) {
427 CattaTimeout *t, *n = NULL;
430 for (t = s->timeouts; t; t = t->timeouts_next) {
432 if (t->dead || !t->enabled)
435 if (!n || catta_timeval_compare(&t->expiry, &n->expiry) < 0)
442 static void timeout_callback(CattaTimeout *t) {
448 t->callback(t, t->userdata);
451 int catta_simple_poll_prepare(CattaSimplePoll *s, int timeout) {
452 CattaTimeout *next_timeout;
455 assert(s->state == STATE_INIT || s->state == STATE_DISPATCHED || s->state == STATE_FAILURE);
456 s->state = STATE_PREPARING;
458 /* Clear pending wakeup requests */
461 /* Cleanup things first */
462 if (s->watch_req_cleanup)
463 cleanup_watches(s, 0);
465 if (s->timeout_req_cleanup)
466 cleanup_timeouts(s, 0);
468 /* Check whether a quit was requested */
470 s->state = STATE_QUIT;
474 /* Do we need to rebuild our array of pollfds? */
475 if (s->rebuild_pollfds)
476 if (rebuild(s) < 0) {
477 s->state = STATE_FAILURE;
481 /* Calculate the wakeup time */
482 if ((next_timeout = find_next_timeout(s))) {
487 if (next_timeout->expiry.tv_sec == 0 &&
488 next_timeout->expiry.tv_usec == 0) {
490 /* Just a shortcut so that we don't need to call gettimeofday() */
495 gettimeofday(&now, NULL);
496 usec = catta_timeval_diff(&next_timeout->expiry, &now);
499 /* Timeout elapsed */
505 /* Calculate sleep time. We add 1ms because otherwise we'd
506 * wake up too early most of the time */
507 t = (int) (usec / 1000) + 1;
509 if (timeout < 0 || timeout > t)
514 s->prepared_timeout = timeout;
515 s->state = STATE_PREPARED;
519 int catta_simple_poll_run(CattaSimplePoll *s) {
521 assert(s->state == STATE_PREPARED || s->state == STATE_FAILURE);
523 s->state = STATE_RUNNING;
528 if (s->poll_func(s->pollfds, s->n_pollfds, s->prepared_timeout, s->poll_func_userdata) < 0) {
533 s->state = STATE_FAILURE;
540 /* The poll events are now valid again */
544 s->state = STATE_RAN;
548 int catta_simple_poll_dispatch(CattaSimplePoll *s) {
549 CattaTimeout *next_timeout;
553 assert(s->state == STATE_RAN);
554 s->state = STATE_DISPATCHING;
556 /* We execute only one callback in every iteration */
558 /* Check whether the wakeup time has been reached now */
559 if ((next_timeout = find_next_timeout(s))) {
561 if (next_timeout->expiry.tv_sec == 0 && next_timeout->expiry.tv_usec == 0) {
563 /* Just a shortcut so that we don't need to call gettimeofday() */
564 timeout_callback(next_timeout);
568 if (catta_age(&next_timeout->expiry) >= 0) {
570 /* Timeout elapsed */
571 timeout_callback(next_timeout);
576 /* Look for some kind of I/O event */
577 for (w = s->watches; w; w = w->watches_next) {
583 assert(w->idx < s->n_pollfds);
585 if (s->pollfds[w->idx].revents != 0) {
586 w->callback(w, w->pollfd.fd, s->pollfds[w->idx].revents, w->userdata);
593 s->state = STATE_DISPATCHED;
597 int catta_simple_poll_iterate(CattaSimplePoll *s, int timeout) {
600 if ((r = catta_simple_poll_prepare(s, timeout)) != 0)
603 if ((r = catta_simple_poll_run(s)) != 0)
606 if ((r = catta_simple_poll_dispatch(s)) != 0)
612 void catta_simple_poll_quit(CattaSimplePoll *s) {
617 /* If there is a background thread running the poll() for us, tell it to exit the poll() */
618 catta_simple_poll_wakeup(s);
621 const CattaPoll* catta_simple_poll_get(CattaSimplePoll *s) {
627 static int system_poll(struct pollfd *ufds, unsigned int nfds, int timeout, CATTA_GCC_UNUSED void *userdata) {
628 return poll(ufds, nfds, timeout);
631 void catta_simple_poll_set_func(CattaSimplePoll *s, CattaPollFunc func, void *userdata) {
634 s->poll_func = func ? func : system_poll;
635 s->poll_func_userdata = func ? userdata : NULL;
637 /* If there is a background thread running the poll() for us, tell it to exit the poll() */
638 catta_simple_poll_wakeup(s);
641 int catta_simple_poll_loop(CattaSimplePoll *s) {
647 if ((r = catta_simple_poll_iterate(s, -1)) != 0)
648 if (r >= 0 || errno != EINTR)