From 81439c5f35e604174408a2aaf4e4dec11b81ac39 Mon Sep 17 00:00:00 2001 From: wm4 Date: Fri, 17 May 2013 19:54:37 +0200 Subject: timer: refactor, add 64 bit timer function Make OS specific timer code export a mp_raw_time_us() function, and add generic implementations of GetTimer()/GetTimerMS() using this function. New mpv code is supposed to call mp_time_us() in situations where precision is absolutely needed, or mp_time_s() otherwise. Make it so that mp_time_us() will return a value near program start. We don't set it to 0 though to avoid confusion with relative vs. absolute time. Instead, pick an arbitrary offset. Move the test program in timer-darwin.c to timer.c, and modify it to work with the generic timer functions. --- osdep/timer-darwin.c | 66 +++++++--------------------------------------------- 1 file changed, 9 insertions(+), 57 deletions(-) (limited to 'osdep/timer-darwin.c') diff --git a/osdep/timer-darwin.c b/osdep/timer-darwin.c index 1ce9cd584b..e5f751915e 100644 --- a/osdep/timer-darwin.c +++ b/osdep/timer-darwin.c @@ -27,72 +27,24 @@ #include "core/mp_msg.h" #include "timer.h" -/* global variables */ static double timebase_ratio; - -/* the core sleep function, uses floats and is used in MPlayer G2 */ -static float sleep_accurate(float time_frame) +void mp_sleep_us(int64_t us) { - uint64_t deadline = time_frame / timebase_ratio + mach_absolute_time(); - - mach_wait_until(deadline); - - return (mach_absolute_time() - deadline) * timebase_ratio; -} + uint64_t deadline = us / 1e6 / timebase_ratio + mach_absolute_time(); -/* wrapper for MPlayer G1 */ -int usec_sleep(int usec_delay) -{ - return sleep_accurate(usec_delay / 1e6) * 1e6; + mach_wait_until(deadline); } - -/* current time in microseconds */ -unsigned int GetTimer(void) +uint64_t mp_raw_time_us(void) { - return (unsigned int)(uint64_t)(mach_absolute_time() * timebase_ratio * 1e6); + return mach_absolute_time() * timebase_ratio * 1e6; } -/* current time in milliseconds */ -unsigned int GetTimerMS(void) +void mp_raw_time_init(void) { - return (unsigned int)(uint64_t)(mach_absolute_time() * timebase_ratio * 1e3); -} - -/* initialize timer, must be called at least once at start */ -void InitTimer(void) -{ - struct mach_timebase_info timebase; - - mach_timebase_info(&timebase); - timebase_ratio = (double)timebase.numer / (double)timebase.denom - * (double)1e-9; -} - -#if 0 -#include - -int main(void) { - int i,j, r, c = 200; - long long t = 0; - - InitTimer(); - - for (i = 0; i < c; i++) { - const int delay = rand() / (RAND_MAX / 1e5); - j = GetTimer(); -#if 1 - r = usec_sleep(delay); -#else - r = sleep_accurate(delay / 1e6) * 1e6; -#endif - j = (GetTimer() - j) - delay; - printf("sleep time:%8i %5i (%i)\n", delay, j, j - r); - t += j - r; - } - fprintf(stderr, "average error:\t%lli\n", t / c); + struct mach_timebase_info timebase; - return 0; + mach_timebase_info(&timebase); + timebase_ratio = (double)timebase.numer / (double)timebase.denom * 1e-9; } -#endif -- cgit v1.2.3