]> code.delx.au - pulseaudio/blob - src/pulse/mainloop.c
0dd0e119d9c7edfb98573bb2c5eb89e44c18aa82
[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 e->enabled = 0;
309 }
310 }
311
312 static void mainloop_defer_set_destroy(pa_defer_event *e, pa_defer_event_destroy_cb_t callback) {
313 assert(e);
314 assert(!e->dead);
315
316 e->destroy_callback = callback;
317 }
318
319 /* Time events */
320 static pa_time_event* mainloop_time_new(
321 pa_mainloop_api*a,
322 const struct timeval *tv,
323 pa_time_event_cb_t callback,
324 void *userdata) {
325
326 pa_mainloop *m;
327 pa_time_event *e;
328
329 assert(a);
330 assert(a->userdata);
331 assert(callback);
332
333 m = a->userdata;
334 assert(a == &m->api);
335
336 e = pa_xnew(pa_time_event, 1);
337 e->mainloop = m;
338 e->dead = 0;
339
340 if ((e->enabled = !!tv)) {
341 e->timeval = *tv;
342
343 m->n_enabled_time_events++;
344
345 if (m->cached_next_time_event) {
346 assert(m->cached_next_time_event->enabled);
347
348 if (pa_timeval_cmp(tv, &m->cached_next_time_event->timeval) < 0)
349 m->cached_next_time_event = e;
350 }
351 }
352
353 e->callback = callback;
354 e->userdata = userdata;
355 e->destroy_callback = NULL;
356
357 PA_LLIST_PREPEND(pa_time_event, m->time_events, e);
358
359 if (e->enabled)
360 pa_mainloop_wakeup(m);
361
362 return e;
363 }
364
365 static void mainloop_time_restart(pa_time_event *e, const struct timeval *tv) {
366 assert(e);
367 assert(!e->dead);
368
369 if (e->enabled && !tv) {
370 assert(e->mainloop->n_enabled_time_events > 0);
371 e->mainloop->n_enabled_time_events--;
372 } else if (!e->enabled && tv)
373 e->mainloop->n_enabled_time_events++;
374
375 if ((e->enabled = !!tv)) {
376 e->timeval = *tv;
377 pa_mainloop_wakeup(e->mainloop);
378 }
379
380 if (e->mainloop->cached_next_time_event && e->enabled) {
381 assert(e->mainloop->cached_next_time_event->enabled);
382
383 if (pa_timeval_cmp(tv, &e->mainloop->cached_next_time_event->timeval) < 0)
384 e->mainloop->cached_next_time_event = e;
385 } else if (e->mainloop->cached_next_time_event == e)
386 e->mainloop->cached_next_time_event = NULL;
387 }
388
389 static void mainloop_time_free(pa_time_event *e) {
390 assert(e);
391 assert(!e->dead);
392
393 e->dead = 1;
394 e->mainloop->time_events_please_scan ++;
395
396 if (e->enabled) {
397 assert(e->mainloop->n_enabled_time_events > 0);
398 e->mainloop->n_enabled_time_events--;
399 e->enabled = 0;
400 }
401
402 if (e->mainloop->cached_next_time_event == e)
403 e->mainloop->cached_next_time_event = NULL;
404
405 /* no wakeup needed here. Think about it! */
406 }
407
408 static void mainloop_time_set_destroy(pa_time_event *e, pa_time_event_destroy_cb_t callback) {
409 assert(e);
410 assert(!e->dead);
411
412 e->destroy_callback = callback;
413 }
414
415 /* quit() */
416
417 static void mainloop_quit(pa_mainloop_api*a, int retval) {
418 pa_mainloop *m;
419
420 assert(a);
421 assert(a->userdata);
422 m = a->userdata;
423 assert(a == &m->api);
424
425 pa_mainloop_quit(m, retval);
426 }
427
428 static const pa_mainloop_api vtable = {
429 .userdata = NULL,
430
431 .io_new= mainloop_io_new,
432 .io_enable= mainloop_io_enable,
433 .io_free= mainloop_io_free,
434 .io_set_destroy= mainloop_io_set_destroy,
435
436 .time_new = mainloop_time_new,
437 .time_restart = mainloop_time_restart,
438 .time_free = mainloop_time_free,
439 .time_set_destroy = mainloop_time_set_destroy,
440
441 .defer_new = mainloop_defer_new,
442 .defer_enable = mainloop_defer_enable,
443 .defer_free = mainloop_defer_free,
444 .defer_set_destroy = mainloop_defer_set_destroy,
445
446 .quit = mainloop_quit,
447 };
448
449 pa_mainloop *pa_mainloop_new(void) {
450 pa_mainloop *m;
451
452 m = pa_xnew(pa_mainloop, 1);
453
454 m->wakeup_pipe_type = 0;
455 if (pipe(m->wakeup_pipe) < 0) {
456 pa_log_error("ERROR: cannot create wakeup pipe");
457 pa_xfree(m);
458 return NULL;
459 }
460
461 pa_make_nonblock_fd(m->wakeup_pipe[0]);
462 pa_make_nonblock_fd(m->wakeup_pipe[1]);
463 pa_fd_set_cloexec(m->wakeup_pipe[0], 1);
464 pa_fd_set_cloexec(m->wakeup_pipe[1], 1);
465 m->wakeup_requested = 0;
466
467 PA_LLIST_HEAD_INIT(pa_io_event, m->io_events);
468 PA_LLIST_HEAD_INIT(pa_time_event, m->time_events);
469 PA_LLIST_HEAD_INIT(pa_defer_event, m->defer_events);
470
471 m->n_enabled_defer_events = m->n_enabled_time_events = m->n_io_events = 0;
472 m->io_events_please_scan = m->time_events_please_scan = m->defer_events_please_scan = 0;
473
474 m->cached_next_time_event = NULL;
475 m->prepared_timeout = 0;
476
477 m->pollfds = NULL;
478 m->max_pollfds = m->n_pollfds = 0;
479 m->rebuild_pollfds = 1;
480
481 m->quit = m->retval = 0;
482
483 m->api = vtable;
484 m->api.userdata = m;
485
486 m->state = STATE_PASSIVE;
487
488 m->poll_func = NULL;
489 m->poll_func_userdata = NULL;
490 m->poll_func_ret = -1;
491
492 return m;
493 }
494
495 static void cleanup_io_events(pa_mainloop *m, int force) {
496 pa_io_event *e;
497
498 e = m->io_events;
499 while (e) {
500 pa_io_event *n = e->next;
501
502 if (!force && m->io_events_please_scan <= 0)
503 break;
504
505 if (force || e->dead) {
506 PA_LLIST_REMOVE(pa_io_event, m->io_events, e);
507
508 if (e->dead) {
509 assert(m->io_events_please_scan > 0);
510 m->io_events_please_scan--;
511 }
512
513 if (e->destroy_callback)
514 e->destroy_callback(&m->api, e, e->userdata);
515
516 pa_xfree(e);
517
518 m->rebuild_pollfds = 1;
519 }
520
521 e = n;
522 }
523
524 assert(m->io_events_please_scan == 0);
525 }
526
527 static void cleanup_time_events(pa_mainloop *m, int force) {
528 pa_time_event *e;
529
530 e = m->time_events;
531 while (e) {
532 pa_time_event *n = e->next;
533
534 if (!force && m->time_events_please_scan <= 0)
535 break;
536
537 if (force || e->dead) {
538 PA_LLIST_REMOVE(pa_time_event, m->time_events, e);
539
540 if (e->dead) {
541 assert(m->time_events_please_scan > 0);
542 m->time_events_please_scan--;
543 }
544
545 if (!e->dead && e->enabled) {
546 assert(m->n_enabled_time_events > 0);
547 m->n_enabled_time_events--;
548 e->enabled = 0;
549 }
550
551 if (e->destroy_callback)
552 e->destroy_callback(&m->api, e, e->userdata);
553
554 pa_xfree(e);
555 }
556
557 e = n;
558 }
559
560 assert(m->time_events_please_scan == 0);
561 }
562
563 static void cleanup_defer_events(pa_mainloop *m, int force) {
564 pa_defer_event *e;
565
566 e = m->defer_events;
567 while (e) {
568 pa_defer_event *n = e->next;
569
570 if (!force && m->defer_events_please_scan <= 0)
571 break;
572
573 if (force || e->dead) {
574 PA_LLIST_REMOVE(pa_defer_event, m->defer_events, e);
575
576 if (e->dead) {
577 assert(m->defer_events_please_scan > 0);
578 m->defer_events_please_scan--;
579 }
580
581 if (!e->dead && e->enabled) {
582 assert(m->n_enabled_defer_events > 0);
583 m->n_enabled_defer_events--;
584 e->enabled = 0;
585 }
586
587 if (e->destroy_callback)
588 e->destroy_callback(&m->api, e, e->userdata);
589
590 pa_xfree(e);
591 }
592
593 e = n;
594 }
595
596 assert(m->defer_events_please_scan == 0);
597 }
598
599
600 void pa_mainloop_free(pa_mainloop* m) {
601 assert(m);
602
603 cleanup_io_events(m, 1);
604 cleanup_defer_events(m, 1);
605 cleanup_time_events(m, 1);
606
607 pa_xfree(m->pollfds);
608
609 if (m->wakeup_pipe[0] >= 0)
610 close(m->wakeup_pipe[0]);
611 if (m->wakeup_pipe[1] >= 0)
612 close(m->wakeup_pipe[1]);
613
614 pa_xfree(m);
615 }
616
617 static void scan_dead(pa_mainloop *m) {
618 assert(m);
619
620 if (m->io_events_please_scan)
621 cleanup_io_events(m, 0);
622
623 if (m->time_events_please_scan)
624 cleanup_time_events(m, 0);
625
626 if (m->defer_events_please_scan)
627 cleanup_defer_events(m, 0);
628 }
629
630 static void rebuild_pollfds(pa_mainloop *m) {
631 pa_io_event*e;
632 struct pollfd *p;
633 unsigned l;
634
635 l = m->n_io_events + 1;
636 if (m->max_pollfds < l) {
637 l *= 2;
638 m->pollfds = pa_xrealloc(m->pollfds, sizeof(struct pollfd)*l);
639 m->max_pollfds = l;
640 }
641
642 m->n_pollfds = 0;
643 p = m->pollfds;
644
645 if (m->wakeup_pipe[0] >= 0) {
646 m->pollfds[0].fd = m->wakeup_pipe[0];
647 m->pollfds[0].events = POLLIN;
648 m->pollfds[0].revents = 0;
649 p++;
650 m->n_pollfds++;
651 }
652
653 for (e = m->io_events; e; e = e->next) {
654 if (e->dead) {
655 e->pollfd = NULL;
656 continue;
657 }
658
659 e->pollfd = p;
660 p->fd = e->fd;
661 p->events = map_flags_to_libc(e->events);
662 p->revents = 0;
663
664 p++;
665 m->n_pollfds++;
666 }
667
668 m->rebuild_pollfds = 0;
669 }
670
671 static int dispatch_pollfds(pa_mainloop *m) {
672 pa_io_event *e;
673 int r = 0, k;
674
675 assert(m->poll_func_ret > 0);
676
677 for (e = m->io_events, k = m->poll_func_ret; e && !m->quit && k > 0; e = e->next) {
678 if (e->dead || !e->pollfd || !e->pollfd->revents)
679 continue;
680
681 assert(e->pollfd->fd == e->fd && e->callback);
682 e->callback(&m->api, e, e->fd, map_flags_from_libc(e->pollfd->revents), e->userdata);
683 e->pollfd->revents = 0;
684 r++;
685
686 k--;
687 }
688
689 return r;
690 }
691
692 static int dispatch_defer(pa_mainloop *m) {
693 pa_defer_event *e;
694 int r = 0;
695
696 if (m->n_enabled_defer_events <= 0)
697 return 0;
698
699 for (e = m->defer_events; e && !m->quit; e = e->next) {
700 if (e->dead || !e->enabled)
701 continue;
702
703 assert(e->callback);
704 e->callback(&m->api, e, e->userdata);
705 r++;
706 }
707
708 return r;
709 }
710
711 static pa_time_event* find_next_time_event(pa_mainloop *m) {
712 pa_time_event *t, *n = NULL;
713 assert(m);
714
715 if (m->cached_next_time_event)
716 return m->cached_next_time_event;
717
718 for (t = m->time_events; t; t = t->next) {
719
720 if (t->dead || !t->enabled)
721 continue;
722
723 if (!n || pa_timeval_cmp(&t->timeval, &n->timeval) < 0) {
724 n = t;
725
726 /* Shortcut for tv = { 0, 0 } */
727 if (n->timeval.tv_sec <= 0)
728 break;
729 }
730 }
731
732 m->cached_next_time_event = n;
733 return n;
734 }
735
736 static int calc_next_timeout(pa_mainloop *m) {
737 pa_time_event *t;
738 struct timeval now;
739 pa_usec_t usec;
740
741 if (!m->n_enabled_time_events)
742 return -1;
743
744 t = find_next_time_event(m);
745 assert(t);
746
747 if (t->timeval.tv_sec <= 0)
748 return 0;
749
750 pa_gettimeofday(&now);
751
752 if (pa_timeval_cmp(&t->timeval, &now) <= 0)
753 return 0;
754
755 usec = pa_timeval_diff(&t->timeval, &now);
756 return (int) (usec / 1000);
757 }
758
759 static int dispatch_timeout(pa_mainloop *m) {
760 pa_time_event *e;
761 struct timeval now;
762 int r = 0;
763 assert(m);
764
765 if (m->n_enabled_time_events <= 0)
766 return 0;
767
768 pa_gettimeofday(&now);
769
770 for (e = m->time_events; e && !m->quit; e = e->next) {
771
772 if (e->dead || !e->enabled)
773 continue;
774
775 if (pa_timeval_cmp(&e->timeval, &now) <= 0) {
776 assert(e->callback);
777
778 /* Disable time event */
779 mainloop_time_restart(e, NULL);
780
781 e->callback(&m->api, e, &e->timeval, e->userdata);
782
783 r++;
784 }
785 }
786
787 return r;
788 }
789
790 void pa_mainloop_wakeup(pa_mainloop *m) {
791 char c = 'W';
792 assert(m);
793
794 if (m->wakeup_pipe[1] >= 0 && m->state == STATE_POLLING) {
795 pa_write(m->wakeup_pipe[1], &c, sizeof(c), &m->wakeup_pipe_type);
796 m->wakeup_requested++;
797 }
798 }
799
800 static void clear_wakeup(pa_mainloop *m) {
801 char c[10];
802
803 assert(m);
804
805 if (m->wakeup_pipe[0] < 0)
806 return;
807
808 if (m->wakeup_requested) {
809 while (pa_read(m->wakeup_pipe[0], &c, sizeof(c), &m->wakeup_pipe_type) == sizeof(c));
810 m->wakeup_requested = 0;
811 }
812 }
813
814 int pa_mainloop_prepare(pa_mainloop *m, int timeout) {
815 assert(m);
816 assert(m->state == STATE_PASSIVE);
817
818 clear_wakeup(m);
819 scan_dead(m);
820
821 if (m->quit)
822 goto quit;
823
824 if (m->n_enabled_defer_events <= 0) {
825 if (m->rebuild_pollfds)
826 rebuild_pollfds(m);
827
828 m->prepared_timeout = calc_next_timeout(m);
829 if (timeout >= 0 && (timeout < m->prepared_timeout || m->prepared_timeout < 0))
830 m->prepared_timeout = timeout;
831 }
832
833 m->state = STATE_PREPARED;
834 return 0;
835
836 quit:
837 m->state = STATE_QUIT;
838 return -2;
839 }
840
841 int pa_mainloop_poll(pa_mainloop *m) {
842 assert(m);
843 assert(m->state == STATE_PREPARED);
844
845 if (m->quit)
846 goto quit;
847
848 m->state = STATE_POLLING;
849
850 if (m->n_enabled_defer_events )
851 m->poll_func_ret = 0;
852 else {
853 assert(!m->rebuild_pollfds);
854
855 if (m->poll_func)
856 m->poll_func_ret = m->poll_func(m->pollfds, m->n_pollfds, m->prepared_timeout, m->poll_func_userdata);
857 else
858 m->poll_func_ret = poll(m->pollfds, m->n_pollfds, m->prepared_timeout);
859
860 if (m->poll_func_ret < 0) {
861 if (errno == EINTR)
862 m->poll_func_ret = 0;
863 else
864 pa_log("poll(): %s", pa_cstrerror(errno));
865 }
866 }
867
868 m->state = m->poll_func_ret < 0 ? STATE_PASSIVE : STATE_POLLED;
869 return m->poll_func_ret;
870
871 quit:
872 m->state = STATE_QUIT;
873 return -2;
874 }
875
876 int pa_mainloop_dispatch(pa_mainloop *m) {
877 int dispatched = 0;
878
879 assert(m);
880 assert(m->state == STATE_POLLED);
881
882 if (m->quit)
883 goto quit;
884
885 if (m->n_enabled_defer_events)
886 dispatched += dispatch_defer(m);
887 else {
888 if (m->n_enabled_time_events)
889 dispatched += dispatch_timeout(m);
890
891 if (m->quit)
892 goto quit;
893
894 if (m->poll_func_ret > 0)
895 dispatched += dispatch_pollfds(m);
896 }
897
898 if (m->quit)
899 goto quit;
900
901 m->state = STATE_PASSIVE;
902
903 return dispatched;
904
905 quit:
906 m->state = STATE_QUIT;
907 return -2;
908 }
909
910 int pa_mainloop_get_retval(pa_mainloop *m) {
911 assert(m);
912 return m->retval;
913 }
914
915 int pa_mainloop_iterate(pa_mainloop *m, int block, int *retval) {
916 int r;
917 assert(m);
918
919 if ((r = pa_mainloop_prepare(m, block ? -1 : 0)) < 0)
920 goto quit;
921
922 if ((r = pa_mainloop_poll(m)) < 0)
923 goto quit;
924
925 if ((r = pa_mainloop_dispatch(m)) < 0)
926 goto quit;
927
928 return r;
929
930 quit:
931
932 if ((r == -2) && retval)
933 *retval = pa_mainloop_get_retval(m);
934 return r;
935 }
936
937 int pa_mainloop_run(pa_mainloop *m, int *retval) {
938 int r;
939
940 while ((r = pa_mainloop_iterate(m, 1, retval)) >= 0);
941
942 if (r == -2)
943 return 1;
944 else if (r < 0)
945 return -1;
946 else
947 return 0;
948 }
949
950 void pa_mainloop_quit(pa_mainloop *m, int retval) {
951 assert(m);
952
953 m->quit = 1;
954 m->retval = retval;
955 pa_mainloop_wakeup(m);
956 }
957
958 pa_mainloop_api* pa_mainloop_get_api(pa_mainloop*m) {
959 assert(m);
960 return &m->api;
961 }
962
963 void pa_mainloop_set_poll_func(pa_mainloop *m, pa_poll_func poll_func, void *userdata) {
964 assert(m);
965
966 m->poll_func = poll_func;
967 m->poll_func_userdata = userdata;
968 }