]> code.delx.au - pulseaudio/blobdiff - src/pulsecore/rtclock.c
Merge commit 'origin/master-tx'
[pulseaudio] / src / pulsecore / rtclock.c
index a4e9875d691eb7b1f9b47277d5d8a0aafe5ad55a..56ab2ef02a593d76f37d6f6504c9aac92567ede6 100644 (file)
@@ -1,5 +1,3 @@
-/* $Id$ */
-
 /***
   This file is part of PulseAudio.
 
 #endif
 
 #include <stddef.h>
+#include <time.h>
 #include <sys/time.h>
+#include <errno.h>
+
+#ifdef HAVE_SYS_PRCTL_H
+#include <sys/prctl.h>
+#endif
 
 #include <pulse/timeval.h>
 #include <pulsecore/macro.h>
+#include <pulsecore/core-error.h>
 
 #include "rtclock.h"
 
-struct timespec *pa_timespec_store(struct timespec *a, pa_usec_t u) {
-    pa_assert(a);
+pa_usec_t pa_rtclock_age(const struct timeval *tv) {
+    struct timeval now;
+    pa_assert(tv);
 
-    a->tv_sec = u / PA_USEC_PER_SEC;
+    return pa_timeval_diff(pa_rtclock_get(&now), tv);
+}
 
-    u -= (pa_usec_t) a->tv_sec * PA_USEC_PER_SEC;
-    
-    a->tv_nsec = u * 1000;
+struct timeval *pa_rtclock_get(struct timeval *tv) {
+#ifdef HAVE_CLOCK_GETTIME
+    struct timespec ts;
 
-    return a;
-}
+#ifdef CLOCK_MONOTONIC
+    /* No locking or atomic ops for no_monotonic here */
+    static pa_bool_t no_monotonic = FALSE;
 
-struct timespec *pa_timespec_reset(struct timespec *a) {
-    pa_assert(a);
+    if (!no_monotonic)
+        if (clock_gettime(CLOCK_MONOTONIC, &ts) < 0)
+            no_monotonic = TRUE;
 
-    a->tv_sec = a->tv_nsec = 0;
-    return a;
-}
+    if (no_monotonic)
+#endif
+        pa_assert_se(clock_gettime(CLOCK_REALTIME, &ts) == 0);
 
-pa_usec_t pa_timespec_load(struct timespec *ts) {
-    pa_assert(ts);
-    
-    return (pa_usec_t) ts->tv_sec * PA_USEC_PER_SEC + (pa_usec_t) (ts->tv_nsec / 1000);
-}
+    pa_assert(tv);
 
-pa_usec_t pa_timespec_diff(const struct timespec *a, const struct timespec *b) {
-    pa_usec_t r;
-    
-    pa_assert(a);
-    pa_assert(b);
-
-    /* Check which whan is the earlier time and swap the two arguments if required. */
-    if (pa_timespec_cmp(a, b) < 0) {
-        const struct timespec *c;
-        c = a;
-        a = b;
-        b = c;
-    }
+    tv->tv_sec = ts.tv_sec;
+    tv->tv_usec = ts.tv_nsec / 1000;
+
+    return tv;
 
-    /* Calculate the second difference*/
-    r = ((pa_usec_t) a->tv_sec - b->tv_sec) * PA_USEC_PER_SEC;
+#else /* HAVE_CLOCK_GETTIME */
 
-    /* Calculate the microsecond difference */
-    if (a->tv_nsec > b->tv_nsec)
-        r += (pa_usec_t) ((a->tv_nsec - b->tv_nsec) / 1000);
-    else if (a->tv_nsec < b->tv_nsec)
-        r -= (pa_usec_t) ((b->tv_nsec - a->tv_nsec) / 1000);
+    return pa_gettimeofday(tv);
 
-    return r;
+#endif
 }
 
-int pa_timespec_cmp(const struct timespec *a, const struct timespec *b) {
-    pa_assert(a);
-    pa_assert(b);
+pa_bool_t pa_rtclock_hrtimer(void) {
+#ifdef HAVE_CLOCK_GETTIME
+    struct timespec ts;
 
-    if (a->tv_sec < b->tv_sec)
-        return -1;
+#ifdef CLOCK_MONOTONIC
+    if (clock_getres(CLOCK_MONOTONIC, &ts) >= 0)
+        return ts.tv_sec == 0 && ts.tv_nsec <= PA_HRTIMER_THRESHOLD_USEC*1000;
+#endif
 
-    if (a->tv_sec > b->tv_sec)
-        return 1;
+    pa_assert_se(clock_getres(CLOCK_REALTIME, &ts) == 0);
+    return ts.tv_sec == 0 && ts.tv_nsec <= PA_HRTIMER_THRESHOLD_USEC*1000;
 
-    if (a->tv_nsec < b->tv_nsec)
-        return -1;
+#else /* HAVE_CLOCK_GETTIME */
 
-    if (a->tv_nsec > b->tv_nsec)
-        return 1;
+    return FALSE;
 
-    return 0;
+#endif
 }
 
-struct timespec* pa_timespec_add(struct timespec *ts, pa_usec_t v) {
-    unsigned long secs;
-    pa_assert(ts);
+void pa_rtclock_hrtimer_enable(void) {
+#ifdef PR_SET_TIMERSLACK
+    int slack_ns;
+
+    if ((slack_ns = prctl(PR_GET_TIMERSLACK, 0, 0, 0, 0)) < 0) {
+        pa_log_info("PR_GET_TIMERSLACK/PR_SET_TIMERSLACK not supported.");
+        return;
+    }
 
-    secs = (unsigned long) (v/PA_USEC_PER_SEC);
-    ts->tv_sec += secs;
-    v -= ((pa_usec_t) secs) * PA_USEC_PER_SEC;
+    pa_log_debug("Timer slack set to %i us.", slack_ns/1000);
 
-    ts->tv_nsec += (long) (v*1000);
+    slack_ns = 500000000;
 
-    /* Normalize */
-    while (ts->tv_nsec >= PA_NSEC_PER_SEC) {
-        ts->tv_sec++;
-        ts->tv_nsec -= PA_NSEC_PER_SEC;
+    pa_log_debug("Setting timer slack to %i us.", slack_ns/1000);
+
+    if (prctl(PR_SET_TIMERSLACK, slack_ns, 0, 0, 0) < 0) {
+        pa_log_warn("PR_SET_TIMERSLACK failed: %s", pa_cstrerror(errno));
+        return;
     }
 
-    return ts;
+#endif
 }
 
-pa_usec_t pa_rtclock_age(const struct timespec *ts) {
-    struct timespec now;
-    pa_assert(ts);
+pa_usec_t pa_rtclock_usec(void) {
+    struct timeval tv;
 
-    return pa_timespec_diff(pa_rtclock_get(&now), ts);
+    return pa_timeval_load(pa_rtclock_get(&tv));
 }
 
-struct timespec *pa_rtclock_get(struct timespec *ts) {
-    static int no_monotonic = 0;
+struct timeval* pa_rtclock_from_wallclock(struct timeval *tv) {
 
-    /* No locking or atomic ops for no_monotonic here */
-    
-    pa_assert(ts);
+#ifdef HAVE_CLOCK_GETTIME
+    struct timeval wc_now, rt_now;
 
-    if (!no_monotonic) {
-        if (clock_gettime(CLOCK_MONOTONIC, ts) >= 0)
-            return ts;
-        
-        no_monotonic = 1;
-    }
+    pa_gettimeofday(&wc_now);
+    pa_rtclock_get(&rt_now);
 
-    pa_assert_se(clock_gettime(CLOCK_REALTIME, ts) == 0);
-    return ts;
-}
+    pa_assert(tv);
 
-int pa_rtclock_hrtimer(void) {
-    struct timespec ts;
-    
-    if (clock_getres(CLOCK_MONOTONIC, &ts) >= 0)
-        return ts.tv_sec == 0 && ts.tv_nsec <= PA_HRTIMER_THRESHOLD_USEC*1000;
+    if (pa_timeval_cmp(&wc_now, tv) < 0)
+        pa_timeval_add(&rt_now, pa_timeval_diff(tv, &wc_now));
+    else
+        pa_timeval_sub(&rt_now, pa_timeval_diff(&wc_now, tv));
 
-    pa_assert_se(clock_getres(CLOCK_REALTIME, &ts) == 0);
-    return ts.tv_sec == 0 && ts.tv_nsec <= PA_HRTIMER_THRESHOLD_USEC*1000;
+    *tv = rt_now;
+#endif
+
+    return tv;
 }
 
+pa_usec_t pa_timespec_load(const struct timespec *ts) {
+    pa_assert(ts);
+
+    return
+        (pa_usec_t) ts->tv_sec * PA_USEC_PER_SEC +
+        (pa_usec_t) ts->tv_nsec / PA_NSEC_PER_USEC;
+}