timer_t timer;
#ifdef __linux__
pa_bool_t dont_use_ppoll;
-#endif
#endif
-
+#endif
+
PA_LLIST_HEAD(pa_rtpoll_item, items);
};
int (*before_cb)(pa_rtpoll_item *i);
void (*after_cb)(pa_rtpoll_item *i);
void *userdata;
-
+
PA_LLIST_FIELDS(pa_rtpoll_item);
};
#ifdef __linux__
/* ppoll is broken on Linux < 2.6.16 */
p->dont_use_ppoll = FALSE;
-
+
{
struct utsname u;
unsigned major, minor, micro;
-
+
pa_assert_se(uname(&u) == 0);
if (sscanf(u.release, "%u.%u.%u", &major, &minor, µ) != 3 ||
p->rtsig = -1;
sigemptyset(&p->sigset_unblocked);
p->timer = (timer_t) -1;
-
+
#endif
p->n_pollfd_alloc = 32;
p->scan_for_dead = FALSE;
p->rebuild_needed = FALSE;
p->quit = FALSE;
-
+
PA_LLIST_HEAD_INIT(pa_rtpoll_item, p->items);
return p;
void pa_rtpoll_install(pa_rtpoll *p) {
pa_assert(p);
pa_assert(!p->installed);
-
+
p->installed = 1;
#ifdef HAVE_PPOLL
{
sigset_t ss;
struct sigaction sa;
-
+
pa_assert_se(sigemptyset(&ss) == 0);
pa_assert_se(sigaddset(&ss, p->rtsig) == 0);
pa_assert_se(pthread_sigmask(SIG_BLOCK, &ss, &p->sigset_unblocked) == 0);
memset(&sa, 0, sizeof(sa));
sa.sa_handler = signal_handler_noop;
pa_assert_se(sigemptyset(&sa.sa_mask) == 0);
-
+
pa_assert_se(sigaction(p->rtsig, &sa, NULL) == 0);
-
+
/* We never reset the signal handler. Why should we? */
}
-
+
#endif
}
struct pollfd *e, *t;
pa_rtpoll_item *i;
int ra = 0;
-
+
pa_assert(p);
p->rebuild_needed = FALSE;
if (i->n_pollfd > 0) {
size_t l = i->n_pollfd * sizeof(struct pollfd);
-
+
if (i->pollfd)
memcpy(e, i->pollfd, l);
else
i->pollfd = e;
} else
i->pollfd = NULL;
-
+
e += i->n_pollfd;
}
t = p->pollfd;
p->pollfd = p->pollfd2;
p->pollfd2 = t;
-
+
if (ra)
p->pollfd2 = pa_xrealloc(p->pollfd2, p->n_pollfd_alloc * sizeof(struct pollfd));
PA_LLIST_REMOVE(pa_rtpoll_item, p->items, i);
p->n_pollfd_used -= i->n_pollfd;
-
+
if (pa_flist_push(PA_STATIC_FLIST_GET(items), i) < 0)
pa_xfree(i);
pa_xfree(p->pollfd2);
#ifdef HAVE_PPOLL
- if (p->timer != (timer_t) -1)
+ if (p->timer != (timer_t) -1)
timer_delete(p->timer);
#endif
-
+
pa_xfree(p);
}
static void reset_revents(pa_rtpoll_item *i) {
struct pollfd *f;
unsigned n;
-
+
pa_assert(i);
if (!(f = pa_rtpoll_item_get_pollfd(i, &n)))
return;
-
+
for (; n > 0; n--)
f[n-1].revents = 0;
}
pa_rtpoll_item *i;
pa_assert(p);
-
+
for (i = p->items; i; i = i->next) {
-
+
if (i->dead)
continue;
-
+
reset_revents(i);
}
}
pa_rtpoll_item *i;
int r = 0;
struct timespec timeout;
-
+
pa_assert(p);
pa_assert(!p->running);
pa_assert(p->installed);
-
+
p->running = TRUE;
/* First, let's do some work */
for (i = p->items; i && i->priority < PA_RTPOLL_NEVER; i = i->next) {
int k;
-
+
if (i->dead)
continue;
-
+
if (!i->work_cb)
continue;
if (p->quit)
goto finish;
-
+
if ((k = i->work_cb(i)) != 0) {
if (k < 0)
r = k;
-
+
goto finish;
}
}
/* Now let's prepare for entering the sleep */
for (i = p->items; i && i->priority < PA_RTPOLL_NEVER; i = i->next) {
int k = 0;
-
+
if (i->dead)
continue;
-
+
if (!i->before_cb)
continue;
/* Hmm, this one doesn't let us enter the poll, so rewind everything */
for (i = i->prev; i; i = i->prev) {
-
+
if (i->dead)
continue;
-
+
if (!i->after_cb)
continue;
if (k < 0)
r = k;
-
+
goto finish;
}
}
else
pa_timespec_store(&timeout, pa_timespec_diff(&p->next_elapse, &now));
}
-
+
/* OK, now let's sleep */
#ifdef HAVE_PPOLL
else
#endif
-#else
- r = poll(p->pollfd, p->n_pollfd_used, p->timer_enabled > 0 ? (timeout.tv_sec*1000) + (timeout.tv_nsec / 1000000) : -1);
#endif
+ r = poll(p->pollfd, p->n_pollfd_used, p->timer_enabled > 0 ? (timeout.tv_sec*1000) + (timeout.tv_nsec / 1000000) : -1);
if (r < 0) {
- reset_all_revents(p);
-
if (errno == EAGAIN || errno == EINTR)
r = 0;
else
pa_log_error("poll(): %s", pa_cstrerror(errno));
+
+ reset_all_revents(p);
}
if (p->timer_enabled) {
finish:
p->running = FALSE;
-
+
if (p->scan_for_dead) {
pa_rtpoll_item *n;
p->scan_for_dead = FALSE;
-
+
for (i = p->items; i; i = n) {
n = i->next;
#ifdef __linux__
if (!p->dont_use_ppoll) {
#endif
-
+
if (p->timer == (timer_t) -1) {
struct sigevent se;
* "disarming" */
if (its.it_value.tv_sec == 0)
its.it_value.tv_nsec = 1;
-
+
if (p->period > 0)
pa_timespec_store(&its.it_interval, p->period);
}
#ifdef __linux__
}
#endif
-
+
#endif
}
void pa_rtpoll_set_timer_absolute(pa_rtpoll *p, const struct timespec *ts) {
pa_assert(p);
pa_assert(ts);
-
+
p->next_elapse = *ts;
p->period = 0;
p->timer_enabled = TRUE;
-
+
update_timer(p);
}
pa_rtpoll_item *pa_rtpoll_item_new(pa_rtpoll *p, pa_rtpoll_priority_t prio, unsigned n_fds) {
pa_rtpoll_item *i, *j, *l = NULL;
-
+
pa_assert(p);
if (!(i = pa_flist_pop(PA_STATIC_FLIST_GET(items))))
struct pollfd *pa_rtpoll_item_get_pollfd(pa_rtpoll_item *i, unsigned *n_fds) {
pa_assert(i);
- if (i->n_pollfd > 0)
+ if (i->n_pollfd > 0)
if (i->rtpoll->rebuild_needed)
rtpoll_rebuild(i->rtpoll);
-
+
if (n_fds)
*n_fds = i->n_pollfd;
-
+
return i->pollfd;
}
void pa_rtpoll_item_set_before_callback(pa_rtpoll_item *i, int (*before_cb)(pa_rtpoll_item *i)) {
pa_assert(i);
pa_assert(i->priority < PA_RTPOLL_NEVER);
-
+
i->before_cb = before_cb;
}
static void fdsem_after(pa_rtpoll_item *i) {
pa_assert(i);
-
+
pa_assert((i->pollfd[0].revents & ~POLLIN) == 0);
pa_fdsem_after_poll(i->userdata);
}
pa_rtpoll_item *pa_rtpoll_item_new_fdsem(pa_rtpoll *p, pa_rtpoll_priority_t prio, pa_fdsem *f) {
pa_rtpoll_item *i;
struct pollfd *pollfd;
-
+
pa_assert(p);
pa_assert(f);
pollfd->fd = pa_fdsem_get(f);
pollfd->events = POLLIN;
-
+
i->before_cb = fdsem_before;
i->after_cb = fdsem_after;
i->userdata = f;
static int asyncmsgq_before(pa_rtpoll_item *i) {
pa_assert(i);
-
+
if (pa_asyncmsgq_before_poll(i->userdata) < 0)
return 1; /* 1 means immediate restart of the loop */
static void asyncmsgq_after(pa_rtpoll_item *i) {
pa_assert(i);
-
+
pa_assert((i->pollfd[0].revents & ~POLLIN) == 0);
pa_asyncmsgq_after_poll(i->userdata);
}
if (pa_asyncmsgq_get(i->userdata, &object, &code, &data, &offset, &chunk, 0) == 0) {
int ret;
-
+
if (!object && code == PA_MESSAGE_SHUTDOWN) {
pa_asyncmsgq_done(i->userdata, 0);
pa_rtpoll_quit(i->rtpoll);
ret = pa_asyncmsgq_dispatch(object, code, data, offset, &chunk);
pa_asyncmsgq_done(i->userdata, ret);
return 1;
- }
+ }
return 0;
}
pa_rtpoll_item *pa_rtpoll_item_new_asyncmsgq(pa_rtpoll *p, pa_rtpoll_priority_t prio, pa_asyncmsgq *q) {
pa_rtpoll_item *i;
struct pollfd *pollfd;
-
+
pa_assert(p);
pa_assert(q);
pollfd = pa_rtpoll_item_get_pollfd(i, NULL);
pollfd->fd = pa_asyncmsgq_get_fd(q);
pollfd->events = POLLIN;
-
+
i->before_cb = asyncmsgq_before;
i->after_cb = asyncmsgq_after;
i->work_cb = asyncmsgq_work;