Mercurial > mplayer.hg
annotate osdep/timer-darwin.c @ 13752:e193600132d5
Important typo noticed by Piero di Vita <scognito at libero dot it>
author | diego |
---|---|
date | Sun, 24 Oct 2004 23:00:28 +0000 |
parents | f9755d9c479a |
children | 3029f9e4f2ac |
rev | line source |
---|---|
10148
139b44930abc
Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff
changeset
|
1 /* |
12954 | 2 * Precise timer routines using Mach timing |
3 * | |
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 | 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: | |
10148
139b44930abc
Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff
changeset
|
13 * |
12954 | 14 * The above copyright notice and this permission notice shall be |
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 | 20 #include <time.h> |
21 #include <math.h> | |
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 |
139b44930abc
Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff
changeset
|
25 #include "../config.h" |
139b44930abc
Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff
changeset
|
26 #include "../mp_msg.h" |
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 | 32 |
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 |
12954 | 35 /* the core sleep function, uses floats and is used in MPlayer G2 */ |
36 float sleep_accurate(float time_frame) | |
37 { | |
38 uint64_t deadline = time_frame / timebase_ratio + mach_absolute_time(); | |
10148
139b44930abc
Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff
changeset
|
39 |
12954 | 40 mach_wait_until(deadline); |
41 | |
42 return (mach_absolute_time() - deadline) * timebase_ratio; | |
43 } | |
44 | |
45 /* wrapper for MPlayer G1 */ | |
10148
139b44930abc
Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff
changeset
|
46 int usec_sleep(int usec_delay) |
139b44930abc
Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff
changeset
|
47 { |
12954 | 48 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
|
49 } |
139b44930abc
Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff
changeset
|
50 |
139b44930abc
Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff
changeset
|
51 |
12954 | 52 /* current time in microseconds */ |
10148
139b44930abc
Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff
changeset
|
53 unsigned int GetTimer() |
139b44930abc
Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff
changeset
|
54 { |
139b44930abc
Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff
changeset
|
55 return (unsigned int)((mach_absolute_time() * timebase_ratio - startup_time) |
139b44930abc
Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff
changeset
|
56 * 1e6); |
12954 | 57 } |
10148
139b44930abc
Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff
changeset
|
58 |
12954 | 59 /* current time in milliseconds */ |
10148
139b44930abc
Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff
changeset
|
60 unsigned int GetTimerMS() |
139b44930abc
Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff
changeset
|
61 { |
139b44930abc
Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff
changeset
|
62 return (unsigned int)(GetTimer() / 1000); |
139b44930abc
Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff
changeset
|
63 } |
139b44930abc
Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff
changeset
|
64 |
12954 | 65 /* 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
|
66 float GetRelativeTime() |
139b44930abc
Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff
changeset
|
67 { |
12954 | 68 double last_time = relative_time; |
69 | |
70 if (!relative_time) | |
71 InitTimer(); | |
10148
139b44930abc
Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff
changeset
|
72 |
139b44930abc
Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff
changeset
|
73 relative_time = mach_absolute_time() * timebase_ratio; |
139b44930abc
Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff
changeset
|
74 |
139b44930abc
Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff
changeset
|
75 return (float)(relative_time-last_time); |
139b44930abc
Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff
changeset
|
76 } |
139b44930abc
Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff
changeset
|
77 |
12954 | 78 /* 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
|
79 void InitTimer() |
139b44930abc
Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff
changeset
|
80 { |
139b44930abc
Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff
changeset
|
81 struct mach_timebase_info timebase; |
139b44930abc
Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff
changeset
|
82 |
139b44930abc
Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff
changeset
|
83 mach_timebase_info(&timebase); |
139b44930abc
Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff
changeset
|
84 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
|
85 * (double)1e-9; |
12954 | 86 |
10148
139b44930abc
Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff
changeset
|
87 relative_time = startup_time = |
139b44930abc
Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff
changeset
|
88 (double)(mach_absolute_time() * timebase_ratio); |
139b44930abc
Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff
changeset
|
89 } |
139b44930abc
Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff
changeset
|
90 |
12954 | 91 #if 0 |
92 #include <stdio.h> | |
10148
139b44930abc
Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff
changeset
|
93 |
12954 | 94 int main() { |
95 int i,j, r, c = 200; | |
96 long long t = 0; | |
10148
139b44930abc
Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff
changeset
|
97 |
139b44930abc
Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff
changeset
|
98 InitTimer(); |
139b44930abc
Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff
changeset
|
99 |
12954 | 100 for (i = 0; i < c; i++) { |
101 const int delay = rand() / (RAND_MAX / 1e5); | |
102 j = GetTimer(); | |
103 #if 1 | |
104 r = usec_sleep(delay); | |
105 #else | |
106 r = sleep_accurate(delay / 1e6) * 1e6; | |
107 #endif | |
108 j = (GetTimer() - j) - delay; | |
109 printf("sleep time:%8i %5i (%i)\n", delay, j, j - r); | |
110 t += j - r; | |
10148
139b44930abc
Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff
changeset
|
111 } |
12954 | 112 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
|
113 |
139b44930abc
Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff
changeset
|
114 return 0; |
139b44930abc
Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff
changeset
|
115 } |
139b44930abc
Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff
changeset
|
116 #endif |