X-Git-Url: https://code.delx.au/pulseaudio/blobdiff_plain/717b1641614ed7a47c2b0188496dd39be806975c..b4bb747ba70caf2675c61c7d93e700adae28e209:/src/pulsecore/rtclock.c diff --git a/src/pulsecore/rtclock.c b/src/pulsecore/rtclock.c index 02abde3c..cec7124f 100644 --- a/src/pulsecore/rtclock.c +++ b/src/pulsecore/rtclock.c @@ -35,127 +35,67 @@ #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; - - u -= (pa_usec_t) a->tv_sec * PA_USEC_PER_SEC; - - a->tv_nsec = u * 1000; - - return a; + return pa_timeval_diff(pa_rtclock_get(&now), tv); } -struct timespec *pa_timespec_reset(struct timespec *a) { - pa_assert(a); +struct timeval *pa_rtclock_get(struct timeval *tv) { +#ifdef HAVE_CLOCK_GETTIME + static int no_monotonic = 0; + struct timespec ts; - a->tv_sec = a->tv_nsec = 0; - return a; -} + /* No locking or atomic ops for no_monotonic here */ -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); -} + if (!no_monotonic) { +#ifdef CLOCK_MONOTONIC + if (clock_gettime(CLOCK_MONOTONIC, &ts) >= 0) + goto out; +#endif -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; + no_monotonic = 1; } - /* Calculate the second difference*/ - r = ((pa_usec_t) a->tv_sec - b->tv_sec) * PA_USEC_PER_SEC; + pa_assert_se(clock_gettime(CLOCK_REALTIME, &ts) == 0); - /* 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); +out: + pa_assert(tv); - return r; -} - -int pa_timespec_cmp(const struct timespec *a, const struct timespec *b) { - pa_assert(a); - pa_assert(b); - - if (a->tv_sec < b->tv_sec) - return -1; - - if (a->tv_sec > b->tv_sec) - return 1; - - if (a->tv_nsec < b->tv_nsec) - return -1; + tv->tv_sec = ts.tv_sec; + tv->tv_usec = ts.tv_nsec / 1000; - if (a->tv_nsec > b->tv_nsec) - return 1; + return tv; - return 0; -} - -struct timespec* pa_timespec_add(struct timespec *ts, pa_usec_t v) { - unsigned long secs; - pa_assert(ts); - - secs = (unsigned long) (v/PA_USEC_PER_SEC); - ts->tv_sec += secs; - v -= ((pa_usec_t) secs) * PA_USEC_PER_SEC; +#else /* HAVE_CLOCK_GETTIME */ - ts->tv_nsec += (long) (v*1000); - - /* Normalize */ - while (ts->tv_nsec >= PA_NSEC_PER_SEC) { - ts->tv_sec++; - ts->tv_nsec -= PA_NSEC_PER_SEC; - } + return pa_gettimeofday(tv); - return ts; +#endif } -pa_usec_t pa_rtclock_age(const struct timespec *ts) { - struct timespec now; - pa_assert(ts); +pa_bool_t pa_rtclock_hrtimer(void) { +#ifdef HAVE_CLOCK_GETTIME + struct timespec ts; - return pa_timespec_diff(pa_rtclock_get(&now), ts); -} +#ifdef CLOCK_MONOTONIC + if (clock_getres(CLOCK_MONOTONIC, &ts) >= 0) + return ts.tv_sec == 0 && ts.tv_nsec <= PA_HRTIMER_THRESHOLD_USEC*1000; +#endif -struct timespec *pa_rtclock_get(struct timespec *ts) { - static int no_monotonic = 0; + pa_assert_se(clock_getres(CLOCK_REALTIME, &ts) == 0); + return ts.tv_sec == 0 && ts.tv_nsec <= PA_HRTIMER_THRESHOLD_USEC*1000; - /* No locking or atomic ops for no_monotonic here */ - - pa_assert(ts); +#else /* HAVE_CLOCK_GETTIME */ - if (!no_monotonic) { - if (clock_gettime(CLOCK_MONOTONIC, ts) >= 0) - return ts; - - no_monotonic = 1; - } + return FALSE; - pa_assert_se(clock_gettime(CLOCK_REALTIME, ts) == 0); - return ts; +#endif } -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; +pa_usec_t pa_rtclock_usec(void) { + struct timeval tv; - pa_assert_se(clock_getres(CLOCK_REALTIME, &ts) == 0); - return ts.tv_sec == 0 && ts.tv_nsec <= PA_HRTIMER_THRESHOLD_USEC*1000; + return pa_timeval_load(pa_rtclock_get(&tv)); } -