From 0a6c179026654d8c672ce782feaa86a767cbbfe6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kacper=20Michaj=C5=82ow?= Date: Sat, 28 Oct 2023 21:17:28 +0200 Subject: [PATCH] osdep/timer-linux: check clock availability on init --- osdep/timer-linux.c | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/osdep/timer-linux.c b/osdep/timer-linux.c index bcae787645..559a496636 100644 --- a/osdep/timer-linux.c +++ b/osdep/timer-linux.c @@ -18,10 +18,15 @@ * License along with mpv. If not, see . */ +#include #include #include + +#include "common/common.h" #include "timer.h" +static clockid_t clk_id; + void mp_sleep_ns(int64_t ns) { if (ns < 0) @@ -35,15 +40,25 @@ void mp_sleep_ns(int64_t ns) uint64_t mp_raw_time_ns(void) { struct timespec tp = {0}; - int ret = -1; -#if defined(CLOCK_MONOTONIC_RAW) - ret = clock_gettime(CLOCK_MONOTONIC_RAW, &tp); -#endif - if (ret) - clock_gettime(CLOCK_MONOTONIC, &tp); + clock_gettime(clk_id, &tp); return MP_TIME_S_TO_NS(tp.tv_sec) + tp.tv_nsec; } void mp_raw_time_init(void) { + static const clockid_t clock_ids[] = { +#ifdef CLOCK_MONOTONIC_RAW + CLOCK_MONOTONIC_RAW, +#endif + CLOCK_MONOTONIC, + }; + + struct timespec tp; + for (int i = 0; i < MP_ARRAY_SIZE(clock_ids); ++i) { + clk_id = clock_ids[i]; + if (!clock_gettime(clk_id, &tp)) + return; + } + fputs("No clock source available!\n", stderr); + abort(); }