thread/Cond: add timed_wait() overload with std::chrono support

This commit is contained in:
Max Kellermann 2016-12-28 21:47:04 +01:00
parent 249e8d59cb
commit 871ba5a488
2 changed files with 21 additions and 0 deletions

View File

@ -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

View File

@ -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);
}