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
) {
502 PA_LLIST_FOREACH_SAFE(e
, n
, m
->io_events
) {
504 if (!force
&& m
->io_events_please_scan
<= 0)
507 if (force
|| e
->dead
) {
508 PA_LLIST_REMOVE(pa_io_event
, m
->io_events
, e
);
511 pa_assert(m
->io_events_please_scan
> 0);
512 m
->io_events_please_scan
--;
515 if (e
->destroy_callback
)
516 e
->destroy_callback(&m
->api
, e
, e
->userdata
);
520 m
->rebuild_pollfds
= TRUE
;
524 pa_assert(m
->io_events_please_scan
== 0);
527 static void cleanup_time_events(pa_mainloop
*m
, pa_bool_t force
) {
528 pa_time_event
*e
, *n
;
530 PA_LLIST_FOREACH_SAFE(e
, n
, m
->time_events
) {
532 if (!force
&& m
->time_events_please_scan
<= 0)
535 if (force
|| e
->dead
) {
536 PA_LLIST_REMOVE(pa_time_event
, m
->time_events
, e
);
539 pa_assert(m
->time_events_please_scan
> 0);
540 m
->time_events_please_scan
--;
543 if (!e
->dead
&& e
->enabled
) {
544 pa_assert(m
->n_enabled_time_events
> 0);
545 m
->n_enabled_time_events
--;
549 if (e
->destroy_callback
)
550 e
->destroy_callback(&m
->api
, e
, e
->userdata
);
556 pa_assert(m
->time_events_please_scan
== 0);
559 static void cleanup_defer_events(pa_mainloop
*m
, pa_bool_t force
) {
560 pa_defer_event
*e
, *n
;
562 PA_LLIST_FOREACH_SAFE(e
, n
, m
->defer_events
) {
564 if (!force
&& m
->defer_events_please_scan
<= 0)
567 if (force
|| e
->dead
) {
568 PA_LLIST_REMOVE(pa_defer_event
, m
->defer_events
, e
);
571 pa_assert(m
->defer_events_please_scan
> 0);
572 m
->defer_events_please_scan
--;
575 if (!e
->dead
&& e
->enabled
) {
576 pa_assert(m
->n_enabled_defer_events
> 0);
577 m
->n_enabled_defer_events
--;
581 if (e
->destroy_callback
)
582 e
->destroy_callback(&m
->api
, e
, e
->userdata
);
588 pa_assert(m
->defer_events_please_scan
== 0);
592 void pa_mainloop_free(pa_mainloop
* m
) {
595 cleanup_io_events(m
, TRUE
);
596 cleanup_defer_events(m
, TRUE
);
597 cleanup_time_events(m
, TRUE
);
599 pa_xfree(m
->pollfds
);
601 pa_close_pipe(m
->wakeup_pipe
);
606 static void scan_dead(pa_mainloop
*m
) {
609 if (m
->io_events_please_scan
)
610 cleanup_io_events(m
, FALSE
);
612 if (m
->time_events_please_scan
)
613 cleanup_time_events(m
, FALSE
);
615 if (m
->defer_events_please_scan
)
616 cleanup_defer_events(m
, FALSE
);
619 static void rebuild_pollfds(pa_mainloop
*m
) {
624 l
= m
->n_io_events
+ 1;
625 if (m
->max_pollfds
< l
) {
627 m
->pollfds
= pa_xrealloc(m
->pollfds
, sizeof(struct pollfd
)*l
);
634 if (m
->wakeup_pipe
[0] >= 0) {
635 m
->pollfds
[0].fd
= m
->wakeup_pipe
[0];
636 m
->pollfds
[0].events
= POLLIN
;
637 m
->pollfds
[0].revents
= 0;
642 PA_LLIST_FOREACH(e
, m
->io_events
) {
650 p
->events
= map_flags_to_libc(e
->events
);
657 m
->rebuild_pollfds
= FALSE
;
660 static unsigned dispatch_pollfds(pa_mainloop
*m
) {
664 pa_assert(m
->poll_func_ret
> 0);
666 k
= m
->poll_func_ret
;
668 PA_LLIST_FOREACH(e
, m
->io_events
) {
670 if (k
<= 0 || m
->quit
)
673 if (e
->dead
|| !e
->pollfd
|| !e
->pollfd
->revents
)
676 pa_assert(e
->pollfd
->fd
== e
->fd
);
677 pa_assert(e
->callback
);
679 e
->callback(&m
->api
, e
, e
->fd
, map_flags_from_libc(e
->pollfd
->revents
), e
->userdata
);
680 e
->pollfd
->revents
= 0;
688 static unsigned dispatch_defer(pa_mainloop
*m
) {
692 if (m
->n_enabled_defer_events
<= 0)
695 PA_LLIST_FOREACH(e
, m
->defer_events
) {
700 if (e
->dead
|| !e
->enabled
)
703 pa_assert(e
->callback
);
704 e
->callback(&m
->api
, e
, e
->userdata
);
711 static pa_time_event
* find_next_time_event(pa_mainloop
*m
) {
712 pa_time_event
*t
, *n
= NULL
;
715 if (m
->cached_next_time_event
)
716 return m
->cached_next_time_event
;
718 PA_LLIST_FOREACH(t
, m
->time_events
) {
720 if (t
->dead
|| !t
->enabled
)
723 if (!n
|| t
->time
< n
->time
) {
726 /* Shortcut for time == 0 */
732 m
->cached_next_time_event
= n
;
736 static pa_usec_t
calc_next_timeout(pa_mainloop
*m
) {
740 if (m
->n_enabled_time_events
<= 0)
741 return PA_USEC_INVALID
;
743 pa_assert_se(t
= find_next_time_event(m
));
748 clock_now
= pa_rtclock_now();
750 if (t
->time
<= clock_now
)
753 return t
->time
- clock_now
;
756 static unsigned dispatch_timeout(pa_mainloop
*m
) {
762 if (m
->n_enabled_time_events
<= 0)
765 now
= pa_rtclock_now();
767 PA_LLIST_FOREACH(e
, m
->time_events
) {
772 if (e
->dead
|| !e
->enabled
)
775 if (e
->time
<= now
) {
777 pa_assert(e
->callback
);
779 /* Disable time event */
780 mainloop_time_restart(e
, NULL
);
782 e
->callback(&m
->api
, e
, pa_timeval_rtstore(&tv
, e
->time
, FALSE
), e
->userdata
);
791 void pa_mainloop_wakeup(pa_mainloop
*m
) {
795 if (m
->wakeup_pipe
[1] >= 0 && m
->state
== STATE_POLLING
) {
796 pa_write(m
->wakeup_pipe
[1], &c
, sizeof(c
), &m
->wakeup_pipe_type
);
797 m
->wakeup_requested
++;
801 static void clear_wakeup(pa_mainloop
*m
) {
806 if (m
->wakeup_pipe
[0] < 0)
809 if (m
->wakeup_requested
) {
810 while (pa_read(m
->wakeup_pipe
[0], &c
, sizeof(c
), &m
->wakeup_pipe_type
) == sizeof(c
))
812 m
->wakeup_requested
= 0;
816 int pa_mainloop_prepare(pa_mainloop
*m
, int timeout
) {
818 pa_assert(m
->state
== STATE_PASSIVE
);
826 if (m
->n_enabled_defer_events
<= 0) {
828 if (m
->rebuild_pollfds
)
831 m
->prepared_timeout
= calc_next_timeout(m
);
833 uint64_t u
= (uint64_t) timeout
* PA_USEC_PER_MSEC
;
835 if (u
< m
->prepared_timeout
|| m
->prepared_timeout
== PA_USEC_INVALID
)
836 m
->prepared_timeout
= timeout
;
840 m
->state
= STATE_PREPARED
;
844 m
->state
= STATE_QUIT
;
848 static int usec_to_timeout(pa_usec_t u
) {
849 if (u
== PA_USEC_INVALID
)
852 return (u
+ PA_USEC_PER_MSEC
- 1) / PA_USEC_PER_MSEC
;
855 int pa_mainloop_poll(pa_mainloop
*m
) {
857 pa_assert(m
->state
== STATE_PREPARED
);
862 m
->state
= STATE_POLLING
;
864 if (m
->n_enabled_defer_events
)
865 m
->poll_func_ret
= 0;
867 pa_assert(!m
->rebuild_pollfds
);
870 m
->poll_func_ret
= m
->poll_func(
871 m
->pollfds
, m
->n_pollfds
,
872 usec_to_timeout(m
->prepared_timeout
),
873 m
->poll_func_userdata
);
878 m
->poll_func_ret
= ppoll(
879 m
->pollfds
, m
->n_pollfds
,
880 m
->prepared_timeout
== PA_USEC_INVALID
? NULL
: pa_timespec_store(&ts
, m
->prepared_timeout
),
883 m
->poll_func_ret
= poll(
884 m
->pollfds
, m
->n_pollfds
,
885 usec_to_timeout(m
->prepared_timeout
));
889 if (m
->poll_func_ret
< 0) {
891 m
->poll_func_ret
= 0;
893 pa_log("poll(): %s", pa_cstrerror(errno
));
897 m
->state
= m
->poll_func_ret
< 0 ? STATE_PASSIVE
: STATE_POLLED
;
898 return m
->poll_func_ret
;
901 m
->state
= STATE_QUIT
;
905 int pa_mainloop_dispatch(pa_mainloop
*m
) {
906 unsigned dispatched
= 0;
909 pa_assert(m
->state
== STATE_POLLED
);
914 if (m
->n_enabled_defer_events
)
915 dispatched
+= dispatch_defer(m
);
917 if (m
->n_enabled_time_events
)
918 dispatched
+= dispatch_timeout(m
);
923 if (m
->poll_func_ret
> 0)
924 dispatched
+= dispatch_pollfds(m
);
930 m
->state
= STATE_PASSIVE
;
932 return (int) dispatched
;
935 m
->state
= STATE_QUIT
;
939 int pa_mainloop_get_retval(pa_mainloop
*m
) {
945 int pa_mainloop_iterate(pa_mainloop
*m
, int block
, int *retval
) {
949 if ((r
= pa_mainloop_prepare(m
, block
? -1 : 0)) < 0)
952 if ((r
= pa_mainloop_poll(m
)) < 0)
955 if ((r
= pa_mainloop_dispatch(m
)) < 0)
962 if ((r
== -2) && retval
)
963 *retval
= pa_mainloop_get_retval(m
);
967 int pa_mainloop_run(pa_mainloop
*m
, int *retval
) {
970 while ((r
= pa_mainloop_iterate(m
, 1, retval
)) >= 0)
981 void pa_mainloop_quit(pa_mainloop
*m
, int retval
) {
986 pa_mainloop_wakeup(m
);
989 pa_mainloop_api
* pa_mainloop_get_api(pa_mainloop
*m
) {
995 void pa_mainloop_set_poll_func(pa_mainloop
*m
, pa_poll_func poll_func
, void *userdata
) {
998 m
->poll_func
= poll_func
;
999 m
->poll_func_userdata
= userdata
;
1002 pa_bool_t
pa_mainloop_is_our_api(pa_mainloop_api
*m
) {
1005 return m
->io_new
== mainloop_io_new
;