The open source OpenXR runtime

u/wait: Add wait helper

authored by

Jakob Bornecrantz and committed by
Ryan Pavlik
a0d63d2f 29a43797

+55
+55
src/xrt/auxiliary/util/u_wait.h
··· 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 + */ 39 + static inline void 40 + u_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 + }