30 #ifndef THREAD_POSIX_COND_HXX
31 #define THREAD_POSIX_COND_HXX
49 constexpr
PosixCond():cond(PTHREAD_COND_INITIALIZER) {}
54 pthread_cond_init(&cond,
nullptr);
58 pthread_cond_destroy(&cond);
62 PosixCond(
const PosixCond &other) =
delete;
63 PosixCond &
operator=(
const PosixCond &other) =
delete;
66 pthread_cond_signal(&cond);
70 pthread_cond_broadcast(&cond);
74 pthread_cond_wait(&cond, &mutex.mutex);
78 bool timed_wait(
PosixMutex &mutex,
unsigned timeout_ms) {
80 gettimeofday(&now,
nullptr);
83 ts.tv_sec = now.tv_sec + timeout_ms / 1000;
84 ts.tv_nsec = (now.tv_usec + (timeout_ms % 1000) * 1000) * 1000;
86 if (ts.tv_nsec >= 1000000000) {
87 ts.tv_nsec -= 1000000000;
91 return pthread_cond_timedwait(&cond, &mutex.mutex, &ts) == 0;
96 std::chrono::steady_clock::duration timeout) {
97 auto timeout_ms = std::chrono::duration_cast<std::chrono::milliseconds>(timeout).count();
100 else if (timeout_ms > std::numeric_limits<unsigned>::max())
101 timeout_ms = std::numeric_limits<unsigned>::max();
103 return timed_wait(mutex, timeout_ms);
bool timed_wait(PosixMutex &mutex, std::chrono::steady_clock::duration timeout)
Low-level wrapper for a pthread_cond_t.
void wait(PosixMutex &mutex)
Low-level wrapper for a pthread_mutex_t.
PosixCond & operator=(const PosixCond &other)=delete