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
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
|
/***
This file is part of PulseAudio.
Copyright 2004-2006 Lennart Poettering
Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
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.1 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 <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
#include <errno.h>
#ifndef HAVE_PIPE
#include <pulsecore/pipe.h>
#endif
#include <pulse/i18n.h>
#include <pulse/rtclock.h>
#include <pulse/timeval.h>
#include <pulse/xmalloc.h>
#include <pulsecore/poll.h>
#include <pulsecore/core-rtclock.h>
#include <pulsecore/core-util.h>
#include <pulsecore/llist.h>
#include <pulsecore/log.h>
#include <pulsecore/core-error.h>
#include <pulsecore/socket.h>
#include <pulsecore/macro.h>
#include "mainloop.h"
#include "internal.h"
struct pa_io_event {
pa_mainloop *mainloop;
pa_bool_t dead:1;
int fd;
pa_io_event_flags_t events;
struct pollfd *pollfd;
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_mainloop *mainloop;
pa_bool_t dead:1;
pa_bool_t enabled:1;
pa_bool_t use_rtclock:1;
pa_usec_t time;
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_mainloop *mainloop;
pa_bool_t dead:1;
pa_bool_t enabled:1;
pa_defer_event_cb_t callback;
void *userdata;
pa_defer_event_destroy_cb_t destroy_callback;
PA_LLIST_FIELDS(pa_defer_event);
};
struct pa_mainloop {
PA_LLIST_HEAD(pa_io_event, io_events);
PA_LLIST_HEAD(pa_time_event, time_events);
PA_LLIST_HEAD(pa_defer_event, defer_events);
unsigned n_enabled_defer_events, n_enabled_time_events, n_io_events;
unsigned io_events_please_scan, time_events_please_scan, defer_events_please_scan;
pa_bool_t rebuild_pollfds:1;
struct pollfd *pollfds;
unsigned max_pollfds, n_pollfds;
pa_usec_t prepared_timeout;
pa_time_event *cached_next_time_event;
pa_mainloop_api api;
int retval;
pa_bool_t quit:1;
pa_bool_t wakeup_requested:1;
int wakeup_pipe[2];
int wakeup_pipe_type;
enum {
STATE_PASSIVE,
STATE_PREPARED,
STATE_POLLING,
STATE_POLLED,
STATE_QUIT
} state;
pa_poll_func poll_func;
void *poll_func_userdata;
int poll_func_ret;
};
static short map_flags_to_libc(pa_io_event_flags_t flags) {
return (short)
((flags & PA_IO_EVENT_INPUT ? POLLIN : 0) |
(flags & PA_IO_EVENT_OUTPUT ? POLLOUT : 0) |
(flags & PA_IO_EVENT_ERROR ? POLLERR : 0) |
(flags & PA_IO_EVENT_HANGUP ? POLLHUP : 0));
}
static pa_io_event_flags_t map_flags_from_libc(short flags) {
return
(flags & POLLIN ? PA_IO_EVENT_INPUT : 0) |
(flags & POLLOUT ? PA_IO_EVENT_OUTPUT : 0) |
(flags & POLLERR ? PA_IO_EVENT_ERROR : 0) |
(flags & POLLHUP ? PA_IO_EVENT_HANGUP : 0);
}
/* IO events */
static pa_io_event* mainloop_io_new(
pa_mainloop_api *a,
int fd,
pa_io_event_flags_t events,
pa_io_event_cb_t callback,
void *userdata) {
pa_mainloop *m;
pa_io_event *e;
pa_assert(a);
pa_assert(a->userdata);
pa_assert(fd >= 0);
pa_assert(callback);
m = a->userdata;
pa_assert(a == &m->api);
e = pa_xnew0(pa_io_event, 1);
e->mainloop = m;
e->fd = fd;
e->events = events;
e->callback = callback;
e->userdata = userdata;
#ifdef OS_IS_WIN32
{
fd_set xset;
struct timeval tv;
tv.tv_sec = 0;
tv.tv_usec = 0;
FD_ZERO (&xset);
FD_SET (fd, &xset);
if ((select((SELECT_TYPE_ARG1) fd, NULL, NULL, SELECT_TYPE_ARG234 &xset,
SELECT_TYPE_ARG5 &tv) == -1) &&
(WSAGetLastError() == WSAENOTSOCK)) {
pa_log_warn("Cannot monitor non-socket file descriptors.");
e->dead = TRUE;
}
}
#endif
PA_LLIST_PREPEND(pa_io_event, m->io_events, e);
m->rebuild_pollfds = TRUE;
m->n_io_events ++;
pa_mainloop_wakeup(m);
return e;
}
static void mainloop_io_enable(pa_io_event *e, pa_io_event_flags_t events) {
pa_assert(e);
pa_assert(!e->dead);
if (e->events == events)
return;
e->events = events;
if (e->pollfd)
e->pollfd->events = map_flags_to_libc(events);
else
e->mainloop->rebuild_pollfds = TRUE;
pa_mainloop_wakeup(e->mainloop);
}
static void mainloop_io_free(pa_io_event *e) {
pa_assert(e);
pa_assert(!e->dead);
e->dead = TRUE;
e->mainloop->io_events_please_scan ++;
e->mainloop->n_io_events --;
e->mainloop->rebuild_pollfds = TRUE;
pa_mainloop_wakeup(e->mainloop);
}
static void mainloop_io_set_destroy(pa_io_event *e, pa_io_event_destroy_cb_t callback) {
pa_assert(e);
e->destroy_callback = callback;
}
/* Defer events */
static pa_defer_event* mainloop_defer_new(
pa_mainloop_api *a,
pa_defer_event_cb_t callback,
void *userdata) {
pa_mainloop *m;
pa_defer_event *e;
pa_assert(a);
pa_assert(a->userdata);
pa_assert(callback);
m = a->userdata;
pa_assert(a == &m->api);
e = pa_xnew0(pa_defer_event, 1);
e->mainloop = m;
e->enabled = TRUE;
m->n_enabled_defer_events++;
e->callback = callback;
e->userdata = userdata;
PA_LLIST_PREPEND(pa_defer_event, m->defer_events, e);
pa_mainloop_wakeup(e->mainloop);
return e;
}
static void mainloop_defer_enable(pa_defer_event *e, int b) {
pa_assert(e);
pa_assert(!e->dead);
if (e->enabled && !b) {
pa_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++;
pa_mainloop_wakeup(e->mainloop);
}
e->enabled = b;
}
static void mainloop_defer_free(pa_defer_event *e) {
pa_assert(e);
pa_assert(!e->dead);
e->dead = TRUE;
e->mainloop->defer_events_please_scan ++;
if (e->enabled) {
pa_assert(e->mainloop->n_enabled_defer_events > 0);
e->mainloop->n_enabled_defer_events--;
e->enabled = FALSE;
}
}
static void mainloop_defer_set_destroy(pa_defer_event *e, pa_defer_event_destroy_cb_t callback) {
pa_assert(e);
pa_assert(!e->dead);
e->destroy_callback = callback;
}
/* Time events */
static pa_usec_t make_rt(const struct timeval *tv, pa_bool_t *use_rtclock) {
struct timeval ttv;
if (!tv) {
*use_rtclock = FALSE;
return PA_USEC_INVALID;
}
ttv = *tv;
*use_rtclock = !!(ttv.tv_usec & PA_TIMEVAL_RTCLOCK);
if (*use_rtclock)
ttv.tv_usec &= ~PA_TIMEVAL_RTCLOCK;
else
pa_rtclock_from_wallclock(&ttv);
return pa_timeval_load(&ttv);
}
static pa_time_event* mainloop_time_new(
pa_mainloop_api *a,
const struct timeval *tv,
pa_time_event_cb_t callback,
void *userdata) {
pa_mainloop *m;
pa_time_event *e;
pa_usec_t t;
pa_bool_t use_rtclock = FALSE;
pa_assert(a);
pa_assert(a->userdata);
pa_assert(callback);
t = make_rt(tv, &use_rtclock);
m = a->userdata;
pa_assert(a == &m->api);
e = pa_xnew0(pa_time_event, 1);
e->mainloop = m;
if ((e->enabled = (t != PA_USEC_INVALID))) {
e->time = t;
e->use_rtclock = use_rtclock;
m->n_enabled_time_events++;
if (m->cached_next_time_event) {
pa_assert(m->cached_next_time_event->enabled);
if (t < m->cached_next_time_event->time)
m->cached_next_time_event = e;
}
}
e->callback = callback;
e->userdata = userdata;
PA_LLIST_PREPEND(pa_time_event, m->time_events, e);
if (e->enabled)
pa_mainloop_wakeup(m);
return e;
}
static void mainloop_time_restart(pa_time_event *e, const struct timeval *tv) {
pa_bool_t valid;
pa_usec_t t;
pa_bool_t use_rtclock = FALSE;
pa_assert(e);
pa_assert(!e->dead);
t = make_rt(tv, &use_rtclock);
valid = (t != PA_USEC_INVALID);
if (e->enabled && !valid) {
pa_assert(e->mainloop->n_enabled_time_events > 0);
e->mainloop->n_enabled_time_events--;
} else if (!e->enabled && valid)
e->mainloop->n_enabled_time_events++;
if ((e->enabled = valid)) {
e->time = t;
e->use_rtclock = use_rtclock;
pa_mainloop_wakeup(e->mainloop);
}
if (e->mainloop->cached_next_time_event && e->enabled) {
pa_assert(e->mainloop->cached_next_time_event->enabled);
if (t < e->mainloop->cached_next_time_event->time)
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 mainloop_time_free(pa_time_event *e) {
pa_assert(e);
pa_assert(!e->dead);
e->dead = TRUE;
e->mainloop->time_events_please_scan ++;
if (e->enabled) {
pa_assert(e->mainloop->n_enabled_time_events > 0);
e->mainloop->n_enabled_time_events--;
e->enabled = FALSE;
}
if (e->mainloop->cached_next_time_event == e)
e->mainloop->cached_next_time_event = NULL;
/* no wakeup needed here. Think about it! */
}
static void mainloop_time_set_destroy(pa_time_event *e, pa_time_event_destroy_cb_t callback) {
pa_assert(e);
pa_assert(!e->dead);
e->destroy_callback = callback;
}
/* quit() */
static void mainloop_quit(pa_mainloop_api *a, int retval) {
pa_mainloop *m;
pa_assert(a);
pa_assert(a->userdata);
m = a->userdata;
pa_assert(a == &m->api);
pa_mainloop_quit(m, retval);
}
static const pa_mainloop_api vtable = {
.userdata = NULL,
.io_new = mainloop_io_new,
.io_enable = mainloop_io_enable,
.io_free = mainloop_io_free,
.io_set_destroy = mainloop_io_set_destroy,
.time_new = mainloop_time_new,
.time_restart = mainloop_time_restart,
.time_free = mainloop_time_free,
.time_set_destroy = mainloop_time_set_destroy,
.defer_new = mainloop_defer_new,
.defer_enable = mainloop_defer_enable,
.defer_free = mainloop_defer_free,
.defer_set_destroy = mainloop_defer_set_destroy,
.quit = mainloop_quit,
};
pa_mainloop *pa_mainloop_new(void) {
pa_mainloop *m;
pa_init_i18n();
m = pa_xnew0(pa_mainloop, 1);
if (pa_pipe_cloexec(m->wakeup_pipe) < 0) {
pa_log_error("ERROR: cannot create wakeup pipe");
pa_xfree(m);
return NULL;
}
pa_make_fd_nonblock(m->wakeup_pipe[0]);
pa_make_fd_nonblock(m->wakeup_pipe[1]);
m->rebuild_pollfds = TRUE;
m->api = vtable;
m->api.userdata = m;
m->state = STATE_PASSIVE;
m->poll_func_ret = -1;
return m;
}
static void cleanup_io_events(pa_mainloop *m, pa_bool_t force) {
pa_io_event *e, *n;
PA_LLIST_FOREACH_SAFE(e, n, m->io_events) {
if (!force && m->io_events_please_scan <= 0)
break;
if (force || e->dead) {
PA_LLIST_REMOVE(pa_io_event, m->io_events, e);
if (e->dead) {
pa_assert(m->io_events_please_scan > 0);
m->io_events_please_scan--;
}
if (e->destroy_callback)
e->destroy_callback(&m->api, e, e->userdata);
pa_xfree(e);
m->rebuild_pollfds = TRUE;
}
}
pa_assert(m->io_events_please_scan == 0);
}
static void cleanup_time_events(pa_mainloop *m, pa_bool_t force) {
pa_time_event *e, *n;
PA_LLIST_FOREACH_SAFE(e, n, m->time_events) {
if (!force && m->time_events_please_scan <= 0)
break;
if (force || e->dead) {
PA_LLIST_REMOVE(pa_time_event, m->time_events, e);
if (e->dead) {
pa_assert(m->time_events_please_scan > 0);
m->time_events_please_scan--;
}
if (!e->dead && e->enabled) {
pa_assert(m->n_enabled_time_events > 0);
m->n_enabled_time_events--;
e->enabled = FALSE;
}
if (e->destroy_callback)
e->destroy_callback(&m->api, e, e->userdata);
pa_xfree(e);
}
}
pa_assert(m->time_events_please_scan == 0);
}
static void cleanup_defer_events(pa_mainloop *m, pa_bool_t force) {
pa_defer_event *e, *n;
PA_LLIST_FOREACH_SAFE(e, n, m->defer_events) {
if (!force && m->defer_events_please_scan <= 0)
break;
if (force || e->dead) {
PA_LLIST_REMOVE(pa_defer_event, m->defer_events, e);
if (e->dead) {
pa_assert(m->defer_events_please_scan > 0);
m->defer_events_please_scan--;
}
if (!e->dead && e->enabled) {
pa_assert(m->n_enabled_defer_events > 0);
m->n_enabled_defer_events--;
e->enabled = FALSE;
}
if (e->destroy_callback)
e->destroy_callback(&m->api, e, e->userdata);
pa_xfree(e);
}
}
pa_assert(m->defer_events_please_scan == 0);
}
void pa_mainloop_free(pa_mainloop *m) {
pa_assert(m);
cleanup_io_events(m, TRUE);
cleanup_defer_events(m, TRUE);
cleanup_time_events(m, TRUE);
pa_xfree(m->pollfds);
pa_close_pipe(m->wakeup_pipe);
pa_xfree(m);
}
static void scan_dead(pa_mainloop *m) {
pa_assert(m);
if (m->io_events_please_scan)
cleanup_io_events(m, FALSE);
if (m->time_events_please_scan)
cleanup_time_events(m, FALSE);
if (m->defer_events_please_scan)
cleanup_defer_events(m, FALSE);
}
static void rebuild_pollfds(pa_mainloop *m) {
pa_io_event*e;
struct pollfd *p;
unsigned l;
l = m->n_io_events + 1;
if (m->max_pollfds < l) {
l *= 2;
m->pollfds = pa_xrealloc(m->pollfds, sizeof(struct pollfd)*l);
m->max_pollfds = l;
}
m->n_pollfds = 0;
p = m->pollfds;
if (m->wakeup_pipe[0] >= 0) {
m->pollfds[0].fd = m->wakeup_pipe[0];
m->pollfds[0].events = POLLIN;
m->pollfds[0].revents = 0;
p++;
m->n_pollfds++;
}
PA_LLIST_FOREACH(e, m->io_events) {
if (e->dead) {
e->pollfd = NULL;
continue;
}
e->pollfd = p;
p->fd = e->fd;
p->events = map_flags_to_libc(e->events);
p->revents = 0;
p++;
m->n_pollfds++;
}
m->rebuild_pollfds = FALSE;
}
static unsigned dispatch_pollfds(pa_mainloop *m) {
pa_io_event *e;
unsigned r = 0, k;
pa_assert(m->poll_func_ret > 0);
k = m->poll_func_ret;
PA_LLIST_FOREACH(e, m->io_events) {
if (k <= 0 || m->quit)
break;
if (e->dead || !e->pollfd || !e->pollfd->revents)
continue;
pa_assert(e->pollfd->fd == e->fd);
pa_assert(e->callback);
e->callback(&m->api, e, e->fd, map_flags_from_libc(e->pollfd->revents), e->userdata);
e->pollfd->revents = 0;
r++;
k--;
}
return r;
}
static unsigned dispatch_defer(pa_mainloop *m) {
pa_defer_event *e;
unsigned r = 0;
if (m->n_enabled_defer_events <= 0)
return 0;
PA_LLIST_FOREACH(e, m->defer_events) {
if (m->quit)
break;
if (e->dead || !e->enabled)
continue;
pa_assert(e->callback);
e->callback(&m->api, e, e->userdata);
r++;
}
return r;
}
static pa_time_event* find_next_time_event(pa_mainloop *m) {
pa_time_event *t, *n = NULL;
pa_assert(m);
if (m->cached_next_time_event)
return m->cached_next_time_event;
PA_LLIST_FOREACH(t, m->time_events) {
if (t->dead || !t->enabled)
continue;
if (!n || t->time < n->time) {
n = t;
/* Shortcut for time == 0 */
if (n->time == 0)
break;
}
}
m->cached_next_time_event = n;
return n;
}
static pa_usec_t calc_next_timeout(pa_mainloop *m) {
pa_time_event *t;
pa_usec_t clock_now;
if (m->n_enabled_time_events <= 0)
return PA_USEC_INVALID;
pa_assert_se(t = find_next_time_event(m));
if (t->time <= 0)
return 0;
clock_now = pa_rtclock_now();
if (t->time <= clock_now)
return 0;
return t->time - clock_now;
}
static unsigned dispatch_timeout(pa_mainloop *m) {
pa_time_event *e;
pa_usec_t now;
unsigned r = 0;
pa_assert(m);
if (m->n_enabled_time_events <= 0)
return 0;
now = pa_rtclock_now();
PA_LLIST_FOREACH(e, m->time_events) {
if (m->quit)
break;
if (e->dead || !e->enabled)
continue;
if (e->time <= now) {
struct timeval tv;
pa_assert(e->callback);
/* Disable time event */
mainloop_time_restart(e, NULL);
e->callback(&m->api, e, pa_timeval_rtstore(&tv, e->time, e->use_rtclock), e->userdata);
r++;
}
}
return r;
}
void pa_mainloop_wakeup(pa_mainloop *m) {
char c = 'W';
pa_assert(m);
if (m->wakeup_pipe[1] >= 0 && m->state == STATE_POLLING) {
pa_write(m->wakeup_pipe[1], &c, sizeof(c), &m->wakeup_pipe_type);
m->wakeup_requested++;
}
}
static void clear_wakeup(pa_mainloop *m) {
char c[10];
pa_assert(m);
if (m->wakeup_pipe[0] < 0)
return;
if (m->wakeup_requested) {
while (pa_read(m->wakeup_pipe[0], &c, sizeof(c), &m->wakeup_pipe_type) == sizeof(c))
;
m->wakeup_requested = 0;
}
}
int pa_mainloop_prepare(pa_mainloop *m, int timeout) {
pa_assert(m);
pa_assert(m->state == STATE_PASSIVE);
clear_wakeup(m);
scan_dead(m);
if (m->quit)
goto quit;
if (m->n_enabled_defer_events <= 0) {
if (m->rebuild_pollfds)
rebuild_pollfds(m);
m->prepared_timeout = calc_next_timeout(m);
if (timeout >= 0) {
uint64_t u = (uint64_t) timeout * PA_USEC_PER_MSEC;
if (u < m->prepared_timeout || m->prepared_timeout == PA_USEC_INVALID)
m->prepared_timeout = timeout;
}
}
m->state = STATE_PREPARED;
return 0;
quit:
m->state = STATE_QUIT;
return -2;
}
static int usec_to_timeout(pa_usec_t u) {
int timeout;
if (u == PA_USEC_INVALID)
return -1;
timeout = (u + PA_USEC_PER_MSEC - 1) / PA_USEC_PER_MSEC;
pa_assert(timeout >= 0);
return timeout;
}
int pa_mainloop_poll(pa_mainloop *m) {
pa_assert(m);
pa_assert(m->state == STATE_PREPARED);
if (m->quit)
goto quit;
m->state = STATE_POLLING;
if (m->n_enabled_defer_events )
m->poll_func_ret = 0;
else {
pa_assert(!m->rebuild_pollfds);
if (m->poll_func)
m->poll_func_ret = m->poll_func(
m->pollfds, m->n_pollfds,
usec_to_timeout(m->prepared_timeout),
m->poll_func_userdata);
else {
#ifdef HAVE_PPOLL
struct timespec ts;
m->poll_func_ret = ppoll(
m->pollfds, m->n_pollfds,
m->prepared_timeout == PA_USEC_INVALID ? NULL : pa_timespec_store(&ts, m->prepared_timeout),
NULL);
#else
m->poll_func_ret = pa_poll(
m->pollfds, m->n_pollfds,
usec_to_timeout(m->prepared_timeout));
#endif
}
if (m->poll_func_ret < 0) {
if (errno == EINTR)
m->poll_func_ret = 0;
else
pa_log("poll(): %s", pa_cstrerror(errno));
}
}
m->state = m->poll_func_ret < 0 ? STATE_PASSIVE : STATE_POLLED;
return m->poll_func_ret;
quit:
m->state = STATE_QUIT;
return -2;
}
int pa_mainloop_dispatch(pa_mainloop *m) {
unsigned dispatched = 0;
pa_assert(m);
pa_assert(m->state == STATE_POLLED);
if (m->quit)
goto quit;
if (m->n_enabled_defer_events)
dispatched += dispatch_defer(m);
else {
if (m->n_enabled_time_events)
dispatched += dispatch_timeout(m);
if (m->quit)
goto quit;
if (m->poll_func_ret > 0)
dispatched += dispatch_pollfds(m);
}
if (m->quit)
goto quit;
m->state = STATE_PASSIVE;
return (int) dispatched;
quit:
m->state = STATE_QUIT;
return -2;
}
int pa_mainloop_get_retval(pa_mainloop *m) {
pa_assert(m);
return m->retval;
}
int pa_mainloop_iterate(pa_mainloop *m, int block, int *retval) {
int r;
pa_assert(m);
if ((r = pa_mainloop_prepare(m, block ? -1 : 0)) < 0)
goto quit;
if ((r = pa_mainloop_poll(m)) < 0)
goto quit;
if ((r = pa_mainloop_dispatch(m)) < 0)
goto quit;
return r;
quit:
if ((r == -2) && retval)
*retval = pa_mainloop_get_retval(m);
return r;
}
int pa_mainloop_run(pa_mainloop *m, int *retval) {
int r;
while ((r = pa_mainloop_iterate(m, 1, retval)) >= 0)
;
if (r == -2)
return 1;
else if (r < 0)
return -1;
else
return 0;
}
void pa_mainloop_quit(pa_mainloop *m, int retval) {
pa_assert(m);
m->quit = TRUE;
m->retval = retval;
pa_mainloop_wakeup(m);
}
pa_mainloop_api* pa_mainloop_get_api(pa_mainloop *m) {
pa_assert(m);
return &m->api;
}
void pa_mainloop_set_poll_func(pa_mainloop *m, pa_poll_func poll_func, void *userdata) {
pa_assert(m);
m->poll_func = poll_func;
m->poll_func_userdata = userdata;
}
pa_bool_t pa_mainloop_is_our_api(pa_mainloop_api *m) {
pa_assert(m);
return m->io_new == mainloop_io_new;
}
|