21 #include <bits/time.h>
32 #define CLOCK_REALTIME 0
33 #define CLOCK_MONOTONIC 1
34 #define CLOCK_PROCESS_CPUTIME_ID 2
37 inline static LARGE_INTEGER
51 SystemTimeToFileTime(&s, &f);
52 t.QuadPart = f.dwHighDateTime;
54 t.QuadPart |= f.dwLowDateTime;
59 static inline int clock_gettime(
int clock_type,
struct timespec* p_ts)
64 static LARGE_INTEGER offset;
65 static double frequencyToNanoseconds;
66 static int initialized = 0;
67 static BOOL usePerformanceCounter = 0;
70 LARGE_INTEGER performanceFrequency;
72 usePerformanceCounter = QueryPerformanceFrequency(&performanceFrequency);
73 if (usePerformanceCounter) {
74 QueryPerformanceCounter(&offset);
75 frequencyToNanoseconds = (double)performanceFrequency.QuadPart / 1000000000.;
77 offset = getFILETIMEoffset();
78 frequencyToNanoseconds = 10000.;
82 if (usePerformanceCounter) {
83 QueryPerformanceCounter(&t);
86 GetSystemTimeAsFileTime(&f);
87 t.QuadPart = f.dwHighDateTime;
89 t.QuadPart |= f.dwLowDateTime;
92 t.QuadPart -= offset.QuadPart;
93 nanoseconds = (double)t.QuadPart / frequencyToNanoseconds;
94 t.QuadPart = (LONGLONG)nanoseconds;
95 p_ts->tv_sec = t.QuadPart / 1000000000;
96 p_ts->tv_nsec = t.QuadPart % 1000000000;
108 #ifdef OSX // Macs aren't posix compliant
109 #include <sys/time.h>
110 #include <mach/mach.h>
111 #include <mach/mach_time.h>
114 inline static uint64_t
115 TIMESPEC_TO_MS(
struct timespec *ts )
117 uint64_t r1 = ts->tv_nsec;
119 uint64_t r2 = ts->tv_sec;
126 inline static uint64_t
142 uint64_t date = mach_absolute_time();
149 return date / 1000000;
154 clock_gettime( CLOCK_MONOTONIC, &ts);
155 return ( TIMESPEC_TO_MS( &ts ) );
160 static inline uint64_t
163 struct timespec ts = { 0, 0};
164 clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts);
165 uint64_t micro = (ts.tv_sec * 1000000) + (ts.tv_nsec / 1000);
169 inline static uint64_t
170 TIMESPEC_TO_US(
struct timespec *ts )
172 uint64_t r1 = ts->tv_nsec;
174 uint64_t r2 = ts->tv_sec;
179 inline static uint64_t
182 uint64_t date = mach_absolute_time();
193 clock_gettime( CLOCK_MONOTONIC, &ts);
194 return ( TIMESPEC_TO_US( &ts ) );
198 inline static uint64_t
199 cf_clock_getabsolute() {
202 gettimeofday(&tv, NULL);
204 uint64_t realtime = tv.tv_sec * 1000000 + tv.tv_usec;
208 clock_gettime(CLOCK_REALTIME, &ts);
209 return(TIMESPEC_TO_MS(&ts));
220 #define CITRUSLEAF_EPOCH 1262304000
222 static inline uint32_t
226 clock_gettime(CLOCK_REALTIME, &ts);