On Sat, Oct 31, 2009 at 01:41:19AM +0100, Lennart Poettering wrote: > On Mon, 19.10.09 12:45, Daniel Mack (daniel at caiaq.de) wrote: > > > > And clock_gettime we don't really need either. We need some kind of > > > accurate system timers (preferably monotonic), and on Linux we use > > > clock_gettime() for that. But we already have a fallback there for > > > gettimeofday(). > > > > > > Or in other words, the current APIs pa_rtclock_get(), > > > pa_rtclock_hrtimer() is supposed to be the abstract API that has > > > different backends on different systems. I'd very much prefer if any > > > MacOS specific code would simply be plugged in there instead of > > > creating various new abstraction interfaces! > > > > Ok - what about the version below? I don't particularily like the > > #ifdeffery jungle, but it is a lot less code now. > > Thanks. Looks much better. I reordered things a little now and merged > this. Please verify that I didn't break the MacOS code while I did that! My build bails due to an undefined 'CLOCK_REALTIME', and this is because HAVE_CLOCK_GETTIME is also defined on Darwin. Below is a patch to fix it. Thanks, Daniel >From bc9d25667bf983a0c278a0b151b511de1ea2a246 Mon Sep 17 00:00:00 2001 From: Daniel Mack <daniel@xxxxxxxx> Date: Sun, 1 Nov 2009 20:06:08 +0100 Subject: [PATCH] core-rtclock.c: tweak OS_IS_DARWIN contraints Move the code for OS_IS_DARWIN to the top as on Darwin, HAVE_CLOCK_GETTIME is also defined. --- src/pulsecore/core-rtclock.c | 75 ++++++++++++++++++++---------------------- 1 files changed, 36 insertions(+), 39 deletions(-) diff --git a/src/pulsecore/core-rtclock.c b/src/pulsecore/core-rtclock.c index 4fe0a47..d75674c 100644 --- a/src/pulsecore/core-rtclock.c +++ b/src/pulsecore/core-rtclock.c @@ -54,29 +54,7 @@ pa_usec_t pa_rtclock_age(const struct timeval *tv) { struct timeval *pa_rtclock_get(struct timeval *tv) { -#if defined(HAVE_CLOCK_GETTIME) - struct timespec ts; - -#ifdef CLOCK_MONOTONIC - /* No locking or atomic ops for no_monotonic here */ - static pa_bool_t no_monotonic = FALSE; - - if (!no_monotonic) - if (clock_gettime(CLOCK_MONOTONIC, &ts) < 0) - no_monotonic = TRUE; - - if (no_monotonic) -#endif /* CLOCK_MONOTONIC */ - pa_assert_se(clock_gettime(CLOCK_REALTIME, &ts) == 0); - - pa_assert(tv); - - tv->tv_sec = ts.tv_sec; - tv->tv_usec = ts.tv_nsec / PA_NSEC_PER_USEC; - - return tv; - -#elif defined(OS_IS_DARWIN) +#if defined(OS_IS_DARWIN) static mach_timebase_info_data_t tbi; uint64_t nticks; uint64_t time_nsec; @@ -99,41 +77,60 @@ struct timeval *pa_rtclock_get(struct timeval *tv) { return tv; -#else /* OS_IS_DARWIN */ +#elif defined(HAVE_CLOCK_GETTIME) + struct timespec ts; - return pa_gettimeofday(tv); +#ifdef CLOCK_MONOTONIC + /* No locking or atomic ops for no_monotonic here */ + static pa_bool_t no_monotonic = FALSE; -#endif + if (!no_monotonic) + if (clock_gettime(CLOCK_MONOTONIC, &ts) < 0) + no_monotonic = TRUE; + + if (no_monotonic) +#endif /* CLOCK_MONOTONIC */ + pa_assert_se(clock_gettime(CLOCK_REALTIME, &ts) == 0); + + pa_assert(tv); + + tv->tv_sec = ts.tv_sec; + tv->tv_usec = ts.tv_nsec / PA_NSEC_PER_USEC; + + return tv; +#endif /* HAVE_CLOCK_GETTIME */ + + return pa_gettimeofday(tv); } pa_bool_t pa_rtclock_hrtimer(void) { -#if defined(HAVE_CLOCK_GETTIME) +#if defined (OS_IS_DARWIN) + mach_timebase_info_data_t tbi; + uint64_t time_nsec; + + mach_timebase_info(&tbi); + + /* nsec = nticks * (N/D) - we want 1 tick == resolution !? */ + time_nsec = tbi.numer / tbi.denom; + return time_nsec <= (long) (PA_HRTIMER_THRESHOLD_USEC*PA_NSEC_PER_USEC); + +#elif defined(HAVE_CLOCK_GETTIME) struct timespec ts; #ifdef CLOCK_MONOTONIC if (clock_getres(CLOCK_MONOTONIC, &ts) >= 0) return ts.tv_sec == 0 && ts.tv_nsec <= (long) (PA_HRTIMER_THRESHOLD_USEC*PA_NSEC_PER_USEC); + #endif /* CLOCK_MONOTONIC */ pa_assert_se(clock_getres(CLOCK_REALTIME, &ts) == 0); return ts.tv_sec == 0 && ts.tv_nsec <= (long) (PA_HRTIMER_THRESHOLD_USEC*PA_NSEC_PER_USEC); -#elif defined (OS_IS_DARWIN) - mach_timebase_info_data_t tbi; - uint64_t time_nsec; +#else /* HAVE_CLOCK_GETTIME */ - mach_timebase_info(&tbi); - - /* nsec = nticks * (N/D) - we want 1 tick == resolution !? */ - time_nsec = tbi.numer / tbi.denom; - return time_nsec <= (long) (PA_HRTIMER_THRESHOLD_USEC*PA_NSEC_PER_USEC); - -#else /* OS_IS_DARWIN */ return FALSE; - -#endif } #define TIMER_SLACK_NS (int) ((500 * PA_NSEC_PER_USEC)) -- 1.6.3.3