summaryrefslogtreecommitdiffstats
path: root/src/OSSupport/SleepResolutionBooster.h
diff options
context:
space:
mode:
authorTiger Wang <ziwei.tiger@outlook.com>2021-04-21 17:07:48 +0200
committerGitHub <noreply@github.com>2021-04-21 17:07:48 +0200
commit1100b04b59b461f1a2cc3dfe7b53b5473daa7992 (patch)
treeef38a4a731e0c03c6f923708bac0249560ab611f /src/OSSupport/SleepResolutionBooster.h
parentResets ticks alive on death (#5197) (diff)
downloadcuberite-1100b04b59b461f1a2cc3dfe7b53b5473daa7992.tar
cuberite-1100b04b59b461f1a2cc3dfe7b53b5473daa7992.tar.gz
cuberite-1100b04b59b461f1a2cc3dfe7b53b5473daa7992.tar.bz2
cuberite-1100b04b59b461f1a2cc3dfe7b53b5473daa7992.tar.lz
cuberite-1100b04b59b461f1a2cc3dfe7b53b5473daa7992.tar.xz
cuberite-1100b04b59b461f1a2cc3dfe7b53b5473daa7992.tar.zst
cuberite-1100b04b59b461f1a2cc3dfe7b53b5473daa7992.zip
Diffstat (limited to 'src/OSSupport/SleepResolutionBooster.h')
-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