4 This file is part of avahi.
6 avahi is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as
8 published by the Free Software Foundation; either version 2.1 of the
9 License, or (at your option) any later version.
11 avahi is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
14 Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with avahi; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
34 #include <avahi-common/llist.h>
35 #include <avahi-common/malloc.h>
37 #include "simple-watch.h"
40 AvahiSimplePoll *simple_poll;
46 AvahiWatchCallback callback;
49 AVAHI_LLIST_FIELDS(AvahiWatch, watches);
53 AvahiSimplePoll *simple_poll;
57 struct timeval expiry;
59 AvahiTimeoutCallback callback;
62 AVAHI_LLIST_FIELDS(AvahiTimeout, timeouts);
65 struct AvahiSimplePoll {
67 AvahiPollFunc 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 AVAHI_LLIST_HEAD(AvahiWatch, watches);
79 AVAHI_LLIST_HEAD(AvahiTimeout, timeouts);
99 void avahi_simple_poll_wakeup(AvahiSimplePoll *s) {
103 write(s->wakeup_pipe[1], &c, sizeof(c));
104 s->wakeup_issued = 1;
107 static void clear_wakeup(AvahiSimplePoll *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 int set_nonblock(int fd) {
125 if ((n = fcntl(fd, F_GETFL)) < 0)
131 return fcntl(fd, F_SETFL, n|O_NONBLOCK);
134 static AvahiWatch* watch_new(const AvahiPoll *api, int fd, AvahiWatchEvent event, AvahiWatchCallback callback, void *userdata) {
145 if (!(w = avahi_new(AvahiWatch, 1)))
148 /* If there is a background thread running the poll() for us, tell it to exit the poll() */
149 avahi_simple_poll_wakeup(s);
155 w->pollfd.events = event;
157 w->callback = callback;
158 w->userdata = userdata;
161 s->rebuild_pollfds = 1;
163 AVAHI_LLIST_PREPEND(AvahiWatch, watches, s->watches, w);
169 static void watch_update(AvahiWatch *w, AvahiWatchEvent events) {
173 /* If there is a background thread running the poll() for us, tell it to exit the poll() */
174 avahi_simple_poll_wakeup(w->simple_poll);
176 w->pollfd.events = events;
179 assert(w->simple_poll);
180 w->simple_poll->pollfds[w->idx] = w->pollfd;
182 w->simple_poll->rebuild_pollfds = 1;
185 static AvahiWatchEvent watch_get_events(AvahiWatch *w) {
189 if (w->idx != -1 && w->simple_poll->events_valid)
190 return w->simple_poll->pollfds[w->idx].revents;
195 static void remove_pollfd(AvahiWatch *w) {
201 w->simple_poll->rebuild_pollfds = 1;
204 static void watch_free(AvahiWatch *w) {
209 /* If there is a background thread running the poll() for us, tell it to exit the poll() */
210 avahi_simple_poll_wakeup(w->simple_poll);
215 w->simple_poll->n_watches --;
216 w->simple_poll->watch_req_cleanup = 1;
219 static void destroy_watch(AvahiWatch *w) {
223 AVAHI_LLIST_REMOVE(AvahiWatch, watches, w->simple_poll->watches, w);
226 w->simple_poll->n_watches --;
231 static void cleanup_watches(AvahiSimplePoll *s, int all) {
232 AvahiWatch *w, *next;
235 for (w = s->watches; w; w = next) {
236 next = w->watches_next;
242 s->timeout_req_cleanup = 0;
245 static AvahiTimeout* timeout_new(const AvahiPoll *api, const struct timeval *tv, AvahiTimeoutCallback callback, void *userdata) {
255 if (!(t = avahi_new(AvahiTimeout, 1)))
258 /* If there is a background thread running the poll() for us, tell it to exit the poll() */
259 avahi_simple_poll_wakeup(s);
264 if ((t->enabled = !!tv))
267 t->callback = callback;
268 t->userdata = userdata;
270 AVAHI_LLIST_PREPEND(AvahiTimeout, timeouts, s->timeouts, t);
274 static void timeout_update(AvahiTimeout *t, const struct timeval *tv) {
278 /* If there is a background thread running the poll() for us, tell it to exit the poll() */
279 avahi_simple_poll_wakeup(t->simple_poll);
281 if ((t->enabled = !!tv))
285 static void timeout_free(AvahiTimeout *t) {
289 /* If there is a background thread running the poll() for us, tell it to exit the poll() */
290 avahi_simple_poll_wakeup(t->simple_poll);
293 t->simple_poll->timeout_req_cleanup = 1;
297 static void destroy_timeout(AvahiTimeout *t) {
300 AVAHI_LLIST_REMOVE(AvahiTimeout, timeouts, t->simple_poll->timeouts, t);
305 static void cleanup_timeouts(AvahiSimplePoll *s, int all) {
306 AvahiTimeout *t, *next;
309 for (t = s->timeouts; t; t = next) {
310 next = t->timeouts_next;
316 s->timeout_req_cleanup = 0;
319 AvahiSimplePoll *avahi_simple_poll_new(void) {
322 if (!(s = avahi_new(AvahiSimplePoll, 1)))
325 if (pipe(s->wakeup_pipe) < 0) {
330 set_nonblock(s->wakeup_pipe[0]);
331 set_nonblock(s->wakeup_pipe[1]);
335 s->api.watch_new = watch_new;
336 s->api.watch_free = watch_free;
337 s->api.watch_update = watch_update;
338 s->api.watch_get_events = watch_get_events;
340 s->api.timeout_new = timeout_new;
341 s->api.timeout_free = timeout_free;
342 s->api.timeout_update = timeout_update;
345 s->max_pollfds = s->n_pollfds = 0;
346 s->rebuild_pollfds = 1;
351 s->watch_req_cleanup = 0;
352 s->timeout_req_cleanup = 0;
354 s->prepared_timeout = 0;
356 s->state = STATE_INIT;
358 s->wakeup_issued = 0;
360 avahi_simple_poll_set_func(s, NULL, NULL);
362 AVAHI_LLIST_HEAD_INIT(AvahiWatch, s->watches);
363 AVAHI_LLIST_HEAD_INIT(AvahiTimeout, s->timeouts);
368 void avahi_simple_poll_free(AvahiSimplePoll *s) {
371 cleanup_timeouts(s, 1);
372 cleanup_watches(s, 1);
373 assert(s->n_watches == 0);
375 avahi_free(s->pollfds);
377 if (s->wakeup_pipe[0] >= 0)
378 close(s->wakeup_pipe[0]);
380 if (s->wakeup_pipe[1] >= 0)
381 close(s->wakeup_pipe[1]);
386 static int rebuild(AvahiSimplePoll *s) {
392 if (s->n_watches+1 > s->max_pollfds) {
395 s->max_pollfds = s->n_watches + 10;
397 if (!(n = avahi_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 AvahiTimeout* find_next_timeout(AvahiSimplePoll *s) {
427 AvahiTimeout *t, *n = NULL;
430 for (t = s->timeouts; t; t = t->timeouts_next) {
432 if (t->dead || !t->enabled)
435 if (!n || avahi_timeval_compare(&t->expiry, &n->expiry) < 0)
442 static void timeout_callback(AvahiTimeout *t) {
448 t->callback(t, t->userdata);
451 int avahi_simple_poll_prepare(AvahiSimplePoll *s, int timeout) {
452 AvahiTimeout *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 = avahi_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 avahi_simple_poll_run(AvahiSimplePoll *s) {
521 assert(s->state == STATE_PREPARED);
523 s->state = STATE_RUNNING;
525 if (s->prepared_timeout != 0) {
527 if (s->poll_func(s->pollfds, s->n_pollfds, s->prepared_timeout, s->poll_func_userdata) < 0) {
528 s->state = STATE_FAILURE;
532 /* The poll events are now valid again */
538 s->state = STATE_RAN;
542 int avahi_simple_poll_dispatch(AvahiSimplePoll *s) {
543 AvahiTimeout *next_timeout;
547 assert(s->state == STATE_RAN);
548 s->state = STATE_DISPATCHING;
550 /* We execute only on callback in every iteration */
552 /* Check whether the wakeup time has been reached now */
553 if ((next_timeout = find_next_timeout(s))) {
555 if (next_timeout->expiry.tv_sec == 0 && next_timeout->expiry.tv_usec == 0) {
557 /* Just a shortcut so that we don't need to call gettimeofday() */
558 timeout_callback(next_timeout);
562 if (avahi_age(&next_timeout->expiry) >= 0) {
564 /* Timeout elapsed */
565 timeout_callback(next_timeout);
570 /* Look for some kind of I/O event */
571 for (w = s->watches; w; w = w->watches_next) {
577 assert(w->idx < s->n_pollfds);
579 if (s->pollfds[w->idx].revents != 0) {
580 w->callback(w, w->pollfd.fd, s->pollfds[w->idx].revents, w->userdata);
587 s->state = STATE_DISPATCHED;
591 int avahi_simple_poll_iterate(AvahiSimplePoll *s, int timeout) {
594 if ((r = avahi_simple_poll_prepare(s, timeout)) != 0)
597 if ((r = avahi_simple_poll_run(s)) != 0)
600 if ((r = avahi_simple_poll_dispatch(s)) != 0)
606 void avahi_simple_poll_quit(AvahiSimplePoll *s) {
611 /* If there is a background thread running the poll() for us, tell it to exit the poll() */
612 avahi_simple_poll_wakeup(s);
615 const AvahiPoll* avahi_simple_poll_get(AvahiSimplePoll *s) {
621 static int system_poll(struct pollfd *ufds, unsigned int nfds, int timeout, void *userdata) {
622 return poll(ufds, nfds, timeout);
625 void avahi_simple_poll_set_func(AvahiSimplePoll *s, AvahiPollFunc func, void *userdata) {
628 s->poll_func = func ? func : system_poll;
629 s->poll_func_userdata = func ? userdata : NULL;
631 /* If there is a background thread running the poll() for us, tell it to exit the poll() */
632 avahi_simple_poll_wakeup(s);
635 int avahi_simple_poll_loop(AvahiSimplePoll *s) {
641 if ((r = avahi_simple_poll_iterate(s, -1)) != 0)