Cuberite
A lightweight, fast and extensible game server for Minecraft
SleepResolutionBooster.h
Go to the documentation of this file.
1 
2 // SleepResolutionBooster.h
3 
4 // Increases the accuracy of Sleep on Windows (GH #5140).
5 
6 // This file MUST NOT be included from anywhere other than main.cpp.
7 
8 
9 
10 
11 
12 #ifdef _WIN32
13 
14 #include <timeapi.h>
15 
16 
17 
18 
19 
20 static TIMECAPS g_Resolution;
21 
22 
23 
24 
25 
26 namespace SleepResolutionBooster
27 {
28  static void Register()
29  {
30  // Default sleep resolution on Windows isn't accurate enough (GH #5140) so try to boost it:
31  if (
32  (timeGetDevCaps(&g_Resolution, sizeof(g_Resolution)) == MMSYSERR_NOERROR) &&
33  (timeBeginPeriod(g_Resolution.wPeriodMin) == MMSYSERR_NOERROR)
34  )
35  {
36  return;
37  }
38 
39  // Max < Min sentinel for failure, to prevent bogus timeEndPeriod calls:
40  g_Resolution.wPeriodMax = 0;
41  g_Resolution.wPeriodMin = 1;
42  }
43 
44  static void Unregister()
45  {
46  if (g_Resolution.wPeriodMax >= g_Resolution.wPeriodMin)
47  {
48  timeEndPeriod(g_Resolution.wPeriodMin);
49  }
50  }
51 };
52 
53 #else
54 
56 {
57  static void Register()
58  {
59  }
60 
61  static void Unregister()
62  {
63  }
64 };
65 
66 #endif