Mercurial > mplayer.hg
annotate osdep/timer-darwin.c @ 36018:26836919db4e
Add, group and sort #includes, remove unneeded ones.
author | ib |
---|---|
date | Sun, 31 Mar 2013 12:42:47 +0000 |
parents | c70109fc98b2 |
children |
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> |
32675
c70109fc98b2
Add/remove a few standard header #includes in our libc function replacements.
diego
parents:
31464
diff
changeset
|
19 #include <stdint.h> |
10148
139b44930abc
Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff
changeset
|
20 #include <stdlib.h> |
12954 | 21 #include <time.h> |
22 #include <math.h> | |
23 #include <sys/time.h> | |
10148
139b44930abc
Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff
changeset
|
24 #include <mach/mach_time.h> |
139b44930abc
Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff
changeset
|
25 |
16985 | 26 #include "config.h" |
27 #include "mp_msg.h" | |
10148
139b44930abc
Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff
changeset
|
28 #include "timer.h" |
139b44930abc
Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff
changeset
|
29 |
139b44930abc
Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff
changeset
|
30 /* global variables */ |
26640
503c2fd0c88b
Remove useless variable startup_time, since we do not need it any more.
ulion
parents:
26550
diff
changeset
|
31 static double relative_time; |
10148
139b44930abc
Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff
changeset
|
32 static double timebase_ratio; |
12954 | 33 |
31464
1453fc56d49c
Change timer_name type from char* to char[]; patch taken from Uoti's tree.
diego
parents:
30673
diff
changeset
|
34 const char timer_name[] = "Darwin accurate"; |
10148
139b44930abc
Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff
changeset
|
35 |
139b44930abc
Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff
changeset
|
36 |
12954 | 37 |
15275 | 38 /* the core sleep function, uses floats and is used in MPlayer G2 */ |
30673
cfb6e0b4e2bd
Mark sleep_accurate() as static, it is only used within the file.
diego
parents:
29263
diff
changeset
|
39 static float sleep_accurate(float time_frame) |
15275 | 40 { |
41 uint64_t deadline = time_frame / timebase_ratio + mach_absolute_time(); | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29212
diff
changeset
|
42 |
15275 | 43 mach_wait_until(deadline); |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29212
diff
changeset
|
44 |
15275 | 45 return (mach_absolute_time() - deadline) * timebase_ratio; |
46 } | |
47 | |
12954 | 48 /* wrapper for MPlayer G1 */ |
10148
139b44930abc
Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff
changeset
|
49 int usec_sleep(int usec_delay) |
139b44930abc
Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff
changeset
|
50 { |
15275 | 51 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
|
52 } |
139b44930abc
Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff
changeset
|
53 |
139b44930abc
Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff
changeset
|
54 |
12954 | 55 /* current time in microseconds */ |
29212
eda346733b8c
Add missing 'void' to parameterless function declarations.
diego
parents:
26640
diff
changeset
|
56 unsigned int GetTimer(void) |
10148
139b44930abc
Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff
changeset
|
57 { |
26640
503c2fd0c88b
Remove useless variable startup_time, since we do not need it any more.
ulion
parents:
26550
diff
changeset
|
58 return (unsigned int)(uint64_t)(mach_absolute_time() * timebase_ratio * 1e6); |
12954 | 59 } |
10148
139b44930abc
Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff
changeset
|
60 |
12954 | 61 /* current time in milliseconds */ |
29212
eda346733b8c
Add missing 'void' to parameterless function declarations.
diego
parents:
26640
diff
changeset
|
62 unsigned int GetTimerMS(void) |
10148
139b44930abc
Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff
changeset
|
63 { |
26640
503c2fd0c88b
Remove useless variable startup_time, since we do not need it any more.
ulion
parents:
26550
diff
changeset
|
64 return (unsigned int)(uint64_t)(mach_absolute_time() * timebase_ratio * 1e3); |
10148
139b44930abc
Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff
changeset
|
65 } |
139b44930abc
Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff
changeset
|
66 |
12954 | 67 /* time spent between now and last call in seconds */ |
29212
eda346733b8c
Add missing 'void' to parameterless function declarations.
diego
parents:
26640
diff
changeset
|
68 float GetRelativeTime(void) |
10148
139b44930abc
Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff
changeset
|
69 { |
12954 | 70 double last_time = relative_time; |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29212
diff
changeset
|
71 |
12954 | 72 if (!relative_time) |
73 InitTimer(); | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29212
diff
changeset
|
74 |
10148
139b44930abc
Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff
changeset
|
75 relative_time = mach_absolute_time() * timebase_ratio; |
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 return (float)(relative_time-last_time); |
139b44930abc
Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff
changeset
|
78 } |
139b44930abc
Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff
changeset
|
79 |
12954 | 80 /* initialize timer, must be called at least once at start */ |
29212
eda346733b8c
Add missing 'void' to parameterless function declarations.
diego
parents:
26640
diff
changeset
|
81 void InitTimer(void) |
10148
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 struct mach_timebase_info timebase; |
139b44930abc
Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff
changeset
|
84 |
139b44930abc
Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff
changeset
|
85 mach_timebase_info(&timebase); |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29212
diff
changeset
|
86 timebase_ratio = (double)timebase.numer / (double)timebase.denom |
10148
139b44930abc
Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff
changeset
|
87 * (double)1e-9; |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29212
diff
changeset
|
88 |
26640
503c2fd0c88b
Remove useless variable startup_time, since we do not need it any more.
ulion
parents:
26550
diff
changeset
|
89 relative_time = (double)(mach_absolute_time() * timebase_ratio); |
10148
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 | 92 #if 0 |
93 #include <stdio.h> | |
10148
139b44930abc
Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff
changeset
|
94 |
25100 | 95 int main(void) { |
12954 | 96 int i,j, r, c = 200; |
97 long long t = 0; | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29212
diff
changeset
|
98 |
10148
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 | 101 for (i = 0; i < c; i++) { |
102 const int delay = rand() / (RAND_MAX / 1e5); | |
103 j = GetTimer(); | |
104 #if 1 | |
105 r = usec_sleep(delay); | |
106 #else | |
107 r = sleep_accurate(delay / 1e6) * 1e6; | |
108 #endif | |
109 j = (GetTimer() - j) - delay; | |
110 printf("sleep time:%8i %5i (%i)\n", delay, j, j - r); | |
111 t += j - r; | |
10148
139b44930abc
Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff
changeset
|
112 } |
12954 | 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 |