12 #ifdef HAVE_FEATURES_H
16 #ifdef HAVE_MACH_MACH_TIME_H
17 #include <mach/mach_time.h>
18 #include <mach/clock.h>
19 #include <mach/mach.h>
38 tv.tv_sec = ts.tv_sec;
39 tv.tv_usec = ts.tv_nsec / 1000;
57 LARGE_INTEGER MonotonicClock::getFILETIMEoffset()
70 SystemTimeToFileTime(&s, &f);
71 t.QuadPart = f.dwHighDateTime;
73 t.QuadPart |= f.dwLowDateTime;
83 tv.tv_sec = ts.tv_sec;
84 tv.tv_usec = ts.tv_nsec / 1000;
93 static LARGE_INTEGER offset;
94 static double frequencyToMicroseconds;
95 static int initialized = 0;
96 static BOOL usePerformanceCounter = 0;
99 LARGE_INTEGER performanceFrequency;
101 usePerformanceCounter = QueryPerformanceFrequency(&performanceFrequency);
102 if (usePerformanceCounter) {
103 QueryPerformanceCounter(&offset);
104 frequencyToMicroseconds = (double)performanceFrequency.QuadPart / 1000000.;
106 offset = getFILETIMEoffset();
107 frequencyToMicroseconds = 10.;
110 if (usePerformanceCounter) QueryPerformanceCounter(&t);
112 GetSystemTimeAsFileTime(&f);
113 t.QuadPart = f.dwHighDateTime;
115 t.QuadPart |= f.dwLowDateTime;
118 t.QuadPart -= offset.QuadPart;
119 microseconds = (double)t.QuadPart / frequencyToMicroseconds;
120 t.QuadPart = microseconds;
121 ts.tv_sec = t.QuadPart / 1000000;
122 ts.tv_nsec = (t.QuadPart % 1000000) * 1000;
123 #elif HAVE_MACH_MACH_TIME_H // OS X does not have clock_gettime, use clock_get_time
126 host_get_clock_service(mach_host_self(), REALTIME_CLOCK, &cclock);
127 clock_get_time(cclock, &mts);
128 mach_port_deallocate(mach_task_self(), cclock);
129 ts.tv_sec = mts.tv_sec;
130 ts.tv_nsec = mts.tv_nsec;
132 ::clock_gettime(CLOCK_MONOTONIC, &ts);
138 if ((end.tv_nsec - start.tv_nsec) < 0)
140 diff.tv_sec = end.tv_sec - start.tv_sec - 1;
141 diff.tv_nsec = (1000000000 + end.tv_nsec) - start.tv_nsec;
145 diff.tv_sec = end.tv_sec - start.tv_sec;
146 diff.tv_nsec = end.tv_nsec - start.tv_nsec;