]> git.meshlink.io Git - catta/blob - avahi-common/simple-watch.c
implement new main loop abstraction layer
[catta] / avahi-common / simple-watch.c
1 /* $Id$ */
2
3 /***
4   This file is part of avahi.
5  
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.
10  
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.
15  
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
19   USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <sys/poll.h>
27 #include <assert.h>
28 #include <string.h>
29 #include <errno.h>
30
31 #include <avahi-common/llist.h>
32 #include <avahi-common/malloc.h>
33
34 #include "simple-watch.h"
35
36 struct AvahiWatch {
37     AvahiSimplePoll *simple_poll;
38     int dead;
39     int idx;
40     struct pollfd pollfd;
41     AvahiWatchCallback callback;
42     void *userdata;
43
44     AVAHI_LLIST_FIELDS(AvahiWatch, watches);
45 };
46
47 struct AvahiSimplePoll {
48     AvahiPoll api;
49
50     struct pollfd* pollfds;
51     int n_pollfds, max_pollfds, rebuild_pollfds;
52
53     struct timeval wakeup;
54     int use_wakeup;
55
56     int req_cleanup;
57     
58     int quit;
59
60     int n_watches;
61     AVAHI_LLIST_HEAD(AvahiWatch, watches);
62 };
63
64 static AvahiWatch* watch_new(AvahiPoll *api, int fd, AvahiWatchEvent event, AvahiWatchCallback callback, void *userdata) {
65     AvahiWatch *w;
66     AvahiSimplePoll *s;
67     
68     assert(api);
69     assert(fd >= 0);
70     assert(callback);
71
72     s = api->userdata;
73     assert(s);
74
75     if (!(w = avahi_new(AvahiWatch, 1)))
76         return NULL;
77     
78     w->simple_poll = s;
79     w->pollfd.fd = fd;
80     w->pollfd.events = event;
81     w->callback = callback;
82     w->userdata = userdata;
83     w->dead = 0;
84
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;
89         
90     } else {
91         /* Unfortunately there's no place for this pollfd, so request a rebuild of the array */
92         w->idx = -1;
93         s->rebuild_pollfds = 1;
94     }
95
96     AVAHI_LLIST_PREPEND(AvahiWatch, watches, s->watches, w);
97     s->n_watches++;
98
99     return w;
100 }
101
102 static void watch_update(AvahiWatch *w, AvahiWatchEvent events) {
103     assert(w);
104     assert(!w->dead);
105
106     w->pollfd.events = events;
107
108     if (w->idx != -1) {
109         assert(w->simple_poll);
110         w->simple_poll->pollfds[w->idx] = w->pollfd;
111     } else
112         w->simple_poll->rebuild_pollfds = 1;
113 }
114
115 static void remove_pollfd(AvahiWatch *w) {
116     assert(w);
117
118     if (w->idx == -1)
119         return;
120     
121     if (w->idx == w->simple_poll->n_pollfds-1) {
122
123         /* This pollfd is at the end of the array, so we can easily cut it */
124
125         assert(w->simple_poll->n_pollfds > 0);
126         w->simple_poll->n_pollfds -= 1;
127     } else
128
129         /* Unfortunately this pollfd is in the middle of the array, so request a rebuild of it */
130         w->simple_poll->rebuild_pollfds = 1;
131 }
132
133 static void watch_free(AvahiWatch *w) {
134     assert(w);
135     assert(!w->dead);
136
137     remove_pollfd(w);
138     
139     w->dead = 1;
140     w->simple_poll->n_watches --;
141     w->simple_poll->req_cleanup = 1;
142 }
143
144 static void set_wakeup_time(AvahiPoll *api, const struct timeval *tv) {
145     AvahiSimplePoll *s;
146
147     assert(api);
148     s = api->userdata;
149
150     if (tv) {
151         s->wakeup = *tv;
152         s->use_wakeup = 1;
153     } else
154         s->use_wakeup = 0;
155 }
156
157 static void destroy_watch(AvahiWatch *w) {
158     assert(w);
159
160     remove_pollfd(w);
161     AVAHI_LLIST_REMOVE(AvahiWatch, watches, w->simple_poll->watches, w);
162
163     if (!w->dead)
164         w->simple_poll->n_watches --;
165     
166     avahi_free(w);
167 }
168
169 static void cleanup(AvahiSimplePoll *s, int all) {
170     AvahiWatch *w, *next;
171     assert(s);
172
173     for (w = s->watches; w; w = next) {
174         next = w->watches_next;
175
176         if (all || w->dead)
177             destroy_watch(w);
178     }
179
180     s->req_cleanup = 0;
181 }
182
183 AvahiSimplePoll *avahi_simple_poll_new(void) {
184     AvahiSimplePoll *s;
185
186     if (!(s = avahi_new(AvahiSimplePoll, 1)))
187         return NULL;
188     
189     s->api.userdata = s;
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;
194     s->pollfds = NULL;
195     s->max_pollfds = s->n_pollfds = 0;
196     s->use_wakeup = 0;
197     s->rebuild_pollfds = 0;
198     s->quit = 0;
199     s->n_watches = 0;
200     s->req_cleanup = 0;
201
202     AVAHI_LLIST_HEAD_INIT(AvahiWatch, s->watches);
203
204     return s;
205 }
206
207 void avahi_simple_poll_free(AvahiSimplePoll *s) {
208     assert(s);
209
210     cleanup(s, 1);
211     
212     assert(s->n_watches == 0);
213     
214     avahi_free(s->pollfds);
215     avahi_free(s);
216 }
217
218 static int rebuild(AvahiSimplePoll *s) {
219     AvahiWatch *w;
220     int idx;
221     
222     assert(s);
223
224     if (s->n_watches > s->max_pollfds) {
225         struct pollfd *n;
226
227         s->max_pollfds = s->n_watches + 10;
228         
229         if (!(n = avahi_realloc(s->pollfds, sizeof(struct pollfd) * s->max_pollfds)))
230             return -1;
231
232         s->pollfds = n;
233     }
234
235     for (idx = 0, w = s->watches; w; w = w->watches_next) {
236
237         if(w->dead)
238             continue;
239
240         assert(w->idx < s->max_pollfds);
241         s->pollfds[w->idx = idx++] = w->pollfd;
242     }
243
244     s->n_pollfds = idx;
245     
246     s->rebuild_pollfds = 0;
247
248     return 0;
249 }
250
251 int avahi_simple_poll_iterate(AvahiSimplePoll *s, int block) {
252     int timeout, r, ret = 0;
253     assert(s);
254
255     if (s->quit)
256         return 1;
257
258     if (s->req_cleanup)
259         cleanup(s, 0);
260     
261     if (s->rebuild_pollfds)
262         if (rebuild(s) < 0)
263             return -1;
264
265     if (block) {
266         if (s->use_wakeup) {
267             struct timeval now;
268             AvahiUsec usec;
269
270             gettimeofday(&now, NULL);
271
272             usec = avahi_timeval_diff(&s->wakeup, &now);
273             
274             timeout = usec <= 0 ? 0 : (int) (usec / 1000);
275         } else
276             timeout = -1;
277     } else
278         timeout = 0;
279
280     if ((r = poll(s->pollfds, s->n_pollfds, timeout)) < 0)
281         return -1;
282
283     else if (r > 0) {
284         AvahiWatch *w;
285
286         for (w = s->watches; w; w = w->watches_next) {
287
288             if (w->dead)
289                 continue;
290
291             assert(w->idx >= 0);
292             assert(w->idx < s->n_pollfds);
293
294             if (s->pollfds[w->idx].revents > 0)
295                 w->callback(w, w->pollfd.fd, s->pollfds[w->idx].revents, w->userdata);
296
297             if (s->quit) {
298                 ret = 1;
299                 goto finish;
300             }
301         }
302     }
303
304     ret = 0;
305
306 finish:
307
308     if (s->req_cleanup)
309         cleanup(s, 0);
310     
311     return ret;
312 }
313
314 void avahi_simple_poll_quit(AvahiSimplePoll *w) {
315     assert(w);
316
317     w->quit = 1;
318 }
319
320 AvahiPoll* avahi_simple_poll_get(AvahiSimplePoll *s) {
321     assert(s);
322     
323     return &s->api;
324 }