summaryrefslogtreecommitdiffstats
path: root/src/OSSupport/SleepResolutionBooster.h
blob: 956a009ef347fb30ed4da4cf0103e520622acef9 (plain) (blame)
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

// SleepResolutionBooster.h

// Increases the accuracy of Sleep on Windows (GH #5140).

// This file MUST NOT be included from anywhere other than main.cpp.





#ifdef _WIN32

#include <timeapi.h>





static TIMECAPS g_Resolution;





namespace SleepResolutionBooster
{
static void Register()
{
	// Default sleep resolution on Windows isn't accurate enough (GH #5140) so try to boost it:
	if ((timeGetDevCaps(&g_Resolution, sizeof(g_Resolution)) == MMSYSERR_NOERROR) &&
		(timeBeginPeriod(g_Resolution.wPeriodMin) == MMSYSERR_NOERROR))
	{
		return;
	}

	// Max < Min sentinel for failure, to prevent bogus timeEndPeriod calls:
	g_Resolution.wPeriodMax = 0;
	g_Resolution.wPeriodMin = 1;
}

static void Unregister()
{
	if (g_Resolution.wPeriodMax >= g_Resolution.wPeriodMin)
	{
		timeEndPeriod(g_Resolution.wPeriodMin);
	}
}
};  // namespace SleepResolutionBooster

#else

namespace SleepResolutionBooster
{
static void Register() {}

static void Unregister() {}
};  // namespace SleepResolutionBooster

#endif