From c7f2f9fa1f38a74e3d076dd2c147abc8a8dfff83 Mon Sep 17 00:00:00 2001 From: Lorenz Junglas Date: Mon, 12 Oct 2020 16:27:31 +0200 Subject: [PATCH] Fix nanosleep usage nanosleep returns 0 or -1 not the error code. The error code "EINTR" (if encountered) is placed in errno, in which case nanosleep can be safely recalled with the remaining time. This is required, so that nanosleep continues if the calling thread is interrupted by a signal. See manpage nanosleep(2) for additional details. (cherry picked from commit 1107c7f327f6d14010b4bd45b25e3c47f89b9948) --- drivers/unix/os_unix.cpp | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/drivers/unix/os_unix.cpp b/drivers/unix/os_unix.cpp index 10ebf6bb1da..4bb56ab931c 100644 --- a/drivers/unix/os_unix.cpp +++ b/drivers/unix/os_unix.cpp @@ -257,9 +257,11 @@ OS::TimeZoneInfo OS_Unix::get_time_zone_info() const { } void OS_Unix::delay_usec(uint32_t p_usec) const { - - struct timespec rem = { static_cast(p_usec / 1000000), (static_cast(p_usec) % 1000000) * 1000 }; - while (nanosleep(&rem, &rem) == EINTR) { + struct timespec requested = { static_cast(p_usec / 1000000), (static_cast(p_usec) % 1000000) * 1000 }; + struct timespec remaining; + while (nanosleep(&requested, &remaining) == -1 && errno == EINTR) { + requested.tv_sec = remaining.tv_sec; + requested.tv_nsec = remaining.tv_nsec; } } uint64_t OS_Unix::get_ticks_usec() const {