From c71a3419d11c256080fa5ab177e50207f2e17cd1 Mon Sep 17 00:00:00 2001 From: Richard Fuchs Date: Fri, 7 Jul 2023 12:29:43 -0400 Subject: [PATCH] MT#56374 switch from usleep to nanosleep This allows for longer (> 1 second) sleep times reliably. Change-Id: I054f3b5ebf499e208c3618447e7ad1b54e52bfcf --- daemon/helpers.c | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/daemon/helpers.c b/daemon/helpers.c index 9e49062fa..447140b18 100644 --- a/daemon/helpers.c +++ b/daemon/helpers.c @@ -321,6 +321,10 @@ static void thread_looper_helper(void *fp) { #endif static const long long warn_limit_pct = 20; // 20% long long warn_limit_us = interval_us * warn_limit_pct / 100; + struct timespec interval_ts = { + .tv_sec = interval_us / 1000000, + .tv_nsec = (interval_us % 1000000) * 1000, + }; while (!rtpe_shutdown) { gettimeofday(&rtpe_now, NULL); @@ -341,9 +345,18 @@ static void thread_looper_helper(void *fp) { if (ret == TLA_BREAK) break; - thread_cancel_enable(); - usleep(interval_us); - thread_cancel_disable(); + struct timespec sleeptime = interval_ts; + struct timespec remtime; + while (true) { + thread_cancel_enable(); + int res = nanosleep(&sleeptime, &remtime); + thread_cancel_disable(); + if (res == -1 && errno == EINTR) { + sleeptime = remtime; + continue; + } + break; + } } }