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 pa_mainloop_wakeup(m
);
126 e
= pa_xmalloc(sizeof(pa_io_event
));
132 e
->callback
= callback
;
133 e
->userdata
= userdata
;
134 e
->destroy_callback
= NULL
;
148 if ((select((SELECT_TYPE_ARG1
) fd
, NULL
, NULL
, SELECT_TYPE_ARG234
&xset
,
149 SELECT_TYPE_ARG5
&tv
) == -1) &&
150 (WSAGetLastError() == WSAENOTSOCK
)) {
151 pa_log_warn(__FILE__
": WARNING: cannot monitor non-socket file descriptors.");
157 pa_idxset_put(m
->io_events
, e
, NULL
);
158 m
->rebuild_pollfds
= 1;
162 static void mainloop_io_enable(pa_io_event
*e
, pa_io_event_flags_t events
) {
163 assert(e
&& e
->mainloop
);
165 pa_mainloop_wakeup(e
->mainloop
);
168 e
->mainloop
->rebuild_pollfds
= 1;
171 static void mainloop_io_free(pa_io_event
*e
) {
172 assert(e
&& e
->mainloop
);
174 pa_mainloop_wakeup(e
->mainloop
);
176 e
->dead
= e
->mainloop
->io_events_scan_dead
= e
->mainloop
->rebuild_pollfds
= 1;
179 static void mainloop_io_set_destroy(pa_io_event
*e
, void (*callback
)(pa_mainloop_api
*a
, pa_io_event
*e
, void *userdata
)) {
181 e
->destroy_callback
= callback
;
185 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
) {
189 assert(a
&& a
->userdata
&& callback
);
191 assert(a
== &m
->api
);
193 e
= pa_xmalloc(sizeof(pa_defer_event
));
198 e
->callback
= callback
;
199 e
->userdata
= userdata
;
200 e
->destroy_callback
= NULL
;
202 pa_idxset_put(m
->defer_events
, e
, NULL
);
204 m
->deferred_pending
++;
208 static void mainloop_defer_enable(pa_defer_event
*e
, int b
) {
211 if (e
->enabled
&& !b
) {
212 assert(e
->mainloop
->deferred_pending
> 0);
213 e
->mainloop
->deferred_pending
--;
214 } else if (!e
->enabled
&& b
)
215 e
->mainloop
->deferred_pending
++;
220 static void mainloop_defer_free(pa_defer_event
*e
) {
222 e
->dead
= e
->mainloop
->defer_events_scan_dead
= 1;
226 assert(e
->mainloop
->deferred_pending
> 0);
227 e
->mainloop
->deferred_pending
--;
231 static void mainloop_defer_set_destroy(pa_defer_event
*e
, void (*callback
)(pa_mainloop_api
*a
, pa_defer_event
*e
, void *userdata
)) {
233 e
->destroy_callback
= callback
;
237 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
) {
241 assert(a
&& a
->userdata
&& callback
);
243 assert(a
== &m
->api
);
245 pa_mainloop_wakeup(m
);
247 e
= pa_xmalloc(sizeof(pa_time_event
));
255 e
->callback
= callback
;
256 e
->userdata
= userdata
;
257 e
->destroy_callback
= NULL
;
259 pa_idxset_put(m
->time_events
, e
, NULL
);
264 static void mainloop_time_restart(pa_time_event
*e
, const struct timeval
*tv
) {
267 pa_mainloop_wakeup(e
->mainloop
);
276 static void mainloop_time_free(pa_time_event
*e
) {
279 pa_mainloop_wakeup(e
->mainloop
);
281 e
->dead
= e
->mainloop
->time_events_scan_dead
= 1;
284 static void mainloop_time_set_destroy(pa_time_event
*e
, void (*callback
)(pa_mainloop_api
*a
, pa_time_event
*e
, void *userdata
)) {
286 e
->destroy_callback
= callback
;
291 static void mainloop_quit(pa_mainloop_api
*a
, int retval
) {
293 assert(a
&& a
->userdata
);
295 assert(a
== &m
->api
);
297 pa_mainloop_wakeup(m
);
303 static const pa_mainloop_api vtable
= {
306 .io_new
= mainloop_io_new
,
307 .io_enable
= mainloop_io_enable
,
308 .io_free
= mainloop_io_free
,
309 .io_set_destroy
= mainloop_io_set_destroy
,
311 .time_new
= mainloop_time_new
,
312 .time_restart
= mainloop_time_restart
,
313 .time_free
= mainloop_time_free
,
314 .time_set_destroy
= mainloop_time_set_destroy
,
316 .defer_new
= mainloop_defer_new
,
317 .defer_enable
= mainloop_defer_enable
,
318 .defer_free
= mainloop_defer_free
,
319 .defer_set_destroy
= mainloop_defer_set_destroy
,
321 .quit
= mainloop_quit
,
324 pa_mainloop
*pa_mainloop_new(void) {
327 m
= pa_xmalloc(sizeof(pa_mainloop
));
330 if (pipe(m
->wakeup_pipe
) < 0) {
335 pa_make_nonblock_fd(m
->wakeup_pipe
[0]);
336 pa_make_nonblock_fd(m
->wakeup_pipe
[1]);
338 m
->wakeup_pipe
[0] = -1;
339 m
->wakeup_pipe
[1] = -1;
342 m
->io_events
= pa_idxset_new(NULL
, NULL
);
343 m
->defer_events
= pa_idxset_new(NULL
, NULL
);
344 m
->time_events
= pa_idxset_new(NULL
, NULL
);
346 assert(m
->io_events
&& m
->defer_events
&& m
->time_events
);
348 m
->io_events_scan_dead
= m
->defer_events_scan_dead
= m
->time_events_scan_dead
= 0;
351 m
->max_pollfds
= m
->n_pollfds
= m
->rebuild_pollfds
= 0;
353 m
->quit
= m
->retval
= 0;
358 m
->deferred_pending
= 0;
360 m
->state
= STATE_PASSIVE
;
363 m
->poll_func_userdata
= NULL
;
368 static int io_foreach(void *p
, uint32_t PA_GCC_UNUSED idx
, int *del
, void*userdata
) {
371 assert(e
&& del
&& all
);
373 if (!*all
&& !e
->dead
)
376 if (e
->destroy_callback
)
377 e
->destroy_callback(&e
->mainloop
->api
, e
, e
->userdata
);
383 static int time_foreach(void *p
, uint32_t PA_GCC_UNUSED idx
, int *del
, void*userdata
) {
384 pa_time_event
*e
= p
;
386 assert(e
&& del
&& all
);
388 if (!*all
&& !e
->dead
)
391 if (e
->destroy_callback
)
392 e
->destroy_callback(&e
->mainloop
->api
, e
, e
->userdata
);
398 static int defer_foreach(void *p
, PA_GCC_UNUSED
uint32_t idx
, int *del
, void*userdata
) {
399 pa_defer_event
*e
= p
;
401 assert(e
&& del
&& all
);
403 if (!*all
&& !e
->dead
)
406 if (e
->destroy_callback
)
407 e
->destroy_callback(&e
->mainloop
->api
, e
, e
->userdata
);
413 void pa_mainloop_free(pa_mainloop
* m
) {
415 assert(m
&& (m
->state
!= STATE_POLLING
));
417 pa_idxset_foreach(m
->io_events
, io_foreach
, &all
);
418 pa_idxset_foreach(m
->time_events
, time_foreach
, &all
);
419 pa_idxset_foreach(m
->defer_events
, defer_foreach
, &all
);
421 pa_idxset_free(m
->io_events
, NULL
, NULL
);
422 pa_idxset_free(m
->time_events
, NULL
, NULL
);
423 pa_idxset_free(m
->defer_events
, NULL
, NULL
);
425 pa_xfree(m
->pollfds
);
427 if (m
->wakeup_pipe
[0] >= 0)
428 close(m
->wakeup_pipe
[0]);
429 if (m
->wakeup_pipe
[1] >= 0)
430 close(m
->wakeup_pipe
[1]);
435 static void scan_dead(pa_mainloop
*m
) {
439 if (m
->io_events_scan_dead
)
440 pa_idxset_foreach(m
->io_events
, io_foreach
, &all
);
441 if (m
->time_events_scan_dead
)
442 pa_idxset_foreach(m
->time_events
, time_foreach
, &all
);
443 if (m
->defer_events_scan_dead
)
444 pa_idxset_foreach(m
->defer_events
, defer_foreach
, &all
);
446 m
->io_events_scan_dead
= m
->time_events_scan_dead
= m
->defer_events_scan_dead
= 0;
449 static void rebuild_pollfds(pa_mainloop
*m
) {
452 uint32_t idx
= PA_IDXSET_INVALID
;
455 l
= pa_idxset_size(m
->io_events
) + 1;
456 if (m
->max_pollfds
< l
) {
457 m
->pollfds
= pa_xrealloc(m
->pollfds
, sizeof(struct pollfd
)*l
);
464 if (m
->wakeup_pipe
[0] >= 0) {
465 m
->pollfds
[0].fd
= m
->wakeup_pipe
[0];
466 m
->pollfds
[0].events
= POLLIN
;
467 m
->pollfds
[0].revents
= 0;
472 for (e
= pa_idxset_first(m
->io_events
, &idx
); e
; e
= pa_idxset_next(m
->io_events
, &idx
)) {
481 ((e
->events
& PA_IO_EVENT_INPUT
) ? POLLIN
: 0) |
482 ((e
->events
& PA_IO_EVENT_OUTPUT
) ? POLLOUT
: 0) |
491 m
->rebuild_pollfds
= 0;
494 static int dispatch_pollfds(pa_mainloop
*m
) {
495 uint32_t idx
= PA_IDXSET_INVALID
;
499 for (e
= pa_idxset_first(m
->io_events
, &idx
); e
&& !m
->quit
; e
= pa_idxset_next(m
->io_events
, &idx
)) {
500 if (e
->dead
|| !e
->pollfd
|| !e
->pollfd
->revents
)
503 assert(e
->pollfd
->fd
== e
->fd
&& e
->callback
);
504 e
->callback(&m
->api
, e
, e
->fd
,
505 (e
->pollfd
->revents
& POLLHUP
? PA_IO_EVENT_HANGUP
: 0) |
506 (e
->pollfd
->revents
& POLLIN
? PA_IO_EVENT_INPUT
: 0) |
507 (e
->pollfd
->revents
& POLLOUT
? PA_IO_EVENT_OUTPUT
: 0) |
508 (e
->pollfd
->revents
& POLLERR
? PA_IO_EVENT_ERROR
: 0),
510 e
->pollfd
->revents
= 0;
517 static int dispatch_defer(pa_mainloop
*m
) {
522 if (!m
->deferred_pending
)
525 for (e
= pa_idxset_first(m
->defer_events
, &idx
); e
&& !m
->quit
; e
= pa_idxset_next(m
->defer_events
, &idx
)) {
526 if (e
->dead
|| !e
->enabled
)
530 e
->callback(&m
->api
, e
, e
->userdata
);
537 static int calc_next_timeout(pa_mainloop
*m
) {
544 if (pa_idxset_isempty(m
->time_events
))
547 for (e
= pa_idxset_first(m
->time_events
, &idx
); e
; e
= pa_idxset_next(m
->time_events
, &idx
)) {
550 if (e
->dead
|| !e
->enabled
)
553 /* Let's save a system call */
555 pa_gettimeofday(&now
);
559 if (e
->timeval
.tv_sec
< now
.tv_sec
|| (e
->timeval
.tv_sec
== now
.tv_sec
&& e
->timeval
.tv_usec
<= now
.tv_usec
))
562 tmp
= (e
->timeval
.tv_sec
- now
.tv_sec
)*1000;
564 if (e
->timeval
.tv_usec
> now
.tv_usec
)
565 tmp
+= (e
->timeval
.tv_usec
- now
.tv_usec
)/1000;
567 tmp
-= (now
.tv_usec
- e
->timeval
.tv_usec
)/1000;
571 else if (t
== -1 || tmp
< t
)
578 static int dispatch_timeout(pa_mainloop
*m
) {
586 if (pa_idxset_isempty(m
->time_events
))
589 for (e
= pa_idxset_first(m
->time_events
, &idx
); e
&& !m
->quit
; e
= pa_idxset_next(m
->time_events
, &idx
)) {
591 if (e
->dead
|| !e
->enabled
)
594 /* Let's save a system call */
596 pa_gettimeofday(&now
);
600 if (e
->timeval
.tv_sec
< now
.tv_sec
|| (e
->timeval
.tv_sec
== now
.tv_sec
&& e
->timeval
.tv_usec
<= now
.tv_usec
)) {
604 e
->callback(&m
->api
, e
, &e
->timeval
, e
->userdata
);
613 void pa_mainloop_wakeup(pa_mainloop
*m
) {
617 if (m
->wakeup_pipe
[1] >= 0)
618 write(m
->wakeup_pipe
[1], &c
, sizeof(c
));
621 static void clear_wakeup(pa_mainloop
*m
) {
626 if (m
->wakeup_pipe
[0] < 0)
629 while (read(m
->wakeup_pipe
[0], &c
, sizeof(c
)) == sizeof(c
));
632 int pa_mainloop_prepare(pa_mainloop
*m
, int timeout
) {
634 assert(m
->state
== STATE_PASSIVE
);
642 if (!m
->deferred_pending
) {
644 if (m
->rebuild_pollfds
)
647 m
->prepared_timeout
= calc_next_timeout(m
);
648 if (timeout
>= 0 && (timeout
< m
->prepared_timeout
|| m
->prepared_timeout
< 0))
649 m
->prepared_timeout
= timeout
;
652 m
->state
= STATE_PREPARED
;
656 m
->state
= STATE_QUIT
;
660 int pa_mainloop_poll(pa_mainloop
*m
) {
664 assert(m
->state
== STATE_PREPARED
);
669 m
->state
= STATE_POLLING
;
671 if (m
->deferred_pending
)
675 r
= m
->poll_func(m
->pollfds
, m
->n_pollfds
, m
->prepared_timeout
, m
->poll_func_userdata
);
677 r
= poll(m
->pollfds
, m
->n_pollfds
, m
->prepared_timeout
);
683 pa_log(__FILE__
": poll(): %s", strerror(errno
));
687 m
->state
= r
< 0 ? STATE_PASSIVE
: STATE_POLLED
;
691 m
->state
= STATE_QUIT
;
695 int pa_mainloop_dispatch(pa_mainloop
*m
) {
699 assert(m
->state
== STATE_POLLED
);
704 if (m
->deferred_pending
)
705 dispatched
+= dispatch_defer(m
);
707 dispatched
+= dispatch_timeout(m
);
712 dispatched
+= dispatch_pollfds(m
);
719 m
->state
= STATE_PASSIVE
;
724 m
->state
= STATE_QUIT
;
728 int pa_mainloop_get_retval(pa_mainloop
*m
) {
733 int pa_mainloop_iterate(pa_mainloop
*m
, int block
, int *retval
) {
737 if ((r
= pa_mainloop_prepare(m
, block
? -1 : 0)) < 0)
740 if ((r
= pa_mainloop_poll(m
)) < 0)
743 if ((r
= pa_mainloop_dispatch(m
)) < 0)
750 if ((r
== -2) && retval
)
751 *retval
= pa_mainloop_get_retval(m
);
755 int pa_mainloop_run(pa_mainloop
*m
, int *retval
) {
758 while ((r
= pa_mainloop_iterate(m
, 1, retval
)) >= 0);
768 void pa_mainloop_quit(pa_mainloop
*m
, int r
) {
770 pa_mainloop_wakeup(m
);
774 pa_mainloop_api
* pa_mainloop_get_api(pa_mainloop
*m
) {
779 void pa_mainloop_set_poll_func(pa_mainloop
*m
, pa_poll_func poll_func
, void *userdata
) {
782 m
->poll_func
= poll_func
;
783 m
->poll_func_userdata
= userdata
;
788 void pa_mainloop_dump(pa_mainloop
*m
) {
791 pa_log(__FILE__
": Dumping mainloop sources START");
794 uint32_t idx
= PA_IDXSET_INVALID
;
796 for (e
= pa_idxset_first(m
->io_events
, &idx
); e
; e
= pa_idxset_next(m
->io_events
, &idx
)) {
800 pa_log(__FILE__
": kind=io fd=%i events=%i callback=%p userdata=%p", e
->fd
, (int) e
->events
, (void*) e
->callback
, (void*) e
->userdata
);
804 uint32_t idx
= PA_IDXSET_INVALID
;
806 for (e
= pa_idxset_first(m
->defer_events
, &idx
); e
; e
= pa_idxset_next(m
->defer_events
, &idx
)) {
810 pa_log(__FILE__
": kind=defer enabled=%i callback=%p userdata=%p", e
->enabled
, (void*) e
->callback
, (void*) e
->userdata
);
814 uint32_t idx
= PA_IDXSET_INVALID
;
816 for (e
= pa_idxset_first(m
->time_events
, &idx
); e
; e
= pa_idxset_next(m
->time_events
, &idx
)) {
820 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
);
824 pa_log(__FILE__
": Dumping mainloop sources STOP");