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
33 #include <avahi-common/llist.h>
34 #include <avahi-common/malloc.h>
36 #include "simple-watch.h"
39 AvahiSimplePoll *simple_poll;
45 AvahiWatchCallback callback;
48 AVAHI_LLIST_FIELDS(AvahiWatch, watches);
52 AvahiSimplePoll *simple_poll;
56 struct timeval expiry;
58 AvahiTimeoutCallback callback;
61 AVAHI_LLIST_FIELDS(AvahiTimeout, timeouts);
64 struct AvahiSimplePoll {
66 AvahiPollFunc poll_func;
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);
82 static void wakeup(AvahiSimplePoll *s) {
86 write(s->wakeup_pipe[1], &c, sizeof(c));
89 static void clear_wakeup(AvahiSimplePoll *s) {
90 char c[10]; /* Read ten at a time */
93 if (read(s->wakeup_pipe[0], &c, sizeof(c)) != sizeof(c))
97 static int set_nonblock(int fd) {
102 if ((n = fcntl(fd, F_GETFL)) < 0)
108 return fcntl(fd, F_SETFL, n|O_NONBLOCK);
111 static AvahiWatch* watch_new(const AvahiPoll *api, int fd, AvahiWatchEvent event, AvahiWatchCallback callback, void *userdata) {
122 if (!(w = avahi_new(AvahiWatch, 1)))
129 w->pollfd.events = event;
131 w->callback = callback;
132 w->userdata = userdata;
134 if (s->n_pollfds < s->max_pollfds) {
135 /* If there's space for this pollfd, go on and allocate it */
136 w->idx = s->n_pollfds++;
137 s->pollfds[w->idx] = w->pollfd;
140 /* Unfortunately there's no place for this pollfd, so request a rebuild of the array */
142 s->rebuild_pollfds = 1;
145 AVAHI_LLIST_PREPEND(AvahiWatch, watches, s->watches, w);
148 wakeup(w->simple_poll);
153 static void watch_update(AvahiWatch *w, AvahiWatchEvent events) {
157 w->pollfd.events = events;
160 assert(w->simple_poll);
161 w->simple_poll->pollfds[w->idx] = w->pollfd;
163 w->simple_poll->rebuild_pollfds = 1;
165 wakeup(w->simple_poll);
168 static AvahiWatchEvent watch_get_events(AvahiWatch *w) {
172 if (w->idx != -1 && w->simple_poll->events_valid)
173 return w->simple_poll->pollfds[w->idx].revents;
178 static void remove_pollfd(AvahiWatch *w) {
184 if (w->idx == w->simple_poll->n_pollfds-1) {
186 /* This pollfd is at the end of the array, so we can easily cut it */
188 assert(w->simple_poll->n_pollfds > 0);
189 w->simple_poll->n_pollfds -= 1;
192 /* Unfortunately this pollfd is in the middle of the array, so request a rebuild of it */
193 w->simple_poll->rebuild_pollfds = 1;
196 static void watch_free(AvahiWatch *w) {
204 w->simple_poll->n_watches --;
205 w->simple_poll->watch_req_cleanup = 1;
207 wakeup(w->simple_poll);
210 static void destroy_watch(AvahiWatch *w) {
214 AVAHI_LLIST_REMOVE(AvahiWatch, watches, w->simple_poll->watches, w);
217 w->simple_poll->n_watches --;
222 static void cleanup_watches(AvahiSimplePoll *s, int all) {
223 AvahiWatch *w, *next;
226 for (w = s->watches; w; w = next) {
227 next = w->watches_next;
233 s->timeout_req_cleanup = 0;
236 static AvahiTimeout* timeout_new(const AvahiPoll *api, const struct timeval *tv, AvahiTimeoutCallback callback, void *userdata) {
246 if (!(t = avahi_new(AvahiTimeout, 1)))
252 if ((t->enabled = !!tv))
255 t->callback = callback;
256 t->userdata = userdata;
258 AVAHI_LLIST_PREPEND(AvahiTimeout, timeouts, s->timeouts, t);
260 wakeup(t->simple_poll);
264 static void timeout_update(AvahiTimeout *t, const struct timeval *tv) {
268 if ((t->enabled = !!tv))
271 wakeup(t->simple_poll);
274 static void timeout_free(AvahiTimeout *t) {
279 t->simple_poll->timeout_req_cleanup = 1;
281 wakeup(t->simple_poll);
285 static void destroy_timeout(AvahiTimeout *t) {
288 AVAHI_LLIST_REMOVE(AvahiTimeout, timeouts, t->simple_poll->timeouts, t);
293 static void cleanup_timeouts(AvahiSimplePoll *s, int all) {
294 AvahiTimeout *t, *next;
297 for (t = s->timeouts; t; t = next) {
298 next = t->timeouts_next;
304 s->timeout_req_cleanup = 0;
307 AvahiSimplePoll *avahi_simple_poll_new(void) {
310 if (!(s = avahi_new(AvahiSimplePoll, 1)))
313 if (pipe(s->wakeup_pipe) < 0) {
318 set_nonblock(s->wakeup_pipe[0]);
319 set_nonblock(s->wakeup_pipe[1]);
323 s->api.watch_new = watch_new;
324 s->api.watch_free = watch_free;
325 s->api.watch_update = watch_update;
326 s->api.watch_get_events = watch_get_events;
328 s->api.timeout_new = timeout_new;
329 s->api.timeout_free = timeout_free;
330 s->api.timeout_update = timeout_update;
333 s->max_pollfds = s->n_pollfds = 0;
334 s->rebuild_pollfds = 0;
339 s->watch_req_cleanup = 0;
340 s->timeout_req_cleanup = 0;
342 avahi_simple_poll_set_func(s, NULL);
344 AVAHI_LLIST_HEAD_INIT(AvahiWatch, s->watches);
345 AVAHI_LLIST_HEAD_INIT(AvahiTimeout, s->timeouts);
350 void avahi_simple_poll_free(AvahiSimplePoll *s) {
353 cleanup_timeouts(s, 1);
354 cleanup_watches(s, 1);
355 assert(s->n_watches == 0);
357 avahi_free(s->pollfds);
359 if (s->wakeup_pipe[0] >= 0)
360 close(s->wakeup_pipe[0]);
362 if (s->wakeup_pipe[1] >= 0)
363 close(s->wakeup_pipe[1]);
368 static int rebuild(AvahiSimplePoll *s) {
374 if (s->n_watches+1 > s->max_pollfds) {
377 s->max_pollfds = s->n_watches + 10;
379 if (!(n = avahi_realloc(s->pollfds, sizeof(struct pollfd) * s->max_pollfds)))
386 s->pollfds[0].fd = s->wakeup_pipe[0];
387 s->pollfds[0].events = POLLIN;
388 s->pollfds[0].revents = 0;
390 for (idx = 1, w = s->watches; w; w = w->watches_next) {
395 assert(w->idx < s->max_pollfds);
396 s->pollfds[w->idx = idx++] = w->pollfd;
401 s->rebuild_pollfds = 0;
406 static AvahiTimeout* find_next_timeout(AvahiSimplePoll *s) {
407 AvahiTimeout *t, *n = NULL;
410 for (t = s->timeouts; t; t = t->timeouts_next) {
412 if (t->dead || !t->enabled)
415 if (!n || avahi_timeval_compare(&t->expiry, &n->expiry) < 0)
422 static int start_timeout_callback(AvahiTimeout *t) {
428 t->callback(t, t->userdata);
432 int avahi_simple_poll_iterate(AvahiSimplePoll *s, int timeout) {
434 AvahiTimeout *next_timeout;
437 /* Clear pending wakeup requests */
440 /* Cleanup things first */
441 if (s->watch_req_cleanup)
442 cleanup_watches(s, 0);
444 if (s->timeout_req_cleanup)
445 cleanup_timeouts(s, 0);
447 /* Check whether a quit was requested */
451 /* Do we need to rebuild our array of pollfds? */
452 if (s->rebuild_pollfds)
456 /* Calculate the wakeup time */
457 if ((next_timeout = find_next_timeout(s))) {
462 if (next_timeout->expiry.tv_sec == 0 &&
463 next_timeout->expiry.tv_usec == 0) {
465 /* Just a shortcut so that we don't need to call gettimeofday() */
467 /* The events poll() returned in the last call are now no longer valid */
469 return start_timeout_callback(next_timeout);
473 gettimeofday(&now, NULL);
474 usec = avahi_timeval_diff(&next_timeout->expiry, &now);
477 /* Timeout elapsed */
479 /* The events poll() returned in the last call are now no longer valid */
481 return start_timeout_callback(next_timeout);
484 /* Calculate sleep time. We add 1ms because otherwise we'd
485 * wake up too early most of the time */
486 t = (int) (usec / 1000) + 1;
488 if (timeout < 0 || timeout > t)
492 if ((r = s->poll_func(s->pollfds, s->n_pollfds, timeout)) < 0)
495 /* The pollf events are now valid again */
498 /* Check whether the wakeup time has been reached now */
499 if ((next_timeout = find_next_timeout(s))) {
502 gettimeofday(&now, NULL);
504 if (avahi_timeval_compare(&next_timeout->expiry, &now) <= 0)
506 return start_timeout_callback(next_timeout);
512 /* Look for some kind of I/O event */
514 for (w = s->watches; w; w = w->watches_next) {
520 assert(w->idx < s->n_pollfds);
522 if (s->pollfds[w->idx].revents > 0) {
523 /* We execute only on callback in every iteration */
524 w->callback(w, w->pollfd.fd, s->pollfds[w->idx].revents, w->userdata);
533 void avahi_simple_poll_quit(AvahiSimplePoll *s) {
540 const AvahiPoll* avahi_simple_poll_get(AvahiSimplePoll *s) {
546 void avahi_simple_poll_set_func(AvahiSimplePoll *s, AvahiPollFunc func) {
549 s->poll_func = func ? func : (AvahiPollFunc) poll;