4 This file is part of polypaudio.
6 polypaudio 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.
11 polypaudio 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.
16 You should have received a copy of the GNU Lesser General Public License
17 along with polypaudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
35 #ifdef HAVE_SYS_POLL_H
38 #include "../polypcore/poll.h"
41 #include "../polypcore/winsock.h"
44 #include "../polypcore/pipe.h"
47 #include <polypcore/util.h>
48 #include <polypcore/idxset.h>
49 #include <polypcore/xmalloc.h>
50 #include <polypcore/log.h>
55 pa_mainloop
*mainloop
;
58 pa_io_event_flags_t events
;
59 void (*callback
) (pa_mainloop_api
*a
, pa_io_event
*e
, int fd
, pa_io_event_flags_t f
, void *userdata
);
60 struct pollfd
*pollfd
;
62 void (*destroy_callback
) (pa_mainloop_api
*a
, pa_io_event
*e
, void *userdata
);
65 struct pa_time_event
{
66 pa_mainloop
*mainloop
;
69 struct timeval timeval
;
70 void (*callback
)(pa_mainloop_api
*a
, pa_time_event
*e
, const struct timeval
*tv
, void *userdata
);
72 void (*destroy_callback
) (pa_mainloop_api
*a
, pa_time_event
*e
, void *userdata
);
75 struct pa_defer_event
{
76 pa_mainloop
*mainloop
;
79 void (*callback
)(pa_mainloop_api
*a
, pa_defer_event
*e
, void *userdata
);
81 void (*destroy_callback
) (pa_mainloop_api
*a
, pa_defer_event
*e
, void *userdata
);
85 pa_idxset
*io_events
, *time_events
, *defer_events
;
86 int io_events_scan_dead
, defer_events_scan_dead
, time_events_scan_dead
;
88 struct pollfd
*pollfds
;
89 unsigned max_pollfds
, n_pollfds
;
109 pa_poll_func poll_func
;
110 void *poll_func_userdata
;
114 static pa_io_event
* mainloop_io_new(
117 pa_io_event_flags_t events
,
118 void (*callback
) (pa_mainloop_api
*a
, pa_io_event
*e
, int fd
, pa_io_event_flags_t events
, void *userdata
),
124 assert(a
&& a
->userdata
&& fd
>= 0 && callback
);
126 assert(a
== &m
->api
);
128 e
= pa_xmalloc(sizeof(pa_io_event
));
134 e
->callback
= callback
;
135 e
->userdata
= userdata
;
136 e
->destroy_callback
= NULL
;
150 if ((select((SELECT_TYPE_ARG1
) fd
, NULL
, NULL
, SELECT_TYPE_ARG234
&xset
,
151 SELECT_TYPE_ARG5
&tv
) == -1) &&
152 (WSAGetLastError() == WSAENOTSOCK
)) {
153 pa_log_warn(__FILE__
": WARNING: cannot monitor non-socket file descriptors.");
159 pa_idxset_put(m
->io_events
, e
, NULL
);
160 m
->rebuild_pollfds
= 1;
162 pa_mainloop_wakeup(m
);
167 static void mainloop_io_enable(pa_io_event
*e
, pa_io_event_flags_t events
) {
168 assert(e
&& e
->mainloop
);
171 e
->mainloop
->rebuild_pollfds
= 1;
173 pa_mainloop_wakeup(e
->mainloop
);
176 static void mainloop_io_free(pa_io_event
*e
) {
177 assert(e
&& e
->mainloop
);
179 e
->dead
= e
->mainloop
->io_events_scan_dead
= e
->mainloop
->rebuild_pollfds
= 1;
181 pa_mainloop_wakeup(e
->mainloop
);
184 static void mainloop_io_set_destroy(pa_io_event
*e
, void (*callback
)(pa_mainloop_api
*a
, pa_io_event
*e
, void *userdata
)) {
186 e
->destroy_callback
= callback
;
190 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
) {
194 assert(a
&& a
->userdata
&& callback
);
196 assert(a
== &m
->api
);
198 e
= pa_xmalloc(sizeof(pa_defer_event
));
203 e
->callback
= callback
;
204 e
->userdata
= userdata
;
205 e
->destroy_callback
= NULL
;
207 pa_idxset_put(m
->defer_events
, e
, NULL
);
209 m
->deferred_pending
++;
211 pa_mainloop_wakeup(e
->mainloop
);
216 static void mainloop_defer_enable(pa_defer_event
*e
, int b
) {
219 if (e
->enabled
&& !b
) {
220 assert(e
->mainloop
->deferred_pending
> 0);
221 e
->mainloop
->deferred_pending
--;
222 } else if (!e
->enabled
&& b
) {
223 e
->mainloop
->deferred_pending
++;
224 pa_mainloop_wakeup(e
->mainloop
);
230 static void mainloop_defer_free(pa_defer_event
*e
) {
232 e
->dead
= e
->mainloop
->defer_events_scan_dead
= 1;
236 assert(e
->mainloop
->deferred_pending
> 0);
237 e
->mainloop
->deferred_pending
--;
241 static void mainloop_defer_set_destroy(pa_defer_event
*e
, void (*callback
)(pa_mainloop_api
*a
, pa_defer_event
*e
, void *userdata
)) {
243 e
->destroy_callback
= callback
;
247 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
) {
251 assert(a
&& a
->userdata
&& callback
);
253 assert(a
== &m
->api
);
255 e
= pa_xmalloc(sizeof(pa_time_event
));
263 e
->callback
= callback
;
264 e
->userdata
= userdata
;
265 e
->destroy_callback
= NULL
;
267 pa_idxset_put(m
->time_events
, e
, NULL
);
270 pa_mainloop_wakeup(m
);
275 static void mainloop_time_restart(pa_time_event
*e
, const struct timeval
*tv
) {
282 pa_mainloop_wakeup(e
->mainloop
);
287 static void mainloop_time_free(pa_time_event
*e
) {
290 e
->dead
= e
->mainloop
->time_events_scan_dead
= 1;
292 /* no wakeup needed here. Think about it! */
295 static void mainloop_time_set_destroy(pa_time_event
*e
, void (*callback
)(pa_mainloop_api
*a
, pa_time_event
*e
, void *userdata
)) {
297 e
->destroy_callback
= callback
;
302 static void mainloop_quit(pa_mainloop_api
*a
, int retval
) {
304 assert(a
&& a
->userdata
);
306 assert(a
== &m
->api
);
308 pa_mainloop_quit(m
, retval
);
311 static const pa_mainloop_api vtable
= {
314 .io_new
= mainloop_io_new
,
315 .io_enable
= mainloop_io_enable
,
316 .io_free
= mainloop_io_free
,
317 .io_set_destroy
= mainloop_io_set_destroy
,
319 .time_new
= mainloop_time_new
,
320 .time_restart
= mainloop_time_restart
,
321 .time_free
= mainloop_time_free
,
322 .time_set_destroy
= mainloop_time_set_destroy
,
324 .defer_new
= mainloop_defer_new
,
325 .defer_enable
= mainloop_defer_enable
,
326 .defer_free
= mainloop_defer_free
,
327 .defer_set_destroy
= mainloop_defer_set_destroy
,
329 .quit
= mainloop_quit
,
332 pa_mainloop
*pa_mainloop_new(void) {
335 m
= pa_xmalloc(sizeof(pa_mainloop
));
337 if (pipe(m
->wakeup_pipe
) < 0) {
338 pa_log_error(__FILE__
": ERROR: cannot create wakeup pipe");
343 pa_make_nonblock_fd(m
->wakeup_pipe
[0]);
344 pa_make_nonblock_fd(m
->wakeup_pipe
[1]);
346 m
->io_events
= pa_idxset_new(NULL
, NULL
);
347 m
->defer_events
= pa_idxset_new(NULL
, NULL
);
348 m
->time_events
= pa_idxset_new(NULL
, NULL
);
350 assert(m
->io_events
&& m
->defer_events
&& m
->time_events
);
352 m
->io_events_scan_dead
= m
->defer_events_scan_dead
= m
->time_events_scan_dead
= 0;
355 m
->max_pollfds
= m
->n_pollfds
= 0;
356 m
->rebuild_pollfds
= 1;
358 m
->quit
= m
->retval
= 0;
363 m
->deferred_pending
= 0;
365 m
->state
= STATE_PASSIVE
;
368 m
->poll_func_userdata
= NULL
;
375 static int io_foreach(void *p
, uint32_t PA_GCC_UNUSED idx
, int *del
, void*userdata
) {
378 assert(e
&& del
&& all
);
380 if (!*all
&& !e
->dead
)
383 if (e
->destroy_callback
)
384 e
->destroy_callback(&e
->mainloop
->api
, e
, e
->userdata
);
390 static int time_foreach(void *p
, uint32_t PA_GCC_UNUSED idx
, int *del
, void*userdata
) {
391 pa_time_event
*e
= p
;
393 assert(e
&& del
&& all
);
395 if (!*all
&& !e
->dead
)
398 if (e
->destroy_callback
)
399 e
->destroy_callback(&e
->mainloop
->api
, e
, e
->userdata
);
405 static int defer_foreach(void *p
, PA_GCC_UNUSED
uint32_t idx
, int *del
, void*userdata
) {
406 pa_defer_event
*e
= p
;
408 assert(e
&& del
&& all
);
410 if (!*all
&& !e
->dead
)
413 if (e
->destroy_callback
)
414 e
->destroy_callback(&e
->mainloop
->api
, e
, e
->userdata
);
420 void pa_mainloop_free(pa_mainloop
* m
) {
424 pa_idxset_foreach(m
->io_events
, io_foreach
, &all
);
425 pa_idxset_foreach(m
->time_events
, time_foreach
, &all
);
426 pa_idxset_foreach(m
->defer_events
, defer_foreach
, &all
);
428 pa_idxset_free(m
->io_events
, NULL
, NULL
);
429 pa_idxset_free(m
->time_events
, NULL
, NULL
);
430 pa_idxset_free(m
->defer_events
, NULL
, NULL
);
432 pa_xfree(m
->pollfds
);
434 if (m
->wakeup_pipe
[0] >= 0)
435 close(m
->wakeup_pipe
[0]);
436 if (m
->wakeup_pipe
[1] >= 0)
437 close(m
->wakeup_pipe
[1]);
442 static void scan_dead(pa_mainloop
*m
) {
446 if (m
->io_events_scan_dead
)
447 pa_idxset_foreach(m
->io_events
, io_foreach
, &all
);
448 if (m
->time_events_scan_dead
)
449 pa_idxset_foreach(m
->time_events
, time_foreach
, &all
);
450 if (m
->defer_events_scan_dead
)
451 pa_idxset_foreach(m
->defer_events
, defer_foreach
, &all
);
453 m
->io_events_scan_dead
= m
->time_events_scan_dead
= m
->defer_events_scan_dead
= 0;
456 static void rebuild_pollfds(pa_mainloop
*m
) {
459 uint32_t idx
= PA_IDXSET_INVALID
;
462 l
= pa_idxset_size(m
->io_events
) + 1;
463 if (m
->max_pollfds
< l
) {
464 m
->pollfds
= pa_xrealloc(m
->pollfds
, sizeof(struct pollfd
)*l
);
471 if (m
->wakeup_pipe
[0] >= 0) {
472 m
->pollfds
[0].fd
= m
->wakeup_pipe
[0];
473 m
->pollfds
[0].events
= POLLIN
;
474 m
->pollfds
[0].revents
= 0;
479 for (e
= pa_idxset_first(m
->io_events
, &idx
); e
; e
= pa_idxset_next(m
->io_events
, &idx
)) {
488 ((e
->events
& PA_IO_EVENT_INPUT
) ? POLLIN
: 0) |
489 ((e
->events
& PA_IO_EVENT_OUTPUT
) ? POLLOUT
: 0) |
498 m
->rebuild_pollfds
= 0;
501 static int dispatch_pollfds(pa_mainloop
*m
) {
502 uint32_t idx
= PA_IDXSET_INVALID
;
506 for (e
= pa_idxset_first(m
->io_events
, &idx
); e
&& !m
->quit
; e
= pa_idxset_next(m
->io_events
, &idx
)) {
507 if (e
->dead
|| !e
->pollfd
|| !e
->pollfd
->revents
)
510 assert(e
->pollfd
->fd
== e
->fd
&& e
->callback
);
511 e
->callback(&m
->api
, e
, e
->fd
,
512 (e
->pollfd
->revents
& POLLHUP
? PA_IO_EVENT_HANGUP
: 0) |
513 (e
->pollfd
->revents
& POLLIN
? PA_IO_EVENT_INPUT
: 0) |
514 (e
->pollfd
->revents
& POLLOUT
? PA_IO_EVENT_OUTPUT
: 0) |
515 (e
->pollfd
->revents
& POLLERR
? PA_IO_EVENT_ERROR
: 0),
517 e
->pollfd
->revents
= 0;
524 static int dispatch_defer(pa_mainloop
*m
) {
529 if (!m
->deferred_pending
)
532 for (e
= pa_idxset_first(m
->defer_events
, &idx
); e
&& !m
->quit
; e
= pa_idxset_next(m
->defer_events
, &idx
)) {
533 if (e
->dead
|| !e
->enabled
)
537 e
->callback(&m
->api
, e
, e
->userdata
);
544 static int calc_next_timeout(pa_mainloop
*m
) {
551 if (pa_idxset_isempty(m
->time_events
))
554 for (e
= pa_idxset_first(m
->time_events
, &idx
); e
; e
= pa_idxset_next(m
->time_events
, &idx
)) {
557 if (e
->dead
|| !e
->enabled
)
560 /* Let's save a system call */
562 pa_gettimeofday(&now
);
566 if (e
->timeval
.tv_sec
< now
.tv_sec
|| (e
->timeval
.tv_sec
== now
.tv_sec
&& e
->timeval
.tv_usec
<= now
.tv_usec
))
569 tmp
= (e
->timeval
.tv_sec
- now
.tv_sec
)*1000;
571 if (e
->timeval
.tv_usec
> now
.tv_usec
)
572 tmp
+= (e
->timeval
.tv_usec
- now
.tv_usec
)/1000;
574 tmp
-= (now
.tv_usec
- e
->timeval
.tv_usec
)/1000;
578 else if (t
== -1 || tmp
< t
)
585 static int dispatch_timeout(pa_mainloop
*m
) {
593 if (pa_idxset_isempty(m
->time_events
))
596 for (e
= pa_idxset_first(m
->time_events
, &idx
); e
&& !m
->quit
; e
= pa_idxset_next(m
->time_events
, &idx
)) {
598 if (e
->dead
|| !e
->enabled
)
601 /* Let's save a system call */
603 pa_gettimeofday(&now
);
607 if (e
->timeval
.tv_sec
< now
.tv_sec
|| (e
->timeval
.tv_sec
== now
.tv_sec
&& e
->timeval
.tv_usec
<= now
.tv_usec
)) {
611 e
->callback(&m
->api
, e
, &e
->timeval
, e
->userdata
);
620 void pa_mainloop_wakeup(pa_mainloop
*m
) {
624 if (m
->wakeup_pipe
[1] >= 0)
625 pa_write(m
->wakeup_pipe
[1], &c
, sizeof(c
));
628 static void clear_wakeup(pa_mainloop
*m
) {
633 if (m
->wakeup_pipe
[0] < 0)
636 while (pa_read(m
->wakeup_pipe
[0], &c
, sizeof(c
)) == sizeof(c
));
639 int pa_mainloop_prepare(pa_mainloop
*m
, int timeout
) {
641 assert(m
->state
== STATE_PASSIVE
);
649 if (!m
->deferred_pending
) {
651 if (m
->rebuild_pollfds
)
654 m
->prepared_timeout
= calc_next_timeout(m
);
655 if (timeout
>= 0 && (timeout
< m
->prepared_timeout
|| m
->prepared_timeout
< 0))
656 m
->prepared_timeout
= timeout
;
659 m
->state
= STATE_PREPARED
;
663 m
->state
= STATE_QUIT
;
667 int pa_mainloop_poll(pa_mainloop
*m
) {
671 assert(m
->state
== STATE_PREPARED
);
676 m
->state
= STATE_POLLING
;
678 if (m
->deferred_pending
)
682 r
= m
->poll_func(m
->pollfds
, m
->n_pollfds
, m
->prepared_timeout
, m
->poll_func_userdata
);
684 r
= poll(m
->pollfds
, m
->n_pollfds
, m
->prepared_timeout
);
690 pa_log(__FILE__
": poll(): %s", strerror(errno
));
694 m
->state
= r
< 0 ? STATE_PASSIVE
: STATE_POLLED
;
698 m
->state
= STATE_QUIT
;
702 int pa_mainloop_dispatch(pa_mainloop
*m
) {
706 assert(m
->state
== STATE_POLLED
);
711 if (m
->deferred_pending
)
712 dispatched
+= dispatch_defer(m
);
714 dispatched
+= dispatch_timeout(m
);
719 dispatched
+= dispatch_pollfds(m
);
726 m
->state
= STATE_PASSIVE
;
731 m
->state
= STATE_QUIT
;
735 int pa_mainloop_get_retval(pa_mainloop
*m
) {
740 int pa_mainloop_iterate(pa_mainloop
*m
, int block
, int *retval
) {
744 if ((r
= pa_mainloop_prepare(m
, block
? -1 : 0)) < 0)
747 if ((r
= pa_mainloop_poll(m
)) < 0)
750 if ((r
= pa_mainloop_dispatch(m
)) < 0)
757 if ((r
== -2) && retval
)
758 *retval
= pa_mainloop_get_retval(m
);
762 int pa_mainloop_run(pa_mainloop
*m
, int *retval
) {
765 while ((r
= pa_mainloop_iterate(m
, 1, retval
)) >= 0);
775 void pa_mainloop_quit(pa_mainloop
*m
, int retval
) {
780 pa_mainloop_wakeup(m
);
783 pa_mainloop_api
* pa_mainloop_get_api(pa_mainloop
*m
) {
788 void pa_mainloop_set_poll_func(pa_mainloop
*m
, pa_poll_func poll_func
, void *userdata
) {
791 m
->poll_func
= poll_func
;
792 m
->poll_func_userdata
= userdata
;
797 void pa_mainloop_dump(pa_mainloop
*m
) {
800 pa_log(__FILE__
": Dumping mainloop sources START");
803 uint32_t idx
= PA_IDXSET_INVALID
;
805 for (e
= pa_idxset_first(m
->io_events
, &idx
); e
; e
= pa_idxset_next(m
->io_events
, &idx
)) {
809 pa_log(__FILE__
": kind=io fd=%i events=%i callback=%p userdata=%p", e
->fd
, (int) e
->events
, (void*) e
->callback
, (void*) e
->userdata
);
813 uint32_t idx
= PA_IDXSET_INVALID
;
815 for (e
= pa_idxset_first(m
->defer_events
, &idx
); e
; e
= pa_idxset_next(m
->defer_events
, &idx
)) {
819 pa_log(__FILE__
": kind=defer enabled=%i callback=%p userdata=%p", e
->enabled
, (void*) e
->callback
, (void*) e
->userdata
);
823 uint32_t idx
= PA_IDXSET_INVALID
;
825 for (e
= pa_idxset_first(m
->time_events
, &idx
); e
; e
= pa_idxset_next(m
->time_events
, &idx
)) {
829 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
);
833 pa_log(__FILE__
": Dumping mainloop sources STOP");