Mercurial > mplayer.hg
annotate osdep/timer-darwin.c @ 13821:16d7868a65a3
QuickSilver skin author, typo
author | diego |
---|---|
date | Sat, 30 Oct 2004 21:49:55 +0000 |
parents | 3029f9e4f2ac |
children | 0e350095a07b |
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 |
139b44930abc
Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff
changeset
|
35 |
12954 | 36 |
37 /* wrapper for MPlayer G1 */ | |
10148
139b44930abc
Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff
changeset
|
38 int usec_sleep(int usec_delay) |
139b44930abc
Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff
changeset
|
39 { |
13776
3029f9e4f2ac
repace call to sleep_accurate to usleep which fix hang while using -cache option on osx
nplourde
parents:
12954
diff
changeset
|
40 return usleep(usec_delay); |
10148
139b44930abc
Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff
changeset
|
41 } |
139b44930abc
Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff
changeset
|
42 |
139b44930abc
Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff
changeset
|
43 |
12954 | 44 /* current time in microseconds */ |
10148
139b44930abc
Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff
changeset
|
45 unsigned int GetTimer() |
139b44930abc
Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff
changeset
|
46 { |
139b44930abc
Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff
changeset
|
47 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
|
48 * 1e6); |
12954 | 49 } |
10148
139b44930abc
Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff
changeset
|
50 |
12954 | 51 /* current time in milliseconds */ |
10148
139b44930abc
Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff
changeset
|
52 unsigned int GetTimerMS() |
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 return (unsigned int)(GetTimer() / 1000); |
139b44930abc
Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff
changeset
|
55 } |
139b44930abc
Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff
changeset
|
56 |
12954 | 57 /* 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
|
58 float GetRelativeTime() |
139b44930abc
Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff
changeset
|
59 { |
12954 | 60 double last_time = relative_time; |
61 | |
62 if (!relative_time) | |
63 InitTimer(); | |
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 relative_time = mach_absolute_time() * timebase_ratio; |
139b44930abc
Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff
changeset
|
66 |
139b44930abc
Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff
changeset
|
67 return (float)(relative_time-last_time); |
139b44930abc
Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff
changeset
|
68 } |
139b44930abc
Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff
changeset
|
69 |
12954 | 70 /* 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
|
71 void InitTimer() |
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 struct mach_timebase_info timebase; |
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 mach_timebase_info(&timebase); |
139b44930abc
Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff
changeset
|
76 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
|
77 * (double)1e-9; |
12954 | 78 |
10148
139b44930abc
Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff
changeset
|
79 relative_time = startup_time = |
139b44930abc
Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff
changeset
|
80 (double)(mach_absolute_time() * timebase_ratio); |
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 |
12954 | 83 #if 0 |
84 #include <stdio.h> | |
10148
139b44930abc
Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff
changeset
|
85 |
12954 | 86 int main() { |
87 int i,j, r, c = 200; | |
88 long long t = 0; | |
10148
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 InitTimer(); |
139b44930abc
Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff
changeset
|
91 |
12954 | 92 for (i = 0; i < c; i++) { |
93 const int delay = rand() / (RAND_MAX / 1e5); | |
94 j = GetTimer(); | |
95 #if 1 | |
96 r = usec_sleep(delay); | |
97 #else | |
98 r = sleep_accurate(delay / 1e6) * 1e6; | |
99 #endif | |
100 j = (GetTimer() - j) - delay; | |
101 printf("sleep time:%8i %5i (%i)\n", delay, j, j - r); | |
102 t += j - r; | |
10148
139b44930abc
Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff
changeset
|
103 } |
12954 | 104 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
|
105 |
139b44930abc
Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff
changeset
|
106 return 0; |
139b44930abc
Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff
changeset
|
107 } |
139b44930abc
Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff
changeset
|
108 #endif |