]> code.delx.au - pulseaudio/blobdiff - src/pulsecore/rtpoll.c
remap: Change remapping function argument type from void to int16_t / float as approp...
[pulseaudio] / src / pulsecore / rtpoll.c
index 5cbec3211afb10e45071ea210f81fe41f5e7183c..09e5e8a343147e727564c24ce8f1a6d5a5428d08 100644 (file)
 
 #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/poll.h>
 #include <pulsecore/core-error.h>
 #include <pulsecore/core-rtclock.h>
 #include <pulsecore/macro.h>
 #include <pulsecore/llist.h>
 #include <pulsecore/flist.h>
 #include <pulsecore/core-util.h>
-#include <pulsecore/winsock.h>
 #include <pulsecore/ratelimit.h>
+#include <pulse/rtclock.h>
 
 #include "rtpoll.h"
 
@@ -57,12 +51,13 @@ struct pa_rtpoll {
     unsigned n_pollfd_alloc, n_pollfd_used;
 
     struct timeval next_elapse;
-    pa_bool_t timer_enabled:1;
+    bool timer_enabled:1;
 
-    pa_bool_t scan_for_dead:1;
-    pa_bool_t running:1;
-    pa_bool_t rebuild_needed:1;
-    pa_bool_t quit:1;
+    bool scan_for_dead:1;
+    bool running:1;
+    bool rebuild_needed:1;
+    bool quit:1;
+    bool timer_elapsed:1;
 
 #ifdef DEBUG_TIMING
     pa_usec_t timestamp;
@@ -74,7 +69,7 @@ struct pa_rtpoll {
 
 struct pa_rtpoll_item {
     pa_rtpoll *rtpoll;
-    pa_bool_t dead;
+    bool dead;
 
     pa_rtpoll_priority_t priority;
 
@@ -94,26 +89,14 @@ PA_STATIC_FLIST_DECLARE(items, 0, pa_xfree);
 pa_rtpoll *pa_rtpoll_new(void) {
     pa_rtpoll *p;
 
-    p = pa_xnew(pa_rtpoll, 1);
+    p = pa_xnew0(pa_rtpoll, 1);
 
     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;
-
-    pa_zero(p->next_elapse);
-    p->timer_enabled = FALSE;
-
-    p->running = FALSE;
-    p->scan_for_dead = FALSE;
-    p->rebuild_needed = FALSE;
-    p->quit = FALSE;
-
-    PA_LLIST_HEAD_INIT(pa_rtpoll_item, p->items);
 
 #ifdef DEBUG_TIMING
     p->timestamp = pa_rtclock_now();
-    p->slept = p->awake = 0;
 #endif
 
     return p;
@@ -127,7 +110,7 @@ static void rtpoll_rebuild(pa_rtpoll *p) {
 
     pa_assert(p);
 
-    p->rebuild_needed = FALSE;
+    p->rebuild_needed = false;
 
     if (p->n_pollfd_used > p->n_pollfd_alloc) {
         /* Hmm, we have to allocate some more space */
@@ -140,7 +123,7 @@ static void rtpoll_rebuild(pa_rtpoll *p) {
 
     for (i = p->items; i; i = i->next) {
 
-        if (i->n_pollfd > 0)  {
+        if (i->n_pollfd > 0) {
             size_t l = i->n_pollfd * sizeof(struct pollfd);
 
             if (i->pollfd)
@@ -178,7 +161,7 @@ static void rtpoll_item_destroy(pa_rtpoll_item *i) {
     if (pa_flist_push(PA_STATIC_FLIST_GET(items), i) < 0)
         pa_xfree(i);
 
-    p->rebuild_needed = TRUE;
+    p->rebuild_needed = true;
 }
 
 void pa_rtpoll_free(pa_rtpoll *p) {
@@ -220,7 +203,7 @@ static void reset_all_revents(pa_rtpoll *p) {
     }
 }
 
-int pa_rtpoll_run(pa_rtpoll *p, pa_bool_t wait) {
+int pa_rtpoll_run(pa_rtpoll *p, bool wait_op) {
     pa_rtpoll_item *i;
     int r = 0;
     struct timeval timeout;
@@ -228,7 +211,12 @@ int pa_rtpoll_run(pa_rtpoll *p, pa_bool_t wait) {
     pa_assert(p);
     pa_assert(!p->running);
 
-    p->running = TRUE;
+#ifdef DEBUG_TIMING
+    pa_log("rtpoll_run");
+#endif
+
+    p->running = true;
+    p->timer_elapsed = false;
 
     /* First, let's do some work */
     for (i = p->items; i && i->priority < PA_RTPOLL_NEVER; i = i->next) {
@@ -240,13 +228,19 @@ int pa_rtpoll_run(pa_rtpoll *p, pa_bool_t wait) {
         if (!i->work_cb)
             continue;
 
-        if (p->quit)
+        if (p->quit) {
+#ifdef DEBUG_TIMING
+            pa_log("rtpoll finish");
+#endif
             goto finish;
+        }
 
         if ((k = i->work_cb(i)) != 0) {
             if (k < 0)
                 r = k;
-
+#ifdef DEBUG_TIMING
+            pa_log("rtpoll finish");
+#endif
             goto finish;
         }
     }
@@ -278,7 +272,9 @@ int pa_rtpoll_run(pa_rtpoll *p, pa_bool_t wait) {
 
             if (k < 0)
                 r = k;
-
+#ifdef DEBUG_TIMING
+            pa_log("rtpoll finish");
+#endif
             goto finish;
         }
     }
@@ -286,10 +282,10 @@ int pa_rtpoll_run(pa_rtpoll *p, pa_bool_t wait) {
     if (p->rebuild_needed)
         rtpoll_rebuild(p);
 
-    memset(&timeout, 0, sizeof(timeout));
+    pa_zero(timeout);
 
     /* Calculate timeout */
-    if (wait && !p->quit && p->timer_enabled) {
+    if (wait_op && !p->quit && p->timer_enabled) {
         struct timeval now;
         pa_rtclock_get(&now);
 
@@ -302,6 +298,10 @@ int pa_rtpoll_run(pa_rtpoll *p, pa_bool_t wait) {
         pa_usec_t now = pa_rtclock_now();
         p->awake = now - p->timestamp;
         p->timestamp = now;
+        if (!wait_op || p->quit || p->timer_enabled)
+            pa_log("poll timeout: %d ms ",(int) ((timeout.tv_sec*1000) + (timeout.tv_usec / 1000)));
+        else
+            pa_log("poll timeout is ZERO");
     }
 #endif
 
@@ -311,12 +311,14 @@ int pa_rtpoll_run(pa_rtpoll *p, pa_bool_t wait) {
         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);
+        r = ppoll(p->pollfd, p->n_pollfd_used, (!wait_op || p->quit || p->timer_enabled) ? &ts : NULL, NULL);
     }
 #else
-        r = poll(p->pollfd, p->n_pollfd_used, (!wait || p->quit || p->timer_enabled) ? (int) ((timeout.tv_sec*1000) + (timeout.tv_usec / 1000)) : -1);
+    r = pa_poll(p->pollfd, p->n_pollfd_used, (!wait_op || p->quit || p->timer_enabled) ? (int) ((timeout.tv_sec*1000) + (timeout.tv_usec / 1000)) : -1);
 #endif
 
+    p->timer_elapsed = r == 0;
+
 #ifdef DEBUG_TIMING
     {
         pa_usec_t now = pa_rtclock_now();
@@ -352,12 +354,12 @@ int pa_rtpoll_run(pa_rtpoll *p, pa_bool_t wait) {
 
 finish:
 
-    p->running = FALSE;
+    p->running = false;
 
     if (p->scan_for_dead) {
         pa_rtpoll_item *n;
 
-        p->scan_for_dead = FALSE;
+        p->scan_for_dead = false;
 
         for (i = p->items; i; i = n) {
             n = i->next;
@@ -374,7 +376,7 @@ void pa_rtpoll_set_timer_absolute(pa_rtpoll *p, pa_usec_t usec) {
     pa_assert(p);
 
     pa_timeval_store(&p->next_elapse, usec);
-    p->timer_enabled = TRUE;
+    p->timer_enabled = true;
 }
 
 void pa_rtpoll_set_timer_relative(pa_rtpoll *p, pa_usec_t usec) {
@@ -385,14 +387,14 @@ void pa_rtpoll_set_timer_relative(pa_rtpoll *p, pa_usec_t usec) {
 
     pa_rtclock_get(&p->next_elapse);
     pa_timeval_add(&p->next_elapse, usec);
-    p->timer_enabled = TRUE;
+    p->timer_enabled = true;
 }
 
 void pa_rtpoll_set_timer_disabled(pa_rtpoll *p) {
     pa_assert(p);
 
     memset(&p->next_elapse, 0, sizeof(p->next_elapse));
-    p->timer_enabled = FALSE;
+    p->timer_enabled = false;
 }
 
 pa_rtpoll_item *pa_rtpoll_item_new(pa_rtpoll *p, pa_rtpoll_priority_t prio, unsigned n_fds) {
@@ -404,7 +406,7 @@ pa_rtpoll_item *pa_rtpoll_item_new(pa_rtpoll *p, pa_rtpoll_priority_t prio, unsi
         i = pa_xnew(pa_rtpoll_item, 1);
 
     i->rtpoll = p;
-    i->dead = FALSE;
+    i->dead = false;
     i->n_pollfd = n_fds;
     i->pollfd = NULL;
     i->priority = prio;
@@ -435,8 +437,8 @@ void pa_rtpoll_item_free(pa_rtpoll_item *i) {
     pa_assert(i);
 
     if (i->rtpoll->running) {
-        i->dead = TRUE;
-        i->rtpoll->scan_for_dead = TRUE;
+        i->dead = true;
+        i->rtpoll->scan_for_dead = true;
         return;
     }
 
@@ -626,5 +628,11 @@ pa_rtpoll_item *pa_rtpoll_item_new_asyncmsgq_write(pa_rtpoll *p, pa_rtpoll_prior
 void pa_rtpoll_quit(pa_rtpoll *p) {
     pa_assert(p);
 
-    p->quit = TRUE;
+    p->quit = true;
+}
+
+bool pa_rtpoll_timer_elapsed(pa_rtpoll *p) {
+    pa_assert(p);
+
+    return p->timer_elapsed;
 }