sleeper_thread: Allow sleep_for with max duration
The standard library has the implicit requirement that for std::condition_variable::sleep_for() the duration must not cause an overflow if added to the current time. This commit will reduce the duration accordingly to fit into the duration type.
This commit is contained in:
parent
a459d8a9b3
commit
ad7d4eb07d
|
@ -67,6 +67,12 @@ class SleeperThread {
|
||||||
auto sleep_for(std::chrono::system_clock::duration dur) {
|
auto sleep_for(std::chrono::system_clock::duration dur) {
|
||||||
std::unique_lock lk(mutex_);
|
std::unique_lock lk(mutex_);
|
||||||
CancellationGuard cancel_lock;
|
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);
|
||||||
|
}
|
||||||
return condvar_.wait_for(lk, dur, [this] { return signal_ || !do_run_; });
|
return condvar_.wait_for(lk, dur, [this] { return signal_ || !do_run_; });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue