100.00% Lines (62/62) 100.00% Functions (9/9)
TLA Baseline Branch
Line Hits Code Line Hits Code
1   // 1   //
2   // Copyright (c) 2026 Steve Gerbino 2   // Copyright (c) 2026 Steve Gerbino
3   // 3   //
4   // Distributed under the Boost Software License, Version 1.0. (See accompanying 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) 5   // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6   // 6   //
7   // Official repository: https://github.com/cppalliance/corosio 7   // Official repository: https://github.com/cppalliance/corosio
8   // 8   //
9   9  
10   #ifndef BOOST_COROSIO_DETAIL_TIMEOUT_AWAITABLE_HPP 10   #ifndef BOOST_COROSIO_DETAIL_TIMEOUT_AWAITABLE_HPP
11   #define BOOST_COROSIO_DETAIL_TIMEOUT_AWAITABLE_HPP 11   #define BOOST_COROSIO_DETAIL_TIMEOUT_AWAITABLE_HPP
12   12  
13   #include <boost/corosio/io_context.hpp> 13   #include <boost/corosio/io_context.hpp>
14   #include <boost/corosio/detail/timeout_coro.hpp> 14   #include <boost/corosio/detail/timeout_coro.hpp>
15   #include <boost/corosio/detail/timer.hpp> 15   #include <boost/corosio/detail/timer.hpp>
16   #include <boost/corosio/detail/except.hpp> 16   #include <boost/corosio/detail/except.hpp>
17   #include <boost/capy/cond.hpp> 17   #include <boost/capy/cond.hpp>
18   #include <boost/capy/error.hpp> 18   #include <boost/capy/error.hpp>
19   #include <boost/capy/ex/io_env.hpp> 19   #include <boost/capy/ex/io_env.hpp>
20   #include <boost/capy/io_result.hpp> 20   #include <boost/capy/io_result.hpp>
21   21  
22   #include <chrono> 22   #include <chrono>
23   #include <coroutine> 23   #include <coroutine>
24   #include <new> 24   #include <new>
25   #include <optional> 25   #include <optional>
26   #include <stdexcept> 26   #include <stdexcept>
27   #include <stop_token> 27   #include <stop_token>
28   #include <type_traits> 28   #include <type_traits>
29   #include <utility> 29   #include <utility>
30   30  
31   /* Races an inner IoAwaitable against a timer via a shared 31   /* Races an inner IoAwaitable against a timer via a shared
32   stop_source. await_suspend arms the timer by launching a 32   stop_source. await_suspend arms the timer by launching a
33   fire-and-forget timeout_coro, then starts the inner op with 33   fire-and-forget timeout_coro, then starts the inner op with
34   an interposed stop_token. Whichever completes first signals 34   an interposed stop_token. Whichever completes first signals
35   the stop_source, cancelling the other. 35   the stop_source, cancelling the other.
36   36  
37   Parent cancellation is forwarded through a stop_callback 37   Parent cancellation is forwarded through a stop_callback
38   stored in a placement-new buffer (stop_callback is not 38   stored in a placement-new buffer (stop_callback is not
39   movable, but the awaitable must be movable for 39   movable, but the awaitable must be movable for
40   transform_awaiter). The buffer is inert during moves 40   transform_awaiter). The buffer is inert during moves
41   (before await_suspend) and constructed in-place once the 41   (before await_suspend) and constructed in-place once the
42   awaitable is pinned on the coroutine frame. 42   awaitable is pinned on the coroutine frame.
43   43  
44   The timeout_coro can outlive this awaitable — it owns its 44   The timeout_coro can outlive this awaitable — it owns its
45   env and self-destroys via suspend_never. The timer lives in 45   env and self-destroys via suspend_never. The timer lives in
46   std::optional and is constructed lazily in await_suspend, 46   std::optional and is constructed lazily in await_suspend,
47   once the awaiting coroutine's executor context is known. */ 47   once the awaiting coroutine's executor context is known. */
48   48  
49   namespace boost::corosio::detail { 49   namespace boost::corosio::detail {
50   50  
51   // Local stand-in for capy::detail's io_result trait: corosio must not 51   // Local stand-in for capy::detail's io_result trait: corosio must not
52   // reach into capy::detail, but the result-mapping switch in 52   // reach into capy::detail, but the result-mapping switch in
53   // await_resume needs to distinguish io_result from other return types. 53   // await_resume needs to distinguish io_result from other return types.
54   template<typename T> 54   template<typename T>
55   struct is_io_result : std::false_type 55   struct is_io_result : std::false_type
56   { 56   {
57   }; 57   };
58   58  
59   template<typename... Ts> 59   template<typename... Ts>
60   struct is_io_result<capy::io_result<Ts...>> : std::true_type 60   struct is_io_result<capy::io_result<Ts...>> : std::true_type
61   { 61   {
62   }; 62   };
63   63  
64   template<typename T> 64   template<typename T>
65   inline constexpr bool is_io_result_v = is_io_result<T>::value; 65   inline constexpr bool is_io_result_v = is_io_result<T>::value;
66   66  
67   /** Awaitable adapter that cancels an inner operation after a deadline. 67   /** Awaitable adapter that cancels an inner operation after a deadline.
68   68  
69   Races the inner awaitable against a timer. A shared stop_source 69   Races the inner awaitable against a timer. A shared stop_source
70   ties them together: whichever completes first cancels the other. 70   ties them together: whichever completes first cancels the other.
71   Parent cancellation is forwarded via stop_callback. 71   Parent cancellation is forwarded via stop_callback.
72   72  
73   The timer is constructed internally in `await_suspend` from the 73   The timer is constructed internally in `await_suspend` from the
74   execution context in `io_env`. 74   execution context in `io_env`.
75   75  
76   @tparam A The inner IoAwaitable type (decayed). 76   @tparam A The inner IoAwaitable type (decayed).
77   */ 77   */
78   template<typename A> 78   template<typename A>
79   struct timeout_awaitable 79   struct timeout_awaitable
80   { 80   {
81   struct stop_forwarder 81   struct stop_forwarder
82   { 82   {
83   std::stop_source* src_; 83   std::stop_source* src_;
HITCBC 84   2003 void operator()() const noexcept 84   2004 void operator()() const noexcept
85   { 85   {
HITCBC 86   2003 src_->request_stop(); 86   2004 src_->request_stop();
HITCBC 87   2003 } 87   2004 }
88   }; 88   };
89   89  
90   using time_point = std::chrono::steady_clock::time_point; 90   using time_point = std::chrono::steady_clock::time_point;
91   using stop_cb_type = std::stop_callback<stop_forwarder>; 91   using stop_cb_type = std::stop_callback<stop_forwarder>;
92   92  
93   A inner_; 93   A inner_;
94   std::optional<timer> timer_; 94   std::optional<timer> timer_;
95   time_point deadline_; 95   time_point deadline_;
96   std::chrono::nanoseconds dur_{}; 96   std::chrono::nanoseconds dur_{};
97   bool has_deadline_ = true; 97   bool has_deadline_ = true;
98   std::stop_source stop_src_; 98   std::stop_source stop_src_;
99   std::stop_token parent_token_; 99   std::stop_token parent_token_;
100   capy::io_env inner_env_; 100   capy::io_env inner_env_;
101   alignas(stop_cb_type) unsigned char cb_buf_[sizeof(stop_cb_type)]; 101   alignas(stop_cb_type) unsigned char cb_buf_[sizeof(stop_cb_type)];
102   bool cb_active_ = false; 102   bool cb_active_ = false;
103   103  
104   /// Construct without a timer, deadline given as an absolute time. 104   /// Construct without a timer, deadline given as an absolute time.
HITCBC 105   4 timeout_awaitable(A&& inner, time_point deadline) 105   4 timeout_awaitable(A&& inner, time_point deadline)
HITCBC 106   4 : inner_(std::move(inner)) 106   4 : inner_(std::move(inner))
HITCBC 107   4 , deadline_(deadline) 107   4 , deadline_(deadline)
108   { 108   {
HITCBC 109   4 } 109   4 }
110   110  
111   /// Construct without a timer, deadline measured from suspension. 111   /// Construct without a timer, deadline measured from suspension.
HITCBC 112   2038 timeout_awaitable(A&& inner, std::chrono::nanoseconds dur) 112   2038 timeout_awaitable(A&& inner, std::chrono::nanoseconds dur)
HITCBC 113   2038 : inner_(std::move(inner)) 113   2038 : inner_(std::move(inner))
HITCBC 114   2038 , dur_(dur) 114   2038 , dur_(dur)
HITCBC 115   2038 , has_deadline_(false) 115   2038 , has_deadline_(false)
116   { 116   {
HITCBC 117   2038 } 117   2038 }
118   118  
HITCBC 119   4084 ~timeout_awaitable() 119   4084 ~timeout_awaitable()
120   { 120   {
HITCBC 121   4084 destroy_parent_cb(); 121   4084 destroy_parent_cb();
HITCBC 122   4084 } 122   4084 }
123   123  
124   // Only moved before await_suspend, when cb_active_ is false 124   // Only moved before await_suspend, when cb_active_ is false
HITCBC 125   2042 timeout_awaitable(timeout_awaitable&& o) noexcept( 125   2042 timeout_awaitable(timeout_awaitable&& o) noexcept(
126   std::is_nothrow_move_constructible_v<A>) 126   std::is_nothrow_move_constructible_v<A>)
HITCBC 127   2042 : inner_(std::move(o.inner_)) 127   2042 : inner_(std::move(o.inner_))
HITCBC 128   2042 , timer_(std::move(o.timer_)) 128   2042 , timer_(std::move(o.timer_))
HITCBC 129   2042 , deadline_(o.deadline_) 129   2042 , deadline_(o.deadline_)
HITCBC 130   2042 , dur_(o.dur_) 130   2042 , dur_(o.dur_)
HITCBC 131   2042 , has_deadline_(o.has_deadline_) 131   2042 , has_deadline_(o.has_deadline_)
HITCBC 132   2042 , stop_src_(std::move(o.stop_src_)) 132   2042 , stop_src_(std::move(o.stop_src_))
133   { 133   {
HITCBC 134   2042 } 134   2042 }
135   135  
136   timeout_awaitable(timeout_awaitable const&) = delete; 136   timeout_awaitable(timeout_awaitable const&) = delete;
137   timeout_awaitable& operator=(timeout_awaitable const&) = delete; 137   timeout_awaitable& operator=(timeout_awaitable const&) = delete;
138   timeout_awaitable& operator=(timeout_awaitable&&) = delete; 138   timeout_awaitable& operator=(timeout_awaitable&&) = delete;
139   139  
HITCBC 140   2038 bool await_ready() const noexcept 140   2038 bool await_ready() const noexcept
141   { 141   {
HITCBC 142   2038 return false; 142   2038 return false;
143   } 143   }
144   144  
HITCBC 145   2042 auto await_suspend(std::coroutine_handle<> h, capy::io_env const* env) 145   2042 auto await_suspend(std::coroutine_handle<> h, capy::io_env const* env)
146   { 146   {
HITCBC 147   2042 parent_token_ = env->stop_token; 147   2042 parent_token_ = env->stop_token;
148   148  
149   // The deadline timer is built here from the awaiting 149   // The deadline timer is built here from the awaiting
150   // coroutine's executor context, the first point at which it 150   // coroutine's executor context, the first point at which it
151   // is known. await_suspend is driven through a noexcept 151   // is known. await_suspend is driven through a noexcept
152   // wrapper, so a failure cannot be surfaced as a catchable 152   // wrapper, so a failure cannot be surfaced as a catchable
153   // exception. An executor whose context is not an io_context 153   // exception. An executor whose context is not an io_context
154   // cannot supply a timer service; silently running the 154   // cannot supply a timer service; silently running the
155   // operation with no deadline would be a worse failure than 155   // operation with no deadline would be a worse failure than
156   // aborting, so translate the service-lookup error into a 156   // aborting, so translate the service-lookup error into a
157   // clear precondition diagnostic. This terminates by design 157   // clear precondition diagnostic. This terminates by design
158   // (a usage error) rather than dropping the requested timeout. 158   // (a usage error) rather than dropping the requested timeout.
159   // The detached timeout coroutine must own its executor by 159   // The detached timeout coroutine must own its executor by
160   // value (see timeout_coro::set_env_owned); io_env carries 160   // value (see timeout_coro::set_env_owned); io_env carries
161   // only a non-owning executor_ref. Recover the concrete 161   // only a non-owning executor_ref. Recover the concrete
162   // executor from the context rather than the executor_ref: 162   // executor from the context rather than the executor_ref:
163   // wrapped executors (a strand over the io_context) satisfy 163   // wrapped executors (a strand over the io_context) satisfy
164   // the documented precondition but do not expose the io 164   // the documented precondition but do not expose the io
165   // executor as their target. The timer construction below 165   // executor as their target. The timer construction below
166   // validates the context is an io_context, and the detached 166   // validates the context is an io_context, and the detached
167   // coroutine shares only the thread-safe stop_source with 167   // coroutine shares only the thread-safe stop_source with
168   // the caller, so resuming it on the raw io executor instead 168   // the caller, so resuming it on the raw io executor instead
169   // of the caller's wrapper is safe. 169   // of the caller's wrapper is safe.
170   try 170   try
171   { 171   {
HITCBC 172   2042 timer_.emplace(env->executor.context()); 172   2042 timer_.emplace(env->executor.context());
173   } 173   }
HITCBC 174   4 catch (std::logic_error const&) 174   4 catch (std::logic_error const&)
175   { 175   {
HITCBC 176   2 throw_logic_error( 176   2 throw_logic_error(
177   "timeout requires an io_context-backed executor"); 177   "timeout requires an io_context-backed executor");
178   } 178   }
179   auto ex = static_cast<io_context&>( 179   auto ex = static_cast<io_context&>(
HITCBC 180   2040 env->executor.context()).get_executor(); 180   2040 env->executor.context()).get_executor();
181   181  
HITCBC 182   2040 if (has_deadline_) 182   2040 if (has_deadline_)
HITCBC 183   4 timer_->expires_at(deadline_); 183   4 timer_->expires_at(deadline_);
184   else 184   else
HITCBC 185   2036 timer_->expires_after(dur_); 185   2036 timer_->expires_after(dur_);
186   186  
187   // Launch fire-and-forget timeout (starts suspended) 187   // Launch fire-and-forget timeout (starts suspended)
HITCBC 188   2040 auto timeout = make_timeout(*timer_, stop_src_); 188   2040 auto timeout = make_timeout(*timer_, stop_src_);
HITCBC 189   4080 timeout.h_.promise().set_env_owned( 189   4080 timeout.h_.promise().set_env_owned(
HITCBC 190   2040 ex, stop_src_.get_token(), env->frame_allocator); 190   2040 ex, stop_src_.get_token(), env->frame_allocator);
191   // Runs synchronously until timer.wait() suspends 191   // Runs synchronously until timer.wait() suspends
HITCBC 192   2040 timeout.h_.resume(); 192   2040 timeout.h_.resume();
193   // timeout goes out of scope; destructor is a no-op, 193   // timeout goes out of scope; destructor is a no-op,
194   // the coroutine self-destroys via suspend_never 194   // the coroutine self-destroys via suspend_never
195   195  
196   // Forward parent cancellation 196   // Forward parent cancellation
HITCBC 197   2040 new (cb_buf_) stop_cb_type(env->stop_token, stop_forwarder{&stop_src_}); 197   2040 new (cb_buf_) stop_cb_type(env->stop_token, stop_forwarder{&stop_src_});
HITCBC 198   2040 cb_active_ = true; 198   2040 cb_active_ = true;
199   199  
200   // Start the inner op with our interposed stop_token 200   // Start the inner op with our interposed stop_token
HITCBC 201   2040 inner_env_ = { 201   2040 inner_env_ = {
HITCBC 202   2040 env->executor, stop_src_.get_token(), env->frame_allocator}; 202   2040 env->executor, stop_src_.get_token(), env->frame_allocator};
HITCBC 203   4080 return inner_.await_suspend(h, &inner_env_); 203   4080 return inner_.await_suspend(h, &inner_env_);
HITCBC 204   2040 } 204   2040 }
205   205  
HITCBC 206   2036 decltype(auto) await_resume() 206   2036 decltype(auto) await_resume()
207   { 207   {
208   // Read before request_stop: afterwards stop_requested() 208   // Read before request_stop: afterwards stop_requested()
209   // can no longer distinguish who fired first. This must also 209   // can no longer distinguish who fired first. This must also
210   // happen before inner_.await_resume() rather than after: when 210   // happen before inner_.await_resume() rather than after: when
211   // the inner awaitable is itself a timeout_awaitable (nested 211   // the inner awaitable is itself a timeout_awaitable (nested
212   // timeout()), our own request_stop() below is visible through 212   // timeout()), our own request_stop() below is visible through
213   // its parent_token_ (aliasing our stop_src_), and would 213   // its parent_token_ (aliasing our stop_src_), and would
214   // otherwise make its read of "parent" look like a 214   // otherwise make its read of "parent" look like a
215   // cancellation that never happened. 215   // cancellation that never happened.
HITCBC 216   2036 bool const parent = parent_token_.stop_requested(); 216   2036 bool const parent = parent_token_.stop_requested();
HITCBC 217   2036 bool const fired = stop_src_.stop_requested(); 217   2036 bool const fired = stop_src_.stop_requested();
218   218  
219   // If inner_.await_resume() throws below, request_stop() is 219   // If inner_.await_resume() throws below, request_stop() is
220   // skipped; the still-armed timeout coroutine is then drained 220   // skipped; the still-armed timeout coroutine is then drained
221   // by timer_'s destructor rather than by us. 221   // by timer_'s destructor rather than by us.
HITCBC 222   2036 auto r = inner_.await_resume(); 222   2036 auto r = inner_.await_resume();
223   223  
224   // Cancel whichever is still pending (idempotent) 224   // Cancel whichever is still pending (idempotent)
HITCBC 225   2034 stop_src_.request_stop(); 225   2034 stop_src_.request_stop();
HITCBC 226   2034 destroy_parent_cb(); 226   2034 destroy_parent_cb();
227   227  
228   // Deadline won: stop_src_ is assumed to be the only 228   // Deadline won: stop_src_ is assumed to be the only
229   // cancellation source, whose only writers are the timer 229   // cancellation source, whose only writers are the timer
230   // coroutine and the parent forwarder, so fired && !parent 230   // coroutine and the parent forwarder, so fired && !parent
231   // identifies a timeout. A third-party cancellation of the 231   // identifies a timeout. A third-party cancellation of the
232   // inner op (e.g. a socket cancel issued from elsewhere) 232   // inner op (e.g. a socket cancel issued from elsewhere)
233   // landing in the same window as the deadline firing is 233   // landing in the same window as the deadline firing is
234   // reported as a timeout. 234   // reported as a timeout.
HITCBC 235   2054 if (fired && !parent && 235   2054 if (fired && !parent &&
HITCBC 236   2054 r.ec == capy::cond::canceled) 236   2054 r.ec == capy::cond::canceled)
237   { 237   {
HITCBC 238   20 std::remove_cvref_t<decltype(r)> t{}; 238   20 std::remove_cvref_t<decltype(r)> t{};
HITCBC 239   20 t.ec = make_error_code(capy::error::timeout); 239   20 t.ec = make_error_code(capy::error::timeout);
HITCBC 240   20 return t; 240   20 return t;
241   } 241   }
HITCBC 242   2014 return r; 242   2014 return r;
243   } 243   }
244   244  
HITCBC 245   6118 void destroy_parent_cb() noexcept 245   6118 void destroy_parent_cb() noexcept
246   { 246   {
HITCBC 247   6118 if (cb_active_) 247   6118 if (cb_active_)
248   { 248   {
HITCBC 249   2040 std::launder(reinterpret_cast<stop_cb_type*>(cb_buf_)) 249   2040 std::launder(reinterpret_cast<stop_cb_type*>(cb_buf_))
HITCBC 250   2040 ->~stop_cb_type(); 250   2040 ->~stop_cb_type();
HITCBC 251   2040 cb_active_ = false; 251   2040 cb_active_ = false;
252   } 252   }
HITCBC 253   6118 } 253   6118 }
254   }; 254   };
255   255  
256   } // namespace boost::corosio::detail 256   } // namespace boost::corosio::detail
257   257  
258   #endif 258   #endif