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
41 #include <polypcore/winsock.h>
44 #include <polypcore/util.h>
45 #include <polypcore/idxset.h>
46 #include <polypcore/xmalloc.h>
47 #include <polypcore/log.h>
50 pa_mainloop
*mainloop
;
53 pa_io_event_flags_t events
;
54 void (*callback
) (pa_mainloop_api
*a
, pa_io_event
*e
, int fd
, pa_io_event_flags_t f
, void *userdata
);
55 struct pollfd
*pollfd
;
57 void (*destroy_callback
) (pa_mainloop_api
*a
, pa_io_event
*e
, void *userdata
);
60 struct pa_time_event
{
61 pa_mainloop
*mainloop
;
64 struct timeval timeval
;
65 void (*callback
)(pa_mainloop_api
*a
, pa_time_event
*e
, const struct timeval
*tv
, void *userdata
);
67 void (*destroy_callback
) (pa_mainloop_api
*a
, pa_time_event
*e
, void *userdata
);
70 struct pa_defer_event
{
71 pa_mainloop
*mainloop
;
74 void (*callback
)(pa_mainloop_api
*a
, pa_defer_event
*e
, void *userdata
);
76 void (*destroy_callback
) (pa_mainloop_api
*a
, pa_defer_event
*e
, void *userdata
);
80 pa_idxset
*io_events
, *time_events
, *defer_events
;
81 int io_events_scan_dead
, defer_events_scan_dead
, time_events_scan_dead
;
83 struct pollfd
*pollfds
;
84 unsigned max_pollfds
, n_pollfds
;
106 static pa_io_event
* mainloop_io_new(
109 pa_io_event_flags_t events
,
110 void (*callback
) (pa_mainloop_api
*a
, pa_io_event
*e
, int fd
, pa_io_event_flags_t events
, void *userdata
),
116 assert(a
&& a
->userdata
&& fd
>= 0 && callback
);
118 assert(a
== &m
->api
);
120 pa_mainloop_wakeup(m
);
122 e
= pa_xmalloc(sizeof(pa_io_event
));
128 e
->callback
= callback
;
129 e
->userdata
= userdata
;
130 e
->destroy_callback
= NULL
;
144 if ((select((SELECT_TYPE_ARG1
) fd
, NULL
, NULL
, SELECT_TYPE_ARG234
&xset
,
145 SELECT_TYPE_ARG5
&tv
) == -1) &&
146 (WSAGetLastError() == WSAENOTSOCK
)) {
147 pa_log_warn(__FILE__
": WARNING: cannot monitor non-socket file descriptors.\n");
153 pa_idxset_put(m
->io_events
, e
, NULL
);
154 m
->rebuild_pollfds
= 1;
158 static void mainloop_io_enable(pa_io_event
*e
, pa_io_event_flags_t events
) {
159 assert(e
&& e
->mainloop
);
161 pa_mainloop_wakeup(e
->mainloop
);
164 e
->mainloop
->rebuild_pollfds
= 1;
167 static void mainloop_io_free(pa_io_event
*e
) {
168 assert(e
&& e
->mainloop
);
170 pa_mainloop_wakeup(e
->mainloop
);
172 e
->dead
= e
->mainloop
->io_events_scan_dead
= e
->mainloop
->rebuild_pollfds
= 1;
175 static void mainloop_io_set_destroy(pa_io_event
*e
, void (*callback
)(pa_mainloop_api
*a
, pa_io_event
*e
, void *userdata
)) {
177 e
->destroy_callback
= callback
;
181 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
) {
185 assert(a
&& a
->userdata
&& callback
);
187 assert(a
== &m
->api
);
189 e
= pa_xmalloc(sizeof(pa_defer_event
));
194 e
->callback
= callback
;
195 e
->userdata
= userdata
;
196 e
->destroy_callback
= NULL
;
198 pa_idxset_put(m
->defer_events
, e
, NULL
);
200 m
->deferred_pending
++;
204 static void mainloop_defer_enable(pa_defer_event
*e
, int b
) {
207 if (e
->enabled
&& !b
) {
208 assert(e
->mainloop
->deferred_pending
> 0);
209 e
->mainloop
->deferred_pending
--;
210 } else if (!e
->enabled
&& b
)
211 e
->mainloop
->deferred_pending
++;
216 static void mainloop_defer_free(pa_defer_event
*e
) {
218 e
->dead
= e
->mainloop
->defer_events_scan_dead
= 1;
222 assert(e
->mainloop
->deferred_pending
> 0);
223 e
->mainloop
->deferred_pending
--;
227 static void mainloop_defer_set_destroy(pa_defer_event
*e
, void (*callback
)(pa_mainloop_api
*a
, pa_defer_event
*e
, void *userdata
)) {
229 e
->destroy_callback
= callback
;
233 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
) {
237 assert(a
&& a
->userdata
&& callback
);
239 assert(a
== &m
->api
);
241 pa_mainloop_wakeup(m
);
243 e
= pa_xmalloc(sizeof(pa_time_event
));
251 e
->callback
= callback
;
252 e
->userdata
= userdata
;
253 e
->destroy_callback
= NULL
;
255 pa_idxset_put(m
->time_events
, e
, NULL
);
260 static void mainloop_time_restart(pa_time_event
*e
, const struct timeval
*tv
) {
263 pa_mainloop_wakeup(e
->mainloop
);
272 static void mainloop_time_free(pa_time_event
*e
) {
275 pa_mainloop_wakeup(e
->mainloop
);
277 e
->dead
= e
->mainloop
->time_events_scan_dead
= 1;
280 static void mainloop_time_set_destroy(pa_time_event
*e
, void (*callback
)(pa_mainloop_api
*a
, pa_time_event
*e
, void *userdata
)) {
282 e
->destroy_callback
= callback
;
287 static void mainloop_quit(pa_mainloop_api
*a
, int retval
) {
289 assert(a
&& a
->userdata
);
291 assert(a
== &m
->api
);
293 pa_mainloop_wakeup(m
);
299 static const pa_mainloop_api vtable
= {
302 .io_new
= mainloop_io_new
,
303 .io_enable
= mainloop_io_enable
,
304 .io_free
= mainloop_io_free
,
305 .io_set_destroy
= mainloop_io_set_destroy
,
307 .time_new
= mainloop_time_new
,
308 .time_restart
= mainloop_time_restart
,
309 .time_free
= mainloop_time_free
,
310 .time_set_destroy
= mainloop_time_set_destroy
,
312 .defer_new
= mainloop_defer_new
,
313 .defer_enable
= mainloop_defer_enable
,
314 .defer_free
= mainloop_defer_free
,
315 .defer_set_destroy
= mainloop_defer_set_destroy
,
317 .quit
= mainloop_quit
,
320 pa_mainloop
*pa_mainloop_new(void) {
323 m
= pa_xmalloc(sizeof(pa_mainloop
));
326 if (pipe(m
->wakeup_pipe
) < 0) {
331 pa_make_nonblock_fd(m
->wakeup_pipe
[0]);
332 pa_make_nonblock_fd(m
->wakeup_pipe
[1]);
334 m
->wakeup_pipe
[0] = -1;
335 m
->wakeup_pipe
[1] = -1;
338 m
->io_events
= pa_idxset_new(NULL
, NULL
);
339 m
->defer_events
= pa_idxset_new(NULL
, NULL
);
340 m
->time_events
= pa_idxset_new(NULL
, NULL
);
342 assert(m
->io_events
&& m
->defer_events
&& m
->time_events
);
344 m
->io_events_scan_dead
= m
->defer_events_scan_dead
= m
->time_events_scan_dead
= 0;
347 m
->max_pollfds
= m
->n_pollfds
= m
->rebuild_pollfds
= 0;
349 m
->quit
= m
->retval
= 0;
354 m
->deferred_pending
= 0;
356 m
->state
= STATE_PASSIVE
;
361 static int io_foreach(void *p
, uint32_t PA_GCC_UNUSED idx
, int *del
, void*userdata
) {
364 assert(e
&& del
&& all
);
366 if (!*all
&& !e
->dead
)
369 if (e
->destroy_callback
)
370 e
->destroy_callback(&e
->mainloop
->api
, e
, e
->userdata
);
376 static int time_foreach(void *p
, uint32_t PA_GCC_UNUSED idx
, int *del
, void*userdata
) {
377 pa_time_event
*e
= p
;
379 assert(e
&& del
&& all
);
381 if (!*all
&& !e
->dead
)
384 if (e
->destroy_callback
)
385 e
->destroy_callback(&e
->mainloop
->api
, e
, e
->userdata
);
391 static int defer_foreach(void *p
, PA_GCC_UNUSED
uint32_t idx
, int *del
, void*userdata
) {
392 pa_defer_event
*e
= p
;
394 assert(e
&& del
&& all
);
396 if (!*all
&& !e
->dead
)
399 if (e
->destroy_callback
)
400 e
->destroy_callback(&e
->mainloop
->api
, e
, e
->userdata
);
406 void pa_mainloop_free(pa_mainloop
* m
) {
408 assert(m
&& (m
->state
!= STATE_POLLING
));
410 pa_idxset_foreach(m
->io_events
, io_foreach
, &all
);
411 pa_idxset_foreach(m
->time_events
, time_foreach
, &all
);
412 pa_idxset_foreach(m
->defer_events
, defer_foreach
, &all
);
414 pa_idxset_free(m
->io_events
, NULL
, NULL
);
415 pa_idxset_free(m
->time_events
, NULL
, NULL
);
416 pa_idxset_free(m
->defer_events
, NULL
, NULL
);
418 pa_xfree(m
->pollfds
);
420 if (m
->wakeup_pipe
[0] >= 0)
421 close(m
->wakeup_pipe
[0]);
422 if (m
->wakeup_pipe
[1] >= 0)
423 close(m
->wakeup_pipe
[1]);
428 static void scan_dead(pa_mainloop
*m
) {
432 if (m
->io_events_scan_dead
)
433 pa_idxset_foreach(m
->io_events
, io_foreach
, &all
);
434 if (m
->time_events_scan_dead
)
435 pa_idxset_foreach(m
->time_events
, time_foreach
, &all
);
436 if (m
->defer_events_scan_dead
)
437 pa_idxset_foreach(m
->defer_events
, defer_foreach
, &all
);
439 m
->io_events_scan_dead
= m
->time_events_scan_dead
= m
->defer_events_scan_dead
= 0;
442 static void rebuild_pollfds(pa_mainloop
*m
) {
445 uint32_t idx
= PA_IDXSET_INVALID
;
448 l
= pa_idxset_size(m
->io_events
) + 1;
449 if (m
->max_pollfds
< l
) {
450 m
->pollfds
= pa_xrealloc(m
->pollfds
, sizeof(struct pollfd
)*l
);
457 if (m
->wakeup_pipe
[0] >= 0) {
458 m
->pollfds
[0].fd
= m
->wakeup_pipe
[0];
459 m
->pollfds
[0].events
= POLLIN
;
460 m
->pollfds
[0].revents
= 0;
465 for (e
= pa_idxset_first(m
->io_events
, &idx
); e
; e
= pa_idxset_next(m
->io_events
, &idx
)) {
474 ((e
->events
& PA_IO_EVENT_INPUT
) ? POLLIN
: 0) |
475 ((e
->events
& PA_IO_EVENT_OUTPUT
) ? POLLOUT
: 0) |
484 m
->rebuild_pollfds
= 0;
487 static int dispatch_pollfds(pa_mainloop
*m
) {
488 uint32_t idx
= PA_IDXSET_INVALID
;
492 for (e
= pa_idxset_first(m
->io_events
, &idx
); e
&& !m
->quit
; e
= pa_idxset_next(m
->io_events
, &idx
)) {
493 if (e
->dead
|| !e
->pollfd
|| !e
->pollfd
->revents
)
496 assert(e
->pollfd
->fd
== e
->fd
&& e
->callback
);
497 e
->callback(&m
->api
, e
, e
->fd
,
498 (e
->pollfd
->revents
& POLLHUP
? PA_IO_EVENT_HANGUP
: 0) |
499 (e
->pollfd
->revents
& POLLIN
? PA_IO_EVENT_INPUT
: 0) |
500 (e
->pollfd
->revents
& POLLOUT
? PA_IO_EVENT_OUTPUT
: 0) |
501 (e
->pollfd
->revents
& POLLERR
? PA_IO_EVENT_ERROR
: 0),
503 e
->pollfd
->revents
= 0;
510 static int dispatch_defer(pa_mainloop
*m
) {
515 if (!m
->deferred_pending
)
518 for (e
= pa_idxset_first(m
->defer_events
, &idx
); e
&& !m
->quit
; e
= pa_idxset_next(m
->defer_events
, &idx
)) {
519 if (e
->dead
|| !e
->enabled
)
523 e
->callback(&m
->api
, e
, e
->userdata
);
530 static int calc_next_timeout(pa_mainloop
*m
) {
537 if (pa_idxset_isempty(m
->time_events
))
540 for (e
= pa_idxset_first(m
->time_events
, &idx
); e
; e
= pa_idxset_next(m
->time_events
, &idx
)) {
543 if (e
->dead
|| !e
->enabled
)
546 /* Let's save a system call */
548 pa_gettimeofday(&now
);
552 if (e
->timeval
.tv_sec
< now
.tv_sec
|| (e
->timeval
.tv_sec
== now
.tv_sec
&& e
->timeval
.tv_usec
<= now
.tv_usec
))
555 tmp
= (e
->timeval
.tv_sec
- now
.tv_sec
)*1000;
557 if (e
->timeval
.tv_usec
> now
.tv_usec
)
558 tmp
+= (e
->timeval
.tv_usec
- now
.tv_usec
)/1000;
560 tmp
-= (now
.tv_usec
- e
->timeval
.tv_usec
)/1000;
564 else if (t
== -1 || tmp
< t
)
571 static int dispatch_timeout(pa_mainloop
*m
) {
579 if (pa_idxset_isempty(m
->time_events
))
582 for (e
= pa_idxset_first(m
->time_events
, &idx
); e
&& !m
->quit
; e
= pa_idxset_next(m
->time_events
, &idx
)) {
584 if (e
->dead
|| !e
->enabled
)
587 /* Let's save a system call */
589 pa_gettimeofday(&now
);
593 if (e
->timeval
.tv_sec
< now
.tv_sec
|| (e
->timeval
.tv_sec
== now
.tv_sec
&& e
->timeval
.tv_usec
<= now
.tv_usec
)) {
597 e
->callback(&m
->api
, e
, &e
->timeval
, e
->userdata
);
606 void pa_mainloop_wakeup(pa_mainloop
*m
) {
610 if (m
->wakeup_pipe
[1] >= 0)
611 write(m
->wakeup_pipe
[1], &c
, sizeof(c
));
614 static void clear_wakeup(pa_mainloop
*m
) {
619 if (m
->wakeup_pipe
[0] < 0)
622 while (read(m
->wakeup_pipe
[0], &c
, sizeof(c
)) == sizeof(c
));
625 int pa_mainloop_prepare(pa_mainloop
*m
, int timeout
) {
628 assert(m
&& (m
->state
== STATE_PASSIVE
));
637 dispatched
+= dispatch_defer(m
);
642 if (m
->rebuild_pollfds
)
645 m
->prepared_timeout
= calc_next_timeout(m
);
646 if ((timeout
>= 0) && (m
->prepared_timeout
> timeout
))
647 m
->prepared_timeout
= timeout
;
649 m
->state
= STATE_PREPARED
;
655 m
->state
= STATE_QUIT
;
660 int pa_mainloop_poll(pa_mainloop
*m
) {
663 assert(m
&& (m
->state
== STATE_PREPARED
));
665 m
->state
= STATE_POLLING
;
667 r
= poll(m
->pollfds
, m
->n_pollfds
, m
->prepared_timeout
);
669 if ((r
< 0) && (errno
== EINTR
))
673 m
->state
= STATE_PASSIVE
;
675 m
->state
= STATE_POLLED
;
680 int pa_mainloop_dispatch(pa_mainloop
*m
) {
683 assert(m
&& (m
->state
== STATE_POLLED
));
685 dispatched
+= dispatch_timeout(m
);
690 dispatched
+= dispatch_pollfds(m
);
695 m
->state
= STATE_PASSIVE
;
701 m
->state
= STATE_QUIT
;
706 int pa_mainloop_get_retval(pa_mainloop
*m
) {
711 int pa_mainloop_iterate(pa_mainloop
*m
, int block
, int *retval
) {
712 int r
, dispatched
= 0;
716 r
= pa_mainloop_prepare(m
, block
? -1 : 0);
718 if ((r
== -2) && retval
)
719 *retval
= pa_mainloop_get_retval(m
);
725 r
= pa_mainloop_poll(m
);
727 pa_log(__FILE__
": poll(): %s\n", strerror(errno
));
731 r
= pa_mainloop_dispatch(m
);
733 if ((r
== -2) && retval
)
734 *retval
= pa_mainloop_get_retval(m
);
743 int pa_mainloop_run(pa_mainloop
*m
, int *retval
) {
745 while ((r
= pa_mainloop_iterate(m
, 1, retval
)) >= 0);
755 void pa_mainloop_quit(pa_mainloop
*m
, int r
) {
757 pa_mainloop_wakeup(m
);
761 pa_mainloop_api
* pa_mainloop_get_api(pa_mainloop
*m
) {
766 int pa_mainloop_deferred_pending(pa_mainloop
*m
) {
768 return m
->deferred_pending
> 0;
773 void pa_mainloop_dump(pa_mainloop
*m
) {
776 pa_log(__FILE__
": Dumping mainloop sources START\n");
779 uint32_t idx
= PA_IDXSET_INVALID
;
781 for (e
= pa_idxset_first(m
->io_events
, &idx
); e
; e
= pa_idxset_next(m
->io_events
, &idx
)) {
785 pa_log(__FILE__
": kind=io fd=%i events=%i callback=%p userdata=%p\n", e
->fd
, (int) e
->events
, (void*) e
->callback
, (void*) e
->userdata
);
789 uint32_t idx
= PA_IDXSET_INVALID
;
791 for (e
= pa_idxset_first(m
->defer_events
, &idx
); e
; e
= pa_idxset_next(m
->defer_events
, &idx
)) {
795 pa_log(__FILE__
": kind=defer enabled=%i callback=%p userdata=%p\n", e
->enabled
, (void*) e
->callback
, (void*) e
->userdata
);
799 uint32_t idx
= PA_IDXSET_INVALID
;
801 for (e
= pa_idxset_first(m
->time_events
, &idx
); e
; e
= pa_idxset_next(m
->time_events
, &idx
)) {
805 pa_log(__FILE__
": kind=time enabled=%i time=%lu.%lu callback=%p userdata=%p\n", e
->enabled
, (unsigned long) e
->timeval
.tv_sec
, (unsigned long) e
->timeval
.tv_usec
, (void*) e
->callback
, (void*) e
->userdata
);
809 pa_log(__FILE__
": Dumping mainloop sources STOP\n");