comparison osdep/timer-macosx.c @ 9465:350b660ef93c

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