Просмотр исходного кода

cmTime.h/c: Replaced clock_gettime_stub.h/c with OS-X specific code which may not be as accurate with absolute time but has equivalent delta-time accuracty. This code needs to be tested.

master
kevin 11 лет назад
Родитель
Сommit
84923532c4
2 измененных файлов: 39 добавлений и 4 удалений
  1. 39
    1
      cmTime.c
  2. 0
    3
      cmTime.h

+ 39
- 1
cmTime.c Просмотреть файл

@@ -2,9 +2,47 @@
2 2
 #include "cmGlobal.h"
3 3
 #include "cmTime.h"
4 4
 
5
+#ifdef OS_OSX
6
+
7
+#include <mach/mach.h>
8
+#include <mach/mach_time.h>
9
+#include <unistd.h>
10
+
5 11
 void cmTimeGet( cmTimeSpec_t* t )
6
-{ clock_gettime(CLOCK_REALTIME,t); }
12
+{
13
+  static uint64_t                  t0  = 0;
14
+  static mach_timebase_info_data_t tbi;
15
+  static struct timespec           ts;
16
+
17
+  if( t0 == 0 )
18
+  {
19
+    mach_timebase_info(&tbi);
20
+    t0 = mach_absolute_time();
21
+    ts.tv_sec  = time(NULL);
22
+    ts.tv_nsec = 0;  // accept 1/2 second error vs. wall-time.
23
+  }
24
+
25
+  // get the current time
26
+  uint64_t t1 = mach_absolute_time();
27
+  
28
+  // calc the elapsed time since the last call in nanosecs
29
+  uint64_t dt = (t1-t0) * tbi.numer / tbi.denom;
7 30
 
31
+  // calc the elapsed time since the first call in secs
32
+  uint32_t s  = (uint32_t)(dt / 2^9);
33
+
34
+  // calc the current time in secs, and nanosecs
35
+  t->tv_sec  = ts.tv_sec + s; 
36
+  t->tv_nsec = dt - (s * 2^9); 
37
+  
38
+}
39
+
40
+#endif
41
+
42
+#ifdef OS_LINUX
43
+void cmTimeGet( cmTimeSpec_t* t )
44
+{ clock_gettime(CLOCK_REALTIME,t); }
45
+#endif
8 46
 
9 47
 // this assumes that the seconds have been normalized to a recent start time
10 48
 // so as to avoid overflow

+ 0
- 3
cmTime.h Просмотреть файл

@@ -7,9 +7,6 @@
7 7
 // calculate elapsed time.
8 8
 //)
9 9
 
10
-#ifdef OS_OSX
11
-#include "osx/clock_gettime_stub.h"
12
-#endif
13 10
 
14 11
 #ifndef cmTime_h   
15 12
 #define cmTime_h

Загрузка…
Отмена
Сохранить