include/boost/corosio/native/native_timer.hpp
93.3% Lines (28/30)
87.5% List of functions (14/16)
Functions (16)
Function
Calls
Lines
Blocks
boost::corosio::native_timer<boost::corosio::epoll_t{}>::get_impl()
:46
5x
100.0%
100.0%
boost::corosio::native_timer<boost::corosio::select_t{}>::get_impl()
:46
5x
100.0%
100.0%
boost::corosio::native_timer<boost::corosio::epoll_t{}>::native_wait_awaitable::native_wait_awaitable(boost::corosio::native_timer<boost::corosio::epoll_t{}>&)
:58
5x
100.0%
100.0%
boost::corosio::native_timer<boost::corosio::select_t{}>::native_wait_awaitable::native_wait_awaitable(boost::corosio::native_timer<boost::corosio::select_t{}>&)
:58
5x
100.0%
100.0%
boost::corosio::native_timer<boost::corosio::epoll_t{}>::native_wait_awaitable::await_ready() const
:63
5x
100.0%
100.0%
boost::corosio::native_timer<boost::corosio::select_t{}>::native_wait_awaitable::await_ready() const
:63
5x
100.0%
100.0%
boost::corosio::native_timer<boost::corosio::epoll_t{}>::native_wait_awaitable::await_resume() const
:68
5x
75.0%
57.0%
boost::corosio::native_timer<boost::corosio::select_t{}>::native_wait_awaitable::await_resume() const
:68
5x
75.0%
57.0%
boost::corosio::native_timer<boost::corosio::epoll_t{}>::native_wait_awaitable::await_suspend(std::__n4861::coroutine_handle<void>, boost::capy::io_env const*)
:75
5x
100.0%
84.0%
boost::corosio::native_timer<boost::corosio::select_t{}>::native_wait_awaitable::await_suspend(std::__n4861::coroutine_handle<void>, boost::capy::io_env const*)
:75
5x
100.0%
84.0%
boost::corosio::native_timer<boost::corosio::epoll_t{}>::native_timer(boost::capy::execution_context&)
:100
7x
100.0%
100.0%
boost::corosio::native_timer<boost::corosio::select_t{}>::native_timer(boost::capy::execution_context&)
:100
7x
100.0%
100.0%
boost::corosio::native_timer<boost::corosio::epoll_t{}>::native_timer<long, std::ratio<1l, 1000l> >(boost::capy::execution_context&, std::chrono::duration<long, std::ratio<1l, 1000l> >)
:115
1x
100.0%
100.0%
boost::corosio::native_timer<boost::corosio::select_t{}>::native_timer<long, std::ratio<1l, 1000l> >(boost::capy::execution_context&, std::chrono::duration<long, std::ratio<1l, 1000l> >)
:115
1x
100.0%
100.0%
boost::corosio::native_timer<boost::corosio::epoll_t{}>::native_timer(boost::corosio::native_timer<boost::corosio::epoll_t{}>&&)
:129
0
0.0%
0.0%
boost::corosio::native_timer<boost::corosio::select_t{}>::native_timer(boost::corosio::native_timer<boost::corosio::select_t{}>&&)
:129
0
0.0%
0.0%
| Line | TLA | Hits | Source Code |
|---|---|---|---|
| 1 | // | ||
| 2 | // Copyright (c) 2026 Steve Gerbino | ||
| 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_NATIVE_NATIVE_TIMER_HPP | ||
| 11 | #define BOOST_COROSIO_NATIVE_NATIVE_TIMER_HPP | ||
| 12 | |||
| 13 | #include <boost/corosio/timer.hpp> | ||
| 14 | #include <boost/corosio/backend.hpp> | ||
| 15 | #include <boost/corosio/detail/timer_service.hpp> | ||
| 16 | |||
| 17 | namespace boost::corosio { | ||
| 18 | |||
| 19 | /** An asynchronous timer with devirtualized wait operations. | ||
| 20 | |||
| 21 | This class template inherits from @ref timer and shadows the | ||
| 22 | `wait` operation with a version that calls the backend | ||
| 23 | implementation directly, allowing the compiler to inline | ||
| 24 | through the entire call chain. | ||
| 25 | |||
| 26 | Non-async operations (`cancel`, `expires_at`, `expires_after`) | ||
| 27 | remain unchanged and dispatch through the compiled library. | ||
| 28 | |||
| 29 | A `native_timer` IS-A `timer` and can be passed to any function | ||
| 30 | expecting `timer&`. | ||
| 31 | |||
| 32 | @tparam Backend A backend tag value (e.g., `epoll`). | ||
| 33 | The timer implementation is backend-independent; the | ||
| 34 | tag selects the concrete impl type for devirtualization. | ||
| 35 | |||
| 36 | @par Thread Safety | ||
| 37 | Same as @ref timer. | ||
| 38 | |||
| 39 | @see timer, epoll_t, iocp_t | ||
| 40 | */ | ||
| 41 | template<auto Backend> | ||
| 42 | class native_timer : public timer | ||
| 43 | { | ||
| 44 | using impl_type = detail::timer_service::implementation; | ||
| 45 | |||
| 46 | 10x | impl_type& get_impl() noexcept | |
| 47 | { | ||
| 48 | 10x | return *static_cast<impl_type*>(h_.get()); | |
| 49 | } | ||
| 50 | |||
| 51 | struct native_wait_awaitable | ||
| 52 | { | ||
| 53 | native_timer& self_; | ||
| 54 | std::stop_token token_; | ||
| 55 | mutable std::error_code ec_; | ||
| 56 | detail::continuation_op cont_op_; | ||
| 57 | |||
| 58 | 10x | explicit native_wait_awaitable(native_timer& self) noexcept | |
| 59 | 10x | : self_(self) | |
| 60 | { | ||
| 61 | 10x | } | |
| 62 | |||
| 63 | 10x | bool await_ready() const noexcept | |
| 64 | { | ||
| 65 | 10x | return token_.stop_requested(); | |
| 66 | } | ||
| 67 | |||
| 68 | 10x | capy::io_result<> await_resume() const noexcept | |
| 69 | { | ||
| 70 | 10x | if (token_.stop_requested()) | |
| 71 | ✗ | return {capy::error::canceled}; | |
| 72 | 10x | return {ec_}; | |
| 73 | } | ||
| 74 | |||
| 75 | 10x | auto await_suspend(std::coroutine_handle<> h, capy::io_env const* env) | |
| 76 | -> std::coroutine_handle<> | ||
| 77 | { | ||
| 78 | 10x | token_ = env->stop_token; | |
| 79 | 10x | cont_op_.cont.h = h; | |
| 80 | 10x | auto& impl = self_.get_impl(); | |
| 81 | // Fast path: already expired and not in the heap | ||
| 82 | 20x | if (impl.heap_index_ == timer::implementation::npos && | |
| 83 | 20x | (impl.expiry_ == (time_point::min)() || | |
| 84 | 20x | impl.expiry_ <= clock_type::now())) | |
| 85 | { | ||
| 86 | 2x | ec_ = {}; | |
| 87 | 2x | auto d = env->executor; | |
| 88 | 2x | d.post(cont_op_.cont); | |
| 89 | 2x | return std::noop_coroutine(); | |
| 90 | } | ||
| 91 | 8x | return impl.wait(h, env->executor, std::move(token_), &ec_, &cont_op_.cont); | |
| 92 | } | ||
| 93 | }; | ||
| 94 | |||
| 95 | public: | ||
| 96 | /** Construct a native timer from an execution context. | ||
| 97 | |||
| 98 | @param ctx The execution context that will own this timer. | ||
| 99 | */ | ||
| 100 | 14x | explicit native_timer(capy::execution_context& ctx) : timer(ctx) {} | |
| 101 | |||
| 102 | /** Construct a native timer with an initial absolute expiry. | ||
| 103 | |||
| 104 | @param ctx The execution context that will own this timer. | ||
| 105 | @param t The initial expiry time point. | ||
| 106 | */ | ||
| 107 | native_timer(capy::execution_context& ctx, time_point t) : timer(ctx, t) {} | ||
| 108 | |||
| 109 | /** Construct a native timer with an initial relative expiry. | ||
| 110 | |||
| 111 | @param ctx The execution context that will own this timer. | ||
| 112 | @param d The initial expiry duration relative to now. | ||
| 113 | */ | ||
| 114 | template<class Rep, class Period> | ||
| 115 | 2x | native_timer( | |
| 116 | capy::execution_context& ctx, std::chrono::duration<Rep, Period> d) | ||
| 117 | 2x | : timer(ctx, d) | |
| 118 | { | ||
| 119 | 2x | } | |
| 120 | |||
| 121 | /** Move construct. | ||
| 122 | |||
| 123 | @param other The timer to move from. | ||
| 124 | |||
| 125 | @pre No awaitables returned by @p other's methods exist. | ||
| 126 | @pre The execution context associated with @p other must | ||
| 127 | outlive this timer. | ||
| 128 | */ | ||
| 129 | ✗ | native_timer(native_timer&&) noexcept = default; | |
| 130 | |||
| 131 | /** Move assign. | ||
| 132 | |||
| 133 | @param other The timer to move from. | ||
| 134 | |||
| 135 | @pre No awaitables returned by either `*this` or @p other's | ||
| 136 | methods exist. | ||
| 137 | @pre The execution context associated with @p other must | ||
| 138 | outlive this timer. | ||
| 139 | */ | ||
| 140 | native_timer& operator=(native_timer&&) noexcept = default; | ||
| 141 | |||
| 142 | native_timer(native_timer const&) = delete; | ||
| 143 | native_timer& operator=(native_timer const&) = delete; | ||
| 144 | |||
| 145 | /** Wait for the timer to expire. | ||
| 146 | |||
| 147 | Calls the backend implementation directly, bypassing virtual | ||
| 148 | dispatch. Otherwise identical to @ref timer::wait. | ||
| 149 | |||
| 150 | @return An awaitable yielding `io_result<>`. | ||
| 151 | |||
| 152 | This timer must outlive the returned awaitable. | ||
| 153 | */ | ||
| 154 | 10x | auto wait() | |
| 155 | { | ||
| 156 | 10x | return native_wait_awaitable(*this); | |
| 157 | } | ||
| 158 | }; | ||
| 159 | |||
| 160 | } // namespace boost::corosio | ||
| 161 | |||
| 162 | #endif | ||
| 163 |