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