diff options
author | wm4 <wm4@nowhere> | 2013-05-17 19:54:37 +0200 |
---|---|---|
committer | wm4 <wm4@nowhere> | 2013-05-26 16:44:20 +0200 |
commit | 81439c5f35e604174408a2aaf4e4dec11b81ac39 (patch) | |
tree | abd67642f487850c7f16666cc5f456d8f7ca347f /osdep/timer.c | |
parent | 25d66f526ebae36a6c044652e5006450dd5a30df (diff) | |
download | mpv-81439c5f35e604174408a2aaf4e4dec11b81ac39.tar.bz2 mpv-81439c5f35e604174408a2aaf4e4dec11b81ac39.tar.xz |
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.
Diffstat (limited to 'osdep/timer.c')
-rw-r--r-- | osdep/timer.c | 82 |
1 files changed, 82 insertions, 0 deletions
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 <http://www.gnu.org/licenses/>. + */ + +#include <stdlib.h> + +#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 <stdio.h> + +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 |