summaryrefslogtreecommitdiffstats
path: root/src/mainloop.c
blob: b9eee86d4194baa918401533dcb9106fdab6a31e (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
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
/* $Id$ */

/***
  This file is part of polypaudio.
 
  polypaudio is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published
  by the Free Software Foundation; either version 2 of the License,
  or (at your option) any later version.
 
  polypaudio 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
  General Public License for more details.
 
  You should have received a copy of the GNU General Public License
  along with polypaudio; 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 <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <sys/poll.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <fcntl.h>
#include <errno.h>

#include "mainloop.h"
#include "util.h"
#include "idxset.h"

struct mainloop_source_header {
    struct pa_mainloop *mainloop;
    int dead;
};
    
struct mainloop_source_io {
    struct mainloop_source_header header;
    
    int fd;
    enum pa_mainloop_api_io_events events;
    void (*callback) (struct pa_mainloop_api*a, void *id, int fd, enum pa_mainloop_api_io_events events, void *userdata);
    void *userdata;

    struct pollfd *pollfd;
};

struct mainloop_source_fixed_or_idle {
    struct mainloop_source_header header;
    int enabled;

    void (*callback)(struct pa_mainloop_api*a, void *id, void *userdata);
    void *userdata;
};

struct mainloop_source_time {
    struct mainloop_source_header header;
    int enabled;
    
    struct timeval timeval;
    void (*callback)(struct pa_mainloop_api*a, void *id, const struct timeval*tv, void *userdata);
    void *userdata;
};

struct pa_mainloop {
    struct pa_idxset *io_sources, *fixed_sources, *idle_sources, *time_sources;
    int io_sources_scan_dead, fixed_sources_scan_dead, idle_sources_scan_dead, time_sources_scan_dead;

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

    int quit, running, retval;
    struct pa_mainloop_api api;
};

static void setup_api(struct pa_mainloop *m);

struct pa_mainloop *pa_mainloop_new(void) {
    struct pa_mainloop *m;

    m = malloc(sizeof(struct pa_mainloop));
    assert(m);

    m->io_sources = pa_idxset_new(NULL, NULL);
    m->fixed_sources = pa_idxset_new(NULL, NULL);
    m->idle_sources = pa_idxset_new(NULL, NULL);
    m->time_sources = pa_idxset_new(NULL, NULL);

    assert(m->io_sources && m->fixed_sources && m->idle_sources && m->time_sources);

    m->io_sources_scan_dead = m->fixed_sources_scan_dead = m->idle_sources_scan_dead = m->time_sources_scan_dead = 0;
    
    m->pollfds = NULL;
    m->max_pollfds = m->n_pollfds = m->rebuild_pollfds = 0;

    m->quit = m->running = m->retval = 0;

    setup_api(m);
    
    return m;
}

static int foreach(void *p, uint32_t index, int *del, void*userdata) {
    struct mainloop_source_header *h = p;
    int *all = userdata;
    assert(p && del && all);

    if (*all || h->dead) {
        free(h);
        *del = 1;
    }

    return 0;
};

void pa_mainloop_free(struct pa_mainloop* m) {
    int all = 1;
    assert(m);
    pa_idxset_foreach(m->io_sources, foreach, &all);
    pa_idxset_foreach(m->fixed_sources, foreach, &all);
    pa_idxset_foreach(m->idle_sources, foreach, &all);
    pa_idxset_foreach(m->time_sources, foreach, &all);

    pa_idxset_free(m->io_sources, NULL, NULL);
    pa_idxset_free(m->fixed_sources, NULL, NULL);
    pa_idxset_free(m->idle_sources, NULL, NULL);
    pa_idxset_free(m->time_sources, NULL, NULL);

    free(m->pollfds);
    free(m);
}

static void scan_dead(struct pa_mainloop *m) {
    int all = 0;
    assert(m);
    if (m->io_sources_scan_dead)
        pa_idxset_foreach(m->io_sources, foreach, &all);
    if (m->fixed_sources_scan_dead)
        pa_idxset_foreach(m->fixed_sources, foreach, &all);
    if (m->idle_sources_scan_dead)
        pa_idxset_foreach(m->idle_sources, foreach, &all);
    if (m->time_sources_scan_dead)
        pa_idxset_foreach(m->time_sources, foreach, &all);
}

static void rebuild_pollfds(struct pa_mainloop *m) {
    struct mainloop_source_io*s;
    struct pollfd *p;
    uint32_t index = PA_IDXSET_INVALID;
    unsigned l;

    l = pa_idxset_ncontents(m->io_sources);
    if (m->max_pollfds < l) {
        m->pollfds = realloc(m->pollfds, sizeof(struct pollfd)*l);
        m->max_pollfds = l;
    }

    m->n_pollfds = 0;
    p = m->pollfds;
    for (s = pa_idxset_first(m->io_sources, &index); s; s = pa_idxset_next(m->io_sources, &index)) {
        if (s->header.dead) {
            s->pollfd = NULL;
            continue;
        }

        s->pollfd = p;
        p->fd = s->fd;
        p->events = ((s->events & PA_MAINLOOP_API_IO_EVENT_INPUT) ? POLLIN : 0) | ((s->events & PA_MAINLOOP_API_IO_EVENT_OUTPUT) ? POLLOUT : 0);
        p->revents = 0;

        p++;
        m->n_pollfds++;
    }
}

static void dispatch_pollfds(struct pa_mainloop *m) {
    uint32_t index = PA_IDXSET_INVALID;
    struct mainloop_source_io *s;

    for (s = pa_idxset_first(m->io_sources, &index); s; s = pa_idxset_next(m->io_sources, &index)) {
        if (s->header.dead || !s->pollfd || !s->pollfd->revents)
            continue;
        
        assert(s->pollfd->fd == s->fd && s->callback);
        s->callback(&m->api, s, s->fd,
                    ((s->pollfd->revents & POLLHUP) ? PA_MAINLOOP_API_IO_EVENT_HUP : 0) |
                    ((s->pollfd->revents & POLLIN) ? PA_MAINLOOP_API_IO_EVENT_INPUT : 0) |
                    ((s->pollfd->revents & POLLOUT) ? PA_MAINLOOP_API_IO_EVENT_OUTPUT : 0), s->userdata);
        s->pollfd->revents = 0;
    }
}

static void run_fixed_or_idle(struct pa_mainloop *m, struct pa_idxset *i) {
    uint32_t index = PA_IDXSET_INVALID;
    struct mainloop_source_fixed_or_idle *s;

    for (s = pa_idxset_first(i, &index); s; s = pa_idxset_next(i, &index)) {
        if (s->header.dead || !s->enabled)
            continue;

        assert(s->callback);
        s->callback(&m->api, s, s->userdata);
    }
}

static int calc_next_timeout(struct pa_mainloop *m) {
    uint32_t index = PA_IDXSET_INVALID;
    struct mainloop_source_time *s;
    struct timeval now;
    int t = -1;

    if (pa_idxset_isempty(m->time_sources))
        return -1;

    gettimeofday(&now, NULL);
    
    for (s = pa_idxset_first(m->time_sources, &index); s; s = pa_idxset_next(m->time_sources, &index)) {
        int tmp;
        
        if (s->header.dead || !s->enabled)
            continue;

        if (s->timeval.tv_sec < now.tv_sec || (s->timeval.tv_sec == now.tv_sec && s->timeval.tv_usec <= now.tv_usec)) 
            return 0;

        tmp = (s->timeval.tv_sec - now.tv_sec)*1000;
            
        if (s->timeval.tv_usec > now.tv_usec)
            tmp += (s->timeval.tv_usec - now.tv_usec)/1000;
        else
            tmp -= (now.tv_usec - s->timeval.tv_usec)/1000;

        if (tmp == 0)
            return 0;
        else if (t == -1 || tmp < t)
            t = tmp;
    }

    return t;
}

static void dispatch_timeout(struct pa_mainloop *m) {
    uint32_t index = PA_IDXSET_INVALID;
    struct mainloop_source_time *s;
    struct timeval now;
    assert(m);

    if (pa_idxset_isempty(m->time_sources))
        return;

    gettimeofday(&now, NULL);
    for (s = pa_idxset_first(m->time_sources, &index); s; s = pa_idxset_next(m->time_sources, &index)) {
        
        if (s->header.dead || !s->enabled)
            continue;

        if (s->timeval.tv_sec < now.tv_sec || (s->timeval.tv_sec == now.tv_sec && s->timeval.tv_usec <= now.tv_usec)) {
            assert(s->callback);

            s->enabled = 0;
            s->callback(&m->api, s, &s->timeval, s->userdata);
        }
    }
}

static int any_idle_sources(struct pa_mainloop *m) {
    struct mainloop_source_fixed_or_idle *s;
    uint32_t index;
    assert(m);
    
    for (s = pa_idxset_first(m->idle_sources, &index); s; s = pa_idxset_next(m->idle_sources, &index))
        if (!s->header.dead && s->enabled)
            return 1;

    return 0;
}

int pa_mainloop_iterate(struct pa_mainloop *m, int block, int *retval) {
    int r, idle;
    assert(m && !m->running);
    
    if(m->quit) {
        if (retval)
            *retval = m->retval;
        return 1;
    }

    m->running = 1;

    scan_dead(m);
    run_fixed_or_idle(m, m->fixed_sources);

    if (m->rebuild_pollfds) {
        rebuild_pollfds(m);
        m->rebuild_pollfds = 0;
    }

    idle = any_idle_sources(m);

    do {
        int t;

        if (!block || idle)
            t = 0;
        else 
            t = calc_next_timeout(m);
            
        r = poll(m->pollfds, m->n_pollfds, t);
    } while (r < 0 && errno == EINTR);

    dispatch_timeout(m);
    
    if (r > 0)
        dispatch_pollfds(m);
    else if (r == 0 && idle)
        run_fixed_or_idle(m, m->idle_sources);
    else if (r < 0)
        fprintf(stderr, "select(): %s\n", strerror(errno));
    
    m->running = 0;
    return r < 0 ? -1 : 0;
}

int pa_mainloop_run(struct pa_mainloop *m, int *retval) {
    int r;
    while ((r = pa_mainloop_iterate(m, 1, retval)) == 0);
    return r;
}

void pa_mainloop_quit(struct pa_mainloop *m, int r) {
    assert(m);
    m->quit = r;
}

/* IO sources */
static void* mainloop_source_io(struct pa_mainloop_api*a, int fd, enum pa_mainloop_api_io_events events, void (*callback) (struct pa_mainloop_api*a, void *id, int fd, enum pa_mainloop_api_io_events events, void *userdata), void *userdata) {
    struct pa_mainloop *m;
    struct mainloop_source_io *s;
    assert(a && a->userdata && fd >= 0 && callback);
    m = a->userdata;
    assert(a == &m->api);

    s = malloc(sizeof(struct mainloop_source_io));
    assert(s);
    s->header.mainloop = m;
    s->header.dead = 0;

    s->fd = fd;
    s->events = events;
    s->callback = callback;
    s->userdata = userdata;
    s->pollfd = NULL;

    pa_idxset_put(m->io_sources, s, NULL);
    m->rebuild_pollfds = 1;
    return s;
}

static void mainloop_enable_io(struct pa_mainloop_api*a, void* id, enum pa_mainloop_api_io_events events) {
    struct pa_mainloop *m;
    struct mainloop_source_io *s = id;
    assert(a && a->userdata && s && !s->header.dead);
    m = a->userdata;
    assert(a == &m->api && s->header.mainloop == m);

    s->events = events;
    if (s->pollfd)
        s->pollfd->events = ((s->events & PA_MAINLOOP_API_IO_EVENT_INPUT) ? POLLIN : 0) | ((s->events & PA_MAINLOOP_API_IO_EVENT_OUTPUT) ? POLLOUT : 0);
}

static void mainloop_cancel_io(struct pa_mainloop_api*a, void* id) {
    struct pa_mainloop *m;
    struct mainloop_source_io *s = id;
    assert(a && a->userdata && s && !s->header.dead);
    m = a->userdata;
    assert(a == &m->api && s->header.mainloop == m);

    s->header.dead = 1;
    m->io_sources_scan_dead = 1;
    m->rebuild_pollfds = 1;
}

/* Fixed sources */
static void* mainloop_source_fixed(struct pa_mainloop_api*a, void (*callback) (struct pa_mainloop_api*a, void *id, void *userdata), void *userdata) {
    struct pa_mainloop *m;
    struct mainloop_source_fixed_or_idle *s;
    assert(a && a->userdata && callback);
    m = a->userdata;
    assert(a == &m->api);

    s = malloc(sizeof(struct mainloop_source_fixed_or_idle));
    assert(s);
    s->header.mainloop = m;
    s->header.dead = 0;

    s->enabled = 1;
    s->callback = callback;
    s->userdata = userdata;

    pa_idxset_put(m->fixed_sources, s, NULL);
    return s;
}

static void mainloop_enable_fixed(struct pa_mainloop_api*a, void* id, int b) {
    struct pa_mainloop *m;
    struct mainloop_source_fixed_or_idle *s = id;
    assert(a && a->userdata && s && !s->header.dead);
    m = a->userdata;
    assert(a == &m->api);

    s->enabled = b;
}

static void mainloop_cancel_fixed(struct pa_mainloop_api*a, void* id) {
    struct pa_mainloop *m;
    struct mainloop_source_fixed_or_idle *s = id;
    assert(a && a->userdata && s && !s->header.dead);
    m = a->userdata;
    assert(a == &m->api);

    s->header.dead = 1;
    m->fixed_sources_scan_dead = 1;
}

/* Idle sources */
static void* mainloop_source_idle(struct pa_mainloop_api*a, void (*callback) (struct pa_mainloop_api*a, void *id, void *userdata), void *userdata) {
    struct pa_mainloop *m;
    struct mainloop_source_fixed_or_idle *s;
    assert(a && a->userdata && callback);
    m = a->userdata;
    assert(a == &m->api);

    s = malloc(sizeof(struct mainloop_source_fixed_or_idle));
    assert(s);
    s->header.mainloop = m;
    s->header.dead = 0;

    s->enabled = 1;
    s->callback = callback;
    s->userdata = userdata;

    pa_idxset_put(m->idle_sources, s, NULL);
    return s;
}

static void mainloop_cancel_idle(struct pa_mainloop_api*a, void* id) {
    struct pa_mainloop *m;
    struct mainloop_source_fixed_or_idle *s = id;
    assert(a && a->userdata && s && !s->header.dead);
    m = a->userdata;
    assert(a == &m->api);

    s->header.dead = 1;
    m->idle_sources_scan_dead = 1;
}

/* Time sources */
static void* mainloop_source_time(struct pa_mainloop_api*a, const struct timeval *tv, void (*callback) (struct pa_mainloop_api*a, void *id, const struct timeval *tv, void *userdata), void *userdata) {
    struct pa_mainloop *m;
    struct mainloop_source_time *s;
    assert(a && a->userdata && callback);
    m = a->userdata;
    assert(a == &m->api);

    s = malloc(sizeof(struct mainloop_source_time));
    assert(s);
    s->header.mainloop = m;
    s->header.dead = 0;

    s->enabled = !!tv;
    if (tv)
        s->timeval = *tv;

    s->callback = callback;
    s->userdata = userdata;

    pa_idxset_put(m->time_sources, s, NULL);
    return s;
}

static void mainloop_enable_time(struct pa_mainloop_api*a, void *id, const struct timeval *tv) {
    struct pa_mainloop *m;
    struct mainloop_source_time *s = id;
    assert(a && a->userdata && s && !s->header.dead);
    m = a->userdata;
    assert(a == &m->api);

    if (tv) {
        s->enabled = 1;
        s->timeval = *tv;
    } else
        s->enabled = 0;
}

static void mainloop_cancel_time(struct pa_mainloop_api*a, void* id) {
    struct pa_mainloop *m;
    struct mainloop_source_time *s = id;
    assert(a && a->userdata && s && !s->header.dead);
    m = a->userdata;
    assert(a == &m->api);

    s->header.dead = 1;
    m->time_sources_scan_dead = 1;

}

static void mainloop_quit(struct pa_mainloop_api*a, int retval) {
    struct pa_mainloop *m;
    assert(a && a->userdata);
    m = a->userdata;
    assert(a == &m->api);

    m->quit = 1;
    m->retval = retval;
}
    
static void setup_api(struct pa_mainloop *m) {
    assert(m);
    
    m->api.userdata = m;
    m->api.source_io = mainloop_source_io;
    m->api.enable_io = mainloop_enable_io;
    m->api.cancel_io = mainloop_cancel_io;

    m->api.source_fixed = mainloop_source_fixed;
    m->api.enable_fixed = mainloop_enable_fixed;
    m->api.cancel_fixed = mainloop_cancel_fixed;

    m->api.source_idle = mainloop_source_idle;
    m->api.enable_idle = mainloop_enable_fixed; /* (!) */
    m->api.cancel_idle = mainloop_cancel_idle;
    
    m->api.source_time = mainloop_source_time;
    m->api.enable_time = mainloop_enable_time;
    m->api.cancel_time = mainloop_cancel_time;

    m->api.quit = mainloop_quit;
}

struct pa_mainloop_api* pa_mainloop_get_api(struct pa_mainloop*m) {
    assert(m);
    return &m->api;
}