libs/corosio/src/corosio/src/detail/timer_service.hpp

100.0% Lines (5/5) 100.0% Functions (4/4) 50.0% Branches (1/2)
libs/corosio/src/corosio/src/detail/timer_service.hpp
Line Branch Hits Source Code
1 //
2 // Copyright (c) 2025 Vinnie Falco (vinnie dot falco at gmail dot com)
3 //
4 // Distributed under the Boost Software License, Version 1.0. (See accompanying
5 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 //
7 // Official repository: https://github.com/cppalliance/corosio
8 //
9
10 #ifndef BOOST_COROSIO_SRC_DETAIL_TIMER_SERVICE_HPP
11 #define BOOST_COROSIO_SRC_DETAIL_TIMER_SERVICE_HPP
12
13 #include <boost/corosio/timer.hpp>
14 #include <boost/capy/ex/execution_context.hpp>
15
16 #include <chrono>
17 #include <cstddef>
18
19 namespace boost::corosio::detail {
20
21 struct scheduler;
22
23 class timer_service : public capy::execution_context::service
24 {
25 public:
26 using clock_type = std::chrono::steady_clock;
27 using time_point = clock_type::time_point;
28
29 // Nested callback type - context + function pointer
30 class callback
31 {
32 void* ctx_ = nullptr;
33 void(*fn_)(void*) = nullptr;
34
35 public:
36 309 callback() = default;
37 309 callback(void* ctx, void(*fn)(void*)) noexcept
38 309 : ctx_(ctx), fn_(fn) {}
39
40 explicit operator bool() const noexcept { return fn_ != nullptr; }
41
1/2
✓ Branch 0 taken 9343 times.
✗ Branch 1 not taken.
9343 void operator()() const { if (fn_) fn_(ctx_); }
42 };
43
44 // Create timer implementation
45 virtual timer::timer_impl* create_impl() = 0;
46
47 // Query methods for scheduler
48 virtual bool empty() const noexcept = 0;
49 virtual time_point nearest_expiry() const noexcept = 0;
50
51 // Process expired timers - scheduler calls this after wait
52 virtual std::size_t process_expired() = 0;
53
54 // Callback for when earliest timer changes
55 virtual void set_on_earliest_changed(callback cb) = 0;
56
57 protected:
58 309 timer_service() = default;
59 };
60
61 // Get or create the timer service for the given context
62 timer_service&
63 get_timer_service(
64 capy::execution_context& ctx, scheduler& sched);
65
66 } // namespace boost::corosio::detail
67
68 #endif
69