]> code.delx.au - pulseaudio/blobdiff - src/pulsecore/rtpoll.c
Merge most of elmarco/rtclock2
[pulseaudio] / src / pulsecore / rtpoll.c
index 0de8d0ce5801f66c44a5962e8ae613cd278d7dac..5cbec3211afb10e45071ea210f81fe41f5e7183c 100644 (file)
@@ -1,5 +1,3 @@
-/* $Id$ */
-
 /***
   This file is part of PulseAudio.
 
 #include <config.h>
 #endif
 
-#include <sys/utsname.h>
 #include <sys/types.h>
 #include <stdio.h>
 #include <signal.h>
 #include <string.h>
 #include <errno.h>
 
+#ifdef HAVE_POLL_H
+#include <poll.h>
+#else
+#include <pulsecore/poll.h>
+#endif
+
 #include <pulse/xmalloc.h>
+#include <pulse/timeval.h>
 
 #include <pulsecore/core-error.h>
-#include <pulsecore/rtclock.h>
+#include <pulsecore/core-rtclock.h>
 #include <pulsecore/macro.h>
 #include <pulsecore/llist.h>
-#include <pulsecore/rtsig.h>
 #include <pulsecore/flist.h>
+#include <pulsecore/core-util.h>
+#include <pulsecore/winsock.h>
+#include <pulsecore/ratelimit.h>
 
 #include "rtpoll.h"
 
+/* #define DEBUG_TIMING */
+
 struct pa_rtpoll {
     struct pollfd *pollfd, *pollfd2;
     unsigned n_pollfd_alloc, n_pollfd_used;
 
-    int timer_enabled;
-    struct timespec next_elapse;
-    pa_usec_t period;
+    struct timeval next_elapse;
+    pa_bool_t timer_enabled:1;
 
-    int scan_for_dead;
-    int running, installed, rebuild_needed, quit;
+    pa_bool_t scan_for_dead:1;
+    pa_bool_t running:1;
+    pa_bool_t rebuild_needed:1;
+    pa_bool_t quit:1;
 
-#ifdef HAVE_PPOLL
-    int rtsig;
-    sigset_t sigset_unblocked;
-    timer_t timer;
-#ifdef __linux__
-    int dont_use_ppoll;
-#endif    
+#ifdef DEBUG_TIMING
+    pa_usec_t timestamp;
+    pa_usec_t slept, awake;
 #endif
-    
+
     PA_LLIST_HEAD(pa_rtpoll_item, items);
 };
 
 struct pa_rtpoll_item {
     pa_rtpoll *rtpoll;
-    int dead;
+    pa_bool_t dead;
 
     pa_rtpoll_priority_t priority;
 
@@ -80,104 +85,38 @@ struct pa_rtpoll_item {
     int (*before_cb)(pa_rtpoll_item *i);
     void (*after_cb)(pa_rtpoll_item *i);
     void *userdata;
-    
+
     PA_LLIST_FIELDS(pa_rtpoll_item);
 };
 
 PA_STATIC_FLIST_DECLARE(items, 0, pa_xfree);
 
-static void signal_handler_noop(int s) { }
-
 pa_rtpoll *pa_rtpoll_new(void) {
     pa_rtpoll *p;
 
     p = pa_xnew(pa_rtpoll, 1);
 
-#ifdef HAVE_PPOLL
-
-#ifdef __linux__
-    /* ppoll is broken on Linux < 2.6.16 */
-    
-    p->dont_use_ppoll = 0;
-
-    {
-        struct utsname u;
-        unsigned major, minor, micro;
-    
-        pa_assert_se(uname(&u) == 0);
-
-        if (sscanf(u.release, "%u.%u.%u", &major, &minor, &micro) != 3 ||
-            (major < 2) ||
-            (major == 2 && minor < 6) ||
-            (major == 2 && minor == 6 && micro < 16))
-
-            p->dont_use_ppoll = 1;
-    }
-
-#endif
-
-    p->rtsig = -1;
-    sigemptyset(&p->sigset_unblocked);
-    p->timer = (timer_t) -1;
-        
-#endif
-
     p->n_pollfd_alloc = 32;
     p->pollfd = pa_xnew(struct pollfd, p->n_pollfd_alloc);
     p->pollfd2 = pa_xnew(struct pollfd, p->n_pollfd_alloc);
     p->n_pollfd_used = 0;
 
-    p->period = 0;
-    memset(&p->next_elapse, 0, sizeof(p->next_elapse));
-    p->timer_enabled = 0;
-
-    p->running = 0;
-    p->installed = 0;
-    p->scan_for_dead = 0;
-    p->rebuild_needed = 0;
-    p->quit = 0;
-    
-    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;
+    pa_zero(p->next_elapse);
+    p->timer_enabled = FALSE;
 
-#ifdef HAVE_PPOLL
-    if (p->dont_use_ppoll)
-        return;
+    p->running = FALSE;
+    p->scan_for_dead = FALSE;
+    p->rebuild_needed = FALSE;
+    p->quit = FALSE;
 
-    if ((p->rtsig = pa_rtsig_get_for_thread()) < 0) {
-        pa_log_warn("Failed to reserve POSIX realtime signal.");
-        return;
-    }
-
-    pa_log_debug("Acquired POSIX realtime signal SIGRTMIN+%i", p->rtsig - SIGRTMIN);
+    PA_LLIST_HEAD_INIT(pa_rtpoll_item, p->items);
 
-    {
-        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);
-        pa_assert_se(sigdelset(&p->sigset_unblocked, p->rtsig) == 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? */
-    }
-    
+#ifdef DEBUG_TIMING
+    p->timestamp = pa_rtclock_now();
+    p->slept = p->awake = 0;
 #endif
+
+    return p;
 }
 
 static void rtpoll_rebuild(pa_rtpoll *p) {
@@ -185,10 +124,10 @@ static void rtpoll_rebuild(pa_rtpoll *p) {
     struct pollfd *e, *t;
     pa_rtpoll_item *i;
     int ra = 0;
-    
+
     pa_assert(p);
 
-    p->rebuild_needed = 0;
+    p->rebuild_needed = FALSE;
 
     if (p->n_pollfd_used > p->n_pollfd_alloc) {
         /* Hmm, we have to allocate some more space */
@@ -203,7 +142,7 @@ static void rtpoll_rebuild(pa_rtpoll *p) {
 
         if (i->n_pollfd > 0)  {
             size_t l = i->n_pollfd * sizeof(struct pollfd);
-            
+
             if (i->pollfd)
                 memcpy(e, i->pollfd, l);
             else
@@ -212,7 +151,7 @@ static void rtpoll_rebuild(pa_rtpoll *p) {
             i->pollfd = e;
         } else
             i->pollfd = NULL;
-        
+
         e += i->n_pollfd;
     }
 
@@ -220,10 +159,9 @@ static void rtpoll_rebuild(pa_rtpoll *p) {
     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));
-
 }
 
 static void rtpoll_item_destroy(pa_rtpoll_item *i) {
@@ -236,11 +174,11 @@ static void rtpoll_item_destroy(pa_rtpoll_item *i) {
     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);
 
-    p->rebuild_needed = 1;
+    p->rebuild_needed = TRUE;
 }
 
 void pa_rtpoll_free(pa_rtpoll *p) {
@@ -252,23 +190,18 @@ void pa_rtpoll_free(pa_rtpoll *p) {
     pa_xfree(p->pollfd);
     pa_xfree(p->pollfd2);
 
-#ifdef HAVE_PPOLL
-    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;
 }
@@ -277,44 +210,43 @@ static void reset_all_revents(pa_rtpoll *p) {
     pa_rtpoll_item *i;
 
     pa_assert(p);
-    
+
     for (i = p->items; i; i = i->next) {
-        
+
         if (i->dead)
             continue;
-        
+
         reset_revents(i);
     }
 }
 
-int pa_rtpoll_run(pa_rtpoll *p, int wait) {
+int pa_rtpoll_run(pa_rtpoll *p, pa_bool_t wait) {
     pa_rtpoll_item *i;
     int r = 0;
-    struct timespec timeout;
-    
+    struct timeval timeout;
+
     pa_assert(p);
     pa_assert(!p->running);
-    pa_assert(p->installed);
-    
-    p->running = 1;
+
+    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->before_cb)
+
+        if (!i->work_cb)
             continue;
 
         if (p->quit)
             goto finish;
-        
+
         if ((k = i->work_cb(i)) != 0) {
             if (k < 0)
                 r = k;
-            
+
             goto finish;
         }
     }
@@ -322,10 +254,10 @@ int pa_rtpoll_run(pa_rtpoll *p, int wait) {
     /* 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;
 
@@ -334,10 +266,10 @@ int pa_rtpoll_run(pa_rtpoll *p, int wait) {
             /* 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;
 
@@ -346,7 +278,7 @@ int pa_rtpoll_run(pa_rtpoll *p, int wait) {
 
             if (k < 0)
                 r = k;
-            
+
             goto finish;
         }
     }
@@ -354,57 +286,56 @@ int pa_rtpoll_run(pa_rtpoll *p, int wait) {
     if (p->rebuild_needed)
         rtpoll_rebuild(p);
 
+    memset(&timeout, 0, sizeof(timeout));
+
     /* Calculate timeout */
-    if (!wait || p->quit) {
-        timeout.tv_sec = 0;
-        timeout.tv_nsec = 0;
-    } else if (p->timer_enabled) {
-        struct timespec now;
+    if (wait && !p->quit && p->timer_enabled) {
+        struct timeval now;
         pa_rtclock_get(&now);
 
-        if (pa_timespec_cmp(&p->next_elapse, &now) <= 0)
-            memset(&timeout, 0, sizeof(timeout));
-        else
-            pa_timespec_store(&timeout, pa_timespec_diff(&p->next_elapse, &now));
+        if (pa_timeval_cmp(&p->next_elapse, &now) > 0)
+            pa_timeval_add(&timeout, pa_timeval_diff(&p->next_elapse, &now));
     }
-    
-    /* OK, now let's sleep */
-#ifdef HAVE_PPOLL
 
-#ifdef __linux__
-    if (!p->dont_use_ppoll)
-#endif
-        r = ppoll(p->pollfd, p->n_pollfd_used, p->timer_enabled > 0  ? &timeout : NULL, p->rtsig < 0 ? NULL : &p->sigset_unblocked);
-#ifdef __linux__
-    else
+#ifdef DEBUG_TIMING
+    {
+        pa_usec_t now = pa_rtclock_now();
+        p->awake = now - p->timestamp;
+        p->timestamp = now;
+    }
 #endif
 
+    /* OK, now let's sleep */
+#ifdef HAVE_PPOLL
+    {
+        struct timespec ts;
+        ts.tv_sec = timeout.tv_sec;
+        ts.tv_nsec = timeout.tv_usec * 1000;
+        r = ppoll(p->pollfd, p->n_pollfd_used, (!wait || p->quit || p->timer_enabled) ? &ts : NULL, NULL);
+    }
 #else
-        r = poll(p->pollfd, p->n_pollfd_used, p->timer_enabled > 0 ? (timeout.tv_sec*1000) + (timeout.tv_nsec / 1000000) : -1);
+        r = poll(p->pollfd, p->n_pollfd_used, (!wait || p->quit || p->timer_enabled) ? (int) ((timeout.tv_sec*1000) + (timeout.tv_usec / 1000)) : -1);
+#endif
+
+#ifdef DEBUG_TIMING
+    {
+        pa_usec_t now = pa_rtclock_now();
+        p->slept = now - p->timestamp;
+        p->timestamp = now;
+
+        pa_log("Process time %llu ms; sleep time %llu ms",
+               (unsigned long long) (p->awake / PA_USEC_PER_MSEC),
+               (unsigned long long) (p->slept / PA_USEC_PER_MSEC));
+    }
 #endif
 
     if (r < 0) {
-        reset_all_revents(p);
-    
         if (errno == EAGAIN || errno == EINTR)
             r = 0;
         else
             pa_log_error("poll(): %s", pa_cstrerror(errno));
-    }
-
-    if (p->timer_enabled) {
-        if (p->period > 0) {
-            struct timespec now;
-            pa_rtclock_get(&now);
 
-            pa_timespec_add(&p->next_elapse, p->period);
-
-            /* Guarantee that the next timeout will happen in the future */
-            if (pa_timespec_cmp(&p->next_elapse, &now) < 0)
-                pa_timespec_add(&p->next_elapse, (pa_timespec_diff(&now, &p->next_elapse) / p->period + 1) * p->period);
-
-        } else
-            p->timer_enabled = 0;
+        reset_all_revents(p);
     }
 
     /* Let's tell everyone that we left the sleep */
@@ -421,13 +352,13 @@ int pa_rtpoll_run(pa_rtpoll *p, int wait) {
 
 finish:
 
-    p->running = 0;
-        
+    p->running = FALSE;
+
     if (p->scan_for_dead) {
         pa_rtpoll_item *n;
 
-        p->scan_for_dead = 0;
-        
+        p->scan_for_dead = FALSE;
+
         for (i = p->items; i; i = n) {
             n = i->next;
 
@@ -439,108 +370,41 @@ finish:
     return r < 0 ? r : !p->quit;
 }
 
-static void update_timer(pa_rtpoll *p) {
+void pa_rtpoll_set_timer_absolute(pa_rtpoll *p, pa_usec_t usec) {
     pa_assert(p);
 
-#ifdef HAVE_PPOLL
-
-#ifdef __linux__
-    if (!p->dont_use_ppoll) {
-#endif
-        
-        if (p->timer == (timer_t) -1) {
-            struct sigevent se;
-
-            memset(&se, 0, sizeof(se));
-            se.sigev_notify = SIGEV_SIGNAL;
-            se.sigev_signo = p->rtsig;
-
-            if (timer_create(CLOCK_MONOTONIC, &se, &p->timer) < 0)
-                if (timer_create(CLOCK_REALTIME, &se, &p->timer) < 0) {
-                    pa_log_warn("Failed to allocate POSIX timer: %s", pa_cstrerror(errno));
-                    p->timer = (timer_t) -1;
-                }
-        }
-
-        if (p->timer != (timer_t) -1) {
-            struct itimerspec its;
-            memset(&its, 0, sizeof(its));
-
-            if (p->timer_enabled) {
-                its.it_value = p->next_elapse;
-
-                /* Make sure that 0,0 is not understood as
-                 * "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);
-            }
-
-            pa_assert_se(timer_settime(p->timer, TIMER_ABSTIME, &its, NULL) == 0);
-        }
-
-#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 = 1;
-    
-    update_timer(p);
-}
-
-void pa_rtpoll_set_timer_periodic(pa_rtpoll *p, pa_usec_t usec) {
-    pa_assert(p);
-
-    p->period = usec;
-    pa_rtclock_get(&p->next_elapse);
-    pa_timespec_add(&p->next_elapse, usec);
-    p->timer_enabled = 1;
-
-    update_timer(p);
+    pa_timeval_store(&p->next_elapse, usec);
+    p->timer_enabled = TRUE;
 }
 
 void pa_rtpoll_set_timer_relative(pa_rtpoll *p, pa_usec_t usec) {
     pa_assert(p);
 
-    p->period = 0;
-    pa_rtclock_get(&p->next_elapse);
-    pa_timespec_add(&p->next_elapse, usec);
-    p->timer_enabled = 1;
+    /* Scheduling a timeout for more than an hour is very very suspicious */
+    pa_assert(usec <= PA_USEC_PER_SEC*60ULL*60ULL);
 
-    update_timer(p);
+    pa_rtclock_get(&p->next_elapse);
+    pa_timeval_add(&p->next_elapse, usec);
+    p->timer_enabled = TRUE;
 }
 
 void pa_rtpoll_set_timer_disabled(pa_rtpoll *p) {
     pa_assert(p);
 
-    p->period = 0;
     memset(&p->next_elapse, 0, sizeof(p->next_elapse));
-    p->timer_enabled = 0;
-
-    update_timer(p);
+    p->timer_enabled = FALSE;
 }
 
 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))))
         i = pa_xnew(pa_rtpoll_item, 1);
 
     i->rtpoll = p;
-    i->dead = 0;
+    i->dead = FALSE;
     i->n_pollfd = n_fds;
     i->pollfd = NULL;
     i->priority = prio;
@@ -571,8 +435,8 @@ void pa_rtpoll_item_free(pa_rtpoll_item *i) {
     pa_assert(i);
 
     if (i->rtpoll->running) {
-        i->dead = 1;
-        i->rtpoll->scan_for_dead = 1;
+        i->dead = TRUE;
+        i->rtpoll->scan_for_dead = TRUE;
         return;
     }
 
@@ -582,20 +446,20 @@ void pa_rtpoll_item_free(pa_rtpoll_item *i) {
 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;
 }
 
@@ -635,7 +499,7 @@ static int fdsem_before(pa_rtpoll_item *i) {
 
 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);
 }
@@ -643,7 +507,7 @@ static void fdsem_after(pa_rtpoll_item *i) {
 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);
 
@@ -653,7 +517,7 @@ pa_rtpoll_item *pa_rtpoll_item_new_fdsem(pa_rtpoll *p, pa_rtpoll_priority_t prio
 
     pollfd->fd = pa_fdsem_get(f);
     pollfd->events = POLLIN;
-    
+
     i->before_cb = fdsem_before;
     i->after_cb = fdsem_after;
     i->userdata = f;
@@ -661,23 +525,23 @@ pa_rtpoll_item *pa_rtpoll_item_new_fdsem(pa_rtpoll *p, pa_rtpoll_priority_t prio
     return i;
 }
 
-static int asyncmsgq_before(pa_rtpoll_item *i) {
+static int asyncmsgq_read_before(pa_rtpoll_item *i) {
     pa_assert(i);
-    
-    if (pa_asyncmsgq_before_poll(i->userdata) < 0)
+
+    if (pa_asyncmsgq_read_before_poll(i->userdata) < 0)
         return 1; /* 1 means immediate restart of the loop */
 
     return 0;
 }
 
-static void asyncmsgq_after(pa_rtpoll_item *i) {
+static void asyncmsgq_read_after(pa_rtpoll_item *i) {
     pa_assert(i);
-    
+
     pa_assert((i->pollfd[0].revents & ~POLLIN) == 0);
-    pa_asyncmsgq_after_poll(i->userdata);
+    pa_asyncmsgq_read_after_poll(i->userdata);
 }
 
-static int asyncmsgq_work(pa_rtpoll_item *i) {
+static int asyncmsgq_read_work(pa_rtpoll_item *i) {
     pa_msgobject *object;
     int code;
     void *data;
@@ -688,7 +552,7 @@ static int asyncmsgq_work(pa_rtpoll_item *i) {
 
     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);
@@ -698,27 +562,62 @@ static int asyncmsgq_work(pa_rtpoll_item *i) {
         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_read(pa_rtpoll *p, pa_rtpoll_priority_t prio, pa_asyncmsgq *q) {
+    pa_rtpoll_item *i;
+    struct pollfd *pollfd;
+
+    pa_assert(p);
+    pa_assert(q);
+
+    i = pa_rtpoll_item_new(p, prio, 1);
+
+    pollfd = pa_rtpoll_item_get_pollfd(i, NULL);
+    pollfd->fd = pa_asyncmsgq_read_fd(q);
+    pollfd->events = POLLIN;
+
+    i->before_cb = asyncmsgq_read_before;
+    i->after_cb = asyncmsgq_read_after;
+    i->work_cb = asyncmsgq_read_work;
+    i->userdata = q;
 
+    return i;
+}
+
+static int asyncmsgq_write_before(pa_rtpoll_item *i) {
+    pa_assert(i);
+
+    pa_asyncmsgq_write_before_poll(i->userdata);
     return 0;
 }
 
-pa_rtpoll_item *pa_rtpoll_item_new_asyncmsgq(pa_rtpoll *p, pa_rtpoll_priority_t prio, pa_asyncmsgq *q) {
+static void asyncmsgq_write_after(pa_rtpoll_item *i) {
+    pa_assert(i);
+
+    pa_assert((i->pollfd[0].revents & ~POLLIN) == 0);
+    pa_asyncmsgq_write_after_poll(i->userdata);
+}
+
+pa_rtpoll_item *pa_rtpoll_item_new_asyncmsgq_write(pa_rtpoll *p, pa_rtpoll_priority_t prio, pa_asyncmsgq *q) {
     pa_rtpoll_item *i;
     struct pollfd *pollfd;
-    
+
     pa_assert(p);
     pa_assert(q);
 
     i = pa_rtpoll_item_new(p, prio, 1);
 
     pollfd = pa_rtpoll_item_get_pollfd(i, NULL);
-    pollfd->fd = pa_asyncmsgq_get_fd(q);
+    pollfd->fd = pa_asyncmsgq_write_fd(q);
     pollfd->events = POLLIN;
-    
-    i->before_cb = asyncmsgq_before;
-    i->after_cb = asyncmsgq_after;
-    i->work_cb = asyncmsgq_work;
+
+    i->before_cb = asyncmsgq_write_before;
+    i->after_cb = asyncmsgq_write_after;
+    i->work_cb = NULL;
     i->userdata = q;
 
     return i;
@@ -727,5 +626,5 @@ pa_rtpoll_item *pa_rtpoll_item_new_asyncmsgq(pa_rtpoll *p, pa_rtpoll_priority_t
 void pa_rtpoll_quit(pa_rtpoll *p) {
     pa_assert(p);
 
-    p->quit = 1;
+    p->quit = TRUE;
 }