on debe277 help sign in
115 lines 2.8 kB history raw
1/*
2 * Code taken from the Haskell "clock" package.
3 *
4 * Copyright (c) 2009-2012, Cetin Sert
5 * Copyright (c) 2010, Eugene Kirpichov
6 */
8#ifdef _WIN32
9#include <windows.h>
11#if defined(_MSC_VER) || defined(_MSC_EXTENSIONS)
12 #define U64(x) x##Ui64
13#else
14 #define U64(x) x##ULL
15#endif
17#define DELTA_EPOCH_IN_100NS U64(116444736000000000)
19static long ticks_to_nanos(LONGLONG subsecond_time, LONGLONG frequency)
21 return (long)((1e9 * subsecond_time) / frequency);
24static ULONGLONG to_quad_100ns(FILETIME ft)
26 ULARGE_INTEGER li;
27 li.LowPart = ft.dwLowDateTime;
28 li.HighPart = ft.dwHighDateTime;
29 return li.QuadPart;
32static void to_timespec_from_100ns(ULONGLONG t_100ns, long long *t)
34 t[0] = (long)(t_100ns / 10000000UL);
35 t[1] = 100*(long)(t_100ns % 10000000UL);
38void clock_gettime_win32_monotonic(long long* t)
40 LARGE_INTEGER time;
41 LARGE_INTEGER frequency;
42 QueryPerformanceCounter(&time);
43 QueryPerformanceFrequency(&frequency);
44 // seconds
45 t[0] = time.QuadPart / frequency.QuadPart;
46 // nanos =
47 t[1] = ticks_to_nanos(time.QuadPart % frequency.QuadPart, frequency.QuadPart);
50void clock_gettime_win32_realtime(long long* t)
52 FILETIME ft;
53 ULONGLONG tmp;
55 GetSystemTimeAsFileTime(&ft);
57 tmp = to_quad_100ns(ft);
58 tmp -= DELTA_EPOCH_IN_100NS;
60 to_timespec_from_100ns(tmp, t);
63void clock_gettime_win32_processtime(long long* t)
65 FILETIME creation_time, exit_time, kernel_time, user_time;
66 ULONGLONG time;
68 GetProcessTimes(GetCurrentProcess(), &creation_time, &exit_time, &kernel_time, &user_time);
69 // Both kernel and user, acc. to http://www.opengroup.org/onlinepubs/009695399/basedefs/xbd_chap03.html#tag_03_117
71 time = to_quad_100ns(user_time) + to_quad_100ns(kernel_time);
72 to_timespec_from_100ns(time, t);
75void clock_gettime_win32_threadtime(long long* t)
77 FILETIME creation_time, exit_time, kernel_time, user_time;
78 ULONGLONG time;
80 GetThreadTimes(GetCurrentThread(), &creation_time, &exit_time, &kernel_time, &user_time);
81 // Both kernel and user, acc. to http://www.opengroup.org/onlinepubs/009695399/basedefs/xbd_chap03.html#tag_03_117
83 time = to_quad_100ns(user_time) + to_quad_100ns(kernel_time);
84 to_timespec_from_100ns(time, t);
87void clock_getres_win32_monotonic(long long* t)
89 LARGE_INTEGER frequency;
90 QueryPerformanceFrequency(&frequency);
92 ULONGLONG resolution = U64(1000000000)/frequency.QuadPart;
93 t[0] = resolution / U64(1000000000);
94 t[1] = resolution % U64(1000000000);
97void clock_getres_win32_realtime(long long* t)
99 t[0] = 0;
100 t[1] = 100;
103void clock_getres_win32_processtime(long long* t)
105 t[0] = 0;
106 t[1] = 100;
109void clock_getres_win32_threadtime(long long* t)
111 t[0] = 0;
112 t[1] = 100;
115#endif /* _WIN32 */