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