From 9eeb07508936523412c1d2a4daf7f5c31be5eea1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ion=20Gazta=C3=B1aga?= Date: Tue, 9 Jan 2024 02:04:22 +0100 Subject: [PATCH] Add a more robust nanosleep use taking EINTR into account --- .../interprocess/detail/os_thread_functions.hpp | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/include/boost/interprocess/detail/os_thread_functions.hpp b/include/boost/interprocess/detail/os_thread_functions.hpp index 498dde95..4dbcfb74 100644 --- a/include/boost/interprocess/detail/os_thread_functions.hpp +++ b/include/boost/interprocess/detail/os_thread_functions.hpp @@ -44,6 +44,7 @@ # include # include # include +# include # ifdef BOOST_INTERPROCESS_BSD_DERIVATIVE //Some *BSD systems (OpenBSD & NetBSD) need sys/param.h before sys/sysctl.h, whereas //others (FreeBSD & Darwin) need sys/types.h @@ -422,7 +423,12 @@ inline void thread_sleep_tick() //Sleep for the half of the tick time rqt.tv_sec = 0; rqt.tv_nsec = (long)get_system_tick_ns()/2; - ::nanosleep(&rqt, 0); + + struct timespec rmn; + while (0 != ::nanosleep(&rqt, &rmn) && errno == EINTR) { + rqt.tv_sec = rmn.tv_sec; + rqt.tv_nsec = rmn.tv_nsec; + } } inline void thread_sleep_ms(unsigned int ms) @@ -430,7 +436,12 @@ inline void thread_sleep_ms(unsigned int ms) struct timespec rqt; rqt.tv_sec = ms/1000u; rqt.tv_nsec = (ms%1000u)*1000000u; - ::nanosleep(&rqt, 0); + + struct timespec rmn; + while (0 != ::nanosleep(&rqt, &rmn) && errno == EINTR) { + rqt.tv_sec = rmn.tv_sec; + rqt.tv_nsec = rmn.tv_nsec; + } } //systemwide thread