summaryrefslogtreecommitdiffstats
path: root/avahi-common/simple-watch.c
blob: 4bb763a953b718b7a50aed0906ff9a070a9f0546 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
/* $Id$ */

/***
  This file is part of avahi.
 
  avahi is free software; you can redistribute it and/or modify it
  under the terms of the GNU Lesser General Public License as
  published by the Free Software Foundation; either version 2.1 of the
  License, or (at your option) any later version.
 
  avahi is distributed in the hope that it will be useful, but WITHOUT
  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
  Public License for more details.
 
  You should have received a copy of the GNU Lesser General Public
  License along with avahi; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  USA.
***/

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <sys/poll.h>
#include <assert.h>
#include <string.h>
#include <errno.h>

#include <avahi-common/llist.h>
#include <avahi-common/malloc.h>

#include "simple-watch.h"

struct AvahiWatch {
    AvahiSimplePoll *simple_poll;
    int dead;

    int idx;
    struct pollfd pollfd;
    
    AvahiWatchCallback callback;
    void *userdata;

    AVAHI_LLIST_FIELDS(AvahiWatch, watches);
};

struct AvahiTimeout {
    AvahiSimplePoll *simple_poll;
    int dead;

    int enabled;
    struct timeval expiry;
    
    AvahiTimeoutCallback callback;
    void  *userdata;
    
    AVAHI_LLIST_FIELDS(AvahiTimeout, timeouts);
};

struct AvahiSimplePoll {
    AvahiPoll api;
    AvahiPollFunc poll_func;

    struct pollfd* pollfds;
    int n_pollfds, max_pollfds, rebuild_pollfds;

    int watch_req_cleanup, timeout_req_cleanup;
    int quit;
    int events_valid;

    int n_watches;
    AVAHI_LLIST_HEAD(AvahiWatch, watches);
    AVAHI_LLIST_HEAD(AvahiTimeout, timeouts);
};

static AvahiWatch* watch_new(const AvahiPoll *api, int fd, AvahiWatchEvent event, AvahiWatchCallback callback, void *userdata) {
    AvahiWatch *w;
    AvahiSimplePoll *s;
    
    assert(api);
    assert(fd >= 0);
    assert(callback);

    s = api->userdata;
    assert(s);

    if (!(w = avahi_new(AvahiWatch, 1)))
        return NULL;
    
    w->simple_poll = s;
    w->dead = 0;

    w->pollfd.fd = fd;
    w->pollfd.events = event;

    w->callback = callback;
    w->userdata = userdata;

    if (s->n_pollfds < s->max_pollfds) {
        /* If there's space for this pollfd, go on and allocate it */
        w->idx = s->n_pollfds++;
        s->pollfds[w->idx] = w->pollfd;
        
    } else {
        /* Unfortunately there's no place for this pollfd, so request a rebuild of the array */
        w->idx = -1;
        s->rebuild_pollfds = 1;
    }

    AVAHI_LLIST_PREPEND(AvahiWatch, watches, s->watches, w);
    s->n_watches++;

    return w;
}

static void watch_update(AvahiWatch *w, AvahiWatchEvent events) {
    assert(w);
    assert(!w->dead);

    w->pollfd.events = events;

    if (w->idx != -1) {
        assert(w->simple_poll);
        w->simple_poll->pollfds[w->idx] = w->pollfd;
    } else
        w->simple_poll->rebuild_pollfds = 1;
}

static AvahiWatchEvent watch_get_events(AvahiWatch *w) {
    assert(w);
    assert(!w->dead);

    if (w->idx != -1 && w->simple_poll->events_valid)
        return w->simple_poll->pollfds[w->idx].revents;

    return 0;
}

static void remove_pollfd(AvahiWatch *w) {
    assert(w);

    if (w->idx == -1)
        return;
    
    if (w->idx == w->simple_poll->n_pollfds-1) {

        /* This pollfd is at the end of the array, so we can easily cut it */

        assert(w->simple_poll->n_pollfds > 0);
        w->simple_poll->n_pollfds -= 1;
    } else

        /* Unfortunately this pollfd is in the middle of the array, so request a rebuild of it */
        w->simple_poll->rebuild_pollfds = 1;
}

static void watch_free(AvahiWatch *w) {
    assert(w);

    assert(!w->dead);

    remove_pollfd(w);
    
    w->dead = 1;
    w->simple_poll->n_watches --;
    w->simple_poll->watch_req_cleanup = 1;
}

static void destroy_watch(AvahiWatch *w) {
    assert(w);

    remove_pollfd(w);
    AVAHI_LLIST_REMOVE(AvahiWatch, watches, w->simple_poll->watches, w);

    if (!w->dead)
        w->simple_poll->n_watches --;
    
    avahi_free(w);
}

static void cleanup_watches(AvahiSimplePoll *s, int all) {
    AvahiWatch *w, *next;
    assert(s);

    for (w = s->watches; w; w = next) {
        next = w->watches_next;

        if (all || w->dead)
            destroy_watch(w);
    }

    s->timeout_req_cleanup = 0;
}

static AvahiTimeout* timeout_new(const AvahiPoll *api, const struct timeval *tv, AvahiTimeoutCallback callback, void *userdata) {
    AvahiTimeout *t;
    AvahiSimplePoll *s;
    
    assert(api);
    assert(callback);

    s = api->userdata;
    assert(s);

    if (!(t = avahi_new(AvahiTimeout, 1)))
        return NULL;
    
    t->simple_poll = s;
    t->dead = 0;

    if ((t->enabled = !!tv))
        t->expiry = *tv;
        
    t->callback = callback;
    t->userdata = userdata;

    AVAHI_LLIST_PREPEND(AvahiTimeout, timeouts, s->timeouts, t);

    return t;
}

static void timeout_update(AvahiTimeout *t, const struct timeval *tv) {
    assert(t);
    assert(!t->dead);
    
    if ((t->enabled = !!tv))
        t->expiry = *tv;
}

static void timeout_free(AvahiTimeout *t) {
    assert(t);
    assert(!t->dead);

    t->dead = 1;
    t->simple_poll->timeout_req_cleanup = 1;
}


static void destroy_timeout(AvahiTimeout *t) {
    assert(t);

    AVAHI_LLIST_REMOVE(AvahiTimeout, timeouts, t->simple_poll->timeouts, t);

    avahi_free(t);
}

static void cleanup_timeouts(AvahiSimplePoll *s, int all) {
    AvahiTimeout *t, *next;
    assert(s);

    for (t = s->timeouts; t; t = next) {
        next = t->timeouts_next;

        if (all || t->dead)
            destroy_timeout(t);
    }

    s->timeout_req_cleanup = 0;
}

AvahiSimplePoll *avahi_simple_poll_new(void) {
    AvahiSimplePoll *s;

    if (!(s = avahi_new(AvahiSimplePoll, 1)))
        return NULL;
    
    s->api.userdata = s;

    s->api.watch_new = watch_new;
    s->api.watch_free = watch_free;
    s->api.watch_update = watch_update;
    s->api.watch_get_events = watch_get_events;

    s->api.timeout_new = timeout_new;
    s->api.timeout_free = timeout_free;
    s->api.timeout_update = timeout_update;
    
    s->pollfds = NULL;
    s->max_pollfds = s->n_pollfds = 0;
    s->rebuild_pollfds = 0;
    s->quit = 0;
    s->n_watches = 0;
    s->events_valid = 0;
    
    s->watch_req_cleanup = 0;
    s->timeout_req_cleanup = 0;

    avahi_simple_poll_set_func(s, NULL);

    AVAHI_LLIST_HEAD_INIT(AvahiWatch, s->watches);
    AVAHI_LLIST_HEAD_INIT(AvahiTimeout, s->timeouts);

    return s;
}

void avahi_simple_poll_free(AvahiSimplePoll *s) {
    assert(s);

    cleanup_timeouts(s, 1);
    cleanup_watches(s, 1);
    assert(s->n_watches == 0);
    
    avahi_free(s->pollfds);
    avahi_free(s);
}

static int rebuild(AvahiSimplePoll *s) {
    AvahiWatch *w;
    int idx;
    
    assert(s);

    if (s->n_watches > s->max_pollfds) {
        struct pollfd *n;

        s->max_pollfds = s->n_watches + 10;
        
        if (!(n = avahi_realloc(s->pollfds, sizeof(struct pollfd) * s->max_pollfds)))
            return -1;

        s->pollfds = n;
    }

    for (idx = 0, w = s->watches; w; w = w->watches_next) {

        if(w->dead)
            continue;

        assert(w->idx < s->max_pollfds);
        s->pollfds[w->idx = idx++] = w->pollfd;
    }

    s->n_pollfds = idx;
    s->events_valid = 0;
    s->rebuild_pollfds = 0;

    return 0;
}

static AvahiTimeout* find_next_timeout(AvahiSimplePoll *s) {
    AvahiTimeout *t, *n = NULL;
    assert(s);

    for (t = s->timeouts; t; t = t->timeouts_next) {
        
        if (t->dead || !t->enabled)
            continue;
        
        if (!n || avahi_timeval_compare(&t->expiry, &n->expiry) < 0)
            n = t;
    }

    return n;
}

static int start_timeout_callback(AvahiTimeout *t) {
    assert(t);
    assert(!t->dead);
    assert(t->enabled);

    t->enabled = 0;
    t->callback(t, t->userdata);
    return 0;
}

int avahi_simple_poll_iterate(AvahiSimplePoll *s, int timeout) {
    int r;
    AvahiTimeout *next_timeout;
    assert(s);

    /* Cleanup things first */
    if (s->watch_req_cleanup)
        cleanup_watches(s, 0);

    if (s->timeout_req_cleanup)
        cleanup_timeouts(s, 0);

    /* Check whether a quit was requested */
    if (s->quit)
        return 1;

    /* Do we need to rebuild our array of pollfds? */
    if (s->rebuild_pollfds)
        if (rebuild(s) < 0)
            return -1;


    /* Calculate the wakeup time */
    if ((next_timeout = find_next_timeout(s))) {
        struct timeval now;
        int t;
        AvahiUsec usec;

        if (next_timeout->expiry.tv_sec == 0 &&
            next_timeout->expiry.tv_usec == 0) {

            /* Just a shortcut so that we don't need to call gettimeofday() */
            
            /* The events poll() returned in the last call are now no longer valid */
            s->events_valid = 0;
            return start_timeout_callback(next_timeout);
        }

            
        gettimeofday(&now, NULL);
        usec = avahi_timeval_diff(&next_timeout->expiry, &now);

        if (usec <= 0) {
            /* Timeout elapsed */

            /* The events poll() returned in the last call are now no longer valid */
            s->events_valid = 0;
            return start_timeout_callback(next_timeout);
        }

        /* Calculate sleep time. We add 1ms because otherwise we'd
         * wake up too early most of the time */
        t = (int) (usec / 1000) + 1;

        if (timeout < 0 || timeout > t)
            timeout = t;
    }

    if ((r = s->poll_func(s->pollfds, s->n_pollfds, timeout)) < 0)
        return -1;

    /* The pollf events are now valid again */
    s->events_valid = 1;

    /* Check whether the wakeup time has been reached now */
    if (next_timeout) {
        struct timeval now;
        
        gettimeofday(&now, NULL);

        if (avahi_timeval_compare(&next_timeout->expiry, &now) <= 0)
            /* Time elapsed */
            return start_timeout_callback(next_timeout);
    }
    
    if (r > 0) {
        AvahiWatch *w;

        /* Look for some kind of I/O event */

        for (w = s->watches; w; w = w->watches_next) {

            if (w->dead)
                continue;

            assert(w->idx >= 0);
            assert(w->idx < s->n_pollfds);

            if (s->pollfds[w->idx].revents > 0) {
                /* We execute only on callback in every iteration */
                w->callback(w, w->pollfd.fd, s->pollfds[w->idx].revents, w->userdata);
                return 0;
            }
        }
    }

    return 0;
}

void avahi_simple_poll_quit(AvahiSimplePoll *w) {
    assert(w);

    w->quit = 1;
}

const AvahiPoll* avahi_simple_poll_get(AvahiSimplePoll *s) {
    assert(s);
    
    return &s->api;
}

void avahi_simple_poll_set_func(AvahiSimplePoll *s, AvahiPollFunc func) {
    assert(s);

    s->poll_func = func ? func : (AvahiPollFunc) poll;
}