summaryrefslogtreecommitdiff
path: root/src/pulse/glib-mainloop.c
blob: 201b6e236d063ac19e086f75d78c7d679ce06ae9 (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
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
/* $Id$ */

/***
  This file is part of PulseAudio.
 
  PulseAudio 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 of the License,
  or (at your option) any later version.
 
  PulseAudio 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 Lesser General Public License
  along with PulseAudio; 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 <assert.h>

#include <pulse/xmalloc.h>
#include <pulse/timeval.h>

#include <pulsecore/idxset.h>
#include <pulsecore/core-util.h>
#include <pulsecore/log.h>
#include <pulsecore/llist.h>

#include <glib.h>
#include "glib-mainloop.h"

struct pa_io_event  {
    pa_glib_mainloop *mainloop;
    int dead;

    GPollFD poll_fd;
    int poll_fd_added;

    pa_io_event_cb_t callback;
    void *userdata;
    pa_io_event_destroy_cb_t destroy_callback;

    PA_LLIST_FIELDS(pa_io_event);
};

struct pa_time_event {
    pa_glib_mainloop *mainloop;
    int dead;

    int enabled;
    struct timeval timeval;

    pa_time_event_cb_t callback;
    void *userdata;
    pa_time_event_destroy_cb_t destroy_callback;

    PA_LLIST_FIELDS(pa_time_event);
};

struct pa_defer_event {
    pa_glib_mainloop *mainloop;
    int dead;

    int enabled;
    
    pa_defer_event_cb_t callback;
    void *userdata;
    pa_defer_event_destroy_cb_t destroy_callback;

    PA_LLIST_FIELDS(pa_defer_event);
};

struct pa_glib_mainloop {
    GSource source;
    
    pa_mainloop_api api;
    GMainContext *context;

    PA_LLIST_HEAD(pa_io_event, io_events);
    PA_LLIST_HEAD(pa_time_event, time_events);
    PA_LLIST_HEAD(pa_defer_event, defer_events);

    int n_enabled_defer_events, n_enabled_time_events;
    int io_events_please_scan, time_events_please_scan, defer_events_please_scan;

    pa_time_event *cached_next_time_event;
};

static void cleanup_io_events(pa_glib_mainloop *g, int force) {
    pa_io_event *e;

    e = g->io_events;
    while (e) {
        pa_io_event *n = e->next;

        if (!force && g->io_events_please_scan <= 0)
            break;
        
        if (force || e->dead) {
            PA_LLIST_REMOVE(pa_io_event, g->io_events, e);

            if (e->dead) {
                g_assert(g->io_events_please_scan > 0);
                g->io_events_please_scan--;
            }
            
            if (e->poll_fd_added)
                g_source_remove_poll(&g->source, &e->poll_fd);
            
            if (e->destroy_callback)
                e->destroy_callback(&g->api, e, e->userdata);
            
            pa_xfree(e);
        }

        e = n;
    }

    g_assert(g->io_events_please_scan == 0);
}

static void cleanup_time_events(pa_glib_mainloop *g, int force) {
    pa_time_event *e;

    e = g->time_events;
    while (e) {
        pa_time_event *n = e->next;

        if (!force && g->time_events_please_scan <= 0)
            break;
        
        if (force || e->dead) {
            PA_LLIST_REMOVE(pa_time_event, g->time_events, e);

            if (e->dead) {
                g_assert(g->time_events_please_scan > 0);
                g->time_events_please_scan--;
            }

            if (!e->dead && e->enabled) {
                g_assert(g->n_enabled_time_events > 0);
                g->n_enabled_time_events--;
            }
            
            if (e->destroy_callback)
                e->destroy_callback(&g->api, e, e->userdata);
            
            pa_xfree(e);
        }

        e = n;
    }

    g_assert(g->time_events_please_scan == 0);
}

static void cleanup_defer_events(pa_glib_mainloop *g, int force) {
    pa_defer_event *e;

    e = g->defer_events;
    while (e) {
        pa_defer_event *n = e->next;

        if (!force && g->defer_events_please_scan <= 0)
            break;
        
        if (force || e->dead) {
            PA_LLIST_REMOVE(pa_defer_event, g->defer_events, e);

            if (e->dead) {
                g_assert(g->defer_events_please_scan > 0);
                g->defer_events_please_scan--;
            }

            if (!e->dead && e->enabled) {
                g_assert(g->n_enabled_defer_events > 0);
                g->n_enabled_defer_events--;
            }
            
            if (e->destroy_callback)
                e->destroy_callback(&g->api, e, e->userdata);
            
            pa_xfree(e);
        }

        e = n;
    }

    g_assert(g->defer_events_please_scan == 0);
}

static gushort map_flags_to_glib(pa_io_event_flags_t flags) {
    return
        (flags & PA_IO_EVENT_INPUT ? G_IO_IN : 0) |
        (flags & PA_IO_EVENT_OUTPUT ? G_IO_OUT : 0) |
        (flags & PA_IO_EVENT_ERROR ? G_IO_ERR : 0) |
        (flags & PA_IO_EVENT_HANGUP ? G_IO_HUP : 0);
}

static pa_io_event_flags_t map_flags_from_glib(gushort flags) {
    return
        (flags & G_IO_IN ? PA_IO_EVENT_INPUT : 0) |
        (flags & G_IO_OUT ? PA_IO_EVENT_OUTPUT : 0) |
        (flags & G_IO_ERR ? PA_IO_EVENT_ERROR : 0) |
        (flags & G_IO_HUP ? PA_IO_EVENT_HANGUP : 0);
}

static pa_io_event* glib_io_new(
        pa_mainloop_api*m,
        int fd,
        pa_io_event_flags_t f,
        pa_io_event_cb_t cb,
        void *userdata) {
    
    pa_io_event *e;
    pa_glib_mainloop *g;

    g_assert(m);
    g_assert(m->userdata);
    g_assert(fd >= 0);
    g_assert(cb);
    
    g = m->userdata;

    e = pa_xnew(pa_io_event, 1);
    e->mainloop = g;
    e->dead = 0;

    e->poll_fd.fd = fd;
    e->poll_fd.events = map_flags_to_glib(f);
    e->poll_fd.revents = 0;
    
    e->callback = cb;
    e->userdata = userdata;
    e->destroy_callback = NULL;

    PA_LLIST_PREPEND(pa_io_event, g->io_events, e);

    g_source_add_poll(&g->source, &e->poll_fd);
    e->poll_fd_added = 1;
    
    return e;
}

static void glib_io_enable(pa_io_event*e, pa_io_event_flags_t f) {
    g_assert(e);
    g_assert(!e->dead);

    e->poll_fd.events = map_flags_to_glib(f);
}

static void glib_io_free(pa_io_event*e) {
    g_assert(e);
    g_assert(!e->dead);

    e->dead = 1;
    e->mainloop->io_events_please_scan++;

    if (e->poll_fd_added) {
        g_source_remove_poll(&e->mainloop->source, &e->poll_fd);
        e->poll_fd_added = 0;
    }
}

static void glib_io_set_destroy(pa_io_event*e, pa_io_event_destroy_cb_t cb) {
    g_assert(e);
    g_assert(!e->dead);
    
    e->destroy_callback = cb;
}

/* Time sources */

static pa_time_event* glib_time_new(
        pa_mainloop_api*m,
        const struct timeval *tv,
        pa_time_event_cb_t cb,
        void *userdata) {
    
    pa_glib_mainloop *g;
    pa_time_event *e;
    
    g_assert(m);
    g_assert(m->userdata);
    g_assert(cb);
    
    g = m->userdata;

    e = pa_xnew(pa_time_event, 1);
    e->mainloop = g;
    e->dead = 0;

    if ((e->enabled = !!tv)) {
        e->timeval = *tv;
        g->n_enabled_time_events++;

        if (g->cached_next_time_event) {
            g_assert(g->cached_next_time_event->enabled);

            if (pa_timeval_cmp(tv, &g->cached_next_time_event->timeval) < 0)
                g->cached_next_time_event = e;
        }
    }
    
    e->callback = cb;
    e->userdata = userdata;
    e->destroy_callback = NULL;

    PA_LLIST_PREPEND(pa_time_event, g->time_events, e);
    
    return e;
}

static void glib_time_restart(pa_time_event*e, const struct timeval *tv) {
    g_assert(e);
    g_assert(!e->dead);

    if (e->enabled && !tv) {
        g_assert(e->mainloop->n_enabled_time_events > 0);
        e->mainloop->n_enabled_time_events--;
    } else if (!e->enabled && tv)
        e->mainloop->n_enabled_time_events++;

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

    if (e->mainloop->cached_next_time_event && e->enabled) {
        g_assert(e->mainloop->cached_next_time_event->enabled);
            
        if (pa_timeval_cmp(tv, &e->mainloop->cached_next_time_event->timeval) < 0)
            e->mainloop->cached_next_time_event = e;
    } else if (e->mainloop->cached_next_time_event == e)
        e->mainloop->cached_next_time_event = NULL;
 }

static void glib_time_free(pa_time_event *e) {
    g_assert(e);
    g_assert(!e->dead);

    e->dead = 1;
    e->mainloop->time_events_please_scan++;

    if (e->enabled)
        e->mainloop->n_enabled_time_events--;

    if (e->mainloop->cached_next_time_event == e)
        e->mainloop->cached_next_time_event = NULL;
}

static void glib_time_set_destroy(pa_time_event *e, pa_time_event_destroy_cb_t cb) {
    g_assert(e);
    g_assert(!e->dead);
    
    e->destroy_callback = cb;
}

/* Deferred sources */

static pa_defer_event* glib_defer_new(
        pa_mainloop_api*m,
        pa_defer_event_cb_t cb,
        void *userdata) {
    
    pa_defer_event *e;
    pa_glib_mainloop *g;

    g_assert(m);
    g_assert(m->userdata);
    g_assert(cb);
    
    g = m->userdata;
    
    e = pa_xnew(pa_defer_event, 1);
    e->mainloop = g;
    e->dead = 0;

    e->enabled = 1;
    g->n_enabled_defer_events++;
    
    e->callback = cb;
    e->userdata = userdata;
    e->destroy_callback = NULL;
    
    PA_LLIST_PREPEND(pa_defer_event, g->defer_events, e);
    return e;
}

static void glib_defer_enable(pa_defer_event *e, int b) {
    g_assert(e);
    g_assert(!e->dead);

    if (e->enabled && !b) {
        g_assert(e->mainloop->n_enabled_defer_events > 0);
        e->mainloop->n_enabled_defer_events--;
    } else if (!e->enabled && b)
        e->mainloop->n_enabled_defer_events++;

    e->enabled = b;
}

static void glib_defer_free(pa_defer_event *e) {
    g_assert(e);
    g_assert(!e->dead);

    e->dead = 1;
    e->mainloop->defer_events_please_scan++;

    if (e->enabled) {
        g_assert(e->mainloop->n_enabled_defer_events > 0);
        e->mainloop->n_enabled_defer_events--;
    }
}

static void glib_defer_set_destroy(pa_defer_event *e, pa_defer_event_destroy_cb_t cb) {
    g_assert(e);
    g_assert(!e->dead);

    e->destroy_callback = cb;
}

/* quit() */

static void glib_quit(pa_mainloop_api*a, PA_GCC_UNUSED int retval) {

    g_warning("quit() ignored");
    
    /* NOOP */
}

static pa_time_event* find_next_time_event(pa_glib_mainloop *g) {
    pa_time_event *t, *n = NULL;
    g_assert(g);

    if (g->cached_next_time_event)
        return g->cached_next_time_event;
    
    for (t = g->time_events; t; t = t->next) {

        if (t->dead || !t->enabled)
            continue;

        if (!n || pa_timeval_cmp(&t->timeval, &n->timeval) < 0) {
            n = t;

            /* Shortcut for tv = { 0, 0 } */
            if (n->timeval.tv_sec <= 0)
                break;
        }
    }

    g->cached_next_time_event = n;
    return n;
}

static void scan_dead(pa_glib_mainloop *g) {
    g_assert(g);
    
    if (g->io_events_please_scan)
        cleanup_io_events(g, 0);

    if (g->time_events_please_scan)
        cleanup_time_events(g, 0);

    if (g->defer_events_please_scan)
        cleanup_defer_events(g, 0);
}

static gboolean prepare_func(GSource *source, gint *timeout) {
    pa_glib_mainloop *g = (pa_glib_mainloop*) source;

    g_assert(g);
    g_assert(timeout);

    scan_dead(g);

    if (g->n_enabled_defer_events) {
        *timeout = 0;
        return TRUE;
    } else if (g->n_enabled_time_events) {
        pa_time_event *t;
        GTimeVal now;
        struct timeval tvnow;
        pa_usec_t usec;

        t = find_next_time_event(g);
        g_assert(t);

        g_source_get_current_time(source, &now);
        tvnow.tv_sec = now.tv_sec;
        tvnow.tv_usec = now.tv_usec;

        if (pa_timeval_cmp(&t->timeval, &tvnow) <= 0) {
            *timeout = 0;
            return TRUE;
        } 
        usec = pa_timeval_diff(&t->timeval, &tvnow);
        *timeout = (gint) (usec / 1000);
    } else
        *timeout = -1;

    return FALSE;
}
static gboolean check_func(GSource *source) {
    pa_glib_mainloop *g = (pa_glib_mainloop*) source;
    pa_io_event *e;

    g_assert(g);

    if (g->n_enabled_defer_events)
        return TRUE;
    else if (g->n_enabled_time_events) {
        pa_time_event *t;
        GTimeVal now;
        struct timeval tvnow;
    
        t = find_next_time_event(g);
        g_assert(t);
        
        g_source_get_current_time(source, &now);
        tvnow.tv_sec = now.tv_sec;
        tvnow.tv_usec = now.tv_usec;

        if (pa_timeval_cmp(&t->timeval, &tvnow) <= 0)
            return TRUE;
    }

    for (e = g->io_events; e; e = e->next)
        if (!e->dead && e->poll_fd.revents != 0)
            return TRUE;

    return FALSE;
}

static gboolean dispatch_func(GSource *source, PA_GCC_UNUSED GSourceFunc callback, PA_GCC_UNUSED gpointer userdata) {
    pa_glib_mainloop *g = (pa_glib_mainloop*) source;
    pa_io_event *e;

    g_assert(g);

    if (g->n_enabled_defer_events) {
        pa_defer_event *d;

        for (d = g->defer_events; d; d = d->next) {
            if (d->dead || !d->enabled)
                continue;

            break;
        }

        g_assert(d);
        
        d->callback(&g->api, d, d->userdata);
        return TRUE;
    }

    if (g->n_enabled_time_events) {
        GTimeVal now;
        struct timeval tvnow;
        pa_time_event *t;

        t = find_next_time_event(g);
        g_assert(t);
        
        g_source_get_current_time(source, &now);
        tvnow.tv_sec = now.tv_sec;
        tvnow.tv_usec = now.tv_usec;

        if (pa_timeval_cmp(&t->timeval, &tvnow) <= 0) {

            /* Disable time event */
            glib_time_restart(t, NULL);
            
            t->callback(&g->api, t, &t->timeval, t->userdata);
            return TRUE;
        }
    }

    for (e = g->io_events; e; e = e->next)
        if (!e->dead && e->poll_fd.revents != 0) {
            e->callback(&g->api, e, e->poll_fd.fd, map_flags_from_glib(e->poll_fd.revents), e->userdata);
            e->poll_fd.revents = 0;
            return TRUE;
        }

    return FALSE;
}

static const pa_mainloop_api vtable = {
    .userdata = NULL,

    .io_new = glib_io_new,
    .io_enable = glib_io_enable,
    .io_free = glib_io_free,
    .io_set_destroy= glib_io_set_destroy,

    .time_new = glib_time_new,
    .time_restart = glib_time_restart,
    .time_free = glib_time_free,
    .time_set_destroy = glib_time_set_destroy,
    
    .defer_new = glib_defer_new,
    .defer_enable = glib_defer_enable,
    .defer_free = glib_defer_free,
    .defer_set_destroy = glib_defer_set_destroy,
    
    .quit = glib_quit,
};

pa_glib_mainloop *pa_glib_mainloop_new(GMainContext *c) {
    pa_glib_mainloop *g;

    static GSourceFuncs source_funcs = {
        prepare_func,
        check_func,
        dispatch_func,
        NULL,
        NULL,
        NULL
    };
    
    g = (pa_glib_mainloop*) g_source_new(&source_funcs, sizeof(pa_glib_mainloop));
    g_main_context_ref(g->context = c ? c : g_main_context_default());
    
    g->api = vtable;
    g->api.userdata = g;

    PA_LLIST_HEAD_INIT(pa_io_event, g->io_events);
    PA_LLIST_HEAD_INIT(pa_time_event, g->time_events);
    PA_LLIST_HEAD_INIT(pa_defer_event, g->defer_events);

    g->n_enabled_defer_events = g->n_enabled_time_events = 0;
    g->io_events_please_scan = g->time_events_please_scan = g->defer_events_please_scan = 0;

    g->cached_next_time_event = NULL;
    
    g_source_attach(&g->source, g->context);
    g_source_set_can_recurse(&g->source, FALSE);
    
    return g;
}

void pa_glib_mainloop_free(pa_glib_mainloop* g) {
    g_assert(g);

    cleanup_io_events(g, 1);
    cleanup_defer_events(g, 1);
    cleanup_time_events(g, 1);

    g_main_context_unref(g->context);
    g_source_destroy(&g->source);
    g_source_unref(&g->source);
}

pa_mainloop_api* pa_glib_mainloop_get_api(pa_glib_mainloop *g) {
    g_assert(g);
    
    return &g->api;
}