Add copyright notices to all relevant files. (based on svn log)
[profile/ivi/pulseaudio.git] / 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                 assert(m->n_enabled_defer_events > 0);
578                 m->n_enabled_defer_events--;
579             }
580
581             if (e->destroy_callback)
582                 e->destroy_callback(&m->api, e, e->userdata);
583
584             pa_xfree(e);
585         }
586
587         e = n;
588     }
589
590     assert(m->defer_events_please_scan == 0);
591 }
592
593
594 void pa_mainloop_free(pa_mainloop* m) {
595     assert(m);
596
597     cleanup_io_events(m, 1);
598     cleanup_defer_events(m, 1);
599     cleanup_time_events(m, 1);
600
601     pa_xfree(m->pollfds);
602
603     if (m->wakeup_pipe[0] >= 0)
604         close(m->wakeup_pipe[0]);
605     if (m->wakeup_pipe[1] >= 0)
606         close(m->wakeup_pipe[1]);
607
608     pa_xfree(m);
609 }
610
611 static void scan_dead(pa_mainloop *m) {
612     assert(m);
613
614     if (m->io_events_please_scan)
615         cleanup_io_events(m, 0);
616
617     if (m->time_events_please_scan)
618         cleanup_time_events(m, 0);
619
620     if (m->defer_events_please_scan)
621         cleanup_defer_events(m, 0);
622 }
623
624 static void rebuild_pollfds(pa_mainloop *m) {
625     pa_io_event*e;
626     struct pollfd *p;
627     unsigned l;
628
629     l = m->n_io_events + 1;
630     if (m->max_pollfds < l) {
631         l *= 2;
632         m->pollfds = pa_xrealloc(m->pollfds, sizeof(struct pollfd)*l);
633         m->max_pollfds = l;
634     }
635
636     m->n_pollfds = 0;
637     p = m->pollfds;
638
639     if (m->wakeup_pipe[0] >= 0) {
640         m->pollfds[0].fd = m->wakeup_pipe[0];
641         m->pollfds[0].events = POLLIN;
642         m->pollfds[0].revents = 0;
643         p++;
644         m->n_pollfds++;
645     }
646
647     for (e = m->io_events; e; e = e->next) {
648         if (e->dead) {
649             e->pollfd = NULL;
650             continue;
651         }
652
653         e->pollfd = p;
654         p->fd = e->fd;
655         p->events = map_flags_to_libc(e->events);
656         p->revents = 0;
657
658         p++;
659         m->n_pollfds++;
660     }
661
662     m->rebuild_pollfds = 0;
663 }
664
665 static int dispatch_pollfds(pa_mainloop *m) {
666     pa_io_event *e;
667     int r = 0, k;
668
669     assert(m->poll_func_ret > 0);
670
671     for (e = m->io_events, k = m->poll_func_ret; e && !m->quit && k > 0; e = e->next) {
672         if (e->dead || !e->pollfd || !e->pollfd->revents)
673             continue;
674
675         assert(e->pollfd->fd == e->fd && e->callback);
676         e->callback(&m->api, e, e->fd, map_flags_from_libc(e->pollfd->revents), e->userdata);
677         e->pollfd->revents = 0;
678         r++;
679
680         k--;
681     }
682
683     return r;
684 }
685
686 static int dispatch_defer(pa_mainloop *m) {
687     pa_defer_event *e;
688     int r = 0;
689
690     if (m->n_enabled_defer_events <= 0)
691         return 0;
692
693     for (e = m->defer_events; e && !m->quit; e = e->next) {
694         if (e->dead || !e->enabled)
695             continue;
696
697         assert(e->callback);
698         e->callback(&m->api, e, e->userdata);
699         r++;
700     }
701
702     return r;
703 }
704
705 static pa_time_event* find_next_time_event(pa_mainloop *m) {
706     pa_time_event *t, *n = NULL;
707     assert(m);
708
709     if (m->cached_next_time_event)
710         return m->cached_next_time_event;
711
712     for (t = m->time_events; t; t = t->next) {
713
714         if (t->dead || !t->enabled)
715             continue;
716
717         if (!n || pa_timeval_cmp(&t->timeval, &n->timeval) < 0) {
718             n = t;
719
720             /* Shortcut for tv = { 0, 0 } */
721             if (n->timeval.tv_sec <= 0)
722                 break;
723         }
724     }
725
726     m->cached_next_time_event = n;
727     return n;
728 }
729
730 static int calc_next_timeout(pa_mainloop *m) {
731     pa_time_event *t;
732     struct timeval now;
733     pa_usec_t usec;
734
735     if (!m->n_enabled_time_events)
736         return -1;
737
738     t = find_next_time_event(m);
739     assert(t);
740
741     if (t->timeval.tv_sec <= 0)
742         return 0;
743
744     pa_gettimeofday(&now);
745
746     if (pa_timeval_cmp(&t->timeval, &now) <= 0)
747         return 0;
748
749     usec = pa_timeval_diff(&t->timeval, &now);
750     return (int) (usec / 1000);
751 }
752
753 static int dispatch_timeout(pa_mainloop *m) {
754     pa_time_event *e;
755     struct timeval now;
756     int r = 0;
757     assert(m);
758
759     if (m->n_enabled_time_events <= 0)
760         return 0;
761
762     pa_gettimeofday(&now);
763
764     for (e = m->time_events; e && !m->quit; e = e->next) {
765
766         if (e->dead || !e->enabled)
767             continue;
768
769         if (pa_timeval_cmp(&e->timeval, &now) <= 0) {
770             assert(e->callback);
771
772             /* Disable time event */
773             mainloop_time_restart(e, NULL);
774
775             e->callback(&m->api, e, &e->timeval, e->userdata);
776
777             r++;
778         }
779     }
780
781     return r;
782 }
783
784 void pa_mainloop_wakeup(pa_mainloop *m) {
785     char c = 'W';
786     assert(m);
787
788     if (m->wakeup_pipe[1] >= 0 && m->state == STATE_POLLING) {
789         pa_write(m->wakeup_pipe[1], &c, sizeof(c), &m->wakeup_pipe_type);
790         m->wakeup_requested++;
791     }
792 }
793
794 static void clear_wakeup(pa_mainloop *m) {
795     char c[10];
796
797     assert(m);
798
799     if (m->wakeup_pipe[0] < 0)
800         return;
801
802     if (m->wakeup_requested) {
803         while (pa_read(m->wakeup_pipe[0], &c, sizeof(c), &m->wakeup_pipe_type) == sizeof(c));
804         m->wakeup_requested = 0;
805     }
806 }
807
808 int pa_mainloop_prepare(pa_mainloop *m, int timeout) {
809     assert(m);
810     assert(m->state == STATE_PASSIVE);
811
812     clear_wakeup(m);
813     scan_dead(m);
814
815     if (m->quit)
816         goto quit;
817
818     if (m->n_enabled_defer_events <= 0) {
819         if (m->rebuild_pollfds)
820             rebuild_pollfds(m);
821
822         m->prepared_timeout = calc_next_timeout(m);
823         if (timeout >= 0 && (timeout < m->prepared_timeout || m->prepared_timeout < 0))
824             m->prepared_timeout = timeout;
825     }
826
827     m->state = STATE_PREPARED;
828     return 0;
829
830 quit:
831     m->state = STATE_QUIT;
832     return -2;
833 }
834
835 int pa_mainloop_poll(pa_mainloop *m) {
836     assert(m);
837     assert(m->state == STATE_PREPARED);
838
839     if (m->quit)
840         goto quit;
841
842     m->state = STATE_POLLING;
843
844     if (m->n_enabled_defer_events )
845         m->poll_func_ret = 0;
846     else {
847         assert(!m->rebuild_pollfds);
848
849         if (m->poll_func)
850             m->poll_func_ret = m->poll_func(m->pollfds, m->n_pollfds, m->prepared_timeout, m->poll_func_userdata);
851         else
852             m->poll_func_ret = poll(m->pollfds, m->n_pollfds, m->prepared_timeout);
853
854         if (m->poll_func_ret < 0) {
855             if (errno == EINTR)
856                 m->poll_func_ret = 0;
857             else
858                 pa_log("poll(): %s", pa_cstrerror(errno));
859         }
860     }
861
862     m->state = m->poll_func_ret < 0 ? STATE_PASSIVE : STATE_POLLED;
863     return m->poll_func_ret;
864
865 quit:
866     m->state = STATE_QUIT;
867     return -2;
868 }
869
870 int pa_mainloop_dispatch(pa_mainloop *m) {
871     int dispatched = 0;
872
873     assert(m);
874     assert(m->state == STATE_POLLED);
875
876     if (m->quit)
877         goto quit;
878
879     if (m->n_enabled_defer_events)
880         dispatched += dispatch_defer(m);
881     else {
882         if (m->n_enabled_time_events)
883             dispatched += dispatch_timeout(m);
884
885         if (m->quit)
886             goto quit;
887
888         if (m->poll_func_ret > 0)
889             dispatched += dispatch_pollfds(m);
890     }
891
892     if (m->quit)
893         goto quit;
894
895     m->state = STATE_PASSIVE;
896
897     return dispatched;
898
899 quit:
900     m->state = STATE_QUIT;
901     return -2;
902 }
903
904 int pa_mainloop_get_retval(pa_mainloop *m) {
905     assert(m);
906     return m->retval;
907 }
908
909 int pa_mainloop_iterate(pa_mainloop *m, int block, int *retval) {
910     int r;
911     assert(m);
912
913     if ((r = pa_mainloop_prepare(m, block ? -1 : 0)) < 0)
914         goto quit;
915
916     if ((r = pa_mainloop_poll(m)) < 0)
917         goto quit;
918
919     if ((r = pa_mainloop_dispatch(m)) < 0)
920         goto quit;
921
922     return r;
923
924 quit:
925
926     if ((r == -2) && retval)
927         *retval = pa_mainloop_get_retval(m);
928     return r;
929 }
930
931 int pa_mainloop_run(pa_mainloop *m, int *retval) {
932     int r;
933
934     while ((r = pa_mainloop_iterate(m, 1, retval)) >= 0);
935
936     if (r == -2)
937         return 1;
938     else if (r < 0)
939         return -1;
940     else
941         return 0;
942 }
943
944 void pa_mainloop_quit(pa_mainloop *m, int retval) {
945     assert(m);
946
947     m->quit = 1;
948     m->retval = retval;
949     pa_mainloop_wakeup(m);
950 }
951
952 pa_mainloop_api* pa_mainloop_get_api(pa_mainloop*m) {
953     assert(m);
954     return &m->api;
955 }
956
957 void pa_mainloop_set_poll_func(pa_mainloop *m, pa_poll_func poll_func, void *userdata) {
958     assert(m);
959
960     m->poll_func = poll_func;
961     m->poll_func_userdata = userdata;
962 }