include/boost/corosio/detail/cancel_at_awaitable.hpp
100.0% Lines (45/45)
91.7% List of functions (22/24)
100.0% Branches (13/13)
Functions (24)
Function
Calls
Lines
Branches
Blocks
boost::corosio::detail::cancel_at_awaitable<boost::corosio::io_timer::wait_awaitable, boost::corosio::native_timer<boost::corosio::iocp_t{}>, true>::stop_forwarder::operator()() const
:66
0
0.0%
–
0.0%
boost::corosio::detail::cancel_at_awaitable<boost::corosio::io_timer::wait_awaitable, boost::corosio::timer, false>::stop_forwarder::operator()() const
:66
1x
100.0%
–
100.0%
boost::corosio::detail::cancel_at_awaitable<boost::corosio::io_timer::wait_awaitable, boost::corosio::timer, true>::stop_forwarder::operator()() const
:66
0
0.0%
–
0.0%
_ZN5boost7corosio6detail19cancel_at_awaitableINS0_8io_timer14wait_awaitableENS0_5timerELb0EEC1EOS4_RS5_NSt6chrono10time_pointINS9_3_V212steady_clockENS9_8durationIxSt5ratioILx1ELx1000000000EEEEEEQntT1_
:86
9x
100.0%
100.0%
85.7%
_ZN5boost7corosio6detail19cancel_at_awaitableINS0_8io_timer14wait_awaitableENS0_12native_timerIXtlNS0_6iocp_tEEEEELb1EEC1EOS4_NSt6chrono10time_pointINSA_3_V212steady_clockENSA_8durationIxSt5ratioILx1ELx1000000000EEEEEEQT1_
:95
3x
100.0%
100.0%
70.0%
_ZN5boost7corosio6detail19cancel_at_awaitableINS0_8io_timer14wait_awaitableENS0_5timerELb1EEC1EOS4_NSt6chrono10time_pointINS8_3_V212steady_clockENS8_8durationIxSt5ratioILx1ELx1000000000EEEEEEQT1_
:95
3x
100.0%
100.0%
70.0%
boost::corosio::detail::cancel_at_awaitable<boost::corosio::io_timer::wait_awaitable, boost::corosio::native_timer<boost::corosio::iocp_t{}>, true>::~cancel_at_awaitable()
:102
6x
100.0%
–
100.0%
boost::corosio::detail::cancel_at_awaitable<boost::corosio::io_timer::wait_awaitable, boost::corosio::timer, false>::~cancel_at_awaitable()
:102
18x
100.0%
–
100.0%
boost::corosio::detail::cancel_at_awaitable<boost::corosio::io_timer::wait_awaitable, boost::corosio::timer, true>::~cancel_at_awaitable()
:102
6x
100.0%
–
100.0%
boost::corosio::detail::cancel_at_awaitable<boost::corosio::io_timer::wait_awaitable, boost::corosio::native_timer<boost::corosio::iocp_t{}>, true>::cancel_at_awaitable(boost::corosio::detail::cancel_at_awaitable<boost::corosio::io_timer::wait_awaitable, boost::corosio::native_timer<boost::corosio::iocp_t{}>, true>&&)
:108
3x
100.0%
–
100.0%
boost::corosio::detail::cancel_at_awaitable<boost::corosio::io_timer::wait_awaitable, boost::corosio::timer, false>::cancel_at_awaitable(boost::corosio::detail::cancel_at_awaitable<boost::corosio::io_timer::wait_awaitable, boost::corosio::timer, false>&&)
:108
9x
100.0%
–
100.0%
boost::corosio::detail::cancel_at_awaitable<boost::corosio::io_timer::wait_awaitable, boost::corosio::timer, true>::cancel_at_awaitable(boost::corosio::detail::cancel_at_awaitable<boost::corosio::io_timer::wait_awaitable, boost::corosio::timer, true>&&)
:108
3x
100.0%
–
100.0%
boost::corosio::detail::cancel_at_awaitable<boost::corosio::io_timer::wait_awaitable, boost::corosio::native_timer<boost::corosio::iocp_t{}>, true>::await_ready() const
:121
3x
100.0%
–
100.0%
boost::corosio::detail::cancel_at_awaitable<boost::corosio::io_timer::wait_awaitable, boost::corosio::timer, false>::await_ready() const
:121
9x
100.0%
–
100.0%
boost::corosio::detail::cancel_at_awaitable<boost::corosio::io_timer::wait_awaitable, boost::corosio::timer, true>::await_ready() const
:121
3x
100.0%
–
100.0%
boost::corosio::detail::cancel_at_awaitable<boost::corosio::io_timer::wait_awaitable, boost::corosio::native_timer<boost::corosio::iocp_t{}>, true>::await_suspend(std::__n4861::coroutine_handle<void>, boost::capy::io_env const*)
:126
3x
100.0%
100.0%
88.5%
boost::corosio::detail::cancel_at_awaitable<boost::corosio::io_timer::wait_awaitable, boost::corosio::timer, false>::await_suspend(std::__n4861::coroutine_handle<void>, boost::capy::io_env const*)
:126
9x
100.0%
100.0%
86.4%
boost::corosio::detail::cancel_at_awaitable<boost::corosio::io_timer::wait_awaitable, boost::corosio::timer, true>::await_suspend(std::__n4861::coroutine_handle<void>, boost::capy::io_env const*)
:126
3x
100.0%
100.0%
88.5%
boost::corosio::detail::cancel_at_awaitable<boost::corosio::io_timer::wait_awaitable, boost::corosio::native_timer<boost::corosio::iocp_t{}>, true>::await_resume()
:152
3x
100.0%
–
100.0%
boost::corosio::detail::cancel_at_awaitable<boost::corosio::io_timer::wait_awaitable, boost::corosio::timer, false>::await_resume()
:152
9x
100.0%
–
100.0%
boost::corosio::detail::cancel_at_awaitable<boost::corosio::io_timer::wait_awaitable, boost::corosio::timer, true>::await_resume()
:152
3x
100.0%
–
100.0%
boost::corosio::detail::cancel_at_awaitable<boost::corosio::io_timer::wait_awaitable, boost::corosio::native_timer<boost::corosio::iocp_t{}>, true>::destroy_parent_cb()
:160
9x
100.0%
100.0%
100.0%
boost::corosio::detail::cancel_at_awaitable<boost::corosio::io_timer::wait_awaitable, boost::corosio::timer, false>::destroy_parent_cb()
:160
27x
100.0%
100.0%
100.0%
boost::corosio::detail::cancel_at_awaitable<boost::corosio::io_timer::wait_awaitable, boost::corosio::timer, true>::destroy_parent_cb()
:160
9x
100.0%
100.0%
100.0%
| Line | Branch | 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_DETAIL_CANCEL_AT_AWAITABLE_HPP | |||
| 11 | #define BOOST_COROSIO_DETAIL_CANCEL_AT_AWAITABLE_HPP | |||
| 12 | ||||
| 13 | #include <boost/corosio/detail/timeout_coro.hpp> | |||
| 14 | #include <boost/capy/ex/io_env.hpp> | |||
| 15 | ||||
| 16 | #include <chrono> | |||
| 17 | #include <coroutine> | |||
| 18 | #include <new> | |||
| 19 | #include <optional> | |||
| 20 | #include <stop_token> | |||
| 21 | #include <type_traits> | |||
| 22 | #include <utility> | |||
| 23 | ||||
| 24 | /* Races an inner IoAwaitable against a timer via a shared | |||
| 25 | stop_source. await_suspend arms the timer by launching a | |||
| 26 | fire-and-forget timeout_coro, then starts the inner op with | |||
| 27 | an interposed stop_token. Whichever completes first signals | |||
| 28 | the stop_source, cancelling the other. | |||
| 29 | ||||
| 30 | Parent cancellation is forwarded through a stop_callback | |||
| 31 | stored in a placement-new buffer (stop_callback is not | |||
| 32 | movable, but the awaitable must be movable for | |||
| 33 | transform_awaiter). The buffer is inert during moves | |||
| 34 | (before await_suspend) and constructed in-place once the | |||
| 35 | awaitable is pinned on the coroutine frame. | |||
| 36 | ||||
| 37 | The timeout_coro can outlive this awaitable — it owns its | |||
| 38 | env and self-destroys via suspend_never. When Owning is | |||
| 39 | false the caller-supplied timer must outlive both; when | |||
| 40 | Owning is true the timer lives in std::optional and is | |||
| 41 | constructed lazily in await_suspend. */ | |||
| 42 | ||||
| 43 | namespace boost::corosio::detail { | |||
| 44 | ||||
| 45 | /** Awaitable adapter that cancels an inner operation after a deadline. | |||
| 46 | ||||
| 47 | Races the inner awaitable against a timer. A shared stop_source | |||
| 48 | ties them together: whichever completes first cancels the other. | |||
| 49 | Parent cancellation is forwarded via stop_callback. | |||
| 50 | ||||
| 51 | When @p Owning is `false` (default), the caller supplies a timer | |||
| 52 | reference that must outlive the awaitable. When @p Owning is | |||
| 53 | `true`, the timer is constructed internally in `await_suspend` | |||
| 54 | from the execution context in `io_env`. | |||
| 55 | ||||
| 56 | @tparam A The inner IoAwaitable type (decayed). | |||
| 57 | @tparam Timer The timer type (`timer` or `native_timer<B>`). | |||
| 58 | @tparam Owning When `true`, the awaitable owns its timer. | |||
| 59 | */ | |||
| 60 | template<typename A, typename Timer, bool Owning = false> | |||
| 61 | struct cancel_at_awaitable | |||
| 62 | { | |||
| 63 | struct stop_forwarder | |||
| 64 | { | |||
| 65 | std::stop_source* src_; | |||
| 66 | 1x | void operator()() const noexcept | ||
| 67 | { | |||
| 68 | 1x | src_->request_stop(); | ||
| 69 | 1x | } | ||
| 70 | }; | |||
| 71 | ||||
| 72 | using time_point = std::chrono::steady_clock::time_point; | |||
| 73 | using stop_cb_type = std::stop_callback<stop_forwarder>; | |||
| 74 | using timer_storage = | |||
| 75 | std::conditional_t<Owning, std::optional<Timer>, Timer*>; | |||
| 76 | ||||
| 77 | A inner_; | |||
| 78 | timer_storage timer_; | |||
| 79 | time_point deadline_; | |||
| 80 | std::stop_source stop_src_; | |||
| 81 | capy::io_env inner_env_; | |||
| 82 | alignas(stop_cb_type) unsigned char cb_buf_[sizeof(stop_cb_type)]; | |||
| 83 | bool cb_active_ = false; | |||
| 84 | ||||
| 85 | /// Construct with a caller-supplied timer reference. | |||
| 86 | 9x | cancel_at_awaitable(A&& inner, Timer& timer, time_point deadline) | ||
| 87 | requires(!Owning) | |||
| 88 | 9x | : inner_(std::move(inner)) | ||
| 89 | 9x | , timer_(&timer) | ||
| 90 |
1/1✓ Branch 4 → 5 taken 9 times.
|
9x | , deadline_(deadline) | |
| 91 | { | |||
| 92 | 9x | } | ||
| 93 | ||||
| 94 | /// Construct without a timer (created in `await_suspend`). | |||
| 95 | 6x | cancel_at_awaitable(A&& inner, time_point deadline) | ||
| 96 | requires Owning | |||
| 97 | 6x | : inner_(std::move(inner)) | ||
| 98 |
1/1✓ Branch 5 → 6 taken 6 times.
|
6x | , deadline_(deadline) | |
| 99 | { | |||
| 100 | 6x | } | ||
| 101 | ||||
| 102 | 30x | ~cancel_at_awaitable() | ||
| 103 | { | |||
| 104 | 30x | destroy_parent_cb(); | ||
| 105 | 30x | } | ||
| 106 | ||||
| 107 | // Only moved before await_suspend, when cb_active_ is false | |||
| 108 | 15x | cancel_at_awaitable(cancel_at_awaitable&& o) noexcept( | ||
| 109 | std::is_nothrow_move_constructible_v<A>) | |||
| 110 | 15x | : inner_(std::move(o.inner_)) | ||
| 111 | 15x | , timer_(std::move(o.timer_)) | ||
| 112 | 15x | , deadline_(o.deadline_) | ||
| 113 | 15x | , stop_src_(std::move(o.stop_src_)) | ||
| 114 | { | |||
| 115 | 15x | } | ||
| 116 | ||||
| 117 | cancel_at_awaitable(cancel_at_awaitable const&) = delete; | |||
| 118 | cancel_at_awaitable& operator=(cancel_at_awaitable const&) = delete; | |||
| 119 | cancel_at_awaitable& operator=(cancel_at_awaitable&&) = delete; | |||
| 120 | ||||
| 121 | 15x | bool await_ready() const noexcept | ||
| 122 | { | |||
| 123 | 15x | return false; | ||
| 124 | } | |||
| 125 | ||||
| 126 | 15x | auto await_suspend(std::coroutine_handle<> h, capy::io_env const* env) | ||
| 127 | { | |||
| 128 | if constexpr (Owning) | |||
| 129 |
1/1✓ Branch 3 → 4 taken 6 times.
|
6x | timer_.emplace(env->executor.context()); | |
| 130 | ||||
| 131 |
2/2✓ Branch 2 → 3 taken 9 times.
✓ Branch 5 → 6 taken 6 times.
|
15x | timer_->expires_at(deadline_); | |
| 132 | ||||
| 133 | // Launch fire-and-forget timeout (starts suspended) | |||
| 134 |
2/2✓ Branch 4 → 5 taken 9 times.
✓ Branch 8 → 9 taken 6 times.
|
15x | auto timeout = make_timeout(*timer_, stop_src_); | |
| 135 | 30x | timeout.h_.promise().set_env_owned( | ||
| 136 | 15x | {env->executor, stop_src_.get_token(), env->frame_allocator}); | ||
| 137 | // Runs synchronously until timer.wait() suspends | |||
| 138 |
2/2✓ Branch 10 → 11 taken 9 times.
✓ Branch 14 → 15 taken 6 times.
|
15x | timeout.h_.resume(); | |
| 139 | // timeout goes out of scope; destructor is a no-op, | |||
| 140 | // the coroutine self-destroys via suspend_never | |||
| 141 | ||||
| 142 | // Forward parent cancellation | |||
| 143 | 15x | new (cb_buf_) stop_cb_type(env->stop_token, stop_forwarder{&stop_src_}); | ||
| 144 | 15x | cb_active_ = true; | ||
| 145 | ||||
| 146 | // Start the inner op with our interposed stop_token | |||
| 147 | inner_env_ = { | |||
| 148 | 15x | env->executor, stop_src_.get_token(), env->frame_allocator}; | ||
| 149 |
2/2✓ Branch 16 → 17 taken 9 times.
✓ Branch 20 → 21 taken 6 times.
|
30x | return inner_.await_suspend(h, &inner_env_); | |
| 150 | 30x | } | ||
| 151 | ||||
| 152 | 15x | decltype(auto) await_resume() | ||
| 153 | { | |||
| 154 | // Cancel whichever is still pending (idempotent) | |||
| 155 | 15x | stop_src_.request_stop(); | ||
| 156 | 15x | destroy_parent_cb(); | ||
| 157 | 15x | return inner_.await_resume(); | ||
| 158 | } | |||
| 159 | ||||
| 160 | 45x | void destroy_parent_cb() noexcept | ||
| 161 | { | |||
| 162 |
2/2✓ Branch 2 → 3 taken 15 times.
✓ Branch 2 → 6 taken 30 times.
|
45x | if (cb_active_) | |
| 163 | { | |||
| 164 | 15x | std::launder(reinterpret_cast<stop_cb_type*>(cb_buf_)) | ||
| 165 | 15x | ->~stop_cb_type(); | ||
| 166 | 15x | cb_active_ = false; | ||
| 167 | } | |||
| 168 | 45x | } | ||
| 169 | }; | |||
| 170 | ||||
| 171 | } // namespace boost::corosio::detail | |||
| 172 | ||||
| 173 | #endif | |||
| 174 |