Commit 90951670 authored by Max Kellermann's avatar Max Kellermann

thread/*Cond: add wait_for() overload with predicate

parent ad4ca0c4
...@@ -60,11 +60,7 @@ public: ...@@ -60,11 +60,7 @@ public:
private: private:
bool LockWaitFinished() noexcept { bool LockWaitFinished() noexcept {
std::unique_lock<Mutex> lock(mutex); std::unique_lock<Mutex> lock(mutex);
while (!finished) return cond.wait_for(lock, timeout, [this]{ return finished; });
if (!cond.wait_for(lock, timeout))
return false;
return true;
} }
/** /**
......
...@@ -105,6 +105,19 @@ public: ...@@ -105,6 +105,19 @@ public:
return wait_for(lock, timeout_us); return wait_for(lock, timeout_us);
} }
template<typename M, typename P>
bool wait_for(std::unique_lock<M> &lock,
std::chrono::steady_clock::duration timeout,
P &&predicate) noexcept {
while (!predicate()) {
// TODO: without wait_until(), this multiplies the timeout
if (!wait_for(lock, timeout))
return predicate();
}
return true;
}
}; };
#endif #endif
...@@ -70,6 +70,19 @@ public: ...@@ -70,6 +70,19 @@ public:
&lock.mutex()->critical_section, &lock.mutex()->critical_section,
timeout_ms); timeout_ms);
} }
template<typename M, typename P>
bool wait_for(std::unique_lock<M> &lock,
std::chrono::steady_clock::duration timeout,
P &&predicate) noexcept {
while (!predicate()) {
// TODO: without wait_until(), this multiplies the timeout
if (!wait_for(lock, timeout))
return predicate();
}
return true;
}
}; };
#endif #endif
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment