2 This file is part of avahi.
4 avahi 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 avahi 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 avahi; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
35 #include "simple-watch.h"
38 AvahiSimplePoll *simple_poll;
44 AvahiWatchCallback callback;
47 AVAHI_LLIST_FIELDS(AvahiWatch, watches);
51 AvahiSimplePoll *simple_poll;
55 struct timeval expiry;
57 AvahiTimeoutCallback callback;
60 AVAHI_LLIST_FIELDS(AvahiTimeout, timeouts);
63 struct AvahiSimplePoll {
65 AvahiPollFunc poll_func;
66 void *poll_func_userdata;
68 struct pollfd* pollfds;
69 int n_pollfds, max_pollfds, rebuild_pollfds;
71 int watch_req_cleanup, timeout_req_cleanup;
76 AVAHI_LLIST_HEAD(AvahiWatch, watches);
77 AVAHI_LLIST_HEAD(AvahiTimeout, timeouts);
97 void avahi_simple_poll_wakeup(AvahiSimplePoll *s) {
101 write(s->wakeup_pipe[1], &c, sizeof(c));
102 s->wakeup_issued = 1;
105 static void clear_wakeup(AvahiSimplePoll *s) {
106 char c[10]; /* Read ten at a time */
108 if (!s->wakeup_issued)
111 s->wakeup_issued = 0;
114 if (read(s->wakeup_pipe[0], &c, sizeof(c)) != sizeof(c))
118 static int set_nonblock(int fd) {
123 if ((n = fcntl(fd, F_GETFL)) < 0)
129 return fcntl(fd, F_SETFL, n|O_NONBLOCK);
132 static AvahiWatch* watch_new(const AvahiPoll *api, int fd, AvahiWatchEvent event, AvahiWatchCallback callback, void *userdata) {
143 if (!(w = avahi_new(AvahiWatch, 1)))
146 /* If there is a background thread running the poll() for us, tell it to exit the poll() */
147 avahi_simple_poll_wakeup(s);
153 w->pollfd.events = event;
154 w->pollfd.revents = 0;
156 w->callback = callback;
157 w->userdata = userdata;
160 s->rebuild_pollfds = 1;
162 AVAHI_LLIST_PREPEND(AvahiWatch, watches, s->watches, w);
168 static void watch_update(AvahiWatch *w, AvahiWatchEvent events) {
172 /* If there is a background thread running the poll() for us, tell it to exit the poll() */
173 avahi_simple_poll_wakeup(w->simple_poll);
175 w->pollfd.events = events;
178 assert(w->simple_poll);
179 w->simple_poll->pollfds[w->idx] = w->pollfd;
181 w->simple_poll->rebuild_pollfds = 1;
184 static AvahiWatchEvent watch_get_events(AvahiWatch *w) {
188 if (w->idx != -1 && w->simple_poll->events_valid)
189 return w->simple_poll->pollfds[w->idx].revents;
194 static void remove_pollfd(AvahiWatch *w) {
200 w->simple_poll->rebuild_pollfds = 1;
203 static void watch_free(AvahiWatch *w) {
208 /* If there is a background thread running the poll() for us, tell it to exit the poll() */
209 avahi_simple_poll_wakeup(w->simple_poll);
214 w->simple_poll->n_watches --;
215 w->simple_poll->watch_req_cleanup = 1;
218 static void destroy_watch(AvahiWatch *w) {
222 AVAHI_LLIST_REMOVE(AvahiWatch, watches, w->simple_poll->watches, w);
225 w->simple_poll->n_watches --;
230 static void cleanup_watches(AvahiSimplePoll *s, int all) {
231 AvahiWatch *w, *next;
234 for (w = s->watches; w; w = next) {
235 next = w->watches_next;
241 s->timeout_req_cleanup = 0;
244 static AvahiTimeout* timeout_new(const AvahiPoll *api, const struct timeval *tv, AvahiTimeoutCallback callback, void *userdata) {
254 if (!(t = avahi_new(AvahiTimeout, 1)))
257 /* If there is a background thread running the poll() for us, tell it to exit the poll() */
258 avahi_simple_poll_wakeup(s);
263 if ((t->enabled = !!tv))
266 t->callback = callback;
267 t->userdata = userdata;
269 AVAHI_LLIST_PREPEND(AvahiTimeout, timeouts, s->timeouts, t);
273 static void timeout_update(AvahiTimeout *t, const struct timeval *tv) {
277 /* If there is a background thread running the poll() for us, tell it to exit the poll() */
278 avahi_simple_poll_wakeup(t->simple_poll);
280 if ((t->enabled = !!tv))
284 static void timeout_free(AvahiTimeout *t) {
288 /* If there is a background thread running the poll() for us, tell it to exit the poll() */
289 avahi_simple_poll_wakeup(t->simple_poll);
292 t->simple_poll->timeout_req_cleanup = 1;
296 static void destroy_timeout(AvahiTimeout *t) {
299 AVAHI_LLIST_REMOVE(AvahiTimeout, timeouts, t->simple_poll->timeouts, t);
304 static void cleanup_timeouts(AvahiSimplePoll *s, int all) {
305 AvahiTimeout *t, *next;
308 for (t = s->timeouts; t; t = next) {
309 next = t->timeouts_next;
315 s->timeout_req_cleanup = 0;
318 AvahiSimplePoll *avahi_simple_poll_new(void) {
321 if (!(s = avahi_new(AvahiSimplePoll, 1)))
324 if (pipe(s->wakeup_pipe) < 0) {
329 set_nonblock(s->wakeup_pipe[0]);
330 set_nonblock(s->wakeup_pipe[1]);
334 s->api.watch_new = watch_new;
335 s->api.watch_free = watch_free;
336 s->api.watch_update = watch_update;
337 s->api.watch_get_events = watch_get_events;
339 s->api.timeout_new = timeout_new;
340 s->api.timeout_free = timeout_free;
341 s->api.timeout_update = timeout_update;
344 s->max_pollfds = s->n_pollfds = 0;
345 s->rebuild_pollfds = 1;
350 s->watch_req_cleanup = 0;
351 s->timeout_req_cleanup = 0;
353 s->prepared_timeout = 0;
355 s->state = STATE_INIT;
357 s->wakeup_issued = 0;
359 avahi_simple_poll_set_func(s, NULL, NULL);
361 AVAHI_LLIST_HEAD_INIT(AvahiWatch, s->watches);
362 AVAHI_LLIST_HEAD_INIT(AvahiTimeout, s->timeouts);
367 void avahi_simple_poll_free(AvahiSimplePoll *s) {
370 cleanup_timeouts(s, 1);
371 cleanup_watches(s, 1);
372 assert(s->n_watches == 0);
374 avahi_free(s->pollfds);
376 if (s->wakeup_pipe[0] >= 0)
377 close(s->wakeup_pipe[0]);
379 if (s->wakeup_pipe[1] >= 0)
380 close(s->wakeup_pipe[1]);
385 static int rebuild(AvahiSimplePoll *s) {
391 if (s->n_watches+1 > s->max_pollfds) {
394 s->max_pollfds = s->n_watches + 10;
396 if (!(n = avahi_realloc(s->pollfds, sizeof(struct pollfd) * s->max_pollfds)))
403 s->pollfds[0].fd = s->wakeup_pipe[0];
404 s->pollfds[0].events = POLLIN;
405 s->pollfds[0].revents = 0;
409 for (w = s->watches; w; w = w->watches_next) {
414 assert(w->idx < s->max_pollfds);
415 s->pollfds[w->idx = idx++] = w->pollfd;
420 s->rebuild_pollfds = 0;
425 static AvahiTimeout* find_next_timeout(AvahiSimplePoll *s) {
426 AvahiTimeout *t, *n = NULL;
429 for (t = s->timeouts; t; t = t->timeouts_next) {
431 if (t->dead || !t->enabled)
434 if (!n || avahi_timeval_compare(&t->expiry, &n->expiry) < 0)
441 static void timeout_callback(AvahiTimeout *t) {
447 t->callback(t, t->userdata);
450 int avahi_simple_poll_prepare(AvahiSimplePoll *s, int timeout) {
451 AvahiTimeout *next_timeout;
454 assert(s->state == STATE_INIT || s->state == STATE_DISPATCHED || s->state == STATE_FAILURE);
455 s->state = STATE_PREPARING;
457 /* Clear pending wakeup requests */
460 /* Cleanup things first */
461 if (s->watch_req_cleanup)
462 cleanup_watches(s, 0);
464 if (s->timeout_req_cleanup)
465 cleanup_timeouts(s, 0);
467 /* Check whether a quit was requested */
469 s->state = STATE_QUIT;
473 /* Do we need to rebuild our array of pollfds? */
474 if (s->rebuild_pollfds)
475 if (rebuild(s) < 0) {
476 s->state = STATE_FAILURE;
480 /* Calculate the wakeup time */
481 if ((next_timeout = find_next_timeout(s))) {
486 if (next_timeout->expiry.tv_sec == 0 &&
487 next_timeout->expiry.tv_usec == 0) {
489 /* Just a shortcut so that we don't need to call gettimeofday() */
494 gettimeofday(&now, NULL);
495 usec = avahi_timeval_diff(&next_timeout->expiry, &now);
498 /* Timeout elapsed */
504 /* Calculate sleep time. We add 1ms because otherwise we'd
505 * wake up too early most of the time */
506 t = (int) (usec / 1000) + 1;
508 if (timeout < 0 || timeout > t)
513 s->prepared_timeout = timeout;
514 s->state = STATE_PREPARED;
518 int avahi_simple_poll_run(AvahiSimplePoll *s) {
520 assert(s->state == STATE_PREPARED || s->state == STATE_FAILURE);
522 s->state = STATE_RUNNING;
527 if (s->poll_func(s->pollfds, s->n_pollfds, s->prepared_timeout, s->poll_func_userdata) < 0) {
532 s->state = STATE_FAILURE;
539 /* The poll events are now valid again */
543 s->state = STATE_RAN;
547 int avahi_simple_poll_dispatch(AvahiSimplePoll *s) {
548 AvahiTimeout *next_timeout;
552 assert(s->state == STATE_RAN);
553 s->state = STATE_DISPATCHING;
555 /* We execute only on callback in every iteration */
557 /* Check whether the wakeup time has been reached now */
558 if ((next_timeout = find_next_timeout(s))) {
560 if (next_timeout->expiry.tv_sec == 0 && next_timeout->expiry.tv_usec == 0) {
562 /* Just a shortcut so that we don't need to call gettimeofday() */
563 timeout_callback(next_timeout);
567 if (avahi_age(&next_timeout->expiry) >= 0) {
569 /* Timeout elapsed */
570 timeout_callback(next_timeout);
575 /* Look for some kind of I/O event */
576 for (w = s->watches; w; w = w->watches_next) {
582 assert(w->idx < s->n_pollfds);
584 if (s->pollfds[w->idx].revents != 0) {
585 w->callback(w, w->pollfd.fd, s->pollfds[w->idx].revents, w->userdata);
592 s->state = STATE_DISPATCHED;
596 int avahi_simple_poll_iterate(AvahiSimplePoll *s, int timeout) {
599 if ((r = avahi_simple_poll_prepare(s, timeout)) != 0)
602 if ((r = avahi_simple_poll_run(s)) != 0)
605 if ((r = avahi_simple_poll_dispatch(s)) != 0)
611 void avahi_simple_poll_quit(AvahiSimplePoll *s) {
616 /* If there is a background thread running the poll() for us, tell it to exit the poll() */
617 avahi_simple_poll_wakeup(s);
620 const AvahiPoll* avahi_simple_poll_get(AvahiSimplePoll *s) {
626 static int system_poll(struct pollfd *ufds, unsigned int nfds, int timeout, AVAHI_GCC_UNUSED void *userdata) {
627 return poll(ufds, nfds, timeout);
630 void avahi_simple_poll_set_func(AvahiSimplePoll *s, AvahiPollFunc func, void *userdata) {
633 s->poll_func = func ? func : system_poll;
634 s->poll_func_userdata = func ? userdata : NULL;
636 /* If there is a background thread running the poll() for us, tell it to exit the poll() */
637 avahi_simple_poll_wakeup(s);
640 int avahi_simple_poll_loop(AvahiSimplePoll *s) {
646 if ((r = avahi_simple_poll_iterate(s, -1)) != 0)
647 if (r >= 0 || errno != EINTR)