sleeper_thread: Make sleep_for more robust

In the previous fix for a passed max duration, the assumption was made
that at maximum one second will pass between the duration assignment and
the std::condition_variable::sleep_for() call.
This implementation makes the behavior more predictable by using
sleep_until() instead to emulate the sleep_for() behavior.
This commit is contained in:
Tamino Bauknecht 2023-10-23 01:11:38 +02:00
parent ad7d4eb07d
commit 521dac8086
No known key found for this signature in database
GPG Key ID: 77837396BE935C6C
1 changed files with 6 additions and 6 deletions

View File

@ -67,13 +67,13 @@ class SleeperThread {
auto sleep_for(std::chrono::system_clock::duration dur) {
std::unique_lock lk(mutex_);
CancellationGuard cancel_lock;
using timepoint = std::chrono::system_clock::time_point;
static_assert(std::numeric_limits<timepoint::rep>::max() >=
std::numeric_limits<decltype(dur)::rep>::max());
if (std::chrono::system_clock::now() >= timepoint::max() - dur) {
dur = timepoint::max() - std::chrono::system_clock::now() - std::chrono::seconds(1);
constexpr auto max_time_point = std::chrono::steady_clock::time_point::max();
auto wait_end = max_time_point;
auto now = std::chrono::steady_clock::now();
if (now < max_time_point - dur) {
wait_end = now + dur;
}
return condvar_.wait_for(lk, dur, [this] { return signal_ || !do_run_; });
return condvar_.wait_until(lk, wait_end, [this] { return signal_ || !do_run_; });
}
auto sleep_until(