From 699ae0aad9547edcd663b962ede161e24a096058 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Sch=C3=BCrmann?= Date: Thu, 30 May 2024 11:55:46 +0200 Subject: [PATCH] util/time: add os_time_nanosleep_until() function Part-of: --- src/util/os_time.c | 16 +++++++++++++++- src/util/os_time.h | 2 ++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/util/os_time.c b/src/util/os_time.c index da8ad7a80b8..209b7ae442c 100644 --- a/src/util/os_time.c +++ b/src/util/os_time.c @@ -60,7 +60,21 @@ os_time_get_nano(void) return ts.tv_nsec + ts.tv_sec*INT64_C(1000000000); } - +void +os_time_nanosleep_until(int64_t deadline) +{ +#if DETECT_OS_LINUX || DETECT_OS_MANAGARM + struct timespec time; + time.tv_sec = deadline / INT64_C(1000000000); + time.tv_nsec = deadline % INT64_C(1000000000); + while (clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &time, &time) == EINTR); +#else + int64_t duration = deadline - os_time_get_nano(); + if (duration > 0) { + os_time_sleep(duration / 1000); + } +#endif +} void os_time_sleep(int64_t usecs) diff --git a/src/util/os_time.h b/src/util/os_time.h index 6ca37eac769..4217ff37b68 100644 --- a/src/util/os_time.h +++ b/src/util/os_time.h @@ -74,6 +74,8 @@ os_localtime(const time_t *timer, struct tm *buf) #endif } +void +os_time_nanosleep_until(int64_t deadline); /* * Sleep.