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 if (pipe(s->wakeup_pipe) < 0) {
317 catta_set_nonblock(s->wakeup_pipe[0]);
318 catta_set_nonblock(s->wakeup_pipe[1]);
322 s->api.watch_new = watch_new;
323 s->api.watch_free = watch_free;
324 s->api.watch_update = watch_update;
325 s->api.watch_get_events = watch_get_events;
327 s->api.timeout_new = timeout_new;
328 s->api.timeout_free = timeout_free;
329 s->api.timeout_update = timeout_update;
332 s->max_pollfds = s->n_pollfds = 0;
333 s->rebuild_pollfds = 1;
338 s->watch_req_cleanup = 0;
339 s->timeout_req_cleanup = 0;
341 s->prepared_timeout = 0;
343 s->state = STATE_INIT;
345 s->wakeup_issued = 0;
347 catta_simple_poll_set_func(s, NULL, NULL);
349 CATTA_LLIST_HEAD_INIT(CattaWatch, s->watches);
350 CATTA_LLIST_HEAD_INIT(CattaTimeout, s->timeouts);
355 void catta_simple_poll_free(CattaSimplePoll *s) {
358 cleanup_timeouts(s, 1);
359 cleanup_watches(s, 1);
360 assert(s->n_watches == 0);
362 catta_free(s->pollfds);
364 if (s->wakeup_pipe[0] >= 0)
365 closesocket(s->wakeup_pipe[0]);
367 if (s->wakeup_pipe[1] >= 0)
368 closesocket(s->wakeup_pipe[1]);
373 static int rebuild(CattaSimplePoll *s) {
379 if (s->n_watches+1 > s->max_pollfds) {
382 s->max_pollfds = s->n_watches + 10;
384 if (!(n = catta_realloc(s->pollfds, sizeof(struct pollfd) * s->max_pollfds)))
391 s->pollfds[0].fd = s->wakeup_pipe[0];
392 s->pollfds[0].events = POLLIN;
393 s->pollfds[0].revents = 0;
397 for (w = s->watches; w; w = w->watches_next) {
402 assert(w->idx < s->max_pollfds);
403 s->pollfds[w->idx = idx++] = w->pollfd;
408 s->rebuild_pollfds = 0;
413 static CattaTimeout* find_next_timeout(CattaSimplePoll *s) {
414 CattaTimeout *t, *n = NULL;
417 for (t = s->timeouts; t; t = t->timeouts_next) {
419 if (t->dead || !t->enabled)
422 if (!n || catta_timeval_compare(&t->expiry, &n->expiry) < 0)
429 static void timeout_callback(CattaTimeout *t) {
435 t->callback(t, t->userdata);
438 int catta_simple_poll_prepare(CattaSimplePoll *s, int timeout) {
439 CattaTimeout *next_timeout;
442 assert(s->state == STATE_INIT || s->state == STATE_DISPATCHED || s->state == STATE_FAILURE);
443 s->state = STATE_PREPARING;
445 /* Clear pending wakeup requests */
448 /* Cleanup things first */
449 if (s->watch_req_cleanup)
450 cleanup_watches(s, 0);
452 if (s->timeout_req_cleanup)
453 cleanup_timeouts(s, 0);
455 /* Check whether a quit was requested */
457 s->state = STATE_QUIT;
461 /* Do we need to rebuild our array of pollfds? */
462 if (s->rebuild_pollfds)
463 if (rebuild(s) < 0) {
464 s->state = STATE_FAILURE;
468 /* Calculate the wakeup time */
469 if ((next_timeout = find_next_timeout(s))) {
474 if (next_timeout->expiry.tv_sec == 0 &&
475 next_timeout->expiry.tv_usec == 0) {
477 /* Just a shortcut so that we don't need to call gettimeofday() */
482 gettimeofday(&now, NULL);
483 usec = catta_timeval_diff(&next_timeout->expiry, &now);
486 /* Timeout elapsed */
492 /* Calculate sleep time. We add 1ms because otherwise we'd
493 * wake up too early most of the time */
494 t = (int) (usec / 1000) + 1;
496 if (timeout < 0 || timeout > t)
501 s->prepared_timeout = timeout;
502 s->state = STATE_PREPARED;
506 int catta_simple_poll_run(CattaSimplePoll *s) {
508 assert(s->state == STATE_PREPARED || s->state == STATE_FAILURE);
510 s->state = STATE_RUNNING;
515 if (s->poll_func(s->pollfds, s->n_pollfds, s->prepared_timeout, s->poll_func_userdata) < 0) {
520 s->state = STATE_FAILURE;
527 /* The poll events are now valid again */
531 s->state = STATE_RAN;
535 int catta_simple_poll_dispatch(CattaSimplePoll *s) {
536 CattaTimeout *next_timeout;
540 assert(s->state == STATE_RAN);
541 s->state = STATE_DISPATCHING;
543 /* We execute only on callback in every iteration */
545 /* Check whether the wakeup time has been reached now */
546 if ((next_timeout = find_next_timeout(s))) {
548 if (next_timeout->expiry.tv_sec == 0 && next_timeout->expiry.tv_usec == 0) {
550 /* Just a shortcut so that we don't need to call gettimeofday() */
551 timeout_callback(next_timeout);
555 if (catta_age(&next_timeout->expiry) >= 0) {
557 /* Timeout elapsed */
558 timeout_callback(next_timeout);
563 /* Look for some kind of I/O event */
564 for (w = s->watches; w; w = w->watches_next) {
570 assert(w->idx < s->n_pollfds);
572 if (s->pollfds[w->idx].revents != 0) {
573 w->callback(w, w->pollfd.fd, s->pollfds[w->idx].revents, w->userdata);
580 s->state = STATE_DISPATCHED;
584 int catta_simple_poll_iterate(CattaSimplePoll *s, int timeout) {
587 if ((r = catta_simple_poll_prepare(s, timeout)) != 0)
590 if ((r = catta_simple_poll_run(s)) != 0)
593 if ((r = catta_simple_poll_dispatch(s)) != 0)
599 void catta_simple_poll_quit(CattaSimplePoll *s) {
604 /* If there is a background thread running the poll() for us, tell it to exit the poll() */
605 catta_simple_poll_wakeup(s);
608 const CattaPoll* catta_simple_poll_get(CattaSimplePoll *s) {
614 static int system_poll(struct pollfd *ufds, unsigned int nfds, int timeout, CATTA_GCC_UNUSED void *userdata) {
615 return poll(ufds, nfds, timeout);
618 void catta_simple_poll_set_func(CattaSimplePoll *s, CattaPollFunc func, void *userdata) {
621 s->poll_func = func ? func : system_poll;
622 s->poll_func_userdata = func ? userdata : NULL;
624 /* If there is a background thread running the poll() for us, tell it to exit the poll() */
625 catta_simple_poll_wakeup(s);
628 int catta_simple_poll_loop(CattaSimplePoll *s) {
634 if ((r = catta_simple_poll_iterate(s, -1)) != 0)
635 if (r >= 0 || errno != EINTR)