28#include <condition_variable>
43 std::uint64_t current)
noexcept {
44 return armed_for != 0 && armed_for == current;
63 void arm(std::chrono::milliseconds delay, std::uint64_t generation,
64 std::function<
void(std::uint64_t)> on_expire) {
67 std::lock_guard<std::mutex> lk(mu_);
70 worker_ = std::thread([
this, delay, generation,
71 on_expire = std::move(on_expire)] {
73 std::unique_lock<std::mutex> lk(mu_);
77 if (cv_.wait_for(lk, delay, [
this] { return cancelled_; }))
83 if (on_expire) on_expire(generation);
91 if (!worker_.joinable())
return;
93 std::lock_guard<std::mutex> lk(mu_);
102 bool armed() const noexcept {
return worker_.joinable(); }
106 std::condition_variable cv_;
107 bool cancelled_ =
false;
TimeoutController(const TimeoutController &)=delete
void cancel_and_join()
Cancel an armed timeout and join its thread.
Definition trace_timeout.hpp:90
TimeoutController()=default
TimeoutController & operator=(const TimeoutController &)=delete
void arm(std::chrono::milliseconds delay, std::uint64_t generation, std::function< void(std::uint64_t)> on_expire)
Arm on_expire(generation) to run after delay.
Definition trace_timeout.hpp:63
bool armed() const noexcept
True while a timeout thread exists (armed, running, or finished but not yet joined).
Definition trace_timeout.hpp:102
~TimeoutController()
Joins any armed timeout. A controller must never outlive its worker.
Definition trace_timeout.hpp:58
constexpr bool timeout_targets_current_session(std::uint64_t armed_for, std::uint64_t current) noexcept
True when a timeout armed for armed_for may still act on the session currently identified by current.
Definition trace_timeout.hpp:42