]> code.delx.au - pulseaudio/blob - src/pulse/mainloop.c
fix bug in handling of defer events
[pulseaudio] / src / pulse / mainloop.c
1 /* $Id$ */
2
3 /***
4 This file is part of PulseAudio.
5
6 Copyright 2004-2006 Lennart Poettering
7 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
8
9 PulseAudio is free software; you can redistribute it and/or modify
10 it under the terms of the GNU Lesser General Public License as published
11 by the Free Software Foundation; either version 2 of the License,
12 or (at your option) any later version.
13
14 PulseAudio is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 General Public License for more details.
18
19 You should have received a copy of the GNU Lesser General Public License
20 along with PulseAudio; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
22 USA.
23 ***/
24
25 #ifdef HAVE_CONFIG_H
26 #include <config.h>
27 #endif
28
29 #include <stdio.h>
30 #include <signal.h>
31 #include <unistd.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <assert.h>
35 #include <fcntl.h>
36 #include <errno.h>
37
38 #ifdef HAVE_SYS_POLL_H
39 #include <sys/poll.h>
40 #else
41 #include "../pulsecore/poll.h"
42 #endif
43
44 #include "../pulsecore/winsock.h"
45
46 #ifndef HAVE_PIPE
47 #include "../pulsecore/pipe.h"
48 #endif
49
50 #include <pulsecore/core-error.h>
51 #include <pulse/timeval.h>
52 #include <pulse/xmalloc.h>
53
54 #include <pulsecore/core-util.h>
55 #include <pulsecore/llist.h>
56 #include <pulsecore/log.h>
57
58 #include "mainloop.h"
59
60 struct pa_io_event {
61 pa_mainloop *mainloop;
62 int dead;
63
64 int fd;
65 pa_io_event_flags_t events;
66 struct pollfd *pollfd;
67
68 pa_io_event_cb_t callback;
69 void *userdata;
70 pa_io_event_destroy_cb_t destroy_callback;
71
72 PA_LLIST_FIELDS(pa_io_event);
73 };
74
75 struct pa_time_event {
76 pa_mainloop *mainloop;
77 int dead;
78
79 int enabled;
80 struct timeval timeval;
81
82 pa_time_event_cb_t callback;
83 void *userdata;
84 pa_time_event_destroy_cb_t destroy_callback;
85
86 PA_LLIST_FIELDS(pa_time_event);
87 };
88
89 struct pa_defer_event {
90 pa_mainloop *mainloop;
91 int dead;
92
93 int enabled;
94
95 pa_defer_event_cb_t callback;
96 void *userdata;
97 pa_defer_event_destroy_cb_t destroy_callback;
98
99 PA_LLIST_FIELDS(pa_defer_event);
100 };
101
102 struct pa_mainloop {
103 PA_LLIST_HEAD(pa_io_event, io_events);
104 PA_LLIST_HEAD(pa_time_event, time_events);
105 PA_LLIST_HEAD(pa_defer_event, defer_events);
106
107 int n_enabled_defer_events, n_enabled_time_events, n_io_events;
108 int io_events_please_scan, time_events_please_scan, defer_events_please_scan;
109
110 struct pollfd *pollfds;
111 unsigned max_pollfds, n_pollfds;
112 int rebuild_pollfds;
113
114 int prepared_timeout;
115 pa_time_event *cached_next_time_event;
116
117 int quit, retval;
118 pa_mainloop_api api;
119
120 int wakeup_pipe[2];
121 int wakeup_pipe_type;
122 int wakeup_requested;
123
124 enum {
125 STATE_PASSIVE,
126 STATE_PREPARED,
127 STATE_POLLING,
128 STATE_POLLED,
129 STATE_QUIT
130 } state;
131
132 pa_poll_func poll_func;
133 void *poll_func_userdata;
134 int poll_func_ret;
135 };
136
137 static short map_flags_to_libc(pa_io_event_flags_t flags) {
138 return
139 (flags & PA_IO_EVENT_INPUT ? POLLIN : 0) |
140 (flags & PA_IO_EVENT_OUTPUT ? POLLOUT : 0) |
141 (flags & PA_IO_EVENT_ERROR ? POLLERR : 0) |
142 (flags & PA_IO_EVENT_HANGUP ? POLLHUP : 0);
143 }
144
145 static pa_io_event_flags_t map_flags_from_libc(short flags) {
146 return
147 (flags & POLLIN ? PA_IO_EVENT_INPUT : 0) |
148 (flags & POLLOUT ? PA_IO_EVENT_OUTPUT : 0) |
149 (flags & POLLERR ? PA_IO_EVENT_ERROR : 0) |
150 (flags & POLLHUP ? PA_IO_EVENT_HANGUP : 0);
151 }
152
153 /* IO events */
154 static pa_io_event* mainloop_io_new(
155 pa_mainloop_api*a,
156 int fd,
157 pa_io_event_flags_t events,
158 pa_io_event_cb_t callback,
159 void *userdata) {
160
161 pa_mainloop *m;
162 pa_io_event *e;
163
164 assert(a);
165 assert(a->userdata);
166 assert(fd >= 0);
167 assert(callback);
168
169 m = a->userdata;
170 assert(a == &m->api);
171
172 e = pa_xnew(pa_io_event, 1);
173 e->mainloop = m;
174 e->dead = 0;
175
176 e->fd = fd;
177 e->events = events;
178 e->pollfd = NULL;
179
180 e->callback = callback;
181 e->userdata = userdata;
182 e->destroy_callback = NULL;
183
184 #ifdef OS_IS_WIN32
185 {
186 fd_set xset;
187 struct timeval tv;
188
189 tv.tv_sec = 0;
190 tv.tv_usec = 0;
191
192 FD_ZERO (&xset);
193 FD_SET (fd, &xset);
194
195 if ((select((SELECT_TYPE_ARG1) fd, NULL, NULL, SELECT_TYPE_ARG234 &xset,
196 SELECT_TYPE_ARG5 &tv) == -1) &&
197 (WSAGetLastError() == WSAENOTSOCK)) {
198 pa_log_warn("WARNING: cannot monitor non-socket file descriptors.");
199 e->dead = 1;
200 }
201 }
202 #endif
203
204 PA_LLIST_PREPEND(pa_io_event, m->io_events, e);
205 m->rebuild_pollfds = 1;
206 m->n_io_events ++;
207
208 pa_mainloop_wakeup(m);
209
210 return e;
211 }
212
213 static void mainloop_io_enable(pa_io_event *e, pa_io_event_flags_t events) {
214 assert(e);
215 assert(!e->dead);
216
217 if (e->events == events)
218 return;
219
220 e->events = events;
221
222 if (e->pollfd)
223 e->pollfd->events = map_flags_to_libc(events);
224 else
225 e->mainloop->rebuild_pollfds = 1;
226
227 pa_mainloop_wakeup(e->mainloop);
228 }
229
230 static void mainloop_io_free(pa_io_event *e) {
231 assert(e);
232 assert(!e->dead);
233
234 e->dead = 1;
235 e->mainloop->io_events_please_scan ++;
236
237 e->mainloop->n_io_events --;
238 e->mainloop->rebuild_pollfds = 1;
239
240 pa_mainloop_wakeup(e->mainloop);
241 }
242
243 static void mainloop_io_set_destroy(pa_io_event *e, pa_io_event_destroy_cb_t callback) {
244 assert(e);
245
246 e->destroy_callback = callback;
247 }
248
249 /* Defer events */
250 static pa_defer_event* mainloop_defer_new(
251 pa_mainloop_api*a,
252 pa_defer_event_cb_t callback,
253 void *userdata) {
254
255 pa_mainloop *m;
256 pa_defer_event *e;
257
258 assert(a);
259 assert(a->userdata);
260 assert(callback);
261
262 m = a->userdata;
263 assert(a == &m->api);
264
265 e = pa_xnew(pa_defer_event, 1);
266 e->mainloop = m;
267 e->dead = 0;
268
269 e->enabled = 1;
270 m->n_enabled_defer_events++;
271
272 e->callback = callback;
273 e->userdata = userdata;
274 e->destroy_callback = NULL;
275
276 PA_LLIST_PREPEND(pa_defer_event, m->defer_events, e);
277
278 pa_mainloop_wakeup(e->mainloop);
279
280 return e;
281 }
282
283 static void mainloop_defer_enable(pa_defer_event *e, int b) {
284 assert(e);
285 assert(!e->dead);
286
287 if (e->enabled && !b) {
288 assert(e->mainloop->n_enabled_defer_events > 0);
289 e->mainloop->n_enabled_defer_events--;
290 } else if (!e->enabled && b) {
291 e->mainloop->n_enabled_defer_events++;
292 pa_mainloop_wakeup(e->mainloop);
293 }
294
295 e->enabled = b;
296 }
297
298 static void mainloop_defer_free(pa_defer_event *e) {
299 assert(e);
300 assert(!e->dead);
301
302 e->dead = 1;
303 e->mainloop->defer_events_please_scan ++;
304
305 if (e->enabled) {
306 assert(e->mainloop->n_enabled_defer_events > 0);
307 e->mainloop->n_enabled_defer_events--;
308 }
309 }
310
311 static void mainloop_defer_set_destroy(pa_defer_event *e, pa_defer_event_destroy_cb_t callback) {
312 assert(e);
313 assert(!e->dead);
314
315 e->destroy_callback = callback;
316 }
317
318 /* Time events */
319 static pa_time_event* mainloop_time_new(
320 pa_mainloop_api*a,
321 const struct timeval *tv,
322 pa_time_event_cb_t callback,
323 void *userdata) {
324
325 pa_mainloop *m;
326 pa_time_event *e;
327
328 assert(a);
329 assert(a->userdata);
330 assert(callback);
331
332 m = a->userdata;
333 assert(a == &m->api);
334
335 e = pa_xnew(pa_time_event, 1);
336 e->mainloop = m;
337 e->dead = 0;
338
339 if ((e->enabled = !!tv)) {
340 e->timeval = *tv;
341
342 m->n_enabled_time_events++;
343
344 if (m->cached_next_time_event) {
345 assert(m->cached_next_time_event->enabled);
346
347 if (pa_timeval_cmp(tv, &m->cached_next_time_event->timeval) < 0)
348 m->cached_next_time_event = e;
349 }
350 }
351
352 e->callback = callback;
353 e->userdata = userdata;
354 e->destroy_callback = NULL;
355
356 PA_LLIST_PREPEND(pa_time_event, m->time_events, e);
357
358 if (e->enabled)
359 pa_mainloop_wakeup(m);
360
361 return e;
362 }
363
364 static void mainloop_time_restart(pa_time_event *e, const struct timeval *tv) {
365 assert(e);
366 assert(!e->dead);
367
368 if (e->enabled && !tv) {
369 assert(e->mainloop->n_enabled_time_events > 0);
370 e->mainloop->n_enabled_time_events--;
371 } else if (!e->enabled && tv)
372 e->mainloop->n_enabled_time_events++;
373
374 if ((e->enabled = !!tv)) {
375 e->timeval = *tv;
376 pa_mainloop_wakeup(e->mainloop);
377 }
378
379 if (e->mainloop->cached_next_time_event && e->enabled) {
380 assert(e->mainloop->cached_next_time_event->enabled);
381
382 if (pa_timeval_cmp(tv, &e->mainloop->cached_next_time_event->timeval) < 0)
383 e->mainloop->cached_next_time_event = e;
384 } else if (e->mainloop->cached_next_time_event == e)
385 e->mainloop->cached_next_time_event = NULL;
386 }
387
388 static void mainloop_time_free(pa_time_event *e) {
389 assert(e);
390 assert(!e->dead);
391
392 e->dead = 1;
393 e->mainloop->time_events_please_scan ++;
394
395 if (e->enabled) {
396 assert(e->mainloop->n_enabled_time_events > 0);
397 e->mainloop->n_enabled_time_events--;
398 }
399
400 if (e->mainloop->cached_next_time_event == e)
401 e->mainloop->cached_next_time_event = NULL;
402
403 /* no wakeup needed here. Think about it! */
404 }
405
406 static void mainloop_time_set_destroy(pa_time_event *e, pa_time_event_destroy_cb_t callback) {
407 assert(e);
408 assert(!e->dead);
409
410 e->destroy_callback = callback;
411 }
412
413 /* quit() */
414
415 static void mainloop_quit(pa_mainloop_api*a, int retval) {
416 pa_mainloop *m;
417
418 assert(a);
419 assert(a->userdata);
420 m = a->userdata;
421 assert(a == &m->api);
422
423 pa_mainloop_quit(m, retval);
424 }
425
426 static const pa_mainloop_api vtable = {
427 .userdata = NULL,
428
429 .io_new= mainloop_io_new,
430 .io_enable= mainloop_io_enable,
431 .io_free= mainloop_io_free,
432 .io_set_destroy= mainloop_io_set_destroy,
433
434 .time_new = mainloop_time_new,
435 .time_restart = mainloop_time_restart,
436 .time_free = mainloop_time_free,
437 .time_set_destroy = mainloop_time_set_destroy,
438
439 .defer_new = mainloop_defer_new,
440 .defer_enable = mainloop_defer_enable,
441 .defer_free = mainloop_defer_free,
442 .defer_set_destroy = mainloop_defer_set_destroy,
443
444 .quit = mainloop_quit,
445 };
446
447 pa_mainloop *pa_mainloop_new(void) {
448 pa_mainloop *m;
449
450 m = pa_xnew(pa_mainloop, 1);
451
452 m->wakeup_pipe_type = 0;
453 if (pipe(m->wakeup_pipe) < 0) {
454 pa_log_error("ERROR: cannot create wakeup pipe");
455 pa_xfree(m);
456 return NULL;
457 }
458
459 pa_make_nonblock_fd(m->wakeup_pipe[0]);
460 pa_make_nonblock_fd(m->wakeup_pipe[1]);
461 m->wakeup_requested = 0;
462
463 PA_LLIST_HEAD_INIT(pa_io_event, m->io_events);
464 PA_LLIST_HEAD_INIT(pa_time_event, m->time_events);
465 PA_LLIST_HEAD_INIT(pa_defer_event, m->defer_events);
466
467 m->n_enabled_defer_events = m->n_enabled_time_events = m->n_io_events = 0;
468 m->io_events_please_scan = m->time_events_please_scan = m->defer_events_please_scan = 0;
469
470 m->cached_next_time_event = NULL;
471 m->prepared_timeout = 0;
472
473 m->pollfds = NULL;
474 m->max_pollfds = m->n_pollfds = 0;
475 m->rebuild_pollfds = 1;
476
477 m->quit = m->retval = 0;
478
479 m->api = vtable;
480 m->api.userdata = m;
481
482 m->state = STATE_PASSIVE;
483
484 m->poll_func = NULL;
485 m->poll_func_userdata = NULL;
486 m->poll_func_ret = -1;
487
488 return m;
489 }
490
491 static void cleanup_io_events(pa_mainloop *m, int force) {
492 pa_io_event *e;
493
494 e = m->io_events;
495 while (e) {
496 pa_io_event *n = e->next;
497
498 if (!force && m->io_events_please_scan <= 0)
499 break;
500
501 if (force || e->dead) {
502 PA_LLIST_REMOVE(pa_io_event, m->io_events, e);
503
504 if (e->dead) {
505 assert(m->io_events_please_scan > 0);
506 m->io_events_please_scan--;
507 }
508
509 if (e->destroy_callback)
510 e->destroy_callback(&m->api, e, e->userdata);
511
512 pa_xfree(e);
513
514 m->rebuild_pollfds = 1;
515 }
516
517 e = n;
518 }
519
520 assert(m->io_events_please_scan == 0);
521 }
522
523 static void cleanup_time_events(pa_mainloop *m, int force) {
524 pa_time_event *e;
525
526 e = m->time_events;
527 while (e) {
528 pa_time_event *n = e->next;
529
530 if (!force && m->time_events_please_scan <= 0)
531 break;
532
533 if (force || e->dead) {
534 PA_LLIST_REMOVE(pa_time_event, m->time_events, e);
535
536 if (e->dead) {
537 assert(m->time_events_please_scan > 0);
538 m->time_events_please_scan--;
539 }
540
541 if (!e->dead && e->enabled) {
542 assert(m->n_enabled_time_events > 0);
543 m->n_enabled_time_events--;
544 }
545
546 if (e->destroy_callback)
547 e->destroy_callback(&m->api, e, e->userdata);
548
549 pa_xfree(e);
550 }
551
552 e = n;
553 }
554
555 assert(m->time_events_please_scan == 0);
556 }
557
558 static void cleanup_defer_events(pa_mainloop *m, int force) {
559 pa_defer_event *e;
560
561 e = m->defer_events;
562 while (e) {
563 pa_defer_event *n = e->next;
564
565 if (!force && m->defer_events_please_scan <= 0)
566 break;
567
568 if (force || e->dead) {
569 PA_LLIST_REMOVE(pa_defer_event, m->defer_events, e);
570
571 if (e->dead) {
572 assert(m->defer_events_please_scan > 0);
573 m->defer_events_please_scan--;
574 }
575
576 if (!e->dead && e->enabled) {
577 e->enabled = 0;
578 assert(m->n_enabled_defer_events > 0);
579 m->n_enabled_defer_events--;
580 }
581
582 if (e->destroy_callback)
583 e->destroy_callback(&m->api, e, e->userdata);
584
585 pa_xfree(e);
586 }
587
588 e = n;
589 }
590
591 assert(m->defer_events_please_scan == 0);
592 }
593
594
595 void pa_mainloop_free(pa_mainloop* m) {
596 assert(m);
597
598 cleanup_io_events(m, 1);
599 cleanup_defer_events(m, 1);
600 cleanup_time_events(m, 1);
601
602 pa_xfree(m->pollfds);
603
604 if (m->wakeup_pipe[0] >= 0)
605 close(m->wakeup_pipe[0]);
606 if (m->wakeup_pipe[1] >= 0)
607 close(m->wakeup_pipe[1]);
608
609 pa_xfree(m);
610 }
611
612 static void scan_dead(pa_mainloop *m) {
613 assert(m);
614
615 if (m->io_events_please_scan)
616 cleanup_io_events(m, 0);
617
618 if (m->time_events_please_scan)
619 cleanup_time_events(m, 0);
620
621 if (m->defer_events_please_scan)
622 cleanup_defer_events(m, 0);
623 }
624
625 static void rebuild_pollfds(pa_mainloop *m) {
626 pa_io_event*e;
627 struct pollfd *p;
628 unsigned l;
629
630 l = m->n_io_events + 1;
631 if (m->max_pollfds < l) {
632 l *= 2;
633 m->pollfds = pa_xrealloc(m->pollfds, sizeof(struct pollfd)*l);
634 m->max_pollfds = l;
635 }
636
637 m->n_pollfds = 0;
638 p = m->pollfds;
639
640 if (m->wakeup_pipe[0] >= 0) {
641 m->pollfds[0].fd = m->wakeup_pipe[0];
642 m->pollfds[0].events = POLLIN;
643 m->pollfds[0].revents = 0;
644 p++;
645 m->n_pollfds++;
646 }
647
648 for (e = m->io_events; e; e = e->next) {
649 if (e->dead) {
650 e->pollfd = NULL;
651 continue;
652 }
653
654 e->pollfd = p;
655 p->fd = e->fd;
656 p->events = map_flags_to_libc(e->events);
657 p->revents = 0;
658
659 p++;
660 m->n_pollfds++;
661 }
662
663 m->rebuild_pollfds = 0;
664 }
665
666 static int dispatch_pollfds(pa_mainloop *m) {
667 pa_io_event *e;
668 int r = 0, k;
669
670 assert(m->poll_func_ret > 0);
671
672 for (e = m->io_events, k = m->poll_func_ret; e && !m->quit && k > 0; e = e->next) {
673 if (e->dead || !e->pollfd || !e->pollfd->revents)
674 continue;
675
676 assert(e->pollfd->fd == e->fd && e->callback);
677 e->callback(&m->api, e, e->fd, map_flags_from_libc(e->pollfd->revents), e->userdata);
678 e->pollfd->revents = 0;
679 r++;
680
681 k--;
682 }
683
684 return r;
685 }
686
687 static int dispatch_defer(pa_mainloop *m) {
688 pa_defer_event *e;
689 int r = 0;
690
691 if (m->n_enabled_defer_events <= 0)
692 return 0;
693
694 for (e = m->defer_events; e && !m->quit; e = e->next) {
695 if (e->dead || !e->enabled)
696 continue;
697
698 assert(e->callback);
699 e->callback(&m->api, e, e->userdata);
700 r++;
701 }
702
703 return r;
704 }
705
706 static pa_time_event* find_next_time_event(pa_mainloop *m) {
707 pa_time_event *t, *n = NULL;
708 assert(m);
709
710 if (m->cached_next_time_event)
711 return m->cached_next_time_event;
712
713 for (t = m->time_events; t; t = t->next) {
714
715 if (t->dead || !t->enabled)
716 continue;
717
718 if (!n || pa_timeval_cmp(&t->timeval, &n->timeval) < 0) {
719 n = t;
720
721 /* Shortcut for tv = { 0, 0 } */
722 if (n->timeval.tv_sec <= 0)
723 break;
724 }
725 }
726
727 m->cached_next_time_event = n;
728 return n;
729 }
730
731 static int calc_next_timeout(pa_mainloop *m) {
732 pa_time_event *t;
733 struct timeval now;
734 pa_usec_t usec;
735
736 if (!m->n_enabled_time_events)
737 return -1;
738
739 t = find_next_time_event(m);
740 assert(t);
741
742 if (t->timeval.tv_sec <= 0)
743 return 0;
744
745 pa_gettimeofday(&now);
746
747 if (pa_timeval_cmp(&t->timeval, &now) <= 0)
748 return 0;
749
750 usec = pa_timeval_diff(&t->timeval, &now);
751 return (int) (usec / 1000);
752 }
753
754 static int dispatch_timeout(pa_mainloop *m) {
755 pa_time_event *e;
756 struct timeval now;
757 int r = 0;
758 assert(m);
759
760 if (m->n_enabled_time_events <= 0)
761 return 0;
762
763 pa_gettimeofday(&now);
764
765 for (e = m->time_events; e && !m->quit; e = e->next) {
766
767 if (e->dead || !e->enabled)
768 continue;
769
770 if (pa_timeval_cmp(&e->timeval, &now) <= 0) {
771 assert(e->callback);
772
773 /* Disable time event */
774 mainloop_time_restart(e, NULL);
775
776 e->callback(&m->api, e, &e->timeval, e->userdata);
777
778 r++;
779 }
780 }
781
782 return r;
783 }
784
785 void pa_mainloop_wakeup(pa_mainloop *m) {
786 char c = 'W';
787 assert(m);
788
789 if (m->wakeup_pipe[1] >= 0 && m->state == STATE_POLLING) {
790 pa_write(m->wakeup_pipe[1], &c, sizeof(c), &m->wakeup_pipe_type);
791 m->wakeup_requested++;
792 }
793 }
794
795 static void clear_wakeup(pa_mainloop *m) {
796 char c[10];
797
798 assert(m);
799
800 if (m->wakeup_pipe[0] < 0)
801 return;
802
803 if (m->wakeup_requested) {
804 while (pa_read(m->wakeup_pipe[0], &c, sizeof(c), &m->wakeup_pipe_type) == sizeof(c));
805 m->wakeup_requested = 0;
806 }
807 }
808
809 int pa_mainloop_prepare(pa_mainloop *m, int timeout) {
810 assert(m);
811 assert(m->state == STATE_PASSIVE);
812
813 clear_wakeup(m);
814 scan_dead(m);
815
816 if (m->quit)
817 goto quit;
818
819 if (m->n_enabled_defer_events <= 0) {
820 if (m->rebuild_pollfds)
821 rebuild_pollfds(m);
822
823 m->prepared_timeout = calc_next_timeout(m);
824 if (timeout >= 0 && (timeout < m->prepared_timeout || m->prepared_timeout < 0))
825 m->prepared_timeout = timeout;
826 }
827
828 m->state = STATE_PREPARED;
829 return 0;
830
831 quit:
832 m->state = STATE_QUIT;
833 return -2;
834 }
835
836 int pa_mainloop_poll(pa_mainloop *m) {
837 assert(m);
838 assert(m->state == STATE_PREPARED);
839
840 if (m->quit)
841 goto quit;
842
843 m->state = STATE_POLLING;
844
845 if (m->n_enabled_defer_events )
846 m->poll_func_ret = 0;
847 else {
848 assert(!m->rebuild_pollfds);
849
850 if (m->poll_func)
851 m->poll_func_ret = m->poll_func(m->pollfds, m->n_pollfds, m->prepared_timeout, m->poll_func_userdata);
852 else
853 m->poll_func_ret = poll(m->pollfds, m->n_pollfds, m->prepared_timeout);
854
855 if (m->poll_func_ret < 0) {
856 if (errno == EINTR)
857 m->poll_func_ret = 0;
858 else
859 pa_log("poll(): %s", pa_cstrerror(errno));
860 }
861 }
862
863 m->state = m->poll_func_ret < 0 ? STATE_PASSIVE : STATE_POLLED;
864 return m->poll_func_ret;
865
866 quit:
867 m->state = STATE_QUIT;
868 return -2;
869 }
870
871 int pa_mainloop_dispatch(pa_mainloop *m) {
872 int dispatched = 0;
873
874 assert(m);
875 assert(m->state == STATE_POLLED);
876
877 if (m->quit)
878 goto quit;
879
880 if (m->n_enabled_defer_events)
881 dispatched += dispatch_defer(m);
882 else {
883 if (m->n_enabled_time_events)
884 dispatched += dispatch_timeout(m);
885
886 if (m->quit)
887 goto quit;
888
889 if (m->poll_func_ret > 0)
890 dispatched += dispatch_pollfds(m);
891 }
892
893 if (m->quit)
894 goto quit;
895
896 m->state = STATE_PASSIVE;
897
898 return dispatched;
899
900 quit:
901 m->state = STATE_QUIT;
902 return -2;
903 }
904
905 int pa_mainloop_get_retval(pa_mainloop *m) {
906 assert(m);
907 return m->retval;
908 }
909
910 int pa_mainloop_iterate(pa_mainloop *m, int block, int *retval) {
911 int r;
912 assert(m);
913
914 if ((r = pa_mainloop_prepare(m, block ? -1 : 0)) < 0)
915 goto quit;
916
917 if ((r = pa_mainloop_poll(m)) < 0)
918 goto quit;
919
920 if ((r = pa_mainloop_dispatch(m)) < 0)
921 goto quit;
922
923 return r;
924
925 quit:
926
927 if ((r == -2) && retval)
928 *retval = pa_mainloop_get_retval(m);
929 return r;
930 }
931
932 int pa_mainloop_run(pa_mainloop *m, int *retval) {
933 int r;
934
935 while ((r = pa_mainloop_iterate(m, 1, retval)) >= 0);
936
937 if (r == -2)
938 return 1;
939 else if (r < 0)
940 return -1;
941 else
942 return 0;
943 }
944
945 void pa_mainloop_quit(pa_mainloop *m, int retval) {
946 assert(m);
947
948 m->quit = 1;
949 m->retval = retval;
950 pa_mainloop_wakeup(m);
951 }
952
953 pa_mainloop_api* pa_mainloop_get_api(pa_mainloop*m) {
954 assert(m);
955 return &m->api;
956 }
957
958 void pa_mainloop_set_poll_func(pa_mainloop *m, pa_poll_func poll_func, void *userdata) {
959 assert(m);
960
961 m->poll_func = poll_func;
962 m->poll_func_userdata = userdata;
963 }