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"
43 #include <polypcore/util.h>
44 #include <polypcore/idxset.h>
45 #include <polypcore/xmalloc.h>
46 #include <polypcore/log.h>
51 pa_mainloop
*mainloop
;
54 pa_io_event_flags_t events
;
55 void (*callback
) (pa_mainloop_api
*a
, pa_io_event
*e
, int fd
, pa_io_event_flags_t f
, void *userdata
);
56 struct pollfd
*pollfd
;
58 void (*destroy_callback
) (pa_mainloop_api
*a
, pa_io_event
*e
, void *userdata
);
61 struct pa_time_event
{
62 pa_mainloop
*mainloop
;
65 struct timeval timeval
;
66 void (*callback
)(pa_mainloop_api
*a
, pa_time_event
*e
, const struct timeval
*tv
, void *userdata
);
68 void (*destroy_callback
) (pa_mainloop_api
*a
, pa_time_event
*e
, void *userdata
);
71 struct pa_defer_event
{
72 pa_mainloop
*mainloop
;
75 void (*callback
)(pa_mainloop_api
*a
, pa_defer_event
*e
, void *userdata
);
77 void (*destroy_callback
) (pa_mainloop_api
*a
, pa_defer_event
*e
, void *userdata
);
81 pa_idxset
*io_events
, *time_events
, *defer_events
;
82 int io_events_scan_dead
, defer_events_scan_dead
, time_events_scan_dead
;
84 struct pollfd
*pollfds
;
85 unsigned max_pollfds
, n_pollfds
;
105 pa_poll_func poll_func
;
106 void *poll_func_userdata
;
110 static pa_io_event
* mainloop_io_new(
113 pa_io_event_flags_t events
,
114 void (*callback
) (pa_mainloop_api
*a
, pa_io_event
*e
, int fd
, pa_io_event_flags_t events
, void *userdata
),
120 assert(a
&& a
->userdata
&& fd
>= 0 && callback
);
122 assert(a
== &m
->api
);
124 e
= pa_xmalloc(sizeof(pa_io_event
));
130 e
->callback
= callback
;
131 e
->userdata
= userdata
;
132 e
->destroy_callback
= NULL
;
146 if ((select((SELECT_TYPE_ARG1
) fd
, NULL
, NULL
, SELECT_TYPE_ARG234
&xset
,
147 SELECT_TYPE_ARG5
&tv
) == -1) &&
148 (WSAGetLastError() == WSAENOTSOCK
)) {
149 pa_log_warn(__FILE__
": WARNING: cannot monitor non-socket file descriptors.");
155 pa_idxset_put(m
->io_events
, e
, NULL
);
156 m
->rebuild_pollfds
= 1;
158 pa_mainloop_wakeup(m
);
163 static void mainloop_io_enable(pa_io_event
*e
, pa_io_event_flags_t events
) {
164 assert(e
&& e
->mainloop
);
167 e
->mainloop
->rebuild_pollfds
= 1;
169 pa_mainloop_wakeup(e
->mainloop
);
172 static void mainloop_io_free(pa_io_event
*e
) {
173 assert(e
&& e
->mainloop
);
175 e
->dead
= e
->mainloop
->io_events_scan_dead
= e
->mainloop
->rebuild_pollfds
= 1;
177 pa_mainloop_wakeup(e
->mainloop
);
180 static void mainloop_io_set_destroy(pa_io_event
*e
, void (*callback
)(pa_mainloop_api
*a
, pa_io_event
*e
, void *userdata
)) {
182 e
->destroy_callback
= callback
;
186 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
) {
190 assert(a
&& a
->userdata
&& callback
);
192 assert(a
== &m
->api
);
194 e
= pa_xmalloc(sizeof(pa_defer_event
));
199 e
->callback
= callback
;
200 e
->userdata
= userdata
;
201 e
->destroy_callback
= NULL
;
203 pa_idxset_put(m
->defer_events
, e
, NULL
);
205 m
->deferred_pending
++;
207 pa_mainloop_wakeup(e
->mainloop
);
212 static void mainloop_defer_enable(pa_defer_event
*e
, int b
) {
215 if (e
->enabled
&& !b
) {
216 assert(e
->mainloop
->deferred_pending
> 0);
217 e
->mainloop
->deferred_pending
--;
218 } else if (!e
->enabled
&& b
) {
219 e
->mainloop
->deferred_pending
++;
220 pa_mainloop_wakeup(e
->mainloop
);
226 static void mainloop_defer_free(pa_defer_event
*e
) {
228 e
->dead
= e
->mainloop
->defer_events_scan_dead
= 1;
232 assert(e
->mainloop
->deferred_pending
> 0);
233 e
->mainloop
->deferred_pending
--;
237 static void mainloop_defer_set_destroy(pa_defer_event
*e
, void (*callback
)(pa_mainloop_api
*a
, pa_defer_event
*e
, void *userdata
)) {
239 e
->destroy_callback
= callback
;
243 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
) {
247 assert(a
&& a
->userdata
&& callback
);
249 assert(a
== &m
->api
);
251 e
= pa_xmalloc(sizeof(pa_time_event
));
259 e
->callback
= callback
;
260 e
->userdata
= userdata
;
261 e
->destroy_callback
= NULL
;
263 pa_idxset_put(m
->time_events
, e
, NULL
);
266 pa_mainloop_wakeup(m
);
271 static void mainloop_time_restart(pa_time_event
*e
, const struct timeval
*tv
) {
278 pa_mainloop_wakeup(e
->mainloop
);
283 static void mainloop_time_free(pa_time_event
*e
) {
286 e
->dead
= e
->mainloop
->time_events_scan_dead
= 1;
288 /* no wakeup needed here. Think about it! */
291 static void mainloop_time_set_destroy(pa_time_event
*e
, void (*callback
)(pa_mainloop_api
*a
, pa_time_event
*e
, void *userdata
)) {
293 e
->destroy_callback
= callback
;
298 static void mainloop_quit(pa_mainloop_api
*a
, int retval
) {
300 assert(a
&& a
->userdata
);
302 assert(a
== &m
->api
);
307 pa_mainloop_wakeup(m
);
310 static const pa_mainloop_api vtable
= {
313 .io_new
= mainloop_io_new
,
314 .io_enable
= mainloop_io_enable
,
315 .io_free
= mainloop_io_free
,
316 .io_set_destroy
= mainloop_io_set_destroy
,
318 .time_new
= mainloop_time_new
,
319 .time_restart
= mainloop_time_restart
,
320 .time_free
= mainloop_time_free
,
321 .time_set_destroy
= mainloop_time_set_destroy
,
323 .defer_new
= mainloop_defer_new
,
324 .defer_enable
= mainloop_defer_enable
,
325 .defer_free
= mainloop_defer_free
,
326 .defer_set_destroy
= mainloop_defer_set_destroy
,
328 .quit
= mainloop_quit
,
331 pa_mainloop
*pa_mainloop_new(void) {
334 m
= pa_xmalloc(sizeof(pa_mainloop
));
337 if (pipe(m
->wakeup_pipe
) < 0) {
342 pa_make_nonblock_fd(m
->wakeup_pipe
[0]);
343 pa_make_nonblock_fd(m
->wakeup_pipe
[1]);
345 m
->wakeup_pipe
[0] = -1;
346 m
->wakeup_pipe
[1] = -1;
349 m
->io_events
= pa_idxset_new(NULL
, NULL
);
350 m
->defer_events
= pa_idxset_new(NULL
, NULL
);
351 m
->time_events
= pa_idxset_new(NULL
, NULL
);
353 assert(m
->io_events
&& m
->defer_events
&& m
->time_events
);
355 m
->io_events_scan_dead
= m
->defer_events_scan_dead
= m
->time_events_scan_dead
= 0;
358 m
->max_pollfds
= m
->n_pollfds
= m
->rebuild_pollfds
= 0;
360 m
->quit
= m
->retval
= 0;
365 m
->deferred_pending
= 0;
367 m
->state
= STATE_PASSIVE
;
370 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
) {
422 assert(m
&& (m
->state
!= STATE_POLLING
));
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 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 (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 r
) {
777 pa_mainloop_wakeup(m
);
781 pa_mainloop_api
* pa_mainloop_get_api(pa_mainloop
*m
) {
786 void pa_mainloop_set_poll_func(pa_mainloop
*m
, pa_poll_func poll_func
, void *userdata
) {
789 m
->poll_func
= poll_func
;
790 m
->poll_func_userdata
= userdata
;
795 void pa_mainloop_dump(pa_mainloop
*m
) {
798 pa_log(__FILE__
": Dumping mainloop sources START");
801 uint32_t idx
= PA_IDXSET_INVALID
;
803 for (e
= pa_idxset_first(m
->io_events
, &idx
); e
; e
= pa_idxset_next(m
->io_events
, &idx
)) {
807 pa_log(__FILE__
": kind=io fd=%i events=%i callback=%p userdata=%p", e
->fd
, (int) e
->events
, (void*) e
->callback
, (void*) e
->userdata
);
811 uint32_t idx
= PA_IDXSET_INVALID
;
813 for (e
= pa_idxset_first(m
->defer_events
, &idx
); e
; e
= pa_idxset_next(m
->defer_events
, &idx
)) {
817 pa_log(__FILE__
": kind=defer enabled=%i callback=%p userdata=%p", e
->enabled
, (void*) e
->callback
, (void*) e
->userdata
);
821 uint32_t idx
= PA_IDXSET_INVALID
;
823 for (e
= pa_idxset_first(m
->time_events
, &idx
); e
; e
= pa_idxset_next(m
->time_events
, &idx
)) {
827 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
);
831 pa_log(__FILE__
": Dumping mainloop sources STOP");