From 8dc3c0e2db6ce4f571d5b763c396bdab49f94889 Mon Sep 17 00:00:00 2001 From: Bent Bisballe Nyeng Date: Sat, 7 Jan 2017 17:18:23 +0100 Subject: Retry semaphore timedwait on interrupt. --- src/semaphore.cc | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/semaphore.cc b/src/semaphore.cc index 51014b3..a7c8e74 100644 --- a/src/semaphore.cc +++ b/src/semaphore.cc @@ -30,6 +30,8 @@ #include #include #include +#include +#include #include "platform.h" @@ -134,9 +136,18 @@ bool Semaphore::wait(const std::chrono::milliseconds& timeout) ts.tv_sec += 1; } +again: int ret = sem_timedwait(&prv->semaphore, &ts); if(ret < 0) { + if(errno == EINTR) + { + // The timed wait were interrupted prematurely so we need to wait some + // more. To prevent an infinite loop-like behaviour we make a short sleep. + std::this_thread::sleep_for(std::chrono::milliseconds(1)); + goto again; + } + if(errno == ETIMEDOUT) { return false; -- cgit v1.2.3