summaryrefslogtreecommitdiffstats
path: root/src/OSSupport/SleepResolutionBooster.h
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/OSSupport/SleepResolutionBooster.h66
1 files changed, 66 insertions, 0 deletions
diff --git a/src/OSSupport/SleepResolutionBooster.h b/src/OSSupport/SleepResolutionBooster.h
new file mode 100644
index 000000000..f090f6963
--- /dev/null
+++ b/src/OSSupport/SleepResolutionBooster.h
@@ -0,0 +1,66 @@
+
+// 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);
+ }
+ }
+};
+
+#else
+
+namespace SleepResolutionBooster
+{
+ static void Register()
+ {
+ }
+
+ static void Unregister()
+ {
+ }
+};
+
+#endif