Monado OpenXR Runtime
u_wait.h
Go to the documentation of this file.
1// Copyright 2022, Collabora, Ltd.
2// SPDX-License-Identifier: BSL-1.0
3/*!
4 * @file
5 * @brief Tiny file to implement precise waiting functions.
6 * @author Jakob Bornecrantz <jakob@collabora.com>
7 * @ingroup aux_util
8 */
9
10#pragma once
11
12#include "xrt/xrt_config_os.h"
13#include "os/os_time.h"
14
15#if defined(XRT_DOXYGEN)
16
17/*!
18 * OS specific tweak to wait time.
19 *
20 * @todo Measure on Windows.
21 * @ingroup aux_util
22 */
23#define U_WAIT_MEASURED_SCHEDULER_LATENCY_NS (uint64_t)(0)
24
25#elif defined(XRT_OS_LINUX) || defined(XRT_OS_ANDROID)
26#define U_WAIT_MEASURED_SCHEDULER_LATENCY_NS (uint64_t)(50 * 1000)
27#elif defined(XRT_OS_WINDOWS)
28#define U_WAIT_MEASURED_SCHEDULER_LATENCY_NS (uint64_t)(0)
29#else
30#error "Unsupported platform!"
31#endif
32
33
34/*!
35 * Waits until the given time using the @ref os_precise_sleeper.
36 *
37 * @ingroup aux_util
38 */
39static inline void
40u_wait_until(struct os_precise_sleeper *sleeper, uint64_t until_ns)
41{
42 uint64_t now_ns = os_monotonic_get_ns();
43
44 // Lets hope its not to late.
45 bool fuzzy_in_the_past = time_is_less_then_or_within_range(until_ns, now_ns, U_TIME_1MS_IN_NS);
46
47 // When we should wake up is in the past:ish.
48 if (fuzzy_in_the_past) {
49 return;
50 }
51
52 // Sufficiently in the future.
53 uint32_t delay = (uint32_t)(until_ns - now_ns - U_WAIT_MEASURED_SCHEDULER_LATENCY_NS);
54 os_precise_sleeper_nanosleep(sleeper, delay);
55}
static int64_t os_monotonic_get_ns(void)
Return a monotonic clock in nanoseconds.
Definition: os_time.h:309
static bool time_is_less_then_or_within_range(timepoint_ns a, timepoint_ns b, int64_t range)
Fuzzy comparisons.
Definition: u_time.h:164
static void u_wait_until(struct os_precise_sleeper *sleeper, uint64_t until_ns)
Waits until the given time using the os_precise_sleeper.
Definition: u_wait.h:40
#define U_WAIT_MEASURED_SCHEDULER_LATENCY_NS
OS specific tweak to wait time.
Definition: u_wait.h:23
Wrapper around OS native time functions.
Definition: os_time.h:208
static void os_precise_sleeper_nanosleep(struct os_precise_sleeper *ops, int32_t nsec)
Sleep the given number of nanoseconds, trying harder to be precise.
Definition: os_time.h:236
#define U_TIME_1MS_IN_NS
The number of nanoseconds in a millisecond.
Definition: u_time.h:54
Auto detect OS and certain features.