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
31 #include <avahi-common/llist.h>
32 #include <avahi-common/malloc.h>
34 #include "simple-watch.h"
37 AvahiSimplePoll *simple_poll;
41 AvahiWatchCallback callback;
44 AVAHI_LLIST_FIELDS(AvahiWatch, watches);
47 struct AvahiSimplePoll {
50 struct pollfd* pollfds;
51 int n_pollfds, max_pollfds, rebuild_pollfds;
53 struct timeval wakeup;
61 AVAHI_LLIST_HEAD(AvahiWatch, watches);
64 static AvahiWatch* watch_new(AvahiPoll *api, int fd, AvahiWatchEvent event, AvahiWatchCallback callback, void *userdata) {
75 if (!(w = avahi_new(AvahiWatch, 1)))
80 w->pollfd.events = event;
81 w->callback = callback;
82 w->userdata = userdata;
85 if (s->n_pollfds < s->max_pollfds) {
86 /* If there's space for this pollfd, go on and allocate it */
87 w->idx = s->n_pollfds++;
88 s->pollfds[w->idx] = w->pollfd;
91 /* Unfortunately there's no place for this pollfd, so request a rebuild of the array */
93 s->rebuild_pollfds = 1;
96 AVAHI_LLIST_PREPEND(AvahiWatch, watches, s->watches, w);
102 static void watch_update(AvahiWatch *w, AvahiWatchEvent events) {
106 w->pollfd.events = events;
109 assert(w->simple_poll);
110 w->simple_poll->pollfds[w->idx] = w->pollfd;
112 w->simple_poll->rebuild_pollfds = 1;
115 static void remove_pollfd(AvahiWatch *w) {
121 if (w->idx == w->simple_poll->n_pollfds-1) {
123 /* This pollfd is at the end of the array, so we can easily cut it */
125 assert(w->simple_poll->n_pollfds > 0);
126 w->simple_poll->n_pollfds -= 1;
129 /* Unfortunately this pollfd is in the middle of the array, so request a rebuild of it */
130 w->simple_poll->rebuild_pollfds = 1;
133 static void watch_free(AvahiWatch *w) {
140 w->simple_poll->n_watches --;
141 w->simple_poll->req_cleanup = 1;
144 static void set_wakeup_time(AvahiPoll *api, const struct timeval *tv) {
157 static void destroy_watch(AvahiWatch *w) {
161 AVAHI_LLIST_REMOVE(AvahiWatch, watches, w->simple_poll->watches, w);
164 w->simple_poll->n_watches --;
169 static void cleanup(AvahiSimplePoll *s, int all) {
170 AvahiWatch *w, *next;
173 for (w = s->watches; w; w = next) {
174 next = w->watches_next;
183 AvahiSimplePoll *avahi_simple_poll_new(void) {
186 if (!(s = avahi_new(AvahiSimplePoll, 1)))
190 s->api.watch_new = watch_new;
191 s->api.watch_free = watch_free;
192 s->api.watch_update = watch_update;
193 s->api.set_wakeup_time = set_wakeup_time;
195 s->max_pollfds = s->n_pollfds = 0;
197 s->rebuild_pollfds = 0;
202 AVAHI_LLIST_HEAD_INIT(AvahiWatch, s->watches);
207 void avahi_simple_poll_free(AvahiSimplePoll *s) {
212 assert(s->n_watches == 0);
214 avahi_free(s->pollfds);
218 static int rebuild(AvahiSimplePoll *s) {
224 if (s->n_watches > s->max_pollfds) {
227 s->max_pollfds = s->n_watches + 10;
229 if (!(n = avahi_realloc(s->pollfds, sizeof(struct pollfd) * s->max_pollfds)))
235 for (idx = 0, w = s->watches; w; w = w->watches_next) {
240 assert(w->idx < s->max_pollfds);
241 s->pollfds[w->idx = idx++] = w->pollfd;
246 s->rebuild_pollfds = 0;
251 int avahi_simple_poll_iterate(AvahiSimplePoll *s, int block) {
252 int timeout, r, ret = 0;
261 if (s->rebuild_pollfds)
270 gettimeofday(&now, NULL);
272 usec = avahi_timeval_diff(&s->wakeup, &now);
274 timeout = usec <= 0 ? 0 : (int) (usec / 1000);
280 if ((r = poll(s->pollfds, s->n_pollfds, timeout)) < 0)
286 for (w = s->watches; w; w = w->watches_next) {
292 assert(w->idx < s->n_pollfds);
294 if (s->pollfds[w->idx].revents > 0)
295 w->callback(w, w->pollfd.fd, s->pollfds[w->idx].revents, w->userdata);
314 void avahi_simple_poll_quit(AvahiSimplePoll *w) {
320 AvahiPoll* avahi_simple_poll_get(AvahiSimplePoll *s) {