timers: Remove GetRelativeTime()
[mplayer/glamo.git] / osdep / timer-darwin.c
blobe087b33f126ca05d4bfb9cde8a3eb1405824c054
1 /*
2 * Precise timer routines using Mach timing
4 * Copyright (c) 2003-2004, Dan Villiom Podlaski Christiansen
6 * Permission is hereby granted, free of charge, to any person
7 * obtaining a copy of this software and associated documentation
8 * files (the "Software"), to deal in the Software without
9 * restriction, including without limitation the rights to use, copy,
10 * modify, merge, publish, distribute, sublicense, and/or sell copies
11 * of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be
15 * included in all copies or substantial portions of the Software.
18 #include <unistd.h>
19 #include <stdlib.h>
20 #include <time.h>
21 #include <math.h>
22 #include <sys/time.h>
23 #include <mach/mach_time.h>
25 #include "config.h"
26 #include "mp_msg.h"
27 #include "timer.h"
29 /* global variables */
30 static double timebase_ratio;
32 const char timer_name[] = "Darwin accurate";
36 /* the core sleep function, uses floats and is used in MPlayer G2 */
37 float sleep_accurate(float time_frame)
39 uint64_t deadline = time_frame / timebase_ratio + mach_absolute_time();
41 mach_wait_until(deadline);
43 return (mach_absolute_time() - deadline) * timebase_ratio;
46 /* wrapper for MPlayer G1 */
47 int usec_sleep(int usec_delay)
49 return sleep_accurate(usec_delay / 1e6) * 1e6;
53 /* current time in microseconds */
54 unsigned int GetTimer()
56 return (unsigned int)((mach_absolute_time() * timebase_ratio)
57 * 1e6);
60 /* current time in milliseconds */
61 unsigned int GetTimerMS()
63 return (unsigned int)((mach_absolute_time() * timebase_ratio)
64 * 1e3);
67 /* initialize timer, must be called at least once at start */
68 void InitTimer()
70 struct mach_timebase_info timebase;
72 mach_timebase_info(&timebase);
73 timebase_ratio = (double)timebase.numer / (double)timebase.denom
74 * (double)1e-9;
77 #if 0
78 #include <stdio.h>
80 int main(void) {
81 int i,j, r, c = 200;
82 long long t = 0;
84 InitTimer();
86 for (i = 0; i < c; i++) {
87 const int delay = rand() / (RAND_MAX / 1e5);
88 j = GetTimer();
89 #if 1
90 r = usec_sleep(delay);
91 #else
92 r = sleep_accurate(delay / 1e6) * 1e6;
93 #endif
94 j = (GetTimer() - j) - delay;
95 printf("sleep time:%8i %5i (%i)\n", delay, j, j - r);
96 t += j - r;
98 fprintf(stderr, "average error:\t%lli\n", t / c);
100 return 0;
102 #endif