annotate osdep/timer-macosx.c @ 9466:08c717b7b886

Support for native MacOSX APIs by Dan Christiansen <danchr@daimi.au.dk>
author alex
date Wed, 19 Feb 2003 17:26:59 +0000
parents 350b660ef93c
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
9465
350b660ef93c precise macosx timer by Dan Christiansen <danchr@daimi.au.dk> and 10l fix by me
alex
parents:
diff changeset
1 /*
350b660ef93c precise macosx timer by Dan Christiansen <danchr@daimi.au.dk> and 10l fix by me
alex
parents:
diff changeset
2 * Semi-precise timer routines using CoreFoundation
350b660ef93c precise macosx timer by Dan Christiansen <danchr@daimi.au.dk> and 10l fix by me
alex
parents:
diff changeset
3 *
350b660ef93c precise macosx timer by Dan Christiansen <danchr@daimi.au.dk> and 10l fix by me
alex
parents:
diff changeset
4 * (C) 2003 Dan Christiansen
350b660ef93c precise macosx timer by Dan Christiansen <danchr@daimi.au.dk> and 10l fix by me
alex
parents:
diff changeset
5 *
350b660ef93c precise macosx timer by Dan Christiansen <danchr@daimi.au.dk> and 10l fix by me
alex
parents:
diff changeset
6 * Released into the public domain.
350b660ef93c precise macosx timer by Dan Christiansen <danchr@daimi.au.dk> and 10l fix by me
alex
parents:
diff changeset
7 */
350b660ef93c precise macosx timer by Dan Christiansen <danchr@daimi.au.dk> and 10l fix by me
alex
parents:
diff changeset
8
350b660ef93c precise macosx timer by Dan Christiansen <danchr@daimi.au.dk> and 10l fix by me
alex
parents:
diff changeset
9 #include <unistd.h>
350b660ef93c precise macosx timer by Dan Christiansen <danchr@daimi.au.dk> and 10l fix by me
alex
parents:
diff changeset
10 #include <stdlib.h>
350b660ef93c precise macosx timer by Dan Christiansen <danchr@daimi.au.dk> and 10l fix by me
alex
parents:
diff changeset
11 #include <time.h>
350b660ef93c precise macosx timer by Dan Christiansen <danchr@daimi.au.dk> and 10l fix by me
alex
parents:
diff changeset
12 #include <sys/time.h>
350b660ef93c precise macosx timer by Dan Christiansen <danchr@daimi.au.dk> and 10l fix by me
alex
parents:
diff changeset
13 #include "../config.h"
350b660ef93c precise macosx timer by Dan Christiansen <danchr@daimi.au.dk> and 10l fix by me
alex
parents:
diff changeset
14
350b660ef93c precise macosx timer by Dan Christiansen <danchr@daimi.au.dk> and 10l fix by me
alex
parents:
diff changeset
15 #ifdef MACOSX
350b660ef93c precise macosx timer by Dan Christiansen <danchr@daimi.au.dk> and 10l fix by me
alex
parents:
diff changeset
16 # include <CoreFoundation/CFRunLoop.h>
350b660ef93c precise macosx timer by Dan Christiansen <danchr@daimi.au.dk> and 10l fix by me
alex
parents:
diff changeset
17 #endif
350b660ef93c precise macosx timer by Dan Christiansen <danchr@daimi.au.dk> and 10l fix by me
alex
parents:
diff changeset
18
350b660ef93c precise macosx timer by Dan Christiansen <danchr@daimi.au.dk> and 10l fix by me
alex
parents:
diff changeset
19 /* Rather than using CF timers, we simply store the absolute time
350b660ef93c precise macosx timer by Dan Christiansen <danchr@daimi.au.dk> and 10l fix by me
alex
parents:
diff changeset
20 * CFAbsoluteTime == double */
350b660ef93c precise macosx timer by Dan Christiansen <danchr@daimi.au.dk> and 10l fix by me
alex
parents:
diff changeset
21 static CFAbsoluteTime relative_time;
350b660ef93c precise macosx timer by Dan Christiansen <danchr@daimi.au.dk> and 10l fix by me
alex
parents:
diff changeset
22
350b660ef93c precise macosx timer by Dan Christiansen <danchr@daimi.au.dk> and 10l fix by me
alex
parents:
diff changeset
23 int usec_sleep(int usec_delay)
350b660ef93c precise macosx timer by Dan Christiansen <danchr@daimi.au.dk> and 10l fix by me
alex
parents:
diff changeset
24 {
350b660ef93c precise macosx timer by Dan Christiansen <danchr@daimi.au.dk> and 10l fix by me
alex
parents:
diff changeset
25 CFRunLoopRunInMode(kCFRunLoopDefaultMode, usec_delay / 1000000.0, false);
350b660ef93c precise macosx timer by Dan Christiansen <danchr@daimi.au.dk> and 10l fix by me
alex
parents:
diff changeset
26 }
350b660ef93c precise macosx timer by Dan Christiansen <danchr@daimi.au.dk> and 10l fix by me
alex
parents:
diff changeset
27
350b660ef93c precise macosx timer by Dan Christiansen <danchr@daimi.au.dk> and 10l fix by me
alex
parents:
diff changeset
28
350b660ef93c precise macosx timer by Dan Christiansen <danchr@daimi.au.dk> and 10l fix by me
alex
parents:
diff changeset
29 // Returns current time in microseconds
350b660ef93c precise macosx timer by Dan Christiansen <danchr@daimi.au.dk> and 10l fix by me
alex
parents:
diff changeset
30 unsigned int GetTimer(){
350b660ef93c precise macosx timer by Dan Christiansen <danchr@daimi.au.dk> and 10l fix by me
alex
parents:
diff changeset
31 return (unsigned int)(CFAbsoluteTimeGetCurrent() * 1000000);
350b660ef93c precise macosx timer by Dan Christiansen <danchr@daimi.au.dk> and 10l fix by me
alex
parents:
diff changeset
32 }
350b660ef93c precise macosx timer by Dan Christiansen <danchr@daimi.au.dk> and 10l fix by me
alex
parents:
diff changeset
33
350b660ef93c precise macosx timer by Dan Christiansen <danchr@daimi.au.dk> and 10l fix by me
alex
parents:
diff changeset
34 // Returns current time in milliseconds
350b660ef93c precise macosx timer by Dan Christiansen <danchr@daimi.au.dk> and 10l fix by me
alex
parents:
diff changeset
35 unsigned int GetTimerMS(){
350b660ef93c precise macosx timer by Dan Christiansen <danchr@daimi.au.dk> and 10l fix by me
alex
parents:
diff changeset
36 return (unsigned int)(CFAbsoluteTimeGetCurrent() * 1000);
350b660ef93c precise macosx timer by Dan Christiansen <danchr@daimi.au.dk> and 10l fix by me
alex
parents:
diff changeset
37 }
350b660ef93c precise macosx timer by Dan Christiansen <danchr@daimi.au.dk> and 10l fix by me
alex
parents:
diff changeset
38
350b660ef93c precise macosx timer by Dan Christiansen <danchr@daimi.au.dk> and 10l fix by me
alex
parents:
diff changeset
39 // Returns time spent between now and last call in seconds
350b660ef93c precise macosx timer by Dan Christiansen <danchr@daimi.au.dk> and 10l fix by me
alex
parents:
diff changeset
40 float GetRelativeTime(){
350b660ef93c precise macosx timer by Dan Christiansen <danchr@daimi.au.dk> and 10l fix by me
alex
parents:
diff changeset
41 CFAbsoluteTime last_time = relative_time;
350b660ef93c precise macosx timer by Dan Christiansen <danchr@daimi.au.dk> and 10l fix by me
alex
parents:
diff changeset
42 relative_time = CFAbsoluteTimeGetCurrent();
350b660ef93c precise macosx timer by Dan Christiansen <danchr@daimi.au.dk> and 10l fix by me
alex
parents:
diff changeset
43 return (float)(relative_time - last_time);
350b660ef93c precise macosx timer by Dan Christiansen <danchr@daimi.au.dk> and 10l fix by me
alex
parents:
diff changeset
44 }
350b660ef93c precise macosx timer by Dan Christiansen <danchr@daimi.au.dk> and 10l fix by me
alex
parents:
diff changeset
45
350b660ef93c precise macosx timer by Dan Christiansen <danchr@daimi.au.dk> and 10l fix by me
alex
parents:
diff changeset
46 // Initialize timer, must be called at least once at start
350b660ef93c precise macosx timer by Dan Christiansen <danchr@daimi.au.dk> and 10l fix by me
alex
parents:
diff changeset
47 void InitTimer(){
350b660ef93c precise macosx timer by Dan Christiansen <danchr@daimi.au.dk> and 10l fix by me
alex
parents:
diff changeset
48 GetRelativeTime();
350b660ef93c precise macosx timer by Dan Christiansen <danchr@daimi.au.dk> and 10l fix by me
alex
parents:
diff changeset
49 }
350b660ef93c precise macosx timer by Dan Christiansen <danchr@daimi.au.dk> and 10l fix by me
alex
parents:
diff changeset
50
350b660ef93c precise macosx timer by Dan Christiansen <danchr@daimi.au.dk> and 10l fix by me
alex
parents:
diff changeset
51 #if 0
350b660ef93c precise macosx timer by Dan Christiansen <danchr@daimi.au.dk> and 10l fix by me
alex
parents:
diff changeset
52 int main() {
350b660ef93c precise macosx timer by Dan Christiansen <danchr@daimi.au.dk> and 10l fix by me
alex
parents:
diff changeset
53 int i;
350b660ef93c precise macosx timer by Dan Christiansen <danchr@daimi.au.dk> and 10l fix by me
alex
parents:
diff changeset
54
350b660ef93c precise macosx timer by Dan Christiansen <danchr@daimi.au.dk> and 10l fix by me
alex
parents:
diff changeset
55 for (i = 0; i < 20; i++) {
350b660ef93c precise macosx timer by Dan Christiansen <danchr@daimi.au.dk> and 10l fix by me
alex
parents:
diff changeset
56 printf("CF relative time:\t%f\n", GetRelativeTime());
350b660ef93c precise macosx timer by Dan Christiansen <danchr@daimi.au.dk> and 10l fix by me
alex
parents:
diff changeset
57 usec_sleep(1000000);
350b660ef93c precise macosx timer by Dan Christiansen <danchr@daimi.au.dk> and 10l fix by me
alex
parents:
diff changeset
58 printf("usleep relative time:\t%f\n", GetRelativeTime());
350b660ef93c precise macosx timer by Dan Christiansen <danchr@daimi.au.dk> and 10l fix by me
alex
parents:
diff changeset
59 usleep(1000000);
350b660ef93c precise macosx timer by Dan Christiansen <danchr@daimi.au.dk> and 10l fix by me
alex
parents:
diff changeset
60 }
350b660ef93c precise macosx timer by Dan Christiansen <danchr@daimi.au.dk> and 10l fix by me
alex
parents:
diff changeset
61 }
350b660ef93c precise macosx timer by Dan Christiansen <danchr@daimi.au.dk> and 10l fix by me
alex
parents:
diff changeset
62 #endif
350b660ef93c precise macosx timer by Dan Christiansen <danchr@daimi.au.dk> and 10l fix by me
alex
parents:
diff changeset
63
350b660ef93c precise macosx timer by Dan Christiansen <danchr@daimi.au.dk> and 10l fix by me
alex
parents:
diff changeset
64