thread/Cond: add wait() overload which takes a unique_lock<>
Just like std::condition_variable, which however has no way to specify the std::mutex directly.
This commit is contained in:
@@ -33,6 +33,7 @@
|
||||
#include "PosixMutex.hxx"
|
||||
|
||||
#include <chrono>
|
||||
#include <mutex>
|
||||
|
||||
#include <sys/time.h>
|
||||
|
||||
@@ -74,6 +75,11 @@ public:
|
||||
pthread_cond_wait(&cond, &mutex.mutex);
|
||||
}
|
||||
|
||||
template<typename M>
|
||||
void wait(std::unique_lock<M> &lock) noexcept {
|
||||
wait(*lock.mutex());
|
||||
}
|
||||
|
||||
private:
|
||||
bool wait_for(PosixMutex &mutex, uint_least32_t timeout_us) noexcept {
|
||||
struct timeval now;
|
||||
@@ -102,6 +108,12 @@ public:
|
||||
|
||||
return wait_for(mutex, timeout_us);
|
||||
}
|
||||
|
||||
template<typename M>
|
||||
bool wait_for(std::unique_lock<M> &lock,
|
||||
std::chrono::steady_clock::duration timeout) noexcept {
|
||||
return wait_for(*lock.mutex(), timeout);
|
||||
}
|
||||
};
|
||||
|
||||
#endif
|
||||
|
@@ -33,6 +33,7 @@
|
||||
#include "CriticalSection.hxx"
|
||||
|
||||
#include <chrono>
|
||||
#include <mutex>
|
||||
|
||||
/**
|
||||
* Wrapper for a CONDITION_VARIABLE, backend for the Cond class.
|
||||
@@ -69,9 +70,20 @@ public:
|
||||
return wait_for(mutex, timeout_ms);
|
||||
}
|
||||
|
||||
template<typename M>
|
||||
bool wait_for(std::unique_lock<M> &lock,
|
||||
std::chrono::steady_clock::duration timeout) noexcept {
|
||||
return wait_for(*lock.mutex(), timeout);
|
||||
}
|
||||
|
||||
void wait(CriticalSection &mutex) noexcept {
|
||||
wait_for(mutex, INFINITE);
|
||||
}
|
||||
|
||||
template<typename M>
|
||||
void wait(std::unique_lock<M> &lock) noexcept {
|
||||
wait(*lock.mutex());
|
||||
}
|
||||
};
|
||||
|
||||
#endif
|
||||
|
Reference in New Issue
Block a user