15 std::function<void(
bool)> on_active;
16 uint32_t duration = 0;
19 std::lock_guard<std::mutex>
lk(
mtx_);
20 spec_ = std::move(spec);
24 on_active =
spec_.on_active;
36 std::lock_guard<std::mutex>
lk(
mtx_);
39 std::cout <<
"[HoldTimer] " << label <<
" started (" << duration <<
" ms)" << std::endl;
51 const bool was_active =
active_.exchange(
false);
53 std::function<void(
bool)> on_active;
56 std::lock_guard<std::mutex>
lk(
mtx_);
58 on_active =
spec_.on_active;
83 std::cout <<
"[HoldTimer] " << label <<
" stopped" << std::endl;
89 std::lock_guard<std::mutex>
lk(
mtx_);
92 if (
spec_.extended_ms == 0)
99 std::cout <<
"[HoldTimer] " <<
spec_.label <<
" extended to " <<
spec_.extended_ms <<
" ms"
106 std::lock_guard<std::mutex>
lk(
mtx_);
112 std::unique_lock<std::mutex>
lk(
mtx_);
118 cv_.wait_until(
lk, deadline);
133 auto on_expire =
spec_.on_expire;
134 auto on_active =
spec_.on_active;
135 const std::string label =
spec_.label;
138 std::cout <<
"[HoldTimer] " << label <<
" expired" << std::endl;
void start(HoldSpec spec)
Begin a hold.
bool restart_
Set by extend() to make the timer thread recompute its deadline.
void stop()
Cancel the hold and reap the timer thread.
std::atomic< bool > active_
Read without the lock by isActive() and by the wait loop, so atomic.
std::condition_variable cv_
std::thread timer_thread_
uint32_t currentDurationMs() const
The window currently being counted – extended_ms after a successful extend().
Clock::time_point started_at_
bool extend()
Restart the countdown at spec.extended_ms, measured from now.
uint32_t current_duration_ms_
What one hold is: how long, and who to tell.