2 This file is part of PulseAudio.
4 Copyright 2004-2006 Lennart Poettering
5 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
7 PulseAudio is free software; you can redistribute it and/or modify
8 it under the terms of the GNU Lesser General Public License as published
9 by the Free Software Foundation; either version 2.1 of the License,
10 or (at your option) any later version.
12 PulseAudio is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public License
18 along with PulseAudio; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
38 #include <pulsecore/poll.h>
42 #include <pulsecore/pipe.h>
45 #include <pulse/i18n.h>
46 #include <pulse/rtclock.h>
47 #include <pulse/timeval.h>
48 #include <pulse/xmalloc.h>
50 #include <pulsecore/core-rtclock.h>
51 #include <pulsecore/core-util.h>
52 #include <pulsecore/llist.h>
53 #include <pulsecore/log.h>
54 #include <pulsecore/core-error.h>
55 #include <pulsecore/winsock.h>
56 #include <pulsecore/macro.h>
62 pa_mainloop
*mainloop
;
66 pa_io_event_flags_t events
;
67 struct pollfd
*pollfd
;
69 pa_io_event_cb_t callback
;
71 pa_io_event_destroy_cb_t destroy_callback
;
73 PA_LLIST_FIELDS(pa_io_event
);
76 struct pa_time_event
{
77 pa_mainloop
*mainloop
;
83 pa_time_event_cb_t callback
;
85 pa_time_event_destroy_cb_t destroy_callback
;
87 PA_LLIST_FIELDS(pa_time_event
);
90 struct pa_defer_event
{
91 pa_mainloop
*mainloop
;
96 pa_defer_event_cb_t callback
;
98 pa_defer_event_destroy_cb_t destroy_callback
;
100 PA_LLIST_FIELDS(pa_defer_event
);
104 PA_LLIST_HEAD(pa_io_event
, io_events
);
105 PA_LLIST_HEAD(pa_time_event
, time_events
);
106 PA_LLIST_HEAD(pa_defer_event
, defer_events
);
108 unsigned n_enabled_defer_events
, n_enabled_time_events
, n_io_events
;
109 unsigned io_events_please_scan
, time_events_please_scan
, defer_events_please_scan
;
111 pa_bool_t rebuild_pollfds
:1;
112 struct pollfd
*pollfds
;
113 unsigned max_pollfds
, n_pollfds
;
115 pa_usec_t prepared_timeout
;
116 pa_time_event
*cached_next_time_event
;
123 pa_bool_t wakeup_requested
:1;
125 int wakeup_pipe_type
;
135 pa_poll_func poll_func
;
136 void *poll_func_userdata
;
140 static short map_flags_to_libc(pa_io_event_flags_t flags
) {
142 ((flags
& PA_IO_EVENT_INPUT
? POLLIN
: 0) |
143 (flags
& PA_IO_EVENT_OUTPUT
? POLLOUT
: 0) |
144 (flags
& PA_IO_EVENT_ERROR
? POLLERR
: 0) |
145 (flags
& PA_IO_EVENT_HANGUP
? POLLHUP
: 0));
148 static pa_io_event_flags_t
map_flags_from_libc(short flags
) {
150 (flags
& POLLIN
? PA_IO_EVENT_INPUT
: 0) |
151 (flags
& POLLOUT
? PA_IO_EVENT_OUTPUT
: 0) |
152 (flags
& POLLERR
? PA_IO_EVENT_ERROR
: 0) |
153 (flags
& POLLHUP
? PA_IO_EVENT_HANGUP
: 0);
157 static pa_io_event
* mainloop_io_new(
160 pa_io_event_flags_t events
,
161 pa_io_event_cb_t callback
,
168 pa_assert(a
->userdata
);
173 pa_assert(a
== &m
->api
);
175 e
= pa_xnew0(pa_io_event
, 1);
181 e
->callback
= callback
;
182 e
->userdata
= userdata
;
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("Cannot monitor non-socket file descriptors.");
204 PA_LLIST_PREPEND(pa_io_event
, m
->io_events
, e
);
205 m
->rebuild_pollfds
= TRUE
;
208 pa_mainloop_wakeup(m
);
213 static void mainloop_io_enable(pa_io_event
*e
, pa_io_event_flags_t events
) {
217 if (e
->events
== events
)
223 e
->pollfd
->events
= map_flags_to_libc(events
);
225 e
->mainloop
->rebuild_pollfds
= TRUE
;
227 pa_mainloop_wakeup(e
->mainloop
);
230 static void mainloop_io_free(pa_io_event
*e
) {
235 e
->mainloop
->io_events_please_scan
++;
237 e
->mainloop
->n_io_events
--;
238 e
->mainloop
->rebuild_pollfds
= TRUE
;
240 pa_mainloop_wakeup(e
->mainloop
);
243 static void mainloop_io_set_destroy(pa_io_event
*e
, pa_io_event_destroy_cb_t callback
) {
246 e
->destroy_callback
= callback
;
250 static pa_defer_event
* mainloop_defer_new(
252 pa_defer_event_cb_t callback
,
259 pa_assert(a
->userdata
);
263 pa_assert(a
== &m
->api
);
265 e
= pa_xnew0(pa_defer_event
, 1);
269 m
->n_enabled_defer_events
++;
271 e
->callback
= callback
;
272 e
->userdata
= userdata
;
274 PA_LLIST_PREPEND(pa_defer_event
, m
->defer_events
, e
);
276 pa_mainloop_wakeup(e
->mainloop
);
281 static void mainloop_defer_enable(pa_defer_event
*e
, int b
) {
285 if (e
->enabled
&& !b
) {
286 pa_assert(e
->mainloop
->n_enabled_defer_events
> 0);
287 e
->mainloop
->n_enabled_defer_events
--;
288 } else if (!e
->enabled
&& b
) {
289 e
->mainloop
->n_enabled_defer_events
++;
290 pa_mainloop_wakeup(e
->mainloop
);
296 static void mainloop_defer_free(pa_defer_event
*e
) {
301 e
->mainloop
->defer_events_please_scan
++;
304 pa_assert(e
->mainloop
->n_enabled_defer_events
> 0);
305 e
->mainloop
->n_enabled_defer_events
--;
310 static void mainloop_defer_set_destroy(pa_defer_event
*e
, pa_defer_event_destroy_cb_t callback
) {
314 e
->destroy_callback
= callback
;
318 static pa_usec_t
make_rt(const struct timeval
*tv
) {
322 return PA_USEC_INVALID
;
324 if (tv
->tv_usec
& PA_TIMEVAL_RTCLOCK
) {
326 ttv
.tv_usec
&= ~PA_TIMEVAL_RTCLOCK
;
327 tv
= pa_rtclock_from_wallclock(&ttv
);
330 return pa_timeval_load(tv
);
333 static pa_time_event
* mainloop_time_new(
335 const struct timeval
*tv
,
336 pa_time_event_cb_t callback
,
344 pa_assert(a
->userdata
);
350 pa_assert(a
== &m
->api
);
352 e
= pa_xnew0(pa_time_event
, 1);
355 if ((e
->enabled
= (t
!= PA_USEC_INVALID
))) {
358 m
->n_enabled_time_events
++;
360 if (m
->cached_next_time_event
) {
361 pa_assert(m
->cached_next_time_event
->enabled
);
363 if (t
< m
->cached_next_time_event
->time
)
364 m
->cached_next_time_event
= e
;
368 e
->callback
= callback
;
369 e
->userdata
= userdata
;
371 PA_LLIST_PREPEND(pa_time_event
, m
->time_events
, e
);
374 pa_mainloop_wakeup(m
);
379 static void mainloop_time_restart(pa_time_event
*e
, const struct timeval
*tv
) {
388 valid
= (t
!= PA_USEC_INVALID
);
389 if (e
->enabled
&& !valid
) {
390 pa_assert(e
->mainloop
->n_enabled_time_events
> 0);
391 e
->mainloop
->n_enabled_time_events
--;
392 } else if (!e
->enabled
&& valid
)
393 e
->mainloop
->n_enabled_time_events
++;
395 if ((e
->enabled
= valid
)) {
397 pa_mainloop_wakeup(e
->mainloop
);
400 if (e
->mainloop
->cached_next_time_event
&& e
->enabled
) {
401 pa_assert(e
->mainloop
->cached_next_time_event
->enabled
);
403 if (t
< e
->mainloop
->cached_next_time_event
->time
)
404 e
->mainloop
->cached_next_time_event
= e
;
405 } else if (e
->mainloop
->cached_next_time_event
== e
)
406 e
->mainloop
->cached_next_time_event
= NULL
;
409 static void mainloop_time_free(pa_time_event
*e
) {
414 e
->mainloop
->time_events_please_scan
++;
417 pa_assert(e
->mainloop
->n_enabled_time_events
> 0);
418 e
->mainloop
->n_enabled_time_events
--;
422 if (e
->mainloop
->cached_next_time_event
== e
)
423 e
->mainloop
->cached_next_time_event
= NULL
;
425 /* no wakeup needed here. Think about it! */
428 static void mainloop_time_set_destroy(pa_time_event
*e
, pa_time_event_destroy_cb_t callback
) {
432 e
->destroy_callback
= callback
;
437 static void mainloop_quit(pa_mainloop_api
*a
, int retval
) {
441 pa_assert(a
->userdata
);
443 pa_assert(a
== &m
->api
);
445 pa_mainloop_quit(m
, retval
);
448 static const pa_mainloop_api vtable
= {
451 .io_new
= mainloop_io_new
,
452 .io_enable
= mainloop_io_enable
,
453 .io_free
= mainloop_io_free
,
454 .io_set_destroy
= mainloop_io_set_destroy
,
456 .time_new
= mainloop_time_new
,
457 .time_restart
= mainloop_time_restart
,
458 .time_free
= mainloop_time_free
,
459 .time_set_destroy
= mainloop_time_set_destroy
,
461 .defer_new
= mainloop_defer_new
,
462 .defer_enable
= mainloop_defer_enable
,
463 .defer_free
= mainloop_defer_free
,
464 .defer_set_destroy
= mainloop_defer_set_destroy
,
466 .quit
= mainloop_quit
,
469 pa_mainloop
*pa_mainloop_new(void) {
474 m
= pa_xnew0(pa_mainloop
, 1);
476 if (pipe(m
->wakeup_pipe
) < 0) {
477 pa_log_error("ERROR: cannot create wakeup pipe");
482 pa_make_fd_nonblock(m
->wakeup_pipe
[0]);
483 pa_make_fd_nonblock(m
->wakeup_pipe
[1]);
484 pa_make_fd_cloexec(m
->wakeup_pipe
[0]);
485 pa_make_fd_cloexec(m
->wakeup_pipe
[1]);
487 m
->rebuild_pollfds
= TRUE
;
492 m
->state
= STATE_PASSIVE
;
494 m
->poll_func_ret
= -1;
499 static void cleanup_io_events(pa_mainloop
*m
, pa_bool_t force
) {
504 pa_io_event
*n
= e
->next
;
506 if (!force
&& m
->io_events_please_scan
<= 0)
509 if (force
|| e
->dead
) {
510 PA_LLIST_REMOVE(pa_io_event
, m
->io_events
, e
);
513 pa_assert(m
->io_events_please_scan
> 0);
514 m
->io_events_please_scan
--;
517 if (e
->destroy_callback
)
518 e
->destroy_callback(&m
->api
, e
, e
->userdata
);
522 m
->rebuild_pollfds
= TRUE
;
528 pa_assert(m
->io_events_please_scan
== 0);
531 static void cleanup_time_events(pa_mainloop
*m
, pa_bool_t force
) {
536 pa_time_event
*n
= e
->next
;
538 if (!force
&& m
->time_events_please_scan
<= 0)
541 if (force
|| e
->dead
) {
542 PA_LLIST_REMOVE(pa_time_event
, m
->time_events
, e
);
545 pa_assert(m
->time_events_please_scan
> 0);
546 m
->time_events_please_scan
--;
549 if (!e
->dead
&& e
->enabled
) {
550 pa_assert(m
->n_enabled_time_events
> 0);
551 m
->n_enabled_time_events
--;
555 if (e
->destroy_callback
)
556 e
->destroy_callback(&m
->api
, e
, e
->userdata
);
564 pa_assert(m
->time_events_please_scan
== 0);
567 static void cleanup_defer_events(pa_mainloop
*m
, pa_bool_t force
) {
572 pa_defer_event
*n
= e
->next
;
574 if (!force
&& m
->defer_events_please_scan
<= 0)
577 if (force
|| e
->dead
) {
578 PA_LLIST_REMOVE(pa_defer_event
, m
->defer_events
, e
);
581 pa_assert(m
->defer_events_please_scan
> 0);
582 m
->defer_events_please_scan
--;
585 if (!e
->dead
&& e
->enabled
) {
586 pa_assert(m
->n_enabled_defer_events
> 0);
587 m
->n_enabled_defer_events
--;
591 if (e
->destroy_callback
)
592 e
->destroy_callback(&m
->api
, e
, e
->userdata
);
600 pa_assert(m
->defer_events_please_scan
== 0);
604 void pa_mainloop_free(pa_mainloop
* m
) {
607 cleanup_io_events(m
, TRUE
);
608 cleanup_defer_events(m
, TRUE
);
609 cleanup_time_events(m
, TRUE
);
611 pa_xfree(m
->pollfds
);
613 pa_close_pipe(m
->wakeup_pipe
);
618 static void scan_dead(pa_mainloop
*m
) {
621 if (m
->io_events_please_scan
)
622 cleanup_io_events(m
, FALSE
);
624 if (m
->time_events_please_scan
)
625 cleanup_time_events(m
, FALSE
);
627 if (m
->defer_events_please_scan
)
628 cleanup_defer_events(m
, FALSE
);
631 static void rebuild_pollfds(pa_mainloop
*m
) {
636 l
= m
->n_io_events
+ 1;
637 if (m
->max_pollfds
< l
) {
639 m
->pollfds
= pa_xrealloc(m
->pollfds
, sizeof(struct pollfd
)*l
);
646 if (m
->wakeup_pipe
[0] >= 0) {
647 m
->pollfds
[0].fd
= m
->wakeup_pipe
[0];
648 m
->pollfds
[0].events
= POLLIN
;
649 m
->pollfds
[0].revents
= 0;
654 for (e
= m
->io_events
; e
; e
= e
->next
) {
662 p
->events
= map_flags_to_libc(e
->events
);
669 m
->rebuild_pollfds
= FALSE
;
672 static int dispatch_pollfds(pa_mainloop
*m
) {
676 pa_assert(m
->poll_func_ret
> 0);
678 for (e
= m
->io_events
, k
= m
->poll_func_ret
; e
&& !m
->quit
&& k
> 0; e
= e
->next
) {
679 if (e
->dead
|| !e
->pollfd
|| !e
->pollfd
->revents
)
682 pa_assert(e
->pollfd
->fd
== e
->fd
);
683 pa_assert(e
->callback
);
684 e
->callback(&m
->api
, e
, e
->fd
, map_flags_from_libc(e
->pollfd
->revents
), e
->userdata
);
685 e
->pollfd
->revents
= 0;
694 static int dispatch_defer(pa_mainloop
*m
) {
698 if (m
->n_enabled_defer_events
<= 0)
701 for (e
= m
->defer_events
; e
&& !m
->quit
; e
= e
->next
) {
702 if (e
->dead
|| !e
->enabled
)
705 pa_assert(e
->callback
);
706 e
->callback(&m
->api
, e
, e
->userdata
);
713 static pa_time_event
* find_next_time_event(pa_mainloop
*m
) {
714 pa_time_event
*t
, *n
= NULL
;
717 if (m
->cached_next_time_event
)
718 return m
->cached_next_time_event
;
720 for (t
= m
->time_events
; t
; t
= t
->next
) {
722 if (t
->dead
|| !t
->enabled
)
725 if (!n
|| t
->time
< n
->time
) {
728 /* Shortcut for time == 0 */
734 m
->cached_next_time_event
= n
;
738 static pa_usec_t
calc_next_timeout(pa_mainloop
*m
) {
742 if (m
->n_enabled_time_events
<= 0)
743 return PA_USEC_INVALID
;
745 pa_assert_se(t
= find_next_time_event(m
));
750 clock_now
= pa_rtclock_now();
752 if (t
->time
<= clock_now
)
755 return t
->time
- clock_now
;
758 static int dispatch_timeout(pa_mainloop
*m
) {
764 if (m
->n_enabled_time_events
<= 0)
767 now
= pa_rtclock_now();
769 for (e
= m
->time_events
; e
&& !m
->quit
; e
= e
->next
) {
771 if (e
->dead
|| !e
->enabled
)
774 if (e
->time
<= now
) {
776 pa_assert(e
->callback
);
778 /* Disable time event */
779 mainloop_time_restart(e
, NULL
);
781 e
->callback(&m
->api
, e
, pa_timeval_rtstore(&tv
, e
->time
, FALSE
), e
->userdata
);
790 void pa_mainloop_wakeup(pa_mainloop
*m
) {
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
++;
800 static void clear_wakeup(pa_mainloop
*m
) {
805 if (m
->wakeup_pipe
[0] < 0)
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;
814 int pa_mainloop_prepare(pa_mainloop
*m
, int timeout
) {
816 pa_assert(m
->state
== STATE_PASSIVE
);
824 if (m
->n_enabled_defer_events
<= 0) {
826 if (m
->rebuild_pollfds
)
829 m
->prepared_timeout
= calc_next_timeout(m
);
831 uint64_t u
= (uint64_t) timeout
* PA_USEC_PER_MSEC
;
833 if (u
< m
->prepared_timeout
|| m
->prepared_timeout
== PA_USEC_INVALID
)
834 m
->prepared_timeout
= timeout
;
838 m
->state
= STATE_PREPARED
;
842 m
->state
= STATE_QUIT
;
846 static int usec_to_timeout(pa_usec_t u
) {
847 if (u
== PA_USEC_INVALID
)
850 return (u
+ PA_USEC_PER_MSEC
- 1) / PA_USEC_PER_MSEC
;
853 int pa_mainloop_poll(pa_mainloop
*m
) {
855 pa_assert(m
->state
== STATE_PREPARED
);
860 m
->state
= STATE_POLLING
;
862 if (m
->n_enabled_defer_events
)
863 m
->poll_func_ret
= 0;
865 pa_assert(!m
->rebuild_pollfds
);
868 m
->poll_func_ret
= m
->poll_func(
869 m
->pollfds
, m
->n_pollfds
,
870 usec_to_timeout(m
->prepared_timeout
),
871 m
->poll_func_userdata
);
876 m
->poll_func_ret
= ppoll(
877 m
->pollfds
, m
->n_pollfds
,
878 m
->prepared_timeout
== PA_USEC_INVALID
? NULL
: pa_timespec_store(&ts
, m
->prepared_timeout
),
881 m
->poll_func_ret
= poll(
882 m
->pollfds
, m
->n_pollfds
,
883 usec_to_timeout(m
->prepared_timeout
));
887 if (m
->poll_func_ret
< 0) {
889 m
->poll_func_ret
= 0;
891 pa_log("poll(): %s", pa_cstrerror(errno
));
895 m
->state
= m
->poll_func_ret
< 0 ? STATE_PASSIVE
: STATE_POLLED
;
896 return m
->poll_func_ret
;
899 m
->state
= STATE_QUIT
;
903 int pa_mainloop_dispatch(pa_mainloop
*m
) {
907 pa_assert(m
->state
== STATE_POLLED
);
912 if (m
->n_enabled_defer_events
)
913 dispatched
+= dispatch_defer(m
);
915 if (m
->n_enabled_time_events
)
916 dispatched
+= dispatch_timeout(m
);
921 if (m
->poll_func_ret
> 0)
922 dispatched
+= dispatch_pollfds(m
);
928 m
->state
= STATE_PASSIVE
;
933 m
->state
= STATE_QUIT
;
937 int pa_mainloop_get_retval(pa_mainloop
*m
) {
942 int pa_mainloop_iterate(pa_mainloop
*m
, int block
, int *retval
) {
946 if ((r
= pa_mainloop_prepare(m
, block
? -1 : 0)) < 0)
949 if ((r
= pa_mainloop_poll(m
)) < 0)
952 if ((r
= pa_mainloop_dispatch(m
)) < 0)
959 if ((r
== -2) && retval
)
960 *retval
= pa_mainloop_get_retval(m
);
964 int pa_mainloop_run(pa_mainloop
*m
, int *retval
) {
967 while ((r
= pa_mainloop_iterate(m
, 1, retval
)) >= 0);
977 void pa_mainloop_quit(pa_mainloop
*m
, int retval
) {
982 pa_mainloop_wakeup(m
);
985 pa_mainloop_api
* pa_mainloop_get_api(pa_mainloop
*m
) {
990 void pa_mainloop_set_poll_func(pa_mainloop
*m
, pa_poll_func poll_func
, void *userdata
) {
993 m
->poll_func
= poll_func
;
994 m
->poll_func_userdata
= userdata
;
997 pa_bool_t
pa_mainloop_is_our_api(pa_mainloop_api
*m
) {
1000 return m
->io_new
== mainloop_io_new
;