18
18
#include "mysys_priv.h"
19
19
#include "my_static.h"
20
20
21
- #ifdef __WIN__
21
+ #ifdef _WIN32
22
22
#define OFFSET_TO_EPOC 116444736000000000LL
23
23
static ulonglong query_performance_frequency ;
24
+ typedef void (WINAPI * get_system_time_as_filetime_t )(LPFILETIME );
25
+ static get_system_time_as_filetime_t
26
+ my_GetSystemTimePreciseAsFileTime = GetSystemTimeAsFileTime ;
24
27
#endif
25
28
#ifdef HAVE_LINUX_UNISTD_H
26
29
#include <linux/unistd.h>
@@ -53,7 +56,7 @@ ulonglong my_interval_timer()
53
56
return tp .tv_sec * 1000000000ULL + tp .tv_nsec ;
54
57
#elif defined(HAVE_GETHRTIME )
55
58
return gethrtime ();
56
- #elif defined(__WIN__ )
59
+ #elif defined(_WIN32 )
57
60
LARGE_INTEGER t_cnt ;
58
61
if (query_performance_frequency )
59
62
{
@@ -65,7 +68,7 @@ ulonglong my_interval_timer()
65
68
else
66
69
{
67
70
ulonglong newtime ;
68
- GetSystemTimeAsFileTime ((FILETIME * )& newtime );
71
+ my_GetSystemTimePreciseAsFileTime ((FILETIME * )& newtime );
69
72
return newtime * 100ULL ;
70
73
}
71
74
#else
@@ -82,11 +85,10 @@ ulonglong my_interval_timer()
82
85
my_hrtime_t my_hrtime ()
83
86
{
84
87
my_hrtime_t hrtime ;
85
- #if defined(__WIN__ )
88
+ #if defined(_WIN32 )
86
89
ulonglong newtime ;
87
- GetSystemTimeAsFileTime ((FILETIME * )& newtime );
88
- newtime -= OFFSET_TO_EPOC ;
89
- hrtime .val = newtime /10 ;
90
+ my_GetSystemTimePreciseAsFileTime ((FILETIME * )& newtime );
91
+ hrtime .val = (newtime - OFFSET_TO_EPOC )/10 ;
90
92
#elif defined(HAVE_CLOCK_GETTIME )
91
93
struct timespec tp ;
92
94
clock_gettime (CLOCK_REALTIME , & tp );
@@ -100,14 +102,39 @@ my_hrtime_t my_hrtime()
100
102
return hrtime ;
101
103
}
102
104
105
+ #ifdef _WIN32
106
+
107
+ /*
108
+ Low accuracy, "coarse" timer.
109
+ Has lower latency than my_hrtime(). Used in situations, where microsecond
110
+ precision is not needed, e.g in Windows pthread_cond_timedwait, where POSIX
111
+ interface needs nanoseconds, yet the underlying Windows function only
112
+ accepts millisecons.
113
+ */
114
+ my_hrtime_t my_hrtime_coarse ()
115
+ {
116
+ my_hrtime_t hrtime ;
117
+ ulonglong t ;
118
+ GetSystemTimeAsFileTime ((FILETIME * )& t );
119
+ hrtime .val = (t - OFFSET_TO_EPOC )/10 ;
120
+ return hrtime ;
121
+ }
122
+
123
+ #endif
103
124
104
125
void my_time_init ()
105
126
{
106
- #ifdef __WIN__
127
+ #ifdef _WIN32
107
128
compile_time_assert (sizeof (LARGE_INTEGER ) ==
108
129
sizeof (query_performance_frequency ));
109
130
if (QueryPerformanceFrequency ((LARGE_INTEGER * )& query_performance_frequency ) == 0 )
110
131
query_performance_frequency = 0 ;
132
+
133
+ get_system_time_as_filetime_t f = (get_system_time_as_filetime_t )
134
+ GetProcAddress (GetModuleHandle ("kernel32" ),
135
+ "GetSystemTimePreciseAsFileTime" );
136
+ if (f )
137
+ my_GetSystemTimePreciseAsFileTime = f ;
111
138
#endif
112
139
}
113
140
0 commit comments