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 write(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;
116 if (read(s->wakeup_pipe[0], &c, sizeof(c)) != sizeof(c))
120 static CattaWatch* watch_new(const CattaPoll *api, int fd, CattaWatchEvent event, CattaWatchCallback callback, void *userdata) {
131 if (!(w = catta_new(CattaWatch, 1)))
134 /* If there is a background thread running the poll() for us, tell it to exit the poll() */
135 catta_simple_poll_wakeup(s);
141 w->pollfd.events = event;
142 w->pollfd.revents = 0;
144 w->callback = callback;
145 w->userdata = userdata;
148 s->rebuild_pollfds = 1;
150 CATTA_LLIST_PREPEND(CattaWatch, watches, s->watches, w);
156 static void watch_update(CattaWatch *w, CattaWatchEvent events) {
160 /* If there is a background thread running the poll() for us, tell it to exit the poll() */
161 catta_simple_poll_wakeup(w->simple_poll);
163 w->pollfd.events = events;
166 assert(w->simple_poll);
167 w->simple_poll->pollfds[w->idx] = w->pollfd;
169 w->simple_poll->rebuild_pollfds = 1;
172 static CattaWatchEvent watch_get_events(CattaWatch *w) {
176 if (w->idx != -1 && w->simple_poll->events_valid)
177 return w->simple_poll->pollfds[w->idx].revents;
182 static void remove_pollfd(CattaWatch *w) {
188 w->simple_poll->rebuild_pollfds = 1;
191 static void watch_free(CattaWatch *w) {
196 /* If there is a background thread running the poll() for us, tell it to exit the poll() */
197 catta_simple_poll_wakeup(w->simple_poll);
202 w->simple_poll->n_watches --;
203 w->simple_poll->watch_req_cleanup = 1;
206 static void destroy_watch(CattaWatch *w) {
210 CATTA_LLIST_REMOVE(CattaWatch, watches, w->simple_poll->watches, w);
213 w->simple_poll->n_watches --;
218 static void cleanup_watches(CattaSimplePoll *s, int all) {
219 CattaWatch *w, *next;
222 for (w = s->watches; w; w = next) {
223 next = w->watches_next;
229 s->timeout_req_cleanup = 0;
232 static CattaTimeout* timeout_new(const CattaPoll *api, const struct timeval *tv, CattaTimeoutCallback callback, void *userdata) {
242 if (!(t = catta_new(CattaTimeout, 1)))
245 /* If there is a background thread running the poll() for us, tell it to exit the poll() */
246 catta_simple_poll_wakeup(s);
251 if ((t->enabled = !!tv))
254 t->callback = callback;
255 t->userdata = userdata;
257 CATTA_LLIST_PREPEND(CattaTimeout, timeouts, s->timeouts, t);
261 static void timeout_update(CattaTimeout *t, const struct timeval *tv) {
265 /* If there is a background thread running the poll() for us, tell it to exit the poll() */
266 catta_simple_poll_wakeup(t->simple_poll);
268 if ((t->enabled = !!tv))
272 static void timeout_free(CattaTimeout *t) {
276 /* If there is a background thread running the poll() for us, tell it to exit the poll() */
277 catta_simple_poll_wakeup(t->simple_poll);
280 t->simple_poll->timeout_req_cleanup = 1;
284 static void destroy_timeout(CattaTimeout *t) {
287 CATTA_LLIST_REMOVE(CattaTimeout, timeouts, t->simple_poll->timeouts, t);
292 static void cleanup_timeouts(CattaSimplePoll *s, int all) {
293 CattaTimeout *t, *next;
296 for (t = s->timeouts; t; t = next) {
297 next = t->timeouts_next;
303 s->timeout_req_cleanup = 0;
306 CattaSimplePoll *catta_simple_poll_new(void) {
309 if (!(s = catta_new(CattaSimplePoll, 1)))
312 winsock_init(); // on Windows, pipe uses sockets; no-op on other platforms
313 if (pipe(s->wakeup_pipe) < 0) {
319 catta_set_nonblock(s->wakeup_pipe[0]);
320 catta_set_nonblock(s->wakeup_pipe[1]);
324 s->api.watch_new = watch_new;
325 s->api.watch_free = watch_free;
326 s->api.watch_update = watch_update;
327 s->api.watch_get_events = watch_get_events;
329 s->api.timeout_new = timeout_new;
330 s->api.timeout_free = timeout_free;
331 s->api.timeout_update = timeout_update;
334 s->max_pollfds = s->n_pollfds = 0;
335 s->rebuild_pollfds = 1;
340 s->watch_req_cleanup = 0;
341 s->timeout_req_cleanup = 0;
343 s->prepared_timeout = 0;
345 s->state = STATE_INIT;
347 s->wakeup_issued = 0;
349 catta_simple_poll_set_func(s, NULL, NULL);
351 CATTA_LLIST_HEAD_INIT(CattaWatch, s->watches);
352 CATTA_LLIST_HEAD_INIT(CattaTimeout, s->timeouts);
357 void catta_simple_poll_free(CattaSimplePoll *s) {
360 cleanup_timeouts(s, 1);
361 cleanup_watches(s, 1);
362 assert(s->n_watches == 0);
364 catta_free(s->pollfds);
366 if (s->wakeup_pipe[0] >= 0)
367 closesocket(s->wakeup_pipe[0]);
369 if (s->wakeup_pipe[1] >= 0)
370 closesocket(s->wakeup_pipe[1]);
373 winsock_exit(); // match the winsock_init in catta_simple_poll_new
376 static int rebuild(CattaSimplePoll *s) {
382 if (s->n_watches+1 > s->max_pollfds) {
385 s->max_pollfds = s->n_watches + 10;
387 if (!(n = catta_realloc(s->pollfds, sizeof(struct pollfd) * s->max_pollfds)))
394 s->pollfds[0].fd = s->wakeup_pipe[0];
395 s->pollfds[0].events = POLLIN;
396 s->pollfds[0].revents = 0;
400 for (w = s->watches; w; w = w->watches_next) {
405 assert(w->idx < s->max_pollfds);
406 s->pollfds[w->idx = idx++] = w->pollfd;
411 s->rebuild_pollfds = 0;
416 static CattaTimeout* find_next_timeout(CattaSimplePoll *s) {
417 CattaTimeout *t, *n = NULL;
420 for (t = s->timeouts; t; t = t->timeouts_next) {
422 if (t->dead || !t->enabled)
425 if (!n || catta_timeval_compare(&t->expiry, &n->expiry) < 0)
432 static void timeout_callback(CattaTimeout *t) {
438 t->callback(t, t->userdata);
441 int catta_simple_poll_prepare(CattaSimplePoll *s, int timeout) {
442 CattaTimeout *next_timeout;
445 assert(s->state == STATE_INIT || s->state == STATE_DISPATCHED || s->state == STATE_FAILURE);
446 s->state = STATE_PREPARING;
448 /* Clear pending wakeup requests */
451 /* Cleanup things first */
452 if (s->watch_req_cleanup)
453 cleanup_watches(s, 0);
455 if (s->timeout_req_cleanup)
456 cleanup_timeouts(s, 0);
458 /* Check whether a quit was requested */
460 s->state = STATE_QUIT;
464 /* Do we need to rebuild our array of pollfds? */
465 if (s->rebuild_pollfds)
466 if (rebuild(s) < 0) {
467 s->state = STATE_FAILURE;
471 /* Calculate the wakeup time */
472 if ((next_timeout = find_next_timeout(s))) {
477 if (next_timeout->expiry.tv_sec == 0 &&
478 next_timeout->expiry.tv_usec == 0) {
480 /* Just a shortcut so that we don't need to call gettimeofday() */
485 gettimeofday(&now, NULL);
486 usec = catta_timeval_diff(&next_timeout->expiry, &now);
489 /* Timeout elapsed */
495 /* Calculate sleep time. We add 1ms because otherwise we'd
496 * wake up too early most of the time */
497 t = (int) (usec / 1000) + 1;
499 if (timeout < 0 || timeout > t)
504 s->prepared_timeout = timeout;
505 s->state = STATE_PREPARED;
509 int catta_simple_poll_run(CattaSimplePoll *s) {
511 assert(s->state == STATE_PREPARED || s->state == STATE_FAILURE);
513 s->state = STATE_RUNNING;
518 if (s->poll_func(s->pollfds, s->n_pollfds, s->prepared_timeout, s->poll_func_userdata) < 0) {
523 s->state = STATE_FAILURE;
530 /* The poll events are now valid again */
534 s->state = STATE_RAN;
538 int catta_simple_poll_dispatch(CattaSimplePoll *s) {
539 CattaTimeout *next_timeout;
543 assert(s->state == STATE_RAN);
544 s->state = STATE_DISPATCHING;
546 /* We execute only on callback in every iteration */
548 /* Check whether the wakeup time has been reached now */
549 if ((next_timeout = find_next_timeout(s))) {
551 if (next_timeout->expiry.tv_sec == 0 && next_timeout->expiry.tv_usec == 0) {
553 /* Just a shortcut so that we don't need to call gettimeofday() */
554 timeout_callback(next_timeout);
558 if (catta_age(&next_timeout->expiry) >= 0) {
560 /* Timeout elapsed */
561 timeout_callback(next_timeout);
566 /* Look for some kind of I/O event */
567 for (w = s->watches; w; w = w->watches_next) {
573 assert(w->idx < s->n_pollfds);
575 if (s->pollfds[w->idx].revents != 0) {
576 w->callback(w, w->pollfd.fd, s->pollfds[w->idx].revents, w->userdata);
583 s->state = STATE_DISPATCHED;
587 int catta_simple_poll_iterate(CattaSimplePoll *s, int timeout) {
590 if ((r = catta_simple_poll_prepare(s, timeout)) != 0)
593 if ((r = catta_simple_poll_run(s)) != 0)
596 if ((r = catta_simple_poll_dispatch(s)) != 0)
602 void catta_simple_poll_quit(CattaSimplePoll *s) {
607 /* If there is a background thread running the poll() for us, tell it to exit the poll() */
608 catta_simple_poll_wakeup(s);
611 const CattaPoll* catta_simple_poll_get(CattaSimplePoll *s) {
617 static int system_poll(struct pollfd *ufds, unsigned int nfds, int timeout, CATTA_GCC_UNUSED void *userdata) {
618 return poll(ufds, nfds, timeout);
621 void catta_simple_poll_set_func(CattaSimplePoll *s, CattaPollFunc func, void *userdata) {
624 s->poll_func = func ? func : system_poll;
625 s->poll_func_userdata = func ? userdata : NULL;
627 /* If there is a background thread running the poll() for us, tell it to exit the poll() */
628 catta_simple_poll_wakeup(s);
631 int catta_simple_poll_loop(CattaSimplePoll *s) {
637 if ((r = catta_simple_poll_iterate(s, -1)) != 0)
638 if (r >= 0 || errno != EINTR)