thread/*Cond: remove wait() overloads without std::unique_lock<>

This commit is contained in:
Max Kellermann 2019-05-07 19:31:00 +02:00
parent 0a0cc66e8f
commit ad4ca0c449
2 changed files with 16 additions and 39 deletions

View File

@ -71,17 +71,13 @@ public:
pthread_cond_broadcast(&cond); pthread_cond_broadcast(&cond);
} }
void wait(PosixMutex &mutex) noexcept { void wait(std::unique_lock<PosixMutex> &lock) noexcept {
pthread_cond_wait(&cond, &mutex.mutex); pthread_cond_wait(&cond, &lock.mutex()->mutex);
}
template<typename M>
void wait(std::unique_lock<M> &lock) noexcept {
wait(*lock.mutex());
} }
private: private:
bool wait_for(PosixMutex &mutex, uint_least32_t timeout_us) noexcept { bool wait_for(std::unique_lock<PosixMutex> &lock,
uint_least32_t timeout_us) noexcept {
struct timeval now; struct timeval now;
gettimeofday(&now, nullptr); gettimeofday(&now, nullptr);
@ -94,11 +90,12 @@ private:
ts.tv_sec++; ts.tv_sec++;
} }
return pthread_cond_timedwait(&cond, &mutex.mutex, &ts) == 0; return pthread_cond_timedwait(&cond, &lock.mutex()->mutex, &ts) == 0;
} }
public: public:
bool wait_for(PosixMutex &mutex, template<typename M>
bool wait_for(std::unique_lock<M> &lock,
std::chrono::steady_clock::duration timeout) noexcept { std::chrono::steady_clock::duration timeout) noexcept {
auto timeout_us = std::chrono::duration_cast<std::chrono::microseconds>(timeout).count(); auto timeout_us = std::chrono::duration_cast<std::chrono::microseconds>(timeout).count();
if (timeout_us < 0) if (timeout_us < 0)
@ -106,13 +103,7 @@ public:
else if (timeout_us > std::numeric_limits<uint_least32_t>::max()) else if (timeout_us > std::numeric_limits<uint_least32_t>::max())
timeout_us = std::numeric_limits<uint_least32_t>::max(); timeout_us = std::numeric_limits<uint_least32_t>::max();
return wait_for(mutex, timeout_us); return wait_for(lock, 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);
} }
}; };

View File

@ -57,32 +57,18 @@ public:
WakeAllConditionVariable(&cond); WakeAllConditionVariable(&cond);
} }
private: void wait(std::unique_lock<CriticalSection> &lock) noexcept {
bool wait_for(CriticalSection &mutex, DWORD timeout_ms) noexcept { SleepConditionVariableCS(&cond,
return SleepConditionVariableCS(&cond, &mutex.critical_section, &lock.mutex()->critical_section,
timeout_ms); INFINITE);
} }
public: bool wait_for(std::unique_lock<CriticalSection> &lock,
bool wait_for(CriticalSection &mutex,
std::chrono::steady_clock::duration timeout) noexcept { std::chrono::steady_clock::duration timeout) noexcept {
auto timeout_ms = std::chrono::duration_cast<std::chrono::milliseconds>(timeout).count(); auto timeout_ms = std::chrono::duration_cast<std::chrono::milliseconds>(timeout).count();
return wait_for(mutex, timeout_ms); return SleepConditionVariableCS(&cond,
} &lock.mutex()->critical_section,
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());
} }
}; };