Skip to content

Commit

Permalink
Add a more robust nanosleep use taking EINTR into account
Browse files Browse the repository at this point in the history
  • Loading branch information
igaztanaga committed Jan 9, 2024
1 parent 04fa948 commit 9eeb075
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions include/boost/interprocess/detail/os_thread_functions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
# include <unistd.h>
# include <sched.h>
# include <time.h>
# include <errno.h>
# 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
Expand Down Expand Up @@ -422,15 +423,25 @@ 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)
{
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
Expand Down

0 comments on commit 9eeb075

Please sign in to comment.