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:
private:
bool LockWaitFinished() noexcept {
std::unique_lock<Mutex> lock(mutex);
while (!finished)
if (!cond.wait_for(lock, timeout))
return false;
return true;
return cond.wait_for(lock, timeout, [this]{ return finished; });
}
/**
......
......@@ -105,6 +105,19 @@ public:
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
......@@ -70,6 +70,19 @@ public:
&lock.mutex()->critical_section,
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
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