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
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;
156 w->pollfd.revents = 0;
158 w->callback = callback;
159 w->userdata = userdata;
162 s->rebuild_pollfds = 1;
164 AVAHI_LLIST_PREPEND(AvahiWatch, watches, s->watches, w);
170 static void watch_update(AvahiWatch *w, AvahiWatchEvent events) {
174 /* If there is a background thread running the poll() for us, tell it to exit the poll() */
175 avahi_simple_poll_wakeup(w->simple_poll);
177 w->pollfd.events = events;
180 assert(w->simple_poll);
181 w->simple_poll->pollfds[w->idx] = w->pollfd;
183 w->simple_poll->rebuild_pollfds = 1;
186 static AvahiWatchEvent watch_get_events(AvahiWatch *w) {
190 if (w->idx != -1 && w->simple_poll->events_valid)
191 return w->simple_poll->pollfds[w->idx].revents;
196 static void remove_pollfd(AvahiWatch *w) {
202 w->simple_poll->rebuild_pollfds = 1;
205 static void watch_free(AvahiWatch *w) {
210 /* If there is a background thread running the poll() for us, tell it to exit the poll() */
211 avahi_simple_poll_wakeup(w->simple_poll);
216 w->simple_poll->n_watches --;
217 w->simple_poll->watch_req_cleanup = 1;
220 static void destroy_watch(AvahiWatch *w) {
224 AVAHI_LLIST_REMOVE(AvahiWatch, watches, w->simple_poll->watches, w);
227 w->simple_poll->n_watches --;
232 static void cleanup_watches(AvahiSimplePoll *s, int all) {
233 AvahiWatch *w, *next;
236 for (w = s->watches; w; w = next) {
237 next = w->watches_next;
243 s->timeout_req_cleanup = 0;
246 static AvahiTimeout* timeout_new(const AvahiPoll *api, const struct timeval *tv, AvahiTimeoutCallback callback, void *userdata) {
256 if (!(t = avahi_new(AvahiTimeout, 1)))
259 /* If there is a background thread running the poll() for us, tell it to exit the poll() */
260 avahi_simple_poll_wakeup(s);
265 if ((t->enabled = !!tv))
268 t->callback = callback;
269 t->userdata = userdata;
271 AVAHI_LLIST_PREPEND(AvahiTimeout, timeouts, s->timeouts, t);
275 static void timeout_update(AvahiTimeout *t, const struct timeval *tv) {
279 /* If there is a background thread running the poll() for us, tell it to exit the poll() */
280 avahi_simple_poll_wakeup(t->simple_poll);
282 if ((t->enabled = !!tv))
286 static void timeout_free(AvahiTimeout *t) {
290 /* If there is a background thread running the poll() for us, tell it to exit the poll() */
291 avahi_simple_poll_wakeup(t->simple_poll);
294 t->simple_poll->timeout_req_cleanup = 1;
298 static void destroy_timeout(AvahiTimeout *t) {
301 AVAHI_LLIST_REMOVE(AvahiTimeout, timeouts, t->simple_poll->timeouts, t);
306 static void cleanup_timeouts(AvahiSimplePoll *s, int all) {
307 AvahiTimeout *t, *next;
310 for (t = s->timeouts; t; t = next) {
311 next = t->timeouts_next;
317 s->timeout_req_cleanup = 0;
320 AvahiSimplePoll *avahi_simple_poll_new(void) {
323 if (!(s = avahi_new(AvahiSimplePoll, 1)))
326 if (pipe(s->wakeup_pipe) < 0) {
331 set_nonblock(s->wakeup_pipe[0]);
332 set_nonblock(s->wakeup_pipe[1]);
336 s->api.watch_new = watch_new;
337 s->api.watch_free = watch_free;
338 s->api.watch_update = watch_update;
339 s->api.watch_get_events = watch_get_events;
341 s->api.timeout_new = timeout_new;
342 s->api.timeout_free = timeout_free;
343 s->api.timeout_update = timeout_update;
346 s->max_pollfds = s->n_pollfds = 0;
347 s->rebuild_pollfds = 1;
352 s->watch_req_cleanup = 0;
353 s->timeout_req_cleanup = 0;
355 s->prepared_timeout = 0;
357 s->state = STATE_INIT;
359 s->wakeup_issued = 0;
361 avahi_simple_poll_set_func(s, NULL, NULL);
363 AVAHI_LLIST_HEAD_INIT(AvahiWatch, s->watches);
364 AVAHI_LLIST_HEAD_INIT(AvahiTimeout, s->timeouts);
369 void avahi_simple_poll_free(AvahiSimplePoll *s) {
372 cleanup_timeouts(s, 1);
373 cleanup_watches(s, 1);
374 assert(s->n_watches == 0);
376 avahi_free(s->pollfds);
378 if (s->wakeup_pipe[0] >= 0)
379 close(s->wakeup_pipe[0]);
381 if (s->wakeup_pipe[1] >= 0)
382 close(s->wakeup_pipe[1]);
387 static int rebuild(AvahiSimplePoll *s) {
393 if (s->n_watches+1 > s->max_pollfds) {
396 s->max_pollfds = s->n_watches + 10;
398 if (!(n = avahi_realloc(s->pollfds, sizeof(struct pollfd) * s->max_pollfds)))
405 s->pollfds[0].fd = s->wakeup_pipe[0];
406 s->pollfds[0].events = POLLIN;
407 s->pollfds[0].revents = 0;
411 for (w = s->watches; w; w = w->watches_next) {
416 assert(w->idx < s->max_pollfds);
417 s->pollfds[w->idx = idx++] = w->pollfd;
422 s->rebuild_pollfds = 0;
427 static AvahiTimeout* find_next_timeout(AvahiSimplePoll *s) {
428 AvahiTimeout *t, *n = NULL;
431 for (t = s->timeouts; t; t = t->timeouts_next) {
433 if (t->dead || !t->enabled)
436 if (!n || avahi_timeval_compare(&t->expiry, &n->expiry) < 0)
443 static void timeout_callback(AvahiTimeout *t) {
449 t->callback(t, t->userdata);
452 int avahi_simple_poll_prepare(AvahiSimplePoll *s, int timeout) {
453 AvahiTimeout *next_timeout;
456 assert(s->state == STATE_INIT || s->state == STATE_DISPATCHED || s->state == STATE_FAILURE);
457 s->state = STATE_PREPARING;
459 /* Clear pending wakeup requests */
462 /* Cleanup things first */
463 if (s->watch_req_cleanup)
464 cleanup_watches(s, 0);
466 if (s->timeout_req_cleanup)
467 cleanup_timeouts(s, 0);
469 /* Check whether a quit was requested */
471 s->state = STATE_QUIT;
475 /* Do we need to rebuild our array of pollfds? */
476 if (s->rebuild_pollfds)
477 if (rebuild(s) < 0) {
478 s->state = STATE_FAILURE;
482 /* Calculate the wakeup time */
483 if ((next_timeout = find_next_timeout(s))) {
488 if (next_timeout->expiry.tv_sec == 0 &&
489 next_timeout->expiry.tv_usec == 0) {
491 /* Just a shortcut so that we don't need to call gettimeofday() */
496 gettimeofday(&now, NULL);
497 usec = avahi_timeval_diff(&next_timeout->expiry, &now);
500 /* Timeout elapsed */
506 /* Calculate sleep time. We add 1ms because otherwise we'd
507 * wake up too early most of the time */
508 t = (int) (usec / 1000) + 1;
510 if (timeout < 0 || timeout > t)
515 s->prepared_timeout = timeout;
516 s->state = STATE_PREPARED;
520 int avahi_simple_poll_run(AvahiSimplePoll *s) {
522 assert(s->state == STATE_PREPARED || s->state == STATE_FAILURE);
524 s->state = STATE_RUNNING;
526 if (s->prepared_timeout != 0) {
528 if (s->poll_func(s->pollfds, s->n_pollfds, s->prepared_timeout, s->poll_func_userdata) < 0) {
529 s->state = STATE_FAILURE;
533 /* The poll events are now valid again */
539 s->state = STATE_RAN;
543 int avahi_simple_poll_dispatch(AvahiSimplePoll *s) {
544 AvahiTimeout *next_timeout;
548 assert(s->state == STATE_RAN);
549 s->state = STATE_DISPATCHING;
551 /* We execute only on callback in every iteration */
553 /* Check whether the wakeup time has been reached now */
554 if ((next_timeout = find_next_timeout(s))) {
556 if (next_timeout->expiry.tv_sec == 0 && next_timeout->expiry.tv_usec == 0) {
558 /* Just a shortcut so that we don't need to call gettimeofday() */
559 timeout_callback(next_timeout);
563 if (avahi_age(&next_timeout->expiry) >= 0) {
565 /* Timeout elapsed */
566 timeout_callback(next_timeout);
571 /* Look for some kind of I/O event */
572 for (w = s->watches; w; w = w->watches_next) {
578 assert(w->idx < s->n_pollfds);
580 if (s->pollfds[w->idx].revents != 0) {
581 w->callback(w, w->pollfd.fd, s->pollfds[w->idx].revents, w->userdata);
588 s->state = STATE_DISPATCHED;
592 int avahi_simple_poll_iterate(AvahiSimplePoll *s, int timeout) {
595 if ((r = avahi_simple_poll_prepare(s, timeout)) != 0)
598 if ((r = avahi_simple_poll_run(s)) != 0)
601 if ((r = avahi_simple_poll_dispatch(s)) != 0)
607 void avahi_simple_poll_quit(AvahiSimplePoll *s) {
612 /* If there is a background thread running the poll() for us, tell it to exit the poll() */
613 avahi_simple_poll_wakeup(s);
616 const AvahiPoll* avahi_simple_poll_get(AvahiSimplePoll *s) {
622 static int system_poll(struct pollfd *ufds, unsigned int nfds, int timeout, AVAHI_GCC_UNUSED void *userdata) {
623 return poll(ufds, nfds, timeout);
626 void avahi_simple_poll_set_func(AvahiSimplePoll *s, AvahiPollFunc func, void *userdata) {
629 s->poll_func = func ? func : system_poll;
630 s->poll_func_userdata = func ? userdata : NULL;
632 /* If there is a background thread running the poll() for us, tell it to exit the poll() */
633 avahi_simple_poll_wakeup(s);
636 int avahi_simple_poll_loop(AvahiSimplePoll *s) {
642 if ((r = avahi_simple_poll_iterate(s, -1)) != 0)
643 if (r >= 0 || errno != EINTR)