-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.c
108 lines (95 loc) · 3.23 KB
/
util.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/*
* clockperf
*
* Copyright (c) 2016-2021, Steven Noonan <[email protected]>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
*/
#include "prefix.h"
#include "util.h"
#ifdef TARGET_OS_WINDOWS
#ifndef CREATE_WAITABLE_TIMER_HIGH_RESOLUTION
#define CREATE_WAITABLE_TIMER_HIGH_RESOLUTION 0x00000002
#endif
static HANDLE s_timer;
typedef LONG NTSTATUS;
typedef NTSTATUS (NTAPI *NtSetTimerResolution)(ULONG DesiredResolution, BOOLEAN SetResolution, PULONG CurrentResolution);
typedef NTSTATUS (NTAPI *NtQueryTimerResolution)(PULONG MinimumResolution, PULONG MaximumResolution, PULONG CurrentResolution);
#endif
int thread_sleep(unsigned long usec)
{
#if defined(TARGET_OS_WINDOWS)
if (s_timer) {
long long nt_ticks = usec * 10;
LARGE_INTEGER li;
li.QuadPart = -nt_ticks;
if(!SetWaitableTimer(s_timer, &li, 0, NULL, NULL, FALSE)){
return 1;
}
WaitForSingleObject(s_timer, INFINITE);
} else {
usec /= 1000;
if (usec < 1)
usec = 1;
Sleep(usec);
}
return 0;
#else
return usleep(usec);
#endif
}
void timers_init(void)
{
#ifdef TARGET_OS_WINDOWS
NtSetTimerResolution pNtSetTimerResolution;
NtQueryTimerResolution pNtQueryTimerResolution;
HMODULE hNtDll = GetModuleHandleA("ntdll.dll");
if (!hNtDll)
return;
pNtSetTimerResolution = (NtSetTimerResolution)GetProcAddress(hNtDll, "NtSetTimerResolution");
pNtQueryTimerResolution = (NtQueryTimerResolution)GetProcAddress(hNtDll, "NtQueryTimerResolution");
if (pNtSetTimerResolution && pNtQueryTimerResolution)
{
NTSTATUS result;
ULONG ulMinimum = 0, ulMaximum = 0, ulCurrent = 0;
result = pNtQueryTimerResolution(&ulMinimum, &ulMaximum, &ulCurrent);
if (result != ERROR_SUCCESS)
return;
result = pNtSetTimerResolution(ulMaximum, TRUE, &ulCurrent);
if (result != ERROR_SUCCESS)
return;
result = pNtQueryTimerResolution(&ulMinimum, &ulMaximum, &ulCurrent);
if (result != ERROR_SUCCESS)
return;
}
/* Disabled for now, need to understand why a waitable timer would fire too
* early (e.g. run with --drift and you'll see it fire multiple times per
* second)
*/
#if 0
s_timer = CreateWaitableTimerEx(
NULL, NULL,
CREATE_WAITABLE_TIMER_MANUAL_RESET | CREATE_WAITABLE_TIMER_HIGH_RESOLUTION,
TIMER_ALL_ACCESS);
#endif
#endif
}
void timers_destroy(void)
{
#ifdef TARGET_OS_WINDOWS
if (s_timer)
CloseHandle(s_timer);
s_timer = NULL;
#endif
}