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/core-error.h>
48 #include <polyp/timeval.h>
49 #include <polyp/xmalloc.h>
51 #include <polypcore/core-util.h>
52 #include <polypcore/idxset.h>
53 #include <polypcore/log.h>
58 pa_mainloop
*mainloop
;
61 pa_io_event_flags_t events
;
62 void (*callback
) (pa_mainloop_api
*a
, pa_io_event
*e
, int fd
, pa_io_event_flags_t f
, void *userdata
);
63 struct pollfd
*pollfd
;
65 void (*destroy_callback
) (pa_mainloop_api
*a
, pa_io_event
*e
, void *userdata
);
68 struct pa_time_event
{
69 pa_mainloop
*mainloop
;
72 struct timeval timeval
;
73 void (*callback
)(pa_mainloop_api
*a
, pa_time_event
*e
, const struct timeval
*tv
, void *userdata
);
75 void (*destroy_callback
) (pa_mainloop_api
*a
, pa_time_event
*e
, void *userdata
);
78 struct pa_defer_event
{
79 pa_mainloop
*mainloop
;
82 void (*callback
)(pa_mainloop_api
*a
, pa_defer_event
*e
, void *userdata
);
84 void (*destroy_callback
) (pa_mainloop_api
*a
, pa_defer_event
*e
, void *userdata
);
88 pa_idxset
*io_events
, *time_events
, *defer_events
;
89 int io_events_scan_dead
, defer_events_scan_dead
, time_events_scan_dead
;
91 struct pollfd
*pollfds
;
92 unsigned max_pollfds
, n_pollfds
;
100 int deferred_pending
;
112 pa_poll_func poll_func
;
113 void *poll_func_userdata
;
117 static pa_io_event
* mainloop_io_new(
120 pa_io_event_flags_t events
,
121 void (*callback
) (pa_mainloop_api
*a
, pa_io_event
*e
, int fd
, pa_io_event_flags_t events
, void *userdata
),
127 assert(a
&& a
->userdata
&& fd
>= 0 && callback
);
129 assert(a
== &m
->api
);
131 e
= pa_xmalloc(sizeof(pa_io_event
));
137 e
->callback
= callback
;
138 e
->userdata
= userdata
;
139 e
->destroy_callback
= NULL
;
153 if ((select((SELECT_TYPE_ARG1
) fd
, NULL
, NULL
, SELECT_TYPE_ARG234
&xset
,
154 SELECT_TYPE_ARG5
&tv
) == -1) &&
155 (WSAGetLastError() == WSAENOTSOCK
)) {
156 pa_log_warn(__FILE__
": WARNING: cannot monitor non-socket file descriptors.");
162 pa_idxset_put(m
->io_events
, e
, NULL
);
163 m
->rebuild_pollfds
= 1;
165 pa_mainloop_wakeup(m
);
170 static void mainloop_io_enable(pa_io_event
*e
, pa_io_event_flags_t events
) {
171 assert(e
&& e
->mainloop
);
174 e
->mainloop
->rebuild_pollfds
= 1;
176 pa_mainloop_wakeup(e
->mainloop
);
179 static void mainloop_io_free(pa_io_event
*e
) {
180 assert(e
&& e
->mainloop
);
182 e
->dead
= e
->mainloop
->io_events_scan_dead
= e
->mainloop
->rebuild_pollfds
= 1;
184 pa_mainloop_wakeup(e
->mainloop
);
187 static void mainloop_io_set_destroy(pa_io_event
*e
, void (*callback
)(pa_mainloop_api
*a
, pa_io_event
*e
, void *userdata
)) {
189 e
->destroy_callback
= callback
;
193 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
) {
197 assert(a
&& a
->userdata
&& callback
);
199 assert(a
== &m
->api
);
201 e
= pa_xmalloc(sizeof(pa_defer_event
));
206 e
->callback
= callback
;
207 e
->userdata
= userdata
;
208 e
->destroy_callback
= NULL
;
210 pa_idxset_put(m
->defer_events
, e
, NULL
);
212 m
->deferred_pending
++;
214 pa_mainloop_wakeup(e
->mainloop
);
219 static void mainloop_defer_enable(pa_defer_event
*e
, int b
) {
222 if (e
->enabled
&& !b
) {
223 assert(e
->mainloop
->deferred_pending
> 0);
224 e
->mainloop
->deferred_pending
--;
225 } else if (!e
->enabled
&& b
) {
226 e
->mainloop
->deferred_pending
++;
227 pa_mainloop_wakeup(e
->mainloop
);
233 static void mainloop_defer_free(pa_defer_event
*e
) {
235 e
->dead
= e
->mainloop
->defer_events_scan_dead
= 1;
239 assert(e
->mainloop
->deferred_pending
> 0);
240 e
->mainloop
->deferred_pending
--;
244 static void mainloop_defer_set_destroy(pa_defer_event
*e
, void (*callback
)(pa_mainloop_api
*a
, pa_defer_event
*e
, void *userdata
)) {
246 e
->destroy_callback
= callback
;
250 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
) {
254 assert(a
&& a
->userdata
&& callback
);
256 assert(a
== &m
->api
);
258 e
= pa_xmalloc(sizeof(pa_time_event
));
266 e
->callback
= callback
;
267 e
->userdata
= userdata
;
268 e
->destroy_callback
= NULL
;
270 pa_idxset_put(m
->time_events
, e
, NULL
);
273 pa_mainloop_wakeup(m
);
278 static void mainloop_time_restart(pa_time_event
*e
, const struct timeval
*tv
) {
285 pa_mainloop_wakeup(e
->mainloop
);
290 static void mainloop_time_free(pa_time_event
*e
) {
293 e
->dead
= e
->mainloop
->time_events_scan_dead
= 1;
295 /* no wakeup needed here. Think about it! */
298 static void mainloop_time_set_destroy(pa_time_event
*e
, void (*callback
)(pa_mainloop_api
*a
, pa_time_event
*e
, void *userdata
)) {
300 e
->destroy_callback
= callback
;
305 static void mainloop_quit(pa_mainloop_api
*a
, int retval
) {
307 assert(a
&& a
->userdata
);
309 assert(a
== &m
->api
);
311 pa_mainloop_quit(m
, retval
);
314 static const pa_mainloop_api vtable
= {
317 .io_new
= mainloop_io_new
,
318 .io_enable
= mainloop_io_enable
,
319 .io_free
= mainloop_io_free
,
320 .io_set_destroy
= mainloop_io_set_destroy
,
322 .time_new
= mainloop_time_new
,
323 .time_restart
= mainloop_time_restart
,
324 .time_free
= mainloop_time_free
,
325 .time_set_destroy
= mainloop_time_set_destroy
,
327 .defer_new
= mainloop_defer_new
,
328 .defer_enable
= mainloop_defer_enable
,
329 .defer_free
= mainloop_defer_free
,
330 .defer_set_destroy
= mainloop_defer_set_destroy
,
332 .quit
= mainloop_quit
,
335 pa_mainloop
*pa_mainloop_new(void) {
338 m
= pa_xmalloc(sizeof(pa_mainloop
));
340 if (pipe(m
->wakeup_pipe
) < 0) {
341 pa_log_error(__FILE__
": ERROR: cannot create wakeup pipe");
346 pa_make_nonblock_fd(m
->wakeup_pipe
[0]);
347 pa_make_nonblock_fd(m
->wakeup_pipe
[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
= 0;
359 m
->rebuild_pollfds
= 1;
361 m
->quit
= m
->retval
= 0;
366 m
->deferred_pending
= 0;
368 m
->state
= STATE_PASSIVE
;
371 m
->poll_func_userdata
= NULL
;
378 static int io_foreach(void *p
, uint32_t PA_GCC_UNUSED idx
, int *del
, void*userdata
) {
381 assert(e
&& del
&& all
);
383 if (!*all
&& !e
->dead
)
386 if (e
->destroy_callback
)
387 e
->destroy_callback(&e
->mainloop
->api
, e
, e
->userdata
);
393 static int time_foreach(void *p
, uint32_t PA_GCC_UNUSED idx
, int *del
, void*userdata
) {
394 pa_time_event
*e
= p
;
396 assert(e
&& del
&& all
);
398 if (!*all
&& !e
->dead
)
401 if (e
->destroy_callback
)
402 e
->destroy_callback(&e
->mainloop
->api
, e
, e
->userdata
);
408 static int defer_foreach(void *p
, PA_GCC_UNUSED
uint32_t idx
, int *del
, void*userdata
) {
409 pa_defer_event
*e
= p
;
411 assert(e
&& del
&& all
);
413 if (!*all
&& !e
->dead
)
416 if (e
->destroy_callback
)
417 e
->destroy_callback(&e
->mainloop
->api
, e
, e
->userdata
);
423 void pa_mainloop_free(pa_mainloop
* m
) {
427 pa_idxset_foreach(m
->io_events
, io_foreach
, &all
);
428 pa_idxset_foreach(m
->time_events
, time_foreach
, &all
);
429 pa_idxset_foreach(m
->defer_events
, defer_foreach
, &all
);
431 pa_idxset_free(m
->io_events
, NULL
, NULL
);
432 pa_idxset_free(m
->time_events
, NULL
, NULL
);
433 pa_idxset_free(m
->defer_events
, NULL
, NULL
);
435 pa_xfree(m
->pollfds
);
437 if (m
->wakeup_pipe
[0] >= 0)
438 close(m
->wakeup_pipe
[0]);
439 if (m
->wakeup_pipe
[1] >= 0)
440 close(m
->wakeup_pipe
[1]);
445 static void scan_dead(pa_mainloop
*m
) {
449 if (m
->io_events_scan_dead
)
450 pa_idxset_foreach(m
->io_events
, io_foreach
, &all
);
451 if (m
->time_events_scan_dead
)
452 pa_idxset_foreach(m
->time_events
, time_foreach
, &all
);
453 if (m
->defer_events_scan_dead
)
454 pa_idxset_foreach(m
->defer_events
, defer_foreach
, &all
);
456 m
->io_events_scan_dead
= m
->time_events_scan_dead
= m
->defer_events_scan_dead
= 0;
459 static void rebuild_pollfds(pa_mainloop
*m
) {
462 uint32_t idx
= PA_IDXSET_INVALID
;
465 l
= pa_idxset_size(m
->io_events
) + 1;
466 if (m
->max_pollfds
< l
) {
467 m
->pollfds
= pa_xrealloc(m
->pollfds
, sizeof(struct pollfd
)*l
);
474 if (m
->wakeup_pipe
[0] >= 0) {
475 m
->pollfds
[0].fd
= m
->wakeup_pipe
[0];
476 m
->pollfds
[0].events
= POLLIN
;
477 m
->pollfds
[0].revents
= 0;
482 for (e
= pa_idxset_first(m
->io_events
, &idx
); e
; e
= pa_idxset_next(m
->io_events
, &idx
)) {
491 ((e
->events
& PA_IO_EVENT_INPUT
) ? POLLIN
: 0) |
492 ((e
->events
& PA_IO_EVENT_OUTPUT
) ? POLLOUT
: 0) |
501 m
->rebuild_pollfds
= 0;
504 static int dispatch_pollfds(pa_mainloop
*m
) {
505 uint32_t idx
= PA_IDXSET_INVALID
;
509 for (e
= pa_idxset_first(m
->io_events
, &idx
); e
&& !m
->quit
; e
= pa_idxset_next(m
->io_events
, &idx
)) {
510 if (e
->dead
|| !e
->pollfd
|| !e
->pollfd
->revents
)
513 assert(e
->pollfd
->fd
== e
->fd
&& e
->callback
);
514 e
->callback(&m
->api
, e
, e
->fd
,
515 (e
->pollfd
->revents
& POLLHUP
? PA_IO_EVENT_HANGUP
: 0) |
516 (e
->pollfd
->revents
& POLLIN
? PA_IO_EVENT_INPUT
: 0) |
517 (e
->pollfd
->revents
& POLLOUT
? PA_IO_EVENT_OUTPUT
: 0) |
518 (e
->pollfd
->revents
& POLLERR
? PA_IO_EVENT_ERROR
: 0),
520 e
->pollfd
->revents
= 0;
527 static int dispatch_defer(pa_mainloop
*m
) {
532 if (!m
->deferred_pending
)
535 for (e
= pa_idxset_first(m
->defer_events
, &idx
); e
&& !m
->quit
; e
= pa_idxset_next(m
->defer_events
, &idx
)) {
536 if (e
->dead
|| !e
->enabled
)
540 e
->callback(&m
->api
, e
, e
->userdata
);
547 static int calc_next_timeout(pa_mainloop
*m
) {
554 if (pa_idxset_isempty(m
->time_events
))
557 for (e
= pa_idxset_first(m
->time_events
, &idx
); e
; e
= pa_idxset_next(m
->time_events
, &idx
)) {
560 if (e
->dead
|| !e
->enabled
)
563 /* Let's save a system call */
565 pa_gettimeofday(&now
);
569 if (e
->timeval
.tv_sec
< now
.tv_sec
|| (e
->timeval
.tv_sec
== now
.tv_sec
&& e
->timeval
.tv_usec
<= now
.tv_usec
))
572 tmp
= (e
->timeval
.tv_sec
- now
.tv_sec
)*1000;
574 if (e
->timeval
.tv_usec
> now
.tv_usec
)
575 tmp
+= (e
->timeval
.tv_usec
- now
.tv_usec
)/1000;
577 tmp
-= (now
.tv_usec
- e
->timeval
.tv_usec
)/1000;
581 else if (t
== -1 || tmp
< t
)
588 static int dispatch_timeout(pa_mainloop
*m
) {
596 if (pa_idxset_isempty(m
->time_events
))
599 for (e
= pa_idxset_first(m
->time_events
, &idx
); e
&& !m
->quit
; e
= pa_idxset_next(m
->time_events
, &idx
)) {
601 if (e
->dead
|| !e
->enabled
)
604 /* Let's save a system call */
606 pa_gettimeofday(&now
);
610 if (e
->timeval
.tv_sec
< now
.tv_sec
|| (e
->timeval
.tv_sec
== now
.tv_sec
&& e
->timeval
.tv_usec
<= now
.tv_usec
)) {
614 e
->callback(&m
->api
, e
, &e
->timeval
, e
->userdata
);
623 void pa_mainloop_wakeup(pa_mainloop
*m
) {
627 if (m
->wakeup_pipe
[1] >= 0)
628 pa_write(m
->wakeup_pipe
[1], &c
, sizeof(c
));
631 static void clear_wakeup(pa_mainloop
*m
) {
636 if (m
->wakeup_pipe
[0] < 0)
639 while (pa_read(m
->wakeup_pipe
[0], &c
, sizeof(c
)) == sizeof(c
));
642 int pa_mainloop_prepare(pa_mainloop
*m
, int timeout
) {
644 assert(m
->state
== STATE_PASSIVE
);
652 if (!m
->deferred_pending
) {
654 if (m
->rebuild_pollfds
)
657 m
->prepared_timeout
= calc_next_timeout(m
);
658 if (timeout
>= 0 && (timeout
< m
->prepared_timeout
|| m
->prepared_timeout
< 0))
659 m
->prepared_timeout
= timeout
;
662 m
->state
= STATE_PREPARED
;
666 m
->state
= STATE_QUIT
;
670 int pa_mainloop_poll(pa_mainloop
*m
) {
674 assert(m
->state
== STATE_PREPARED
);
679 m
->state
= STATE_POLLING
;
681 if (m
->deferred_pending
)
685 r
= m
->poll_func(m
->pollfds
, m
->n_pollfds
, m
->prepared_timeout
, m
->poll_func_userdata
);
687 r
= poll(m
->pollfds
, m
->n_pollfds
, m
->prepared_timeout
);
693 pa_log(__FILE__
": poll(): %s", pa_cstrerror(errno
));
697 m
->state
= r
< 0 ? STATE_PASSIVE
: STATE_POLLED
;
701 m
->state
= STATE_QUIT
;
705 int pa_mainloop_dispatch(pa_mainloop
*m
) {
709 assert(m
->state
== STATE_POLLED
);
714 if (m
->deferred_pending
)
715 dispatched
+= dispatch_defer(m
);
717 dispatched
+= dispatch_timeout(m
);
722 dispatched
+= dispatch_pollfds(m
);
729 m
->state
= STATE_PASSIVE
;
734 m
->state
= STATE_QUIT
;
738 int pa_mainloop_get_retval(pa_mainloop
*m
) {
743 int pa_mainloop_iterate(pa_mainloop
*m
, int block
, int *retval
) {
747 if ((r
= pa_mainloop_prepare(m
, block
? -1 : 0)) < 0)
750 if ((r
= pa_mainloop_poll(m
)) < 0)
753 if ((r
= pa_mainloop_dispatch(m
)) < 0)
760 if ((r
== -2) && retval
)
761 *retval
= pa_mainloop_get_retval(m
);
765 int pa_mainloop_run(pa_mainloop
*m
, int *retval
) {
768 while ((r
= pa_mainloop_iterate(m
, 1, retval
)) >= 0);
778 void pa_mainloop_quit(pa_mainloop
*m
, int retval
) {
783 pa_mainloop_wakeup(m
);
786 pa_mainloop_api
* pa_mainloop_get_api(pa_mainloop
*m
) {
791 void pa_mainloop_set_poll_func(pa_mainloop
*m
, pa_poll_func poll_func
, void *userdata
) {
794 m
->poll_func
= poll_func
;
795 m
->poll_func_userdata
= userdata
;
800 void pa_mainloop_dump(pa_mainloop
*m
) {
803 pa_log(__FILE__
": Dumping mainloop sources START");
806 uint32_t idx
= PA_IDXSET_INVALID
;
808 for (e
= pa_idxset_first(m
->io_events
, &idx
); e
; e
= pa_idxset_next(m
->io_events
, &idx
)) {
812 pa_log(__FILE__
": kind=io fd=%i events=%i callback=%p userdata=%p", e
->fd
, (int) e
->events
, (void*) e
->callback
, (void*) e
->userdata
);
816 uint32_t idx
= PA_IDXSET_INVALID
;
818 for (e
= pa_idxset_first(m
->defer_events
, &idx
); e
; e
= pa_idxset_next(m
->defer_events
, &idx
)) {
822 pa_log(__FILE__
": kind=defer enabled=%i callback=%p userdata=%p", e
->enabled
, (void*) e
->callback
, (void*) e
->userdata
);
826 uint32_t idx
= PA_IDXSET_INVALID
;
828 for (e
= pa_idxset_first(m
->time_events
, &idx
); e
; e
= pa_idxset_next(m
->time_events
, &idx
)) {
832 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
);
836 pa_log(__FILE__
": Dumping mainloop sources STOP");