@@ -86,19 +86,26 @@ class BOOST_COROSIO_DECL timer_service final
8686 using clock_type = std::chrono::steady_clock;
8787 using time_point = clock_type::time_point;
8888
89+ // / Type-erased callback for earliest-expiry-changed notifications.
8990 class callback
9091 {
9192 void * ctx_ = nullptr ;
9293 void (*fn_)(void *) = nullptr ;
9394
9495 public:
96+ // / Construct an empty callback.
9597 callback () = default ;
98+
99+ // / Construct a callback with the given context and function.
96100 callback (void * ctx, void (*fn)(void *)) noexcept : ctx_(ctx), fn_(fn) {}
97101
102+ // / Return true if the callback is non-empty.
98103 explicit operator bool () const noexcept
99104 {
100105 return fn_ != nullptr ;
101106 }
107+
108+ // / Invoke the callback.
102109 void operator ()() const
103110 {
104111 if (fn_)
@@ -126,49 +133,78 @@ class BOOST_COROSIO_DECL timer_service final
126133 (std::numeric_limits<std::int64_t >::max)()};
127134
128135public:
136+ // / Construct the timer service bound to a scheduler.
129137 inline timer_service (capy::execution_context&, scheduler& sched)
130138 : sched_(&sched)
131139 {
132140 }
133141
142+ // / Return the associated scheduler.
134143 inline scheduler& get_scheduler () noexcept
135144 {
136145 return *sched_;
137146 }
138147
148+ // / Destroy the timer service.
139149 ~timer_service () override = default ;
140150
141151 timer_service (timer_service const &) = delete ;
142152 timer_service& operator =(timer_service const &) = delete ;
143153
154+ // / Register a callback invoked when the earliest expiry changes.
144155 inline void set_on_earliest_changed (callback cb)
145156 {
146157 on_earliest_changed_ = cb;
147158 }
148159
160+ // / Return true if no timers are in the heap.
149161 inline bool empty () const noexcept
150162 {
151163 return cached_nearest_ns_.load (std::memory_order_acquire) ==
152164 (std::numeric_limits<std::int64_t >::max)();
153165 }
154166
167+ // / Return the nearest timer expiry without acquiring the mutex.
155168 inline time_point nearest_expiry () const noexcept
156169 {
157170 auto ns = cached_nearest_ns_.load (std::memory_order_acquire);
158171 return time_point (time_point::duration (ns));
159172 }
160173
174+ // / Cancel all pending timers and free cached resources.
161175 inline void shutdown () override ;
176+
177+ // / Construct a new timer implementation.
162178 inline io_object::implementation* construct () override ;
179+
180+ // / Destroy a timer implementation, cancelling pending waiters.
163181 inline void destroy (io_object::implementation* p) override ;
182+
183+ // / Cancel and recycle a timer implementation.
164184 inline void destroy_impl (implementation& impl);
185+
186+ // / Create or recycle a waiter node.
165187 inline waiter_node* create_waiter ();
188+
189+ // / Return a waiter node to the cache or free list.
166190 inline void destroy_waiter (waiter_node* w);
191+
192+ // / Update the timer expiry, cancelling existing waiters.
167193 inline std::size_t update_timer (implementation& impl, time_point new_time);
194+
195+ // / Insert a waiter into the timer's waiter list and the heap.
168196 inline void insert_waiter (implementation& impl, waiter_node* w);
197+
198+ // / Cancel all waiters on a timer.
169199 inline std::size_t cancel_timer (implementation& impl);
200+
201+ // / Cancel a single waiter ( stop_token callback path ).
170202 inline void cancel_waiter (waiter_node* w);
203+
204+ // / Cancel one waiter on a timer.
171205 inline std::size_t cancel_one_waiter (implementation& impl);
206+
207+ // / Complete all waiters whose timers have expired.
172208 inline std::size_t process_expired ();
173209
174210private:
0 commit comments