os/time: Use timePeriod[Begin|End] when sleeping in precise sleeper

One alternative to this is to use the CreateWaitableTimerExW function
with the CREATE_WAITABLE_TIMER_HIGH_RESOLUTION flag. On my systems the
function/flag was either as good or worse then timePeriod[Begin|End].
Setting the state globally or just just around the wait didn't seem to
have an impact on the precision.
This commit is contained in:
Jakob Bornecrantz 2022-11-24 20:25:31 +00:00
parent 14f9492902
commit 2a12f7d661
2 changed files with 12 additions and 0 deletions
src/xrt/auxiliary/os

View file

@ -12,6 +12,11 @@
add_library(aux_os STATIC os_documentation.h os_hid.h os_hid_hidraw.c os_threading.h)
target_link_libraries(aux_os PUBLIC aux-includes xrt-pthreads)
# Only uses normal Windows libraries, doesn't add anything extra.
if(WIN32)
target_link_libraries(aux_os PRIVATE winmm)
endif()
####
# BLE library
#

View file

@ -30,6 +30,7 @@
#elif defined(XRT_OS_WINDOWS)
#include <time.h>
#include <timeapi.h>
#define XRT_HAVE_TIMESPEC
#elif defined(XRT_DOXYGEN)
@ -216,12 +217,14 @@ static inline void
os_precise_sleeper_nanosleep(struct os_precise_sleeper *ops, int32_t nsec)
{
#if defined(XRT_OS_WINDOWS)
timeBeginPeriod(1);
if (ops->timer) {
LARGE_INTEGER timeperiod;
timeperiod.QuadPart = -(nsec / 100);
if (SetWaitableTimer(ops->timer, &timeperiod, 0, NULL, NULL, FALSE)) {
// OK we could set up the timer, now let's wait.
WaitForSingleObject(ops->timer, INFINITE);
timeEndPeriod(1);
return;
}
}
@ -229,6 +232,10 @@ os_precise_sleeper_nanosleep(struct os_precise_sleeper *ops, int32_t nsec)
// If we fall through from an implementation, or there's no implementation needed for a platform, we
// delegate to the regular os_nanosleep.
os_nanosleep(nsec);
#if defined(XRT_OS_WINDOWS)
timeEndPeriod(1);
#endif
}
#if defined(XRT_HAVE_TIMESPEC)