annotate osdep/timer-darwin.c @ 26584:f865a9546178

Link loader test files against mp_msg-mencoder.o instead of mp_msg.o. The former does not pick up GUI dependencies.
author diego
date Wed, 30 Apr 2008 14:34:18 +0000
parents 8a26190978a6
children 503c2fd0c88b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
10148
139b44930abc Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff changeset
1 /*
12954
f9755d9c479a Native darwin timer update.
wight
parents: 11867
diff changeset
2 * Precise timer routines using Mach timing
f9755d9c479a Native darwin timer update.
wight
parents: 11867
diff changeset
3 *
f9755d9c479a Native darwin timer update.
wight
parents: 11867
diff changeset
4 * Copyright (c) 2003-2004, Dan Villiom Podlaski Christiansen
10148
139b44930abc Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff changeset
5 *
12954
f9755d9c479a Native darwin timer update.
wight
parents: 11867
diff changeset
6 * Permission is hereby granted, free of charge, to any person
f9755d9c479a Native darwin timer update.
wight
parents: 11867
diff changeset
7 * obtaining a copy of this software and associated documentation
f9755d9c479a Native darwin timer update.
wight
parents: 11867
diff changeset
8 * files (the "Software"), to deal in the Software without
f9755d9c479a Native darwin timer update.
wight
parents: 11867
diff changeset
9 * restriction, including without limitation the rights to use, copy,
f9755d9c479a Native darwin timer update.
wight
parents: 11867
diff changeset
10 * modify, merge, publish, distribute, sublicense, and/or sell copies
f9755d9c479a Native darwin timer update.
wight
parents: 11867
diff changeset
11 * of the Software, and to permit persons to whom the Software is
f9755d9c479a Native darwin timer update.
wight
parents: 11867
diff changeset
12 * furnished to do so, subject to the following conditions:
10148
139b44930abc Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff changeset
13 *
12954
f9755d9c479a Native darwin timer update.
wight
parents: 11867
diff changeset
14 * The above copyright notice and this permission notice shall be
f9755d9c479a Native darwin timer update.
wight
parents: 11867
diff changeset
15 * included in all copies or substantial portions of the Software.
10148
139b44930abc Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff changeset
16 */
139b44930abc Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff changeset
17
139b44930abc Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff changeset
18 #include <unistd.h>
139b44930abc Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff changeset
19 #include <stdlib.h>
12954
f9755d9c479a Native darwin timer update.
wight
parents: 11867
diff changeset
20 #include <time.h>
f9755d9c479a Native darwin timer update.
wight
parents: 11867
diff changeset
21 #include <math.h>
f9755d9c479a Native darwin timer update.
wight
parents: 11867
diff changeset
22 #include <sys/time.h>
10148
139b44930abc Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff changeset
23 #include <mach/mach_time.h>
139b44930abc Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff changeset
24
16985
08cac43f1e38 Unify include paths, -I.. is in CFLAGS.
diego
parents: 15275
diff changeset
25 #include "config.h"
08cac43f1e38 Unify include paths, -I.. is in CFLAGS.
diego
parents: 15275
diff changeset
26 #include "mp_msg.h"
10148
139b44930abc Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff changeset
27 #include "timer.h"
139b44930abc Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff changeset
28
139b44930abc Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff changeset
29 /* global variables */
139b44930abc Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff changeset
30 static double relative_time, startup_time;
139b44930abc Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff changeset
31 static double timebase_ratio;
12954
f9755d9c479a Native darwin timer update.
wight
parents: 11867
diff changeset
32
f9755d9c479a Native darwin timer update.
wight
parents: 11867
diff changeset
33 const char *timer_name = "Darwin accurate";
10148
139b44930abc Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff changeset
34
139b44930abc Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff changeset
35
12954
f9755d9c479a Native darwin timer update.
wight
parents: 11867
diff changeset
36
15275
0e350095a07b use sleep_accurate darwin timer
nplourde
parents: 13776
diff changeset
37 /* the core sleep function, uses floats and is used in MPlayer G2 */
0e350095a07b use sleep_accurate darwin timer
nplourde
parents: 13776
diff changeset
38 float sleep_accurate(float time_frame)
0e350095a07b use sleep_accurate darwin timer
nplourde
parents: 13776
diff changeset
39 {
0e350095a07b use sleep_accurate darwin timer
nplourde
parents: 13776
diff changeset
40 uint64_t deadline = time_frame / timebase_ratio + mach_absolute_time();
0e350095a07b use sleep_accurate darwin timer
nplourde
parents: 13776
diff changeset
41
0e350095a07b use sleep_accurate darwin timer
nplourde
parents: 13776
diff changeset
42 mach_wait_until(deadline);
0e350095a07b use sleep_accurate darwin timer
nplourde
parents: 13776
diff changeset
43
0e350095a07b use sleep_accurate darwin timer
nplourde
parents: 13776
diff changeset
44 return (mach_absolute_time() - deadline) * timebase_ratio;
0e350095a07b use sleep_accurate darwin timer
nplourde
parents: 13776
diff changeset
45 }
0e350095a07b use sleep_accurate darwin timer
nplourde
parents: 13776
diff changeset
46
12954
f9755d9c479a Native darwin timer update.
wight
parents: 11867
diff changeset
47 /* wrapper for MPlayer G1 */
10148
139b44930abc Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff changeset
48 int usec_sleep(int usec_delay)
139b44930abc Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff changeset
49 {
15275
0e350095a07b use sleep_accurate darwin timer
nplourde
parents: 13776
diff changeset
50 return sleep_accurate(usec_delay / 1e6) * 1e6;
10148
139b44930abc Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff changeset
51 }
139b44930abc Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff changeset
52
139b44930abc Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff changeset
53
12954
f9755d9c479a Native darwin timer update.
wight
parents: 11867
diff changeset
54 /* current time in microseconds */
10148
139b44930abc Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff changeset
55 unsigned int GetTimer()
139b44930abc Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff changeset
56 {
26550
8a26190978a6 Force to uint64_t first to avoid direct conversion from double to unsigned int.
ulion
parents: 25100
diff changeset
57 return (unsigned int)(uint64_t)((mach_absolute_time() * timebase_ratio - startup_time) * 1e6);
12954
f9755d9c479a Native darwin timer update.
wight
parents: 11867
diff changeset
58 }
10148
139b44930abc Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff changeset
59
12954
f9755d9c479a Native darwin timer update.
wight
parents: 11867
diff changeset
60 /* current time in milliseconds */
10148
139b44930abc Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff changeset
61 unsigned int GetTimerMS()
139b44930abc Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff changeset
62 {
26550
8a26190978a6 Force to uint64_t first to avoid direct conversion from double to unsigned int.
ulion
parents: 25100
diff changeset
63 return (unsigned int)(uint64_t)((mach_absolute_time() * timebase_ratio - startup_time) * 1e3);
10148
139b44930abc Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff changeset
64 }
139b44930abc Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff changeset
65
12954
f9755d9c479a Native darwin timer update.
wight
parents: 11867
diff changeset
66 /* time spent between now and last call in seconds */
10148
139b44930abc Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff changeset
67 float GetRelativeTime()
139b44930abc Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff changeset
68 {
12954
f9755d9c479a Native darwin timer update.
wight
parents: 11867
diff changeset
69 double last_time = relative_time;
f9755d9c479a Native darwin timer update.
wight
parents: 11867
diff changeset
70
f9755d9c479a Native darwin timer update.
wight
parents: 11867
diff changeset
71 if (!relative_time)
f9755d9c479a Native darwin timer update.
wight
parents: 11867
diff changeset
72 InitTimer();
10148
139b44930abc Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff changeset
73
139b44930abc Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff changeset
74 relative_time = mach_absolute_time() * timebase_ratio;
139b44930abc Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff changeset
75
139b44930abc Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff changeset
76 return (float)(relative_time-last_time);
139b44930abc Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff changeset
77 }
139b44930abc Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff changeset
78
12954
f9755d9c479a Native darwin timer update.
wight
parents: 11867
diff changeset
79 /* initialize timer, must be called at least once at start */
10148
139b44930abc Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff changeset
80 void InitTimer()
139b44930abc Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff changeset
81 {
139b44930abc Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff changeset
82 struct mach_timebase_info timebase;
139b44930abc Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff changeset
83
139b44930abc Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff changeset
84 mach_timebase_info(&timebase);
139b44930abc Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff changeset
85 timebase_ratio = (double)timebase.numer / (double)timebase.denom
139b44930abc Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff changeset
86 * (double)1e-9;
12954
f9755d9c479a Native darwin timer update.
wight
parents: 11867
diff changeset
87
10148
139b44930abc Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff changeset
88 relative_time = startup_time =
139b44930abc Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff changeset
89 (double)(mach_absolute_time() * timebase_ratio);
139b44930abc Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff changeset
90 }
139b44930abc Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff changeset
91
12954
f9755d9c479a Native darwin timer update.
wight
parents: 11867
diff changeset
92 #if 0
f9755d9c479a Native darwin timer update.
wight
parents: 11867
diff changeset
93 #include <stdio.h>
10148
139b44930abc Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff changeset
94
25100
531116b7693d main() --> main(void)
diego
parents: 24815
diff changeset
95 int main(void) {
12954
f9755d9c479a Native darwin timer update.
wight
parents: 11867
diff changeset
96 int i,j, r, c = 200;
f9755d9c479a Native darwin timer update.
wight
parents: 11867
diff changeset
97 long long t = 0;
10148
139b44930abc Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff changeset
98
139b44930abc Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff changeset
99 InitTimer();
139b44930abc Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff changeset
100
12954
f9755d9c479a Native darwin timer update.
wight
parents: 11867
diff changeset
101 for (i = 0; i < c; i++) {
f9755d9c479a Native darwin timer update.
wight
parents: 11867
diff changeset
102 const int delay = rand() / (RAND_MAX / 1e5);
f9755d9c479a Native darwin timer update.
wight
parents: 11867
diff changeset
103 j = GetTimer();
f9755d9c479a Native darwin timer update.
wight
parents: 11867
diff changeset
104 #if 1
f9755d9c479a Native darwin timer update.
wight
parents: 11867
diff changeset
105 r = usec_sleep(delay);
f9755d9c479a Native darwin timer update.
wight
parents: 11867
diff changeset
106 #else
f9755d9c479a Native darwin timer update.
wight
parents: 11867
diff changeset
107 r = sleep_accurate(delay / 1e6) * 1e6;
f9755d9c479a Native darwin timer update.
wight
parents: 11867
diff changeset
108 #endif
f9755d9c479a Native darwin timer update.
wight
parents: 11867
diff changeset
109 j = (GetTimer() - j) - delay;
f9755d9c479a Native darwin timer update.
wight
parents: 11867
diff changeset
110 printf("sleep time:%8i %5i (%i)\n", delay, j, j - r);
f9755d9c479a Native darwin timer update.
wight
parents: 11867
diff changeset
111 t += j - r;
10148
139b44930abc Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff changeset
112 }
12954
f9755d9c479a Native darwin timer update.
wight
parents: 11867
diff changeset
113 fprintf(stderr, "average error:\t%lli\n", t / c);
10148
139b44930abc Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff changeset
114
139b44930abc Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff changeset
115 return 0;
139b44930abc Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff changeset
116 }
139b44930abc Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff changeset
117 #endif