4 This file is part of PulseAudio.
6 Copyright 2004-2006 Lennart Poettering
7 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
9 PulseAudio is free software; you can redistribute it and/or modify
10 it under the terms of the GNU Lesser General Public License as
11 published by the Free Software Foundation; either version 2.1 of the
12 License, or (at your option) any later version.
14 PulseAudio is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 Lesser General Public License for more details.
19 You should have received a copy of the GNU Lesser General Public
20 License along with PulseAudio; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
29 #include <sys/utsname.h>
30 #include <sys/types.h>
36 #include <pulse/xmalloc.h>
38 #include <pulsecore/core-error.h>
39 #include <pulsecore/rtclock.h>
40 #include <pulsecore/macro.h>
41 #include <pulsecore/llist.h>
42 #include <pulsecore/rtsig.h>
43 #include <pulsecore/flist.h>
49 struct pollfd
*pollfd
, *pollfd2
;
50 unsigned n_pollfd_alloc
, n_pollfd_used
;
53 struct timespec next_elapse
;
57 int running
, installed
, rebuild_needed
;
61 sigset_t sigset_unblocked
;
68 PA_LLIST_HEAD(pa_rtpoll_item
, items
);
71 struct pa_rtpoll_item
{
75 struct pollfd
*pollfd
;
78 int (*before_cb
)(pa_rtpoll_item
*i
);
79 void (*after_cb
)(pa_rtpoll_item
*i
);
82 PA_LLIST_FIELDS(pa_rtpoll_item
);
85 PA_STATIC_FLIST_DECLARE(items
, 0, pa_xfree
);
87 static void signal_handler_noop(int s
) { }
89 pa_rtpoll
*pa_rtpoll_new(void) {
92 p
= pa_xnew(pa_rtpoll
, 1);
97 /* ppoll is broken on Linux < 2.6.16 */
99 p
->dont_use_ppoll
= 0;
103 unsigned major
, minor
, micro
;
105 pa_assert_se(uname(&u
) == 0);
107 if (sscanf(u
.release
, "%u.%u.%u", &major
, &minor
, µ
) != 3 ||
109 (major
== 2 && minor
< 6) ||
110 (major
== 2 && minor
== 6 && micro
< 16))
112 p
->dont_use_ppoll
= 1;
118 sigemptyset(&p
->sigset_unblocked
);
119 p
->timer
= (timer_t
) -1;
123 p
->n_pollfd_alloc
= 32;
124 p
->pollfd
= pa_xnew(struct pollfd
, p
->n_pollfd_alloc
);
125 p
->pollfd2
= pa_xnew(struct pollfd
, p
->n_pollfd_alloc
);
126 p
->n_pollfd_used
= 0;
129 memset(&p
->next_elapse
, 0, sizeof(p
->next_elapse
));
130 p
->timer_enabled
= 0;
134 p
->scan_for_dead
= 0;
135 p
->rebuild_needed
= 0;
137 PA_LLIST_HEAD_INIT(pa_rtpoll_item
, p
->items
);
142 void pa_rtpoll_install(pa_rtpoll
*p
) {
144 pa_assert(!p
->installed
);
149 if (p
->dont_use_ppoll
)
152 if ((p
->rtsig
= pa_rtsig_get_for_thread()) < 0) {
153 pa_log_warn("Failed to reserve POSIX realtime signal.");
157 pa_log_debug("Acquired POSIX realtime signal SIGRTMIN+%i", p
->rtsig
- SIGRTMIN
);
163 pa_assert_se(sigemptyset(&ss
) == 0);
164 pa_assert_se(sigaddset(&ss
, p
->rtsig
) == 0);
165 pa_assert_se(pthread_sigmask(SIG_BLOCK
, &ss
, &p
->sigset_unblocked
) == 0);
166 pa_assert_se(sigdelset(&p
->sigset_unblocked
, p
->rtsig
) == 0);
168 memset(&sa
, 0, sizeof(sa
));
169 sa
.sa_handler
= signal_handler_noop
;
170 pa_assert_se(sigemptyset(&sa
.sa_mask
) == 0);
172 pa_assert_se(sigaction(p
->rtsig
, &sa
, NULL
) == 0);
174 /* We never reset the signal handler. Why should we? */
180 static void rtpoll_rebuild(pa_rtpoll
*p
) {
182 struct pollfd
*e
, *t
;
188 p
->rebuild_needed
= 0;
190 if (p
->n_pollfd_used
> p
->n_pollfd_alloc
) {
191 /* Hmm, we have to allocate some more space */
192 p
->n_pollfd_alloc
= p
->n_pollfd_used
* 2;
193 p
->pollfd2
= pa_xrealloc(p
->pollfd2
, p
->n_pollfd_alloc
* sizeof(struct pollfd
));
199 for (i
= p
->items
; i
; i
= i
->next
) {
201 if (i
->n_pollfd
> 0) {
202 size_t l
= i
->n_pollfd
* sizeof(struct pollfd
);
205 memcpy(e
, i
->pollfd
, l
);
216 pa_assert((unsigned) (e
- p
->pollfd2
) == p
->n_pollfd_used
);
218 p
->pollfd
= p
->pollfd2
;
222 p
->pollfd2
= pa_xrealloc(p
->pollfd2
, p
->n_pollfd_alloc
* sizeof(struct pollfd
));
226 static void rtpoll_item_destroy(pa_rtpoll_item
*i
) {
233 PA_LLIST_REMOVE(pa_rtpoll_item
, p
->items
, i
);
235 p
->n_pollfd_used
-= i
->n_pollfd
;
237 if (pa_flist_push(PA_STATIC_FLIST_GET(items
), i
) < 0)
240 p
->rebuild_needed
= 1;
243 void pa_rtpoll_free(pa_rtpoll
*p
) {
247 rtpoll_item_destroy(p
->items
);
250 pa_xfree(p
->pollfd2
);
253 if (p
->timer
!= (timer_t
) -1)
254 timer_delete(p
->timer
);
260 int pa_rtpoll_run(pa_rtpoll
*p
) {
265 struct timespec timeout
;
268 pa_assert(!p
->running
);
269 pa_assert(p
->installed
);
273 for (i
= p
->items
; i
; i
= i
->next
) {
281 if (i
->before_cb(i
) < 0) {
283 /* Hmm, this one doesn't let us enter the poll, so rewind everything */
285 for (i
= i
->prev
; i
; i
= i
->prev
) {
300 if (p
->rebuild_needed
)
303 /* Calculate timeout */
304 if (p
->timer_enabled
) {
306 pa_rtclock_get(&now
);
308 if (pa_timespec_cmp(&p
->next_elapse
, &now
) <= 0)
309 memset(&timeout
, 0, sizeof(timeout
));
311 pa_timespec_store(&timeout
, pa_timespec_diff(&p
->next_elapse
, &now
));
314 /* OK, now let's sleep */
318 if (!p
->dont_use_ppoll
)
320 r
= ppoll(p
->pollfd
, p
->n_pollfd_used
, p
->timer_enabled
> 0 ? &timeout
: NULL
, p
->rtsig
< 0 ? NULL
: &p
->sigset_unblocked
);
326 r
= poll(p
->pollfd
, p
->n_pollfd_used
, p
->timer_enabled
> 0 ? (timeout
.tv_sec
*1000) + (timeout
.tv_nsec
/ 1000000) : -1);
331 if (p
->timer_enabled
) {
334 pa_rtclock_get(&now
);
336 pa_timespec_add(&p
->next_elapse
, p
->period
);
338 /* Guarantee that the next timeout will happen in the future */
339 if (pa_timespec_cmp(&p
->next_elapse
, &now
) < 0)
340 pa_timespec_add(&p
->next_elapse
, (pa_timespec_diff(&now
, &p
->next_elapse
) / p
->period
+ 1) * p
->period
);
343 p
->timer_enabled
= 0;
346 if (r
== 0 || (r
< 0 && (errno
== EAGAIN
|| errno
== EINTR
))) {
351 for (i
= p
->items
; i
; i
= i
->next
) {
362 for (j
= 0; j
< i
->n_pollfd
; j
++)
363 i
->pollfd
[j
].revents
= 0;
373 if (p
->scan_for_dead
) {
376 p
->scan_for_dead
= 0;
378 for (i
= p
->items
; i
; i
= n
) {
382 rtpoll_item_destroy(i
);
391 static void update_timer(pa_rtpoll
*p
) {
397 if (!p
->dont_use_ppoll
) {
400 if (p
->timer
== (timer_t
) -1) {
403 memset(&se
, 0, sizeof(se
));
404 se
.sigev_notify
= SIGEV_SIGNAL
;
405 se
.sigev_signo
= p
->rtsig
;
407 if (timer_create(CLOCK_MONOTONIC
, &se
, &p
->timer
) < 0)
408 if (timer_create(CLOCK_REALTIME
, &se
, &p
->timer
) < 0) {
409 pa_log_warn("Failed to allocate POSIX timer: %s", pa_cstrerror(errno
));
410 p
->timer
= (timer_t
) -1;
414 if (p
->timer
!= (timer_t
) -1) {
415 struct itimerspec its
;
416 memset(&its
, 0, sizeof(its
));
418 if (p
->timer_enabled
) {
419 its
.it_value
= p
->next_elapse
;
421 /* Make sure that 0,0 is not understood as
423 if (its
.it_value
.tv_sec
== 0)
424 its
.it_value
.tv_nsec
= 1;
427 pa_timespec_store(&its
.it_interval
, p
->period
);
430 pa_assert_se(timer_settime(p
->timer
, TIMER_ABSTIME
, &its
, NULL
) == 0);
440 void pa_rtpoll_set_timer_absolute(pa_rtpoll
*p
, const struct timespec
*ts
) {
444 p
->next_elapse
= *ts
;
446 p
->timer_enabled
= 1;
451 void pa_rtpoll_set_timer_periodic(pa_rtpoll
*p
, pa_usec_t usec
) {
455 pa_rtclock_get(&p
->next_elapse
);
456 pa_timespec_add(&p
->next_elapse
, usec
);
457 p
->timer_enabled
= 1;
462 void pa_rtpoll_set_timer_relative(pa_rtpoll
*p
, pa_usec_t usec
) {
466 pa_rtclock_get(&p
->next_elapse
);
467 pa_timespec_add(&p
->next_elapse
, usec
);
468 p
->timer_enabled
= 1;
473 void pa_rtpoll_set_timer_disabled(pa_rtpoll
*p
) {
477 memset(&p
->next_elapse
, 0, sizeof(p
->next_elapse
));
478 p
->timer_enabled
= 0;
483 pa_rtpoll_item
*pa_rtpoll_item_new(pa_rtpoll
*p
, unsigned n_fds
) {
487 pa_assert(n_fds
> 0);
489 if (!(i
= pa_flist_pop(PA_STATIC_FLIST_GET(items
))))
490 i
= pa_xnew(pa_rtpoll_item
, 1);
501 PA_LLIST_PREPEND(pa_rtpoll_item
, p
->items
, i
);
503 p
->rebuild_needed
= 1;
504 p
->n_pollfd_used
+= n_fds
;
509 void pa_rtpoll_item_free(pa_rtpoll_item
*i
) {
512 if (i
->rtpoll
->running
) {
514 i
->rtpoll
->scan_for_dead
= 1;
518 rtpoll_item_destroy(i
);
521 struct pollfd
*pa_rtpoll_item_get_pollfd(pa_rtpoll_item
*i
, unsigned *n_fds
) {
524 if (i
->rtpoll
->rebuild_needed
)
525 rtpoll_rebuild(i
->rtpoll
);
528 *n_fds
= i
->n_pollfd
;
533 void pa_rtpoll_item_set_before_callback(pa_rtpoll_item
*i
, int (*before_cb
)(pa_rtpoll_item
*i
)) {
536 i
->before_cb
= before_cb
;
539 void pa_rtpoll_item_set_after_callback(pa_rtpoll_item
*i
, void (*after_cb
)(pa_rtpoll_item
*i
)) {
542 i
->after_cb
= after_cb
;
545 void pa_rtpoll_item_set_userdata(pa_rtpoll_item
*i
, void *userdata
) {
548 i
->userdata
= userdata
;
551 void* pa_rtpoll_item_get_userdata(pa_rtpoll_item
*i
) {
557 static int fdsem_before(pa_rtpoll_item
*i
) {
558 return pa_fdsem_before_poll(i
->userdata
);
561 static void fdsem_after(pa_rtpoll_item
*i
) {
562 pa_assert((i
->pollfd
[0].revents
& ~POLLIN
) == 0);
563 pa_fdsem_after_poll(i
->userdata
);
566 pa_rtpoll_item
*pa_rtpoll_item_new_fdsem(pa_rtpoll
*p
, pa_fdsem
*f
) {
568 struct pollfd
*pollfd
;
573 i
= pa_rtpoll_item_new(p
, 1);
575 pollfd
= pa_rtpoll_item_get_pollfd(i
, NULL
);
577 pollfd
->fd
= pa_fdsem_get(f
);
578 pollfd
->events
= POLLIN
;
580 i
->before_cb
= fdsem_before
;
581 i
->after_cb
= fdsem_after
;
587 static int asyncmsgq_before(pa_rtpoll_item
*i
) {
588 return pa_asyncmsgq_before_poll(i
->userdata
);
591 static void asyncmsgq_after(pa_rtpoll_item
*i
) {
592 pa_assert((i
->pollfd
[0].revents
& ~POLLIN
) == 0);
593 pa_asyncmsgq_after_poll(i
->userdata
);
596 pa_rtpoll_item
*pa_rtpoll_item_new_asyncmsgq(pa_rtpoll
*p
, pa_asyncmsgq
*q
) {
598 struct pollfd
*pollfd
;
603 i
= pa_rtpoll_item_new(p
, 1);
605 pollfd
= pa_rtpoll_item_get_pollfd(i
, NULL
);
606 pollfd
->fd
= pa_asyncmsgq_get_fd(q
);
607 pollfd
->events
= POLLIN
;
609 i
->before_cb
= asyncmsgq_before
;
610 i
->after_cb
= asyncmsgq_after
;