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.c | 82 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 osdep/timer.c (limited to 'osdep/timer.c') diff --git a/osdep/timer.c b/osdep/timer.c new file mode 100644 index 0000000000..2304bb1297 --- /dev/null +++ b/osdep/timer.c @@ -0,0 +1,82 @@ +/* + * This file is part of mpv. + * + * mpv is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * mpv is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with mpv. If not, see . + */ + +#include + +#include "timer.h" + +static uint64_t raw_time_offset; + +void mp_time_init(void) +{ + mp_raw_time_init(); + srand(mp_raw_time_us()); + raw_time_offset = mp_raw_time_us(); + // Arbitrary additional offset to avoid confusing relative/absolute times. + // Also,we rule that the timer never returns 0 (so default-initialized + // time values will be always in the past). + raw_time_offset -= 10000000; +} + +int64_t mp_time_us(void) +{ + return mp_raw_time_us() - raw_time_offset; +} + +double mp_time_sec(void) +{ + return mp_time_us() / (double)(1000 * 1000); +} + +unsigned int GetTimer(void) +{ + return mp_time_us(); +} + +unsigned int GetTimerMS(void) +{ + return (mp_time_us() + 500) / 1000; +} + +int usec_sleep(int usec_delay) +{ + mp_sleep_us(usec_delay); + return 0; +} + +#if 0 +#include + +int main(void) { + int c = 200; + int64_t j, r, t = 0; + + mp_time_init(); + + for (int i = 0; i < c; i++) { + const int delay = rand() / (RAND_MAX / 1e5); + r = mp_time_us(); + mp_sleep_us(delay); + j = (mp_time_us() - r) - delay; + printf("sleep time: sleep=%8i err=%5i\n", delay, (int)j); + t += j; + } + fprintf(stderr, "average error:\t%i\n", (int)(t / c)); + + return 0; +} +#endif -- cgit v1.2.3