include/boost/corosio/detail/timer_service.hpp

92.5% Lines (221/239) 100.0% List of functions (27/27) 75.0% Branches (96/128)
timer_service.hpp
f(x) Functions (27)
Function Calls Lines Branches Blocks
boost::corosio::detail::timer_service::callback::callback() :100 1121x 100.0% 100.0% boost::corosio::detail::timer_service::callback::callback(void*, void (*)(void*)) :103 1121x 100.0% 100.0% boost::corosio::detail::timer_service::callback::operator()() const :112 3346x 100.0% 50.0% 100.0% boost::corosio::detail::timer_service::timer_service(boost::capy::execution_context&, boost::corosio::detail::scheduler&) :141 1121x 100.0% 100.0% boost::corosio::detail::timer_service::get_scheduler() :147 6810x 100.0% 100.0% boost::corosio::detail::timer_service::~timer_service() :153 2242x 100.0% 100.0% boost::corosio::detail::timer_service::set_on_earliest_changed(boost::corosio::detail::timer_service::callback) :159 1121x 100.0% 100.0% boost::corosio::detail::timer_service::nearest_expiry() const :172 4696x 100.0% 70.0% boost::corosio::detail::timer_service::refresh_cached_nearest() :203 6826x 100.0% 100.0% 66.7% boost::corosio::detail::tl_cache_owner::~tl_cache_owner() :231 34x 100.0% 50.0% 80.0% boost::corosio::detail::arm_tl_cache_cleanup() :239 3818x 100.0% 100.0% 100.0% boost::corosio::detail::try_pop_tl_cache(boost::corosio::detail::timer_service*) :245 3861x 87.5% 50.0% 77.8% boost::corosio::detail::try_push_tl_cache(boost::corosio::detail::timer::implementation*) :260 3846x 100.0% 100.0% 100.0% boost::corosio::detail::timer_service_invalidate_cache() :272 2242x 100.0% 100.0% 100.0% boost::corosio::detail::timer_service::shutdown() :281 2242x 100.0% 76.5% 72.6% boost::corosio::detail::timer_service::construct() :332 3861x 100.0% 80.0% 67.2% boost::corosio::detail::timer_service::destroy(boost::corosio::io_object::implementation*) :371 3861x 100.0% 100.0% 100.0% boost::corosio::detail::timer_service::destroy_impl(boost::corosio::detail::timer::implementation&) :385 3846x 75.0% 60.0% 59.5% boost::corosio::detail::timer_service::insert_waiter(boost::corosio::detail::timer::implementation&, boost::corosio::detail::waiter_node*) :413 3418x 74.3% 69.6% 63.6% boost::corosio::detail::timer_service::cancel_timer(boost::corosio::detail::timer::implementation&) :468 3846x 100.0% 75.0% 88.9% boost::corosio::detail::timer_service::cancel_waiter(boost::corosio::detail::waiter_node*) :502 2057x 92.3% 75.0% 76.5% boost::corosio::detail::timer_service::process_expired() :524 1350x 100.0% 92.9% 90.6% boost::corosio::detail::timer_service::remove_timer_impl(boost::corosio::detail::timer::implementation&) :560 3404x 84.6% 70.0% 65.4% boost::corosio::detail::timer_service::up_heap(unsigned long long) :591 3418x 100.0% 100.0% 100.0% boost::corosio::detail::timer_service::down_heap(unsigned long long) :604 1240x 92.3% 62.5% 71.4% boost::corosio::detail::timer_service::swap_heap(unsigned long long, unsigned long long) :624 2454x 100.0% 61.7% boost::corosio::detail::get_timer_service(boost::capy::execution_context&, boost::corosio::detail::scheduler&) :650 1121x 100.0% 100.0%
Line Branch TLA Hits Source Code
1 //
2 // Copyright (c) 2025 Vinnie Falco ([email protected])
3 // Copyright (c) 2026 Steve Gerbino
4 //
5 // Distributed under the Boost Software License, Version 1.0. (See accompanying
6 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7 //
8 // Official repository: https://github.com/cppalliance/corosio
9 //
10
11 #ifndef BOOST_COROSIO_DETAIL_TIMER_SERVICE_HPP
12 #define BOOST_COROSIO_DETAIL_TIMER_SERVICE_HPP
13
14 #include <boost/corosio/detail/timer.hpp>
15 #include <boost/corosio/detail/scheduler.hpp>
16 #include <boost/corosio/detail/scheduler_op.hpp>
17 #include <boost/corosio/detail/intrusive.hpp>
18 #include <boost/corosio/detail/thread_local_ptr.hpp>
19 #include <boost/capy/error.hpp>
20 #include <boost/capy/ex/execution_context.hpp>
21 #include <boost/capy/ex/executor_ref.hpp>
22 #include <system_error>
23
24 #include <atomic>
25 #include <chrono>
26 #include <coroutine>
27 #include <cstddef>
28 #include <limits>
29 #include <mutex>
30 #include <stop_token>
31 #include <utility>
32 #include <vector>
33
34 namespace boost::corosio::detail {
35
36 struct scheduler;
37
38 /*
39 Timer Service
40 =============
41
42 Data Structures
43 ---------------
44 waiter_node (defined in timer.hpp) holds per-waiter state:
45 coroutine handle, executor, error output, embedded
46 completion_op. Each concurrent co_await t.wait() embeds one
47 waiter_node in the awaitable on the suspended coroutine's
48 frame — waits perform no allocation.
49
50 timer::implementation holds per-timer state: expiry, heap
51 index, and the single published waiter. Each timer holds
52 at most one waiter; process_expired's local cross-timer drain
53 list still threads waiters through their intrusive hooks when
54 collecting several timers' waiters past the lock.
55
56 timer_service owns a min-heap of active timers and a free list
57 of recycled impls. The heap is ordered by expiry time; the
58 scheduler queries nearest_expiry() to set the epoll/timerfd
59 timeout.
60
61 Optimization Strategy
62 ---------------------
63 1. Deferred heap insertion — expires_after() stores the expiry
64 but does not insert into the heap. Insertion happens in wait().
65 2. Thread-local impl cache — single-slot per-thread cache.
66 3. Frame-resident waiter_node with embedded completion_op —
67 eliminates heap allocation per wait/fire/cancel.
68 4. Cached nearest expiry — atomic avoids mutex in nearest_expiry().
69 5. might_have_pending_waits_ flag — skips lock when no wait issued.
70
71 Concurrency
72 -----------
73 stop_token callbacks can fire from any thread. The impl_
74 pointer on waiter_node is used as a "still in list" marker.
75 A waiter_node's storage is the suspended coroutine's frame:
76 every completion path must finish touching the node before
77 posting the continuation or destroying the handle.
78 */
79
80 inline void timer_service_invalidate_cache() noexcept;
81
82 // timer_service class body — member function definitions are
83 // out-of-class (after implementation and waiter_node are complete)
84 class BOOST_COROSIO_DECL timer_service final
85 : public capy::execution_context::service
86 , public io_object::io_service
87 {
88 public:
89 using clock_type = std::chrono::steady_clock;
90 using time_point = clock_type::time_point;
91
92 /// Type-erased callback for earliest-expiry-changed notifications.
93 class callback
94 {
95 void* ctx_ = nullptr;
96 void (*fn_)(void*) = nullptr;
97
98 public:
99 /// Construct an empty callback.
100 1121x callback() = default;
101
102 /// Construct a callback with the given context and function.
103 1121x callback(void* ctx, void (*fn)(void*)) noexcept : ctx_(ctx), fn_(fn) {}
104
105 /// Return true if the callback is non-empty.
106 explicit operator bool() const noexcept
107 {
108 return fn_ != nullptr;
109 }
110
111 /// Invoke the callback.
112 3346x void operator()() const
113 {
114
1/2
✓ Branch 2 → 3 taken 3346 times.
✗ Branch 2 → 4 not taken.
3346x if (fn_)
115 3346x fn_(ctx_);
116 3346x }
117 };
118
119 private:
120 struct heap_entry
121 {
122 time_point time_;
123 timer::implementation* timer_;
124 };
125
126 scheduler* sched_ = nullptr;
127 BOOST_COROSIO_MSVC_WARNING_PUSH
128 BOOST_COROSIO_MSVC_WARNING_DISABLE(4251) // std:: members, dll-interface
129 mutable std::mutex mutex_;
130 std::vector<heap_entry> heap_;
131 timer::implementation* free_list_ = nullptr;
132 callback on_earliest_changed_;
133 bool shutting_down_ = false;
134 // Avoids mutex in nearest_expiry() and empty()
135 mutable std::atomic<std::int64_t> cached_nearest_ns_{
136 (std::numeric_limits<std::int64_t>::max)()};
137 BOOST_COROSIO_MSVC_WARNING_POP
138
139 public:
140 /// Construct the timer service bound to a scheduler.
141 1121x inline timer_service(capy::execution_context&, scheduler& sched)
142 1121x : sched_(&sched)
143 {
144 1121x }
145
146 /// Return the associated scheduler.
147 6810x inline scheduler& get_scheduler() noexcept
148 {
149 6810x return *sched_;
150 }
151
152 /// Destroy the timer service.
153 2242x ~timer_service() override = default;
154
155 timer_service(timer_service const&) = delete;
156 timer_service& operator=(timer_service const&) = delete;
157
158 /// Register a callback invoked when the earliest expiry changes.
159 1121x inline void set_on_earliest_changed(callback cb)
160 {
161 1121x on_earliest_changed_ = cb;
162 1121x }
163
164 /// Return true if no timers are in the heap.
165 inline bool empty() const noexcept
166 {
167 return cached_nearest_ns_.load(std::memory_order_acquire) ==
168 (std::numeric_limits<std::int64_t>::max)();
169 }
170
171 /// Return the nearest timer expiry without acquiring the mutex.
172 4696x inline time_point nearest_expiry() const noexcept
173 {
174 4696x auto ns = cached_nearest_ns_.load(std::memory_order_acquire);
175 4696x return time_point(time_point::duration(ns));
176 }
177
178 /// Cancel all pending timers and free cached resources.
179 inline void shutdown() override;
180
181 /// Construct a new timer implementation.
182 inline io_object::implementation* construct() override;
183
184 /// Destroy a timer implementation, cancelling pending waiters.
185 inline void destroy(io_object::implementation* p) override;
186
187 /// Cancel and recycle a timer implementation.
188 inline void destroy_impl(timer::implementation& impl);
189
190 /// Publish the timer's waiter and insert the timer into the heap.
191 inline void insert_waiter(timer::implementation& impl, waiter_node* w);
192
193 /// Cancel the timer's published waiter, if any.
194 inline void cancel_timer(timer::implementation& impl);
195
196 /// Cancel one specific waiter ( stop_token callback path ).
197 inline void cancel_waiter(waiter_node* w);
198
199 /// Complete all waiters whose timers have expired.
200 inline std::size_t process_expired();
201
202 private:
203 6826x inline void refresh_cached_nearest() noexcept
204 {
205
2/2
✓ Branch 3 → 4 taken 2128 times.
✓ Branch 3 → 5 taken 4698 times.
6826x auto ns = heap_.empty() ? (std::numeric_limits<std::int64_t>::max)()
206 4698x : heap_[0].time_.time_since_epoch().count();
207 6826x cached_nearest_ns_.store(ns, std::memory_order_release);
208 6826x }
209
210 inline void remove_timer_impl(timer::implementation& impl);
211 inline void up_heap(std::size_t index);
212 inline void down_heap(std::size_t index);
213 inline void swap_heap(std::size_t i1, std::size_t i2);
214 };
215
216 // Thread-local cache avoids hot-path mutex acquisitions:
217 // single-slot impl cache, validated by comparing svc_. Cleared by
218 // timer_service_invalidate_cache() during shutdown.
219
220 inline thread_local_ptr<timer::implementation> tl_cached_impl;
221
222 // The POD TLS slot above never runs destructors, so a short-lived
223 // run() thread would leak its cached impl. Each push arms this
224 // owner, whose destructor frees the slot at thread exit. A cached
225 // entry is a quiescent heap object (nothing in the heap or free
226 // list) and deletion touches no service state, so it is safe after
227 // the owning service is gone (the stale-entry path in
228 // try_pop_tl_cache deletes the same way).
229 struct tl_cache_owner
230 {
231 34x ~tl_cache_owner()
232 {
233
1/2
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 34 times.
34x delete tl_cached_impl.get();
234 34x tl_cached_impl.set(nullptr);
235 34x }
236 };
237
238 inline void
239 3818x arm_tl_cache_cleanup() noexcept
240 {
241
2/2
✓ Branch 2 → 3 taken 34 times.
✓ Branch 2 → 4 taken 3784 times.
3818x [[maybe_unused]] thread_local tl_cache_owner owner;
242 3818x }
243
244 inline timer::implementation*
245 3861x try_pop_tl_cache(timer_service* svc) noexcept
246 {
247 3861x auto* impl = tl_cached_impl.get();
248
2/2
✓ Branch 3 → 4 taken 3664 times.
✓ Branch 3 → 9 taken 197 times.
3861x if (impl)
249 {
250 3664x tl_cached_impl.set(nullptr);
251
1/2
✓ Branch 5 → 6 taken 3664 times.
✗ Branch 5 → 7 not taken.
3664x if (impl->svc_ == svc)
252 3664x return impl;
253 // Stale impl from a destroyed service
254 delete impl;
255 }
256 197x return nullptr;
257 }
258
259 inline bool
260 3846x try_push_tl_cache(timer::implementation* impl) noexcept
261 {
262
2/2
✓ Branch 3 → 4 taken 3818 times.
✓ Branch 3 → 7 taken 28 times.
3846x if (!tl_cached_impl.get())
263 {
264 3818x arm_tl_cache_cleanup();
265 3818x tl_cached_impl.set(impl);
266 3818x return true;
267 }
268 28x return false;
269 }
270
271 inline void
272 2242x timer_service_invalidate_cache() noexcept
273 {
274
2/2
✓ Branch 3 → 4 taken 144 times.
✓ Branch 3 → 5 taken 2098 times.
2242x delete tl_cached_impl.get();
275 2242x tl_cached_impl.set(nullptr);
276 2242x }
277
278 // timer_service out-of-class member function definitions
279
280 inline void
281 2242x timer_service::shutdown()
282 {
283 2242x timer_service_invalidate_cache();
284 2242x shutting_down_ = true;
285
286 // Snapshot impls and detach them from the heap so that
287 // coroutine-owned timer destructors (triggered by h.destroy()
288 // below) cannot re-enter remove_timer_impl() and mutate the
289 // vector during iteration.
290 2242x std::vector<timer::implementation*> impls;
291
1/1
✓ Branch 4 → 5 taken 2242 times.
2242x impls.reserve(heap_.size());
292
2/2
✓ Branch 32 → 7 taken 14 times.
✓ Branch 32 → 33 taken 2242 times.
2256x for (auto& entry : heap_)
293 {
294 14x entry.timer_->heap_index_.store(
295 (std::numeric_limits<std::size_t>::max)(),
296 std::memory_order_relaxed);
297
1/1
✓ Branch 29 → 30 taken 14 times.
14x impls.push_back(entry.timer_);
298 }
299 2242x heap_.clear();
300 2242x cached_nearest_ns_.store(
301 (std::numeric_limits<std::int64_t>::max)(), std::memory_order_release);
302
303 // Cancel waiting timers. Each waiter called work_started()
304 // in implementation::wait(). On IOCP the scheduler shutdown
305 // loop exits when outstanding_work_ reaches zero, so we must
306 // call work_finished() here to balance it. On other backends
307 // this is harmless.
308
2/2
✓ Branch 72 → 57 taken 14 times.
✓ Branch 72 → 73 taken 2242 times.
2256x for (auto* impl : impls)
309 {
310
1/2
✓ Branch 59 → 60 taken 14 times.
✗ Branch 59 → 68 not taken.
14x if (auto* w = std::exchange(impl->waiter_, nullptr))
311 {
312 14x w->reset_stop_cb();
313 14x auto h = std::exchange(w->h_, {});
314 14x sched_->work_finished();
315 // Destroying the frame also ends the node's storage
316
1/2
✓ Branch 65 → 66 taken 14 times.
✗ Branch 65 → 67 not taken.
14x if (h)
317
1/1
✓ Branch 66 → 67 taken 14 times.
14x h.destroy();
318 }
319
1/2
✓ Branch 68 → 69 taken 14 times.
✗ Branch 68 → 70 not taken.
14x delete impl;
320 }
321
322 // Delete free-listed impls
323
2/2
✓ Branch 77 → 74 taken 27 times.
✓ Branch 77 → 78 taken 2242 times.
2269x while (free_list_)
324 {
325 27x auto* next = free_list_->next_free_;
326
1/2
✓ Branch 74 → 75 taken 27 times.
✗ Branch 74 → 76 not taken.
27x delete free_list_;
327 27x free_list_ = next;
328 }
329 2242x }
330
331 inline io_object::implementation*
332 3861x timer_service::construct()
333 {
334 3861x timer::implementation* impl = try_pop_tl_cache(this);
335
2/2
✓ Branch 3 → 4 taken 3664 times.
✓ Branch 3 → 31 taken 197 times.
3861x if (impl)
336 {
337 3664x impl->svc_ = this;
338 // Reset expiry_ too: a recycled impl must behave like a fresh
339 // one, whose default expiry reads as already elapsed
340 3664x impl->expiry_ = {};
341 3664x impl->heap_index_.store(
342 (std::numeric_limits<std::size_t>::max)(),
343 std::memory_order_relaxed);
344 3664x impl->might_have_pending_waits_.store(false, std::memory_order_relaxed);
345
1/2
✗ Branch 27 → 28 not taken.
✓ Branch 27 → 29 taken 3664 times.
3664x BOOST_COROSIO_ASSERT(impl->waiter_ == nullptr);
346 3664x return impl;
347 }
348
349
1/1
✓ Branch 31 → 32 taken 197 times.
197x std::lock_guard lock(mutex_);
350
2/2
✓ Branch 32 → 33 taken 1 time.
✓ Branch 32 → 60 taken 196 times.
197x if (free_list_)
351 {
352 1x impl = free_list_;
353 1x free_list_ = impl->next_free_;
354 1x impl->next_free_ = nullptr;
355 1x impl->svc_ = this;
356 1x impl->expiry_ = {};
357 1x impl->heap_index_.store(
358 (std::numeric_limits<std::size_t>::max)(),
359 std::memory_order_relaxed);
360 1x impl->might_have_pending_waits_.store(false, std::memory_order_relaxed);
361
1/2
✗ Branch 56 → 57 not taken.
✓ Branch 56 → 58 taken 1 time.
1x BOOST_COROSIO_ASSERT(impl->waiter_ == nullptr);
362 }
363 else
364 {
365
1/1
✓ Branch 60 → 61 taken 196 times.
196x impl = new timer::implementation(*this);
366 }
367 197x return impl;
368 197x }
369
370 inline void
371 3861x timer_service::destroy(io_object::implementation* p)
372 {
373 // During shutdown the drain loop owns every impl and deletes
374 // them directly. A frame destroyed by that loop can unwind a
375 // handle whose impl was freed in an earlier iteration (a
376 // timeout's parent frame owns the timeout timer while
377 // suspended on the inner delay's timer), so bail out before
378 // even downcasting the pointer.
379
2/2
✓ Branch 2 → 3 taken 15 times.
✓ Branch 2 → 4 taken 3846 times.
3861x if (shutting_down_)
380 15x return;
381 3846x destroy_impl(static_cast<timer::implementation&>(*p));
382 }
383
384 inline void
385 3846x timer_service::destroy_impl(timer::implementation& impl)
386 {
387 // During shutdown the impl is owned by the shutdown loop.
388 // Re-entering here (from a coroutine-owned timer destructor
389 // triggered by h.destroy()) must not modify the heap or
390 // recycle the impl — shutdown deletes it directly.
391
1/2
✗ Branch 2 → 3 not taken.
✓ Branch 2 → 4 taken 3846 times.
3846x if (shutting_down_)
392 3818x return;
393
394
1/1
✓ Branch 4 → 5 taken 3846 times.
3846x cancel_timer(impl);
395
396
1/2
✗ Branch 21 → 22 not taken.
✓ Branch 21 → 27 taken 3846 times.
7692x if (impl.heap_index_.load(std::memory_order_relaxed) !=
397 3846x (std::numeric_limits<std::size_t>::max)())
398 {
399 std::lock_guard lock(mutex_);
400 remove_timer_impl(impl);
401 refresh_cached_nearest();
402 }
403
404
2/2
✓ Branch 28 → 29 taken 3818 times.
✓ Branch 28 → 30 taken 28 times.
3846x if (try_push_tl_cache(&impl))
405 3818x return;
406
407
1/1
✓ Branch 30 → 31 taken 28 times.
28x std::lock_guard lock(mutex_);
408 28x impl.next_free_ = free_list_;
409 28x free_list_ = &impl;
410 28x }
411
412 inline void
413 3418x timer_service::insert_waiter(timer::implementation& impl, waiter_node* w)
414 {
415 3418x bool notify = false;
416 3418x bool lost_cancel = false;
417 {
418
1/1
✓ Branch 2 → 3 taken 3418 times.
3418x std::lock_guard lock(mutex_);
419 // Grow before publishing anything, so the push_back below
420 // cannot throw: a failure here leaves the waiter untouched,
421 // the strong guarantee rearm_wait's recovery relies on.
422 3418x if (impl.heap_index_.load(std::memory_order_relaxed) ==
423
5/6
✓ Branch 19 → 20 taken 3418 times.
✗ Branch 19 → 24 not taken.
✓ Branch 22 → 23 taken 156 times.
✓ Branch 22 → 24 taken 3262 times.
✓ Branch 25 → 26 taken 156 times.
✓ Branch 25 → 32 taken 3262 times.
6836x (std::numeric_limits<std::size_t>::max)() &&
424 3418x heap_.size() == heap_.capacity())
425
2/3
✗ Branch 27 → 28 not taken.
✓ Branch 27 → 30 taken 156 times.
✓ Branch 31 → 32 taken 156 times.
156x heap_.reserve(
426 156x heap_.capacity() == 0 ? 16 : 2 * heap_.capacity());
427 // Publish: from here the waiter is visible to the fire path and
428 // to its own stop callback (impl_ non-null enables cancel_waiter).
429 3418x w->impl_ = &impl;
430
1/2
✓ Branch 48 → 49 taken 3418 times.
✗ Branch 48 → 89 not taken.
6836x if (impl.heap_index_.load(std::memory_order_relaxed) ==
431 3418x (std::numeric_limits<std::size_t>::max)())
432 {
433 3418x impl.heap_index_.store(heap_.size(), std::memory_order_relaxed);
434
1/1
✓ Branch 70 → 71 taken 3418 times.
3418x heap_.push_back({impl.expiry_, &impl});
435
1/1
✓ Branch 72 → 73 taken 3418 times.
3418x up_heap(heap_.size() - 1);
436 3418x notify =
437 3418x (impl.heap_index_.load(std::memory_order_relaxed) == 0);
438 3418x refresh_cached_nearest();
439 }
440
1/2
✗ Branch 89 → 90 not taken.
✓ Branch 89 → 91 taken 3418 times.
3418x BOOST_COROSIO_ASSERT(impl.waiter_ == nullptr);
441 3418x impl.waiter_ = w;
442
443 // Lost-cancel re-check: a stop requested after the canceller was
444 // armed in wait() but before this publication found impl_ null
445 // and returned a no-op. Observe it now and undo the insertion.
446
1/2
✗ Branch 93 → 94 not taken.
✓ Branch 93 → 98 taken 3418 times.
3418x if (w->token_->stop_requested())
447 {
448 w->impl_ = nullptr;
449 impl.waiter_ = nullptr;
450 remove_timer_impl(impl);
451 impl.might_have_pending_waits_.store(
452 false, std::memory_order_relaxed);
453 refresh_cached_nearest();
454 lost_cancel = true;
455 notify = false; // insertion undone; nearest unchanged
456 }
457 3418x }
458
2/2
✓ Branch 99 → 100 taken 3346 times.
✓ Branch 99 → 101 taken 72 times.
3418x if (notify)
459 3346x on_earliest_changed_();
460
1/2
✗ Branch 101 → 102 not taken.
✓ Branch 101 → 104 taken 3418 times.
3418x if (lost_cancel)
461 {
462 w->ec_ = make_error_code(capy::error::canceled);
463 sched_->post(&w->op_);
464 }
465 3418x }
466
467 inline void
468 3846x timer_service::cancel_timer(timer::implementation& impl)
469 {
470
2/2
✓ Branch 3 → 4 taken 3845 times.
✓ Branch 3 → 5 taken 1 time.
3846x if (!impl.might_have_pending_waits_.load(std::memory_order_relaxed))
471 3845x return;
472
473 // No unlocked already-done fast-out here: it would need the
474 // non-atomic waiter_ (a race with concurrent drains), and an
475 // index-only check is lifetime-unsafe because npos is stored
476 // before the drain finishes touching the impl. A stale-true
477 // flag is rare with the stateless API; the locked path below
478 // re-validates.
479
480 1x waiter_node* canceled = nullptr;
481
482 {
483
1/1
✓ Branch 5 → 6 taken 1 time.
1x std::lock_guard lock(mutex_);
484
1/1
✓ Branch 6 → 7 taken 1 time.
1x remove_timer_impl(impl);
485 1x canceled = std::exchange(impl.waiter_, nullptr);
486
1/2
✓ Branch 8 → 9 taken 1 time.
✗ Branch 8 → 10 not taken.
1x if (canceled)
487 1x canceled->impl_ = nullptr;
488 // Store false as the final touch of the impl under the lock so
489 // a pre-lock false-flag check trusts it unqualified.
490 1x impl.might_have_pending_waits_.store(false, std::memory_order_relaxed);
491 1x refresh_cached_nearest();
492 1x }
493
494
1/2
✓ Branch 13 → 14 taken 1 time.
✗ Branch 13 → 16 not taken.
1x if (canceled)
495 {
496 1x canceled->ec_ = make_error_code(capy::error::canceled);
497 1x sched_->post(&canceled->op_);
498 }
499 }
500
501 inline void
502 2057x timer_service::cancel_waiter(waiter_node* w)
503 {
504 {
505
1/1
✓ Branch 2 → 3 taken 2057 times.
2057x std::lock_guard lock(mutex_);
506 // Already removed by another drain: cancel_timer,
507 // process_expired, or insert_waiter's lost-cancel recheck
508
1/2
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 2057 times.
2057x if (!w->impl_)
509 return;
510 2057x auto* impl = w->impl_;
511 2057x w->impl_ = nullptr;
512 2057x impl->waiter_ = nullptr;
513
1/1
✓ Branch 5 → 6 taken 2057 times.
2057x remove_timer_impl(*impl);
514 2057x impl->might_have_pending_waits_.store(
515 false, std::memory_order_relaxed);
516 2057x refresh_cached_nearest();
517 2057x }
518
519 2057x w->ec_ = make_error_code(capy::error::canceled);
520 2057x sched_->post(&w->op_);
521 }
522
523 inline std::size_t
524 1350x timer_service::process_expired()
525 {
526 1350x intrusive_list<waiter_node> expired;
527
528 {
529
1/1
✓ Branch 2 → 3 taken 1350 times.
1350x std::lock_guard lock(mutex_);
530 1350x auto now = clock_type::now();
531
532
7/7
✓ Branch 13 → 14 taken 2566 times.
✓ Branch 13 → 19 taken 130 times.
✓ Branch 15 → 16 taken 2566 times.
✓ Branch 17 → 18 taken 1346 times.
✓ Branch 17 → 19 taken 1220 times.
✓ Branch 20 → 5 taken 1346 times.
✓ Branch 20 → 21 taken 1350 times.
2696x while (!heap_.empty() && heap_[0].time_ <= now)
533 {
534 1346x timer::implementation* t = heap_[0].timer_;
535
1/1
✓ Branch 6 → 7 taken 1346 times.
1346x remove_timer_impl(*t);
536
1/2
✓ Branch 8 → 9 taken 1346 times.
✗ Branch 8 → 11 not taken.
1346x if (auto* w = std::exchange(t->waiter_, nullptr))
537 {
538 1346x w->impl_ = nullptr;
539 1346x w->ec_ = {};
540 1346x expired.push_back(w);
541 }
542 1346x t->might_have_pending_waits_.store(
543 false, std::memory_order_relaxed);
544 }
545
546 1350x refresh_cached_nearest();
547 1350x }
548
549 1350x std::size_t count = 0;
550
2/2
✓ Branch 25 → 26 taken 1346 times.
✓ Branch 25 → 28 taken 1350 times.
2696x while (auto* w = expired.pop_front())
551 {
552
1/1
✓ Branch 26 → 27 taken 1346 times.
1346x sched_->post(&w->op_);
553 1346x ++count;
554 1346x }
555
556 1350x return count;
557 }
558
559 inline void
560 3404x timer_service::remove_timer_impl(timer::implementation& impl)
561 {
562 3404x std::size_t index = impl.heap_index_.load(std::memory_order_relaxed);
563
1/2
✗ Branch 18 → 19 not taken.
✓ Branch 18 → 20 taken 3404 times.
3404x if (index >= heap_.size())
564 return; // Not in heap
565
566
2/2
✓ Branch 21 → 22 taken 2164 times.
✓ Branch 21 → 44 taken 1240 times.
3404x if (index == heap_.size() - 1)
567 {
568 // Last element, just pop
569 2164x impl.heap_index_.store(
570 (std::numeric_limits<std::size_t>::max)(),
571 std::memory_order_relaxed);
572 2164x heap_.pop_back();
573 }
574 else
575 {
576 // Swap with last and reheapify
577 1240x swap_heap(index, heap_.size() - 1);
578 1240x impl.heap_index_.store(
579 (std::numeric_limits<std::size_t>::max)(),
580 std::memory_order_relaxed);
581 1240x heap_.pop_back();
582
583
4/6
✓ Branch 68 → 69 taken 2 times.
✓ Branch 68 → 75 taken 1238 times.
✗ Branch 73 → 74 not taken.
✓ Branch 73 → 75 taken 2 times.
✗ Branch 76 → 77 not taken.
✓ Branch 76 → 78 taken 1240 times.
1240x if (index > 0 && heap_[index].time_ < heap_[(index - 1) / 2].time_)
584 up_heap(index);
585 else
586 1240x down_heap(index);
587 }
588 }
589
590 inline void
591 3418x timer_service::up_heap(std::size_t index)
592 {
593
2/2
✓ Branch 11 → 3 taken 1284 times.
✓ Branch 11 → 12 taken 3346 times.
4630x while (index > 0)
594 {
595 1284x std::size_t parent = (index - 1) / 2;
596
2/2
✓ Branch 7 → 8 taken 72 times.
✓ Branch 7 → 9 taken 1212 times.
1284x if (!(heap_[index].time_ < heap_[parent].time_))
597 72x break;
598 1212x swap_heap(index, parent);
599 1212x index = parent;
600 }
601 3418x }
602
603 inline void
604 1240x timer_service::down_heap(std::size_t index)
605 {
606 1240x std::size_t child = index * 2 + 1;
607
2/2
✓ Branch 21 → 3 taken 3 times.
✓ Branch 21 → 22 taken 1239 times.
1242x while (child < heap_.size())
608 {
609 3x std::size_t min_child = (child + 1 == heap_.size() ||
610 heap_[child].time_ < heap_[child + 1].time_)
611
1/2
✗ Branch 4 → 5 not taken.
✓ Branch 4 → 11 taken 3 times.
3x ? child
612 3x : child + 1;
613
614
2/2
✓ Branch 16 → 17 taken 1 time.
✓ Branch 16 → 18 taken 2 times.
3x if (heap_[index].time_ < heap_[min_child].time_)
615 1x break;
616
617 2x swap_heap(index, min_child);
618 2x index = min_child;
619 2x child = index * 2 + 1;
620 }
621 1240x }
622
623 inline void
624 2454x timer_service::swap_heap(std::size_t i1, std::size_t i2)
625 {
626 2454x heap_entry tmp = heap_[i1];
627 2454x heap_[i1] = heap_[i2];
628 2454x heap_[i2] = tmp;
629 2454x heap_[i1].timer_->heap_index_.store(i1, std::memory_order_relaxed);
630 2454x heap_[i2].timer_->heap_index_.store(i2, std::memory_order_relaxed);
631 2454x }
632
633 // waiter_node's completion_op and canceller members are defined in
634 // timer.cpp alongside implementation::wait(), for the same reason
635 // wait() lives there (see below).
636
637 // timer::implementation::wait() is defined in timer.cpp, not here.
638 // It must be a non-inline definition in a translation unit that is
639 // always pulled into the link whenever detail::timer is used (every
640 // consumer needs timer's constructors from that same object file).
641 // An inline definition in this header would only be emitted in
642 // translation units that happen to also include this header, which
643 // is not guaranteed for every caller of wait_awaitable::await_suspend
644 // in timer.hpp (e.g. code that only reaches timer.hpp through
645 // delay.hpp, without transitively including a scheduler header).
646
647 // Free functions
648
649 inline timer_service&
650 1121x get_timer_service(capy::execution_context& ctx, scheduler& sched)
651 {
652 1121x return ctx.make_service<timer_service>(sched);
653 }
654
655 } // namespace boost::corosio::detail
656
657 #endif
658