diff options
-rw-r--r-- | src/thread/PosixCond.hxx | 13 | ||||
-rw-r--r-- | src/thread/WindowsCond.hxx | 8 |
2 files changed, 21 insertions, 0 deletions
diff --git a/src/thread/PosixCond.hxx b/src/thread/PosixCond.hxx index 4e1988ece..2428aa5c7 100644 --- a/src/thread/PosixCond.hxx +++ b/src/thread/PosixCond.hxx @@ -32,6 +32,8 @@ #include "PosixMutex.hxx" +#include <chrono> + #include <sys/time.h> /** @@ -87,6 +89,17 @@ public: return pthread_cond_timedwait(&cond, &mutex.mutex, &ts) == 0; } + + bool timed_wait(PosixMutex &mutex, + std::chrono::steady_clock::duration timeout) { + auto timeout_ms = std::chrono::duration_cast<std::chrono::milliseconds>(timeout).count(); + if (timeout_ms < 0) + timeout_ms = 0; + else if (timeout_ms > std::numeric_limits<unsigned>::max()) + timeout_ms = std::numeric_limits<unsigned>::max(); + + return timed_wait(mutex, timeout_ms); + } }; #endif diff --git a/src/thread/WindowsCond.hxx b/src/thread/WindowsCond.hxx index 2ce7271b9..0419ee3c3 100644 --- a/src/thread/WindowsCond.hxx +++ b/src/thread/WindowsCond.hxx @@ -32,6 +32,8 @@ #include "CriticalSection.hxx" +#include <chrono> + /** * Wrapper for a CONDITION_VARIABLE, backend for the Cond class. */ @@ -59,6 +61,12 @@ public: timeout_ms); } + bool timed_wait(CriticalSection &mutex, + std::chrono::steady_clock::duration timeout) { + auto timeout_ms = std::chrono::duration_cast<std::chrono::milliseconds>(timeout).count(); + return timed_wait(mutex, timeout_ms); + } + void wait(CriticalSection &mutex) { timed_wait(mutex, INFINITE); } |