[pulseaudio-commits] r1703 - in /branches/lennart/src: pulsecore/rtpoll.c pulsecore/rtpoll.h tests/rtpoll-test.c

svnmailer-noreply at 0pointer.de svnmailer-noreply at 0pointer.de
Wed Aug 22 15:26:31 PDT 2007


Author: lennart
Date: Thu Aug 23 00:26:30 2007
New Revision: 1703

URL: http://0pointer.de/cgi-bin/viewcvs.cgi?rev=3D1703&root=3Dpulseaudio&vi=
ew=3Drev
Log:
support absolute, relative and periodic timers in pa_rtpoll

Modified:
    branches/lennart/src/pulsecore/rtpoll.c
    branches/lennart/src/pulsecore/rtpoll.h
    branches/lennart/src/tests/rtpoll-test.c

Modified: branches/lennart/src/pulsecore/rtpoll.c
URL: http://0pointer.de/cgi-bin/viewcvs.cgi/branches/lennart/src/pulsecore/=
rtpoll.c?rev=3D1703&root=3Dpulseaudio&r1=3D1702&r2=3D1703&view=3Ddiff
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D
--- branches/lennart/src/pulsecore/rtpoll.c (original)
+++ branches/lennart/src/pulsecore/rtpoll.c Thu Aug 23 00:26:30 2007
@@ -49,7 +49,9 @@
     struct pollfd *pollfd, *pollfd2;
     unsigned n_pollfd_alloc, n_pollfd_used;
 =

-    pa_usec_t interval;
+    int timer_enabled;
+    struct timespec next_elapse;
+    pa_usec_t period;
 =

     int scan_for_dead;
     int running, installed, rebuild_needed;
@@ -57,7 +59,6 @@
 #ifdef HAVE_PPOLL
     int rtsig;
     sigset_t sigset_unblocked;
-    struct timespec interval_timespec;
     timer_t timer;
 #ifdef __linux__
     int dont_use_ppoll;
@@ -115,7 +116,6 @@
 =

     p->rtsig =3D -1;
     sigemptyset(&p->sigset_unblocked);
-    memset(&p->interval_timespec, 0, sizeof(p->interval_timespec));
     p->timer =3D (timer_t) -1;
         =

 #endif
@@ -125,7 +125,9 @@
     p->pollfd2 =3D pa_xnew(struct pollfd, p->n_pollfd_alloc);
     p->n_pollfd_used =3D 0;
 =

-    p->interval =3D 0;
+    p->period =3D 0;
+    memset(&p->next_elapse, 0, sizeof(p->next_elapse));
+    p->timer_enabled =3D 0;
 =

     p->running =3D 0;
     p->installed =3D 0;
@@ -260,6 +262,7 @@
     int r =3D 0;
     int no_events =3D 0;
     int saved_errno;
+    struct timespec timeout;
     =

     pa_assert(p);
     pa_assert(!p->running);
@@ -296,6 +299,17 @@
 =

     if (p->rebuild_needed)
         rtpoll_rebuild(p);
+
+    /* Calculate timeout */
+    if (p->timer_enabled) {
+        struct timespec now;
+        pa_rtclock_get(&now);
+
+        if (pa_timespec_cmp(&p->next_elapse, &now) <=3D 0)
+            memset(&timeout, 0, sizeof(timeout));
+        else
+            pa_timespec_store(&timeout, pa_timespec_diff(&p->next_elapse, =
&now));
+    }
     =

     /* OK, now let's sleep */
 #ifdef HAVE_PPOLL
@@ -303,18 +317,33 @@
 #ifdef __linux__
     if (!p->dont_use_ppoll)
 #endif
-        r =3D ppoll(p->pollfd, p->n_pollfd_used, p->interval > 0  ? &p->in=
terval_timespec : NULL, p->rtsig < 0 ? NULL : &p->sigset_unblocked);
+        r =3D ppoll(p->pollfd, p->n_pollfd_used, p->timer_enabled > 0  ? &=
timeout : NULL, p->rtsig < 0 ? NULL : &p->sigset_unblocked);
 #ifdef __linux__
     else
 #endif
 =

 #else
-        r =3D poll(p->pollfd, p->n_pollfd_used, p->interval > 0 ? p->inter=
val / 1000 : -1);
+        r =3D poll(p->pollfd, p->n_pollfd_used, p->timer_enabled > 0 ? (ti=
meout.tv_sec*1000) + (timeout.tv_nsec / 1000000) : -1);
 #endif
 =

     saved_errno =3D errno;
-    =

-    if (r < 0 && (errno =3D=3D EAGAIN || errno =3D=3D EINTR)) {
+
+    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 =3D 0;
+    }
+    =

+    if (r =3D=3D 0 || (r < 0 && (errno =3D=3D EAGAIN || errno =3D=3D EINTR=
))) {
         r =3D 0;
         no_events =3D 1;
     }
@@ -359,13 +388,10 @@
     return r;
 }
 =

-void pa_rtpoll_set_itimer(pa_rtpoll *p, pa_usec_t usec) {
-    pa_assert(p);
-
-    p->interval =3D usec;
+static void update_timer(pa_rtpoll *p) {
+    pa_assert(p);
 =

 #ifdef HAVE_PPOLL
-    pa_timespec_store(&p->interval_timespec, usec);
 =

 #ifdef __linux__
     if (!p->dont_use_ppoll) {
@@ -387,12 +413,21 @@
 =

         if (p->timer !=3D (timer_t) -1) {
             struct itimerspec its;
-
             memset(&its, 0, sizeof(its));
-            pa_timespec_store(&its.it_value, usec);
-            pa_timespec_store(&its.it_interval, usec);
-
-            assert(timer_settime(p->timer, 0, &its, NULL) =3D=3D 0);
+
+            if (p->timer_enabled) {
+                its.it_value =3D p->next_elapse;
+
+                /* Make sure that 0,0 is not understood as
+                 * "disarming" */
+                if (its.it_value.tv_sec =3D=3D 0)
+                    its.it_value.tv_nsec =3D 1;
+                =

+                if (p->period > 0)
+                    pa_timespec_store(&its.it_interval, p->period);
+            }
+
+            pa_assert_se(timer_settime(p->timer, TIMER_ABSTIME, &its, NULL=
) =3D=3D 0);
         }
 =

 #ifdef __linux__
@@ -400,6 +435,49 @@
 #endif
     =

 #endif
+}
+
+void pa_rtpoll_set_timer_absolute(pa_rtpoll *p, const struct timespec *ts)=
 {
+    pa_assert(p);
+    pa_assert(ts);
+    =

+    p->next_elapse =3D *ts;
+    p->period =3D 0;
+    p->timer_enabled =3D 1;
+    =

+    update_timer(p);
+}
+
+void pa_rtpoll_set_timer_periodic(pa_rtpoll *p, pa_usec_t usec) {
+    pa_assert(p);
+
+    p->period =3D usec;
+    pa_rtclock_get(&p->next_elapse);
+    pa_timespec_add(&p->next_elapse, usec);
+    p->timer_enabled =3D 1;
+
+    update_timer(p);
+}
+
+void pa_rtpoll_set_timer_relative(pa_rtpoll *p, pa_usec_t usec) {
+    pa_assert(p);
+
+    p->period =3D 0;
+    pa_rtclock_get(&p->next_elapse);
+    pa_timespec_add(&p->next_elapse, usec);
+    p->timer_enabled =3D 1;
+
+    update_timer(p);
+}
+
+void pa_rtpoll_set_timer_disabled(pa_rtpoll *p) {
+    pa_assert(p);
+
+    p->period =3D 0;
+    memset(&p->next_elapse, 0, sizeof(p->next_elapse));
+    p->timer_enabled =3D 0;
+
+    update_timer(p);
 }
 =

 pa_rtpoll_item *pa_rtpoll_item_new(pa_rtpoll *p, unsigned n_fds) {

Modified: branches/lennart/src/pulsecore/rtpoll.h
URL: http://0pointer.de/cgi-bin/viewcvs.cgi/branches/lennart/src/pulsecore/=
rtpoll.h?rev=3D1703&root=3Dpulseaudio&r1=3D1702&r2=3D1703&view=3Ddiff
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D
--- branches/lennart/src/pulsecore/rtpoll.h (original)
+++ branches/lennart/src/pulsecore/rtpoll.h Thu Aug 23 00:26:30 2007
@@ -59,11 +59,18 @@
 void pa_rtpoll_install(pa_rtpoll *p);
 =

 int pa_rtpoll_run(pa_rtpoll *f);
-void pa_rtpoll_set_itimer(pa_rtpoll *p, pa_usec_t usec);
+
+void pa_rtpoll_set_timer_absolute(pa_rtpoll *p, const struct timespec *ts);
+void pa_rtpoll_set_timer_periodic(pa_rtpoll *p, pa_usec_t usec);
+void pa_rtpoll_set_timer_relative(pa_rtpoll *p, pa_usec_t usec);
+void pa_rtpoll_set_timer_disabled(pa_rtpoll *p);
 =

 pa_rtpoll_item *pa_rtpoll_item_new(pa_rtpoll *p, unsigned n_fds);
 void pa_rtpoll_item_free(pa_rtpoll_item *i);
 =

+/* Please note that this pointer might change on every call and when
+ * pa_rtpoll_run() is called. Hence: call this immediately before
+ * using the pointer and don't save the result anywhere */
 struct pollfd *pa_rtpoll_item_get_pollfd(pa_rtpoll_item *i, unsigned *n_fd=
s);
 =

 void pa_rtpoll_item_set_before_callback(pa_rtpoll_item *i, int (*before_cb=
)(pa_rtpoll_item *i));
@@ -74,5 +81,4 @@
 pa_rtpoll_item *pa_rtpoll_item_new_fdsem(pa_rtpoll *p, pa_fdsem *s);
 pa_rtpoll_item *pa_rtpoll_item_new_asyncmsgq(pa_rtpoll *p, pa_asyncmsgq *q=
);
 =

-
 #endif

Modified: branches/lennart/src/tests/rtpoll-test.c
URL: http://0pointer.de/cgi-bin/viewcvs.cgi/branches/lennart/src/tests/rtpo=
ll-test.c?rev=3D1703&root=3Dpulseaudio&r1=3D1702&r2=3D1703&view=3Ddiff
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D
--- branches/lennart/src/tests/rtpoll-test.c (original)
+++ branches/lennart/src/tests/rtpoll-test.c Thu Aug 23 00:26:30 2007
@@ -56,7 +56,7 @@
     pollfd->events =3D POLLIN;
 =

     pa_rtpoll_install(p);
-    pa_rtpoll_set_itimer(p, 10000000); /* 10 s */
+    pa_rtpoll_set_timer_periodic(p, 10000000); /* 10 s */
 =

     pa_rtpoll_run(p);
     =





More information about the pulseaudio-commits mailing list