diff options
author | Bent Bisballe Nyeng <deva@aasimon.org> | 2017-01-07 17:18:23 +0100 |
---|---|---|
committer | Bent Bisballe Nyeng <deva@aasimon.org> | 2017-01-07 17:18:23 +0100 |
commit | 8dc3c0e2db6ce4f571d5b763c396bdab49f94889 (patch) | |
tree | 7554bfab71d4257bd3ae675cfdd80192561f050e /src | |
parent | 0f9b9483ae215e9ba1714b634903d4b22ec00c58 (diff) |
Retry semaphore timedwait on interrupt.
Diffstat (limited to 'src')
-rw-r--r-- | src/semaphore.cc | 11 |
1 files changed, 11 insertions, 0 deletions
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 <limits> #include <assert.h> #include <string.h> +#include <chrono> +#include <thread> #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; |