Skip to content

Commit

Permalink
Swap to a high resolution timer on windows if possible
Browse files Browse the repository at this point in the history
  • Loading branch information
TrentHouliston committed Sep 1, 2024
1 parent b6ac1dd commit cf6e4e8
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/util/precise_sleep.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,17 @@ namespace util {
// Negative for relative time, positive for absolute time
// Measures in 100ns increments so divide by 100
ft.QuadPart = -static_cast<int64_t>(ns.count() / 100);
// Create a waitable timer with as high resolution as possible
::HANDLE timer{};
if (
#ifdef CREATE_WAITABLE_TIMER_HIGH_RESOLUTION
(timer = CreateWaitableTimerEx(NULL, NULL, CREATE_WAITABLE_TIMER_HIGH_RESOLUTION, TIMER_ALL_ACCESS)) == NULL
&&
#endif
(timer = CreateWaitableTimer(NULL, TRUE, NULL)) == NULL) {
throw std::runtime_error("Failed to create waitable timer");
}

::HANDLE timer = ::CreateWaitableTimer(nullptr, TRUE, nullptr);
::SetWaitableTimer(timer, &ft, 0, nullptr, nullptr, 0);
::WaitForSingleObject(timer, INFINITE);
::CloseHandle(timer);
Expand Down

0 comments on commit cf6e4e8

Please sign in to comment.