100.00% Lines (77/77)
100.00% Functions (22/22)
| TLA | Baseline | Branch | ||||||
|---|---|---|---|---|---|---|---|---|
| Line | Hits | Code | Line | Hits | Code | |||
| 1 | // | 1 | // | |||||
| 2 | // Copyright (c) 2025 Vinnie Falco (vinnie.falco@gmail.com) | 2 | // Copyright (c) 2025 Vinnie Falco (vinnie.falco@gmail.com) | |||||
| 3 | // Copyright (c) 2026 Michael Vandeberg | 3 | // Copyright (c) 2026 Michael Vandeberg | |||||
| 4 | // | 4 | // | |||||
| 5 | // Distributed under the Boost Software License, Version 1.0. (See accompanying | 5 | // Distributed under the Boost Software License, Version 1.0. (See accompanying | |||||
| 6 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | 6 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | |||||
| 7 | // | 7 | // | |||||
| 8 | // Official repository: https://github.com/cppalliance/capy | 8 | // Official repository: https://github.com/cppalliance/capy | |||||
| 9 | // | 9 | // | |||||
| 10 | 10 | |||||||
| 11 | #ifndef BOOST_CAPY_IO_WRITE_NOW_HPP | 11 | #ifndef BOOST_CAPY_IO_WRITE_NOW_HPP | |||||
| 12 | #define BOOST_CAPY_IO_WRITE_NOW_HPP | 12 | #define BOOST_CAPY_IO_WRITE_NOW_HPP | |||||
| 13 | 13 | |||||||
| 14 | #include <boost/capy/detail/config.hpp> | 14 | #include <boost/capy/detail/config.hpp> | |||||
| 15 | #include <boost/capy/detail/await_suspend_helper.hpp> | 15 | #include <boost/capy/detail/await_suspend_helper.hpp> | |||||
| 16 | #include <boost/capy/buffers.hpp> | 16 | #include <boost/capy/buffers.hpp> | |||||
| 17 | #include <boost/capy/detail/consuming_buffers.hpp> | 17 | #include <boost/capy/detail/consuming_buffers.hpp> | |||||
| 18 | #include <boost/capy/concept/context_awaitable.hpp> | 18 | #include <boost/capy/concept/context_awaitable.hpp> | |||||
| 19 | #include <boost/capy/concept/write_stream.hpp> | 19 | #include <boost/capy/concept/write_stream.hpp> | |||||
| 20 | #include <coroutine> | 20 | #include <coroutine> | |||||
| 21 | #include <boost/capy/ex/executor_ref.hpp> | 21 | #include <boost/capy/ex/executor_ref.hpp> | |||||
| 22 | #include <boost/capy/ex/frame_allocator.hpp> | 22 | #include <boost/capy/ex/frame_allocator.hpp> | |||||
| 23 | #include <boost/capy/ex/abandon.hpp> | 23 | #include <boost/capy/ex/abandon.hpp> | |||||
| 24 | #include <boost/capy/ex/await_context.hpp> | 24 | #include <boost/capy/ex/await_context.hpp> | |||||
| 25 | #include <boost/capy/results.hpp> | 25 | #include <boost/capy/results.hpp> | |||||
| 26 | 26 | |||||||
| 27 | #include <cstddef> | 27 | #include <cstddef> | |||||
| 28 | #include <exception> | 28 | #include <exception> | |||||
| 29 | #include <new> | 29 | #include <new> | |||||
| 30 | #include <stop_token> | 30 | #include <stop_token> | |||||
| 31 | #include <utility> | 31 | #include <utility> | |||||
| 32 | 32 | |||||||
| 33 | #ifndef BOOST_CAPY_WRITE_NOW_WORKAROUND | 33 | #ifndef BOOST_CAPY_WRITE_NOW_WORKAROUND | |||||
| 34 | # if defined(__GNUC__) && !defined(__clang__) | 34 | # if defined(__GNUC__) && !defined(__clang__) | |||||
| 35 | # define BOOST_CAPY_WRITE_NOW_WORKAROUND 1 | 35 | # define BOOST_CAPY_WRITE_NOW_WORKAROUND 1 | |||||
| 36 | # else | 36 | # else | |||||
| 37 | # define BOOST_CAPY_WRITE_NOW_WORKAROUND 0 | 37 | # define BOOST_CAPY_WRITE_NOW_WORKAROUND 0 | |||||
| 38 | # endif | 38 | # endif | |||||
| 39 | #endif | 39 | #endif | |||||
| 40 | 40 | |||||||
| 41 | namespace boost { | 41 | namespace boost { | |||||
| 42 | namespace capy { | 42 | namespace capy { | |||||
| 43 | 43 | |||||||
| 44 | /** Eagerly writes complete buffer sequences with frame caching. | 44 | /** Eagerly writes complete buffer sequences with frame caching. | |||||
| 45 | 45 | |||||||
| 46 | This class wraps a @ref WriteStream and provides an `operator()` | 46 | This class wraps a @ref WriteStream and provides an `operator()` | |||||
| 47 | that writes an entire buffer sequence, attempting to complete | 47 | that writes an entire buffer sequence, attempting to complete | |||||
| 48 | synchronously. If every `write_some` completes without suspending, | 48 | synchronously. If every `write_some` completes without suspending, | |||||
| 49 | the entire operation finishes in `await_ready` with no coroutine | 49 | the entire operation finishes in `await_ready` with no coroutine | |||||
| 50 | suspension. | 50 | suspension. | |||||
| 51 | 51 | |||||||
| 52 | The class maintains a one-element coroutine frame cache. After | 52 | The class maintains a one-element coroutine frame cache. After | |||||
| 53 | the first call, subsequent calls reuse the cached frame memory, | 53 | the first call, subsequent calls reuse the cached frame memory, | |||||
| 54 | avoiding repeated allocation for the internal coroutine. | 54 | avoiding repeated allocation for the internal coroutine. | |||||
| 55 | 55 | |||||||
| 56 | - | @tparam Stream The stream type, must satisfy @ref WriteStream. | 56 | + | @pre Only one operation may be outstanding at a time. A new call to | |||
| 57 | - | |||||||
| 58 | - | @par Thread Safety | ||||||
| 59 | - | Distinct objects: Safe. | ||||||
| 60 | - | Shared objects: Unsafe. | ||||||
| 61 | - | |||||||
| 62 | - | @par Preconditions | ||||||
| 63 | - | Only one operation may be outstanding at a time. A new call to | ||||||
| 64 | `operator()` must not be made until the previous operation has | 57 | `operator()` must not be made until the previous operation has | |||||
| 65 | completed (i.e., the returned awaitable is fully consumed). | 58 | completed (i.e., the returned awaitable is fully consumed). | |||||
| 66 | 59 | |||||||
| 67 | Destroying the returned operation object while it is being awaited | 60 | Destroying the returned operation object while it is being awaited | |||||
| 68 | calls `std::terminate`, except while its chain is unwound after an | 61 | calls `std::terminate`, except while its chain is unwound after an | |||||
| 69 | @ref abandon. | 62 | @ref abandon. | |||||
| 70 | 63 | |||||||
| 64 | + | @par Thread Safety | ||||||
| 65 | + | Distinct objects: Safe. | ||||||
| 66 | + | Shared objects: Unsafe. | ||||||
| 67 | + | |||||||
| 68 | + | @note With `BOOST_CAPY_WRITE_NOW_WORKAROUND` (the default on GCC) | ||||||
| 69 | + | the caller always suspends. When every `write_some` is ready, the | ||||||
| 70 | + | operation still completes inline without an executor hop. | ||||||
| 71 | + | |||||||
| 71 | @par Example | 72 | @par Example | |||||
| 72 | 73 | |||||||
| 73 | @code | 74 | @code | |||||
| 74 | template< WriteStream Stream > | 75 | template< WriteStream Stream > | |||||
| 75 | task<> send_messages( Stream& stream ) | 76 | task<> send_messages( Stream& stream ) | |||||
| 76 | { | 77 | { | |||||
| 77 | write_now wn( stream ); | 78 | write_now wn( stream ); | |||||
| 78 | - | auto [ec1, n1] = co_await wn( make_buffer( "hello" ) ); | 79 | + | auto [ec1, n1] = co_await wn( make_buffer( std::string_view( "hello" ) ) ); | |||
| 79 | if( ec1 ) | 80 | if( ec1 ) | |||||
| 80 | - | detail::throw_system_error( ec1 ); | 81 | + | throw std::system_error( ec1 ); | |||
| 81 | - | auto [ec2, n2] = co_await wn( make_buffer( "world" ) ); | 82 | + | auto [ec2, n2] = co_await wn( make_buffer( std::string_view( "world" ) ) ); | |||
| 82 | if( ec2 ) | 83 | if( ec2 ) | |||||
| 83 | - | detail::throw_system_error( ec2 ); | 84 | + | throw std::system_error( ec2 ); | |||
| 84 | } | 85 | } | |||||
| 85 | @endcode | 86 | @endcode | |||||
| 86 | 87 | |||||||
| 88 | + | @tparam Stream The stream type, must satisfy @ref WriteStream. | ||||||
| 89 | + | |||||||
| 87 | @see @ref write, @ref write_some, @ref WriteStream, @ref ConstBufferSequence | 90 | @see @ref write, @ref write_some, @ref WriteStream, @ref ConstBufferSequence | |||||
| 88 | */ | 91 | */ | |||||
| 89 | template<class Stream> | 92 | template<class Stream> | |||||
| 90 | requires WriteStream<Stream> | 93 | requires WriteStream<Stream> | |||||
| 91 | class write_now | 94 | class write_now | |||||
| 92 | { | 95 | { | |||||
| 93 | Stream& stream_; | 96 | Stream& stream_; | |||||
| 94 | void* cached_frame_ = nullptr; | 97 | void* cached_frame_ = nullptr; | |||||
| 95 | std::size_t cached_size_ = 0; | 98 | std::size_t cached_size_ = 0; | |||||
| 96 | 99 | |||||||
| 97 | struct [[nodiscard]] BOOST_CAPY_CORO_AWAIT_ELIDABLE | 100 | struct [[nodiscard]] BOOST_CAPY_CORO_AWAIT_ELIDABLE | |||||
| 98 | op_type | 101 | op_type | |||||
| 99 | { | 102 | { | |||||
| 100 | struct promise_type | 103 | struct promise_type | |||||
| 101 | { | 104 | { | |||||
| 102 | results<std::size_t> result_; | 105 | results<std::size_t> result_; | |||||
| 103 | std::exception_ptr ep_; | 106 | std::exception_ptr ep_; | |||||
| 104 | std::coroutine_handle<> cont_{nullptr}; | 107 | std::coroutine_handle<> cont_{nullptr}; | |||||
| 105 | await_context const* env_ = nullptr; | 108 | await_context const* env_ = nullptr; | |||||
| 106 | bool done_ = false; | 109 | bool done_ = false; | |||||
| 107 | 110 | |||||||
| 108 | // The awaiting op_type while a parent awaits this frame. | 111 | // The awaiting op_type while a parent awaits this frame. | |||||
| 109 | op_type* owner_ = nullptr; | 112 | op_type* owner_ = nullptr; | |||||
| 110 | 113 | |||||||
| HITCBC | 111 | 77 | op_type get_return_object() | 114 | 77 | op_type get_return_object() | ||
| 112 | { | 115 | { | |||||
| 113 | return op_type{ | 116 | return op_type{ | |||||
| 114 | std::coroutine_handle< | 117 | std::coroutine_handle< | |||||
| HITCBC | 115 | 77 | promise_type>::from_promise(*this)}; | 118 | 77 | promise_type>::from_promise(*this)}; | ||
| 116 | } | 119 | } | |||||
| 117 | 120 | |||||||
| HITCBC | 118 | 77 | auto initial_suspend() noexcept | 121 | 77 | auto initial_suspend() noexcept | ||
| 119 | { | 122 | { | |||||
| 120 | #if BOOST_CAPY_WRITE_NOW_WORKAROUND | 123 | #if BOOST_CAPY_WRITE_NOW_WORKAROUND | |||||
| HITCBC | 121 | 77 | return std::suspend_always{}; | 124 | 77 | return std::suspend_always{}; | ||
| 122 | #else | 125 | #else | |||||
| 123 | return std::suspend_never{}; | 126 | return std::suspend_never{}; | |||||
| 124 | #endif | 127 | #endif | |||||
| 125 | } | 128 | } | |||||
| 126 | 129 | |||||||
| HITCBC | 127 | 73 | auto final_suspend() noexcept | 130 | 73 | auto final_suspend() noexcept | ||
| 128 | { | 131 | { | |||||
| 129 | struct awaiter | 132 | struct awaiter | |||||
| 130 | { | 133 | { | |||||
| 131 | promise_type* p_; | 134 | promise_type* p_; | |||||
| 132 | 135 | |||||||
| HITCBC | 133 | 73 | bool await_ready() const noexcept | 136 | 73 | bool await_ready() const noexcept | ||
| 134 | { | 137 | { | |||||
| HITCBC | 135 | 73 | return false; | 138 | 73 | return false; | ||
| 136 | } | 139 | } | |||||
| 137 | 140 | |||||||
| HITCBC | 138 | 73 | std::coroutine_handle<> await_suspend(std::coroutine_handle<>) const noexcept | 141 | 73 | std::coroutine_handle<> await_suspend(std::coroutine_handle<>) const noexcept | ||
| 139 | { | 142 | { | |||||
| HITCBC | 140 | 73 | p_->done_ = true; | 143 | 73 | p_->done_ = true; | ||
| HITCBC | 141 | 73 | if(!p_->cont_) | 144 | 73 | if(!p_->cont_) | ||
| 142 | return std::noop_coroutine(); // LCOV_EXCL_LINE cont_ always set on this (suspend_always) path | 145 | return std::noop_coroutine(); // LCOV_EXCL_LINE cont_ always set on this (suspend_always) path | |||||
| HITCBC | 143 | 73 | return p_->cont_; | 146 | 73 | return p_->cont_; | ||
| 144 | } | 147 | } | |||||
| 145 | 148 | |||||||
| 146 | void await_resume() const noexcept {} // LCOV_EXCL_LINE final_suspend awaiter, never resumed | 149 | void await_resume() const noexcept {} // LCOV_EXCL_LINE final_suspend awaiter, never resumed | |||||
| 147 | }; | 150 | }; | |||||
| HITCBC | 148 | 73 | return awaiter{this}; | 151 | 73 | return awaiter{this}; | ||
| 149 | } | 152 | } | |||||
| 150 | 153 | |||||||
| HITCBC | 151 | 50 | void return_value( | 154 | 50 | void return_value( | ||
| 152 | results<std::size_t> r) noexcept | 155 | results<std::size_t> r) noexcept | |||||
| 153 | { | 156 | { | |||||
| HITCBC | 154 | 50 | result_ = r; | 157 | 50 | result_ = r; | ||
| HITCBC | 155 | 50 | } | 158 | 50 | } | ||
| 156 | 159 | |||||||
| HITCBC | 157 | 23 | void unhandled_exception() | 160 | 23 | void unhandled_exception() | ||
| 158 | { | 161 | { | |||||
| HITCBC | 159 | 23 | ep_ = std::current_exception(); | 162 | 23 | ep_ = std::current_exception(); | ||
| HITCBC | 160 | 23 | } | 163 | 23 | } | ||
| 161 | 164 | |||||||
| 162 | std::suspend_always yield_value(int) noexcept | 165 | std::suspend_always yield_value(int) noexcept | |||||
| 163 | { | 166 | { | |||||
| 164 | return {}; | 167 | return {}; | |||||
| 165 | } | 168 | } | |||||
| 166 | 169 | |||||||
| 167 | template<class A> | 170 | template<class A> | |||||
| HITCBC | 168 | 94 | auto await_transform(A&& a) | 171 | 94 | auto await_transform(A&& a) | ||
| 169 | { | 172 | { | |||||
| 170 | using decayed = std::decay_t<A>; | 173 | using decayed = std::decay_t<A>; | |||||
| 171 | if constexpr (ContextAwaitable<decayed>) | 174 | if constexpr (ContextAwaitable<decayed>) | |||||
| 172 | { | 175 | { | |||||
| 173 | // Not context_awaiter: an exception from the | 176 | // Not context_awaiter: an exception from the | |||||
| 174 | // stream's await_suspend is the caller's to handle. | 177 | // stream's await_suspend is the caller's to handle. | |||||
| 175 | struct wrapper | 178 | struct wrapper | |||||
| 176 | { | 179 | { | |||||
| 177 | decayed inner_; | 180 | decayed inner_; | |||||
| 178 | promise_type* p_; | 181 | promise_type* p_; | |||||
| 179 | 182 | |||||||
| HITCBC | 180 | 94 | bool await_ready() | 183 | 94 | bool await_ready() | ||
| 181 | { | 184 | { | |||||
| HITCBC | 182 | 94 | return inner_.await_ready(); | 185 | 94 | return inner_.await_ready(); | ||
| 183 | } | 186 | } | |||||
| 184 | 187 | |||||||
| 185 | // The inner operation may resume or destroy this | 188 | // The inner operation may resume or destroy this | |||||
| 186 | // frame on another thread before control returns | 189 | // frame on another thread before control returns | |||||
| 187 | // here, so the handle must not round-trip through | 190 | // here, so the handle must not round-trip through | |||||
| 188 | // the frame. | 191 | // the frame. | |||||
| HITCBC | 189 | 94 | auto await_suspend(std::coroutine_handle<> h) | 192 | 94 | auto await_suspend(std::coroutine_handle<> h) | ||
| 190 | { | 193 | { | |||||
| HITCBC | 191 | 93 | return detail::symmetric_transfer( | 194 | 93 | return detail::symmetric_transfer( | ||
| 192 | detail::call_await_suspend( | 195 | detail::call_await_suspend( | |||||
| 193 | &inner_, h, | 196 | &inner_, h, | |||||
| HITCBC | 194 | 187 | p_->env_)); | 197 | 187 | p_->env_)); | ||
| 195 | } | 198 | } | |||||
| 196 | 199 | |||||||
| HITCBC | 197 | 91 | decltype(auto) await_resume() | 200 | 91 | decltype(auto) await_resume() | ||
| 198 | { | 201 | { | |||||
| 199 | // The resumption may arrive on another thread. | 202 | // The resumption may arrive on another thread. | |||||
| HITCBC | 200 | 91 | set_current_frame_allocator( | 203 | 91 | set_current_frame_allocator( | ||
| HITCBC | 201 | 91 | p_->env_->frame_allocator); | 204 | 91 | p_->env_->frame_allocator); | ||
| HITCBC | 202 | 91 | return inner_.await_resume(); | 205 | 91 | return inner_.await_resume(); | ||
| 203 | } | 206 | } | |||||
| 204 | }; | 207 | }; | |||||
| 205 | return wrapper{ | 208 | return wrapper{ | |||||
| HITCBC | 206 | 94 | std::forward<A>(a), this}; | 209 | 94 | std::forward<A>(a), this}; | ||
| 207 | } | 210 | } | |||||
| 208 | else | 211 | else | |||||
| 209 | { | 212 | { | |||||
| 210 | return std::forward<A>(a); | 213 | return std::forward<A>(a); | |||||
| 211 | } | 214 | } | |||||
| 212 | } | 215 | } | |||||
| 213 | 216 | |||||||
| 214 | static void* | 217 | static void* | |||||
| HITCBC | 215 | 77 | operator new( | 218 | 77 | operator new( | ||
| 216 | std::size_t size, | 219 | std::size_t size, | |||||
| 217 | write_now& self, | 220 | write_now& self, | |||||
| 218 | auto&) | 221 | auto&) | |||||
| 219 | { | 222 | { | |||||
| HITCBC | 220 | 77 | if(self.cached_frame_ && | 223 | 77 | if(self.cached_frame_ && | ||
| HITCBC | 221 | 6 | self.cached_size_ >= size) | 224 | 6 | self.cached_size_ >= size) | ||
| HITCBC | 222 | 5 | return self.cached_frame_; | 225 | 5 | return self.cached_frame_; | ||
| HITCBC | 223 | 72 | void* p = ::operator new(size); | 226 | 72 | void* p = ::operator new(size); | ||
| HITCBC | 224 | 72 | if(self.cached_frame_) | 227 | 72 | if(self.cached_frame_) | ||
| HITCBC | 225 | 1 | ::operator delete(self.cached_frame_); | 228 | 1 | ::operator delete(self.cached_frame_); | ||
| HITCBC | 226 | 72 | self.cached_frame_ = p; | 229 | 72 | self.cached_frame_ = p; | ||
| HITCBC | 227 | 72 | self.cached_size_ = size; | 230 | 72 | self.cached_size_ = size; | ||
| HITCBC | 228 | 72 | return p; | 231 | 72 | return p; | ||
| 229 | } | 232 | } | |||||
| 230 | 233 | |||||||
| 231 | static void | 234 | static void | |||||
| HITCBC | 232 | 77 | operator delete(void*, std::size_t) noexcept | 235 | 77 | operator delete(void*, std::size_t) noexcept | ||
| 233 | { | 236 | { | |||||
| HITCBC | 234 | 77 | } | 237 | 77 | } | ||
| 235 | }; | 238 | }; | |||||
| 236 | 239 | |||||||
| 237 | std::coroutine_handle<promise_type> h_; | 240 | std::coroutine_handle<promise_type> h_; | |||||
| 238 | 241 | |||||||
| HITCBC | 239 | 152 | ~op_type() | 242 | 152 | ~op_type() | ||
| 240 | { | 243 | { | |||||
| HITCBC | 241 | 152 | if(h_) | 244 | 152 | if(h_) | ||
| 242 | { | 245 | { | |||||
| 243 | // Only the unwind of an abandoned chain destroys a frame | 246 | // Only the unwind of an abandoned chain destroys a frame | |||||
| 244 | // that is still awaited. | 247 | // that is still awaited. | |||||
| HITCBC | 245 | 77 | auto& p = h_.promise(); | 248 | 77 | auto& p = h_.promise(); | ||
| HITCBC | 246 | 79 | if(!p.done_ && p.owner_ && | 249 | 79 | if(!p.done_ && p.owner_ && | ||
| HITCBC | 247 | 2 | !(p.env_->owner && p.env_->owner->abandoned())) | 250 | 2 | !(p.env_->owner && p.env_->owner->abandoned())) | ||
| 248 | std::terminate(); // LCOV_EXCL_LINE reached only in a death test's child process | 251 | std::terminate(); // LCOV_EXCL_LINE reached only in a death test's child process | |||||
| HITCBC | 249 | 77 | h_.destroy(); | 252 | 77 | h_.destroy(); | ||
| 250 | } | 253 | } | |||||
| HITCBC | 251 | 152 | } | 254 | 152 | } | ||
| 252 | 255 | |||||||
| 253 | op_type(op_type const&) = delete; | 256 | op_type(op_type const&) = delete; | |||||
| 254 | op_type& operator=(op_type const&) = delete; | 257 | op_type& operator=(op_type const&) = delete; | |||||
| 255 | 258 | |||||||
| HITCBC | 256 | 75 | op_type(op_type&& other) noexcept | 259 | 75 | op_type(op_type&& other) noexcept | ||
| HITCBC | 257 | 75 | : h_(std::exchange(other.h_, nullptr)) | 260 | 75 | : h_(std::exchange(other.h_, nullptr)) | ||
| 258 | { | 261 | { | |||||
| HITCBC | 259 | 75 | } | 262 | 75 | } | ||
| 260 | 263 | |||||||
| 261 | op_type& operator=(op_type&&) = delete; | 264 | op_type& operator=(op_type&&) = delete; | |||||
| 262 | 265 | |||||||
| HITCBC | 263 | 75 | bool await_ready() const noexcept | 266 | 75 | bool await_ready() const noexcept | ||
| 264 | { | 267 | { | |||||
| HITCBC | 265 | 75 | return h_.promise().done_; | 268 | 75 | return h_.promise().done_; | ||
| 266 | } | 269 | } | |||||
| 267 | 270 | |||||||
| HITCBC | 268 | 75 | std::coroutine_handle<> await_suspend( | 271 | 75 | std::coroutine_handle<> await_suspend( | ||
| 269 | std::coroutine_handle<> cont, | 272 | std::coroutine_handle<> cont, | |||||
| 270 | await_context const* env) | 273 | await_context const* env) | |||||
| 271 | { | 274 | { | |||||
| HITCBC | 272 | 75 | auto& p = h_.promise(); | 275 | 75 | auto& p = h_.promise(); | ||
| HITCBC | 273 | 75 | p.cont_ = cont; | 276 | 75 | p.cont_ = cont; | ||
| HITCBC | 274 | 75 | p.env_ = env; | 277 | 75 | p.env_ = env; | ||
| HITCBC | 275 | 75 | p.owner_ = this; | 278 | 75 | p.owner_ = this; | ||
| HITCBC | 276 | 75 | return h_; | 279 | 75 | return h_; | ||
| 277 | } | 280 | } | |||||
| 278 | 281 | |||||||
| HITCBC | 279 | 73 | [[nodiscard]] results<std::size_t> await_resume() | 282 | 73 | [[nodiscard]] results<std::size_t> await_resume() | ||
| 280 | { | 283 | { | |||||
| HITCBC | 281 | 73 | auto& p = h_.promise(); | 284 | 73 | auto& p = h_.promise(); | ||
| HITCBC | 282 | 73 | p.owner_ = nullptr; | 285 | 73 | p.owner_ = nullptr; | ||
| HITCBC | 283 | 73 | if(p.ep_) | 286 | 73 | if(p.ep_) | ||
| HITCBC | 284 | 23 | std::rethrow_exception(p.ep_); | 287 | 23 | std::rethrow_exception(p.ep_); | ||
| HITCBC | 285 | 50 | return p.result_; | 288 | 50 | return p.result_; | ||
| 286 | } | 289 | } | |||||
| 287 | 290 | |||||||
| 288 | private: | 291 | private: | |||||
| HITCBC | 289 | 77 | explicit op_type( | 292 | 77 | explicit op_type( | ||
| 290 | std::coroutine_handle<promise_type> h) | 293 | std::coroutine_handle<promise_type> h) | |||||
| HITCBC | 291 | 77 | : h_(h) | 294 | 77 | : h_(h) | ||
| 292 | { | 295 | { | |||||
| HITCBC | 293 | 77 | } | 296 | 77 | } | ||
| 294 | }; | 297 | }; | |||||
| 295 | 298 | |||||||
| 296 | public: | 299 | public: | |||||
| 297 | - | /** Destructor. Frees the cached coroutine frame. */ | 300 | + | /// Destroy the object, freeing the cached coroutine frame. | |||
| HITCBC | 298 | 71 | ~write_now() | 301 | 71 | ~write_now() | ||
| 299 | { | 302 | { | |||||
| HITCBC | 300 | 71 | if(cached_frame_) | 303 | 71 | if(cached_frame_) | ||
| HITCBC | 301 | 71 | ::operator delete(cached_frame_); | 304 | 71 | ::operator delete(cached_frame_); | ||
| HITCBC | 302 | 71 | } | 305 | 71 | } | ||
| 303 | 306 | |||||||
| 304 | /** Construct from a stream reference. | 307 | /** Construct from a stream reference. | |||||
| 305 | 308 | |||||||
| 306 | @param s The stream to write to. Must outlive this object. | 309 | @param s The stream to write to. Must outlive this object. | |||||
| 307 | */ | 310 | */ | |||||
| 308 | explicit | 311 | explicit | |||||
| HITCBC | 309 | 71 | write_now(Stream& s) noexcept | 312 | 71 | write_now(Stream& s) noexcept | ||
| HITCBC | 310 | 71 | : stream_(s) | 313 | 71 | : stream_(s) | ||
| 311 | { | 314 | { | |||||
| HITCBC | 312 | 71 | } | 315 | 71 | } | ||
| 313 | 316 | |||||||
| 317 | + | /// Copy construction is disabled. The frame cache is per-instance. | ||||||
| 314 | write_now(write_now const&) = delete; | 318 | write_now(write_now const&) = delete; | |||||
| 319 | + | |||||||
| 320 | + | /// Copy assignment is disabled. The frame cache is per-instance. | ||||||
| 315 | write_now& operator=(write_now const&) = delete; | 321 | write_now& operator=(write_now const&) = delete; | |||||
| 316 | 322 | |||||||
| 317 | /** Eagerly write the entire buffer sequence. | 323 | /** Eagerly write the entire buffer sequence. | |||||
| 318 | 324 | |||||||
| 319 | Writes data to the stream by calling `write_some` repeatedly | 325 | Writes data to the stream by calling `write_some` repeatedly | |||||
| 320 | until the entire buffer sequence is written or an error | 326 | until the entire buffer sequence is written or an error | |||||
| 321 | occurs. The operation attempts to complete synchronously: | 327 | occurs. The operation attempts to complete synchronously: | |||||
| 322 | if every `write_some` completes without suspending, the | 328 | if every `write_some` completes without suspending, the | |||||
| 323 | entire operation finishes in `await_ready`. | 329 | entire operation finishes in `await_ready`. | |||||
| 324 | 330 | |||||||
| 325 | When the fast path cannot complete, the coroutine suspends | 331 | When the fast path cannot complete, the coroutine suspends | |||||
| 326 | and continues asynchronously. The internal coroutine frame | 332 | and continues asynchronously. The internal coroutine frame | |||||
| 327 | is cached and reused across calls. | 333 | is cached and reused across calls. | |||||
| 328 | 334 | |||||||
| 329 | - | @param buffers The buffer sequence to write. Passed by | 335 | + | On success, `n` equals `buffer_size(buffers)`. On error, `n` | |||
| 330 | - | value to ensure the sequence lives in the coroutine | 336 | + | counts the bytes written before the error. Compare error | |||
| 331 | - | frame across suspension points. | 337 | + | codes to conditions: | |||
| 338 | + | @li `cond::canceled`: the operation was cancelled. | ||||||
| 339 | + | @li `std::errc::broken_pipe`: the peer closed the connection. | ||||||
| 332 | 340 | |||||||
| 333 | - | @return An awaitable that await-returns `(error_code,std::size_t)`. | 341 | + | @pre This object outlives the returned awaitable, including | |||
| 334 | - | On success, `n` equals `buffer_size(buffers)`. On | 342 | + | its destruction. The awaitable uses this object's cached frame. | |||
| 335 | - | error, `n` is the number of bytes written before the | 343 | + | ||||
| 336 | - | error. Compare error codes to conditions: | 344 | + | @throws std::bad_alloc If allocating the coroutine frame fails. | |||
| 337 | - | @li `cond::canceled` - Operation was cancelled | 345 | + | @throws Any exception thrown by the stream's `write_some`. | |||
| 338 | - | @li `std::errc::broken_pipe` - Peer closed connection | 346 | + | ||||
| 347 | + | @note With `BOOST_CAPY_WRITE_NOW_WORKAROUND` (the default on GCC) | ||||||
| 348 | + | the caller always suspends. See the class documentation. When the | ||||||
| 349 | + | workaround is off and a `write_some` fails without suspending, | ||||||
| 350 | + | `n` excludes any bytes that failing call reported. | ||||||
| 339 | 351 | |||||||
| 340 | @par Example | 352 | @par Example | |||||
| 341 | 353 | |||||||
| 342 | @code | 354 | @code | |||||
| 343 | write_now wn( stream ); | 355 | write_now wn( stream ); | |||||
| 344 | auto [ec, n] = co_await wn( make_buffer( body ) ); | 356 | auto [ec, n] = co_await wn( make_buffer( body ) ); | |||||
| 345 | if( ec ) | 357 | if( ec ) | |||||
| 346 | - | detail::throw_system_error( ec ); | 358 | + | throw std::system_error( ec ); | |||
| 347 | @endcode | 359 | @endcode | |||||
| 360 | + | |||||||
| 361 | + | @param buffers The buffer sequence to write. Passed by | ||||||
| 362 | + | value to ensure the sequence lives in the coroutine | ||||||
| 363 | + | frame across suspension points. The memory it refers to | ||||||
| 364 | + | is not copied and must remain valid until the operation | ||||||
| 365 | + | completes. | ||||||
| 366 | + | |||||||
| 367 | + | @return An awaitable that await-returns `(error_code,std::size_t)`. | ||||||
| 348 | 368 | |||||||
| 349 | @see @ref write, @ref write_some, @ref WriteStream | 369 | @see @ref write, @ref write_some, @ref WriteStream | |||||
| 350 | */ | 370 | */ | |||||
| 351 | // GCC falsely warns that the coroutine promise's | 371 | // GCC falsely warns that the coroutine promise's | |||||
| 352 | // placement operator new(size_t, write_now&, auto&) | 372 | // placement operator new(size_t, write_now&, auto&) | |||||
| 353 | // mismatches operator delete(void*, size_t). Per the | 373 | // mismatches operator delete(void*, size_t). Per the | |||||
| 354 | // standard, coroutine deallocation lookup is separate. | 374 | // standard, coroutine deallocation lookup is separate. | |||||
| 355 | #if defined(__GNUC__) && !defined(__clang__) | 375 | #if defined(__GNUC__) && !defined(__clang__) | |||||
| 356 | #pragma GCC diagnostic push | 376 | #pragma GCC diagnostic push | |||||
| 357 | #pragma GCC diagnostic ignored "-Wmismatched-new-delete" | 377 | #pragma GCC diagnostic ignored "-Wmismatched-new-delete" | |||||
| 358 | #endif | 378 | #endif | |||||
| 359 | 379 | |||||||
| 360 | #if BOOST_CAPY_WRITE_NOW_WORKAROUND | 380 | #if BOOST_CAPY_WRITE_NOW_WORKAROUND | |||||
| 361 | template<ConstBufferSequence Buffers> | 381 | template<ConstBufferSequence Buffers> | |||||
| 362 | op_type | 382 | op_type | |||||
| HITCBC | 363 | 77 | operator()(Buffers buffers) | 383 | 77 | operator()(Buffers buffers) | ||
| 364 | { | 384 | { | |||||
| 365 | std::size_t const total_size = buffer_size(buffers); | 385 | std::size_t const total_size = buffer_size(buffers); | |||||
| 366 | std::size_t total_written = 0; | 386 | std::size_t total_written = 0; | |||||
| 367 | detail::consuming_buffers cb(buffers); | 387 | detail::consuming_buffers cb(buffers); | |||||
| 368 | while(total_written < total_size) | 388 | while(total_written < total_size) | |||||
| 369 | { | 389 | { | |||||
| 370 | auto r = | 390 | auto r = | |||||
| 371 | co_await stream_.write_some(cb.data()); | 391 | co_await stream_.write_some(cb.data()); | |||||
| 372 | cb.consume(std::get<1>(r)); | 392 | cb.consume(std::get<1>(r)); | |||||
| 373 | total_written += std::get<1>(r); | 393 | total_written += std::get<1>(r); | |||||
| 374 | if(std::get<0>(r)) | 394 | if(std::get<0>(r)) | |||||
| 375 | co_return results<std::size_t>{ | 395 | co_return results<std::size_t>{ | |||||
| 376 | std::get<0>(r), total_written}; | 396 | std::get<0>(r), total_written}; | |||||
| 377 | } | 397 | } | |||||
| 378 | co_return results<std::size_t>{ | 398 | co_return results<std::size_t>{ | |||||
| 379 | std::error_code(), total_written}; | 399 | std::error_code(), total_written}; | |||||
| HITCBC | 380 | 154 | } | 400 | 154 | } | ||
| 381 | #else | 401 | #else | |||||
| 382 | template<ConstBufferSequence Buffers> | 402 | template<ConstBufferSequence Buffers> | |||||
| 383 | op_type | 403 | op_type | |||||
| 384 | operator()(Buffers buffers) | 404 | operator()(Buffers buffers) | |||||
| 385 | { | 405 | { | |||||
| 386 | std::size_t const total_size = buffer_size(buffers); | 406 | std::size_t const total_size = buffer_size(buffers); | |||||
| 387 | std::size_t total_written = 0; | 407 | std::size_t total_written = 0; | |||||
| 388 | 408 | |||||||
| 389 | // GCC ICE in expand_expr_real_1 (expr.cc:11376) | 409 | // GCC ICE in expand_expr_real_1 (expr.cc:11376) | |||||
| 390 | // when the buffer slice spans a co_yield, so | 410 | // when the buffer slice spans a co_yield, so | |||||
| 391 | // the GCC path uses a separate simple coroutine. | 411 | // the GCC path uses a separate simple coroutine. | |||||
| 392 | detail::consuming_buffers cb(buffers); | 412 | detail::consuming_buffers cb(buffers); | |||||
| 393 | while(total_written < total_size) | 413 | while(total_written < total_size) | |||||
| 394 | { | 414 | { | |||||
| 395 | auto inner = stream_.write_some(cb.data()); | 415 | auto inner = stream_.write_some(cb.data()); | |||||
| 396 | if(!inner.await_ready()) | 416 | if(!inner.await_ready()) | |||||
| 397 | break; | 417 | break; | |||||
| 398 | auto r = inner.await_resume(); | 418 | auto r = inner.await_resume(); | |||||
| 399 | if(std::get<0>(r)) | 419 | if(std::get<0>(r)) | |||||
| 400 | co_return results<std::size_t>{ | 420 | co_return results<std::size_t>{ | |||||
| 401 | std::get<0>(r), total_written}; | 421 | std::get<0>(r), total_written}; | |||||
| 402 | cb.consume(std::get<1>(r)); | 422 | cb.consume(std::get<1>(r)); | |||||
| 403 | total_written += std::get<1>(r); | 423 | total_written += std::get<1>(r); | |||||
| 404 | } | 424 | } | |||||
| 405 | 425 | |||||||
| 406 | if(total_written >= total_size) | 426 | if(total_written >= total_size) | |||||
| 407 | co_return results<std::size_t>{ | 427 | co_return results<std::size_t>{ | |||||
| 408 | std::error_code(), total_written}; | 428 | std::error_code(), total_written}; | |||||
| 409 | 429 | |||||||
| 410 | co_yield 0; | 430 | co_yield 0; | |||||
| 411 | 431 | |||||||
| 412 | while(total_written < total_size) | 432 | while(total_written < total_size) | |||||
| 413 | { | 433 | { | |||||
| 414 | auto r = | 434 | auto r = | |||||
| 415 | co_await stream_.write_some(cb.data()); | 435 | co_await stream_.write_some(cb.data()); | |||||
| 416 | cb.consume(std::get<1>(r)); | 436 | cb.consume(std::get<1>(r)); | |||||
| 417 | total_written += std::get<1>(r); | 437 | total_written += std::get<1>(r); | |||||
| 418 | if(std::get<0>(r)) | 438 | if(std::get<0>(r)) | |||||
| 419 | co_return results<std::size_t>{ | 439 | co_return results<std::size_t>{ | |||||
| 420 | std::get<0>(r), total_written}; | 440 | std::get<0>(r), total_written}; | |||||
| 421 | } | 441 | } | |||||
| 422 | co_return results<std::size_t>{ | 442 | co_return results<std::size_t>{ | |||||
| 423 | std::error_code(), total_written}; | 443 | std::error_code(), total_written}; | |||||
| 424 | } | 444 | } | |||||
| 425 | #endif | 445 | #endif | |||||
| 426 | 446 | |||||||
| 427 | #if defined(__GNUC__) && !defined(__clang__) | 447 | #if defined(__GNUC__) && !defined(__clang__) | |||||
| 428 | #pragma GCC diagnostic pop | 448 | #pragma GCC diagnostic pop | |||||
| 429 | #endif | 449 | #endif | |||||
| 430 | }; | 450 | }; | |||||
| 431 | 451 | |||||||
| 432 | template<WriteStream S> | 452 | template<WriteStream S> | |||||
| 433 | write_now(S&) -> write_now<S>; | 453 | write_now(S&) -> write_now<S>; | |||||
| 434 | 454 | |||||||
| 435 | } // namespace capy | 455 | } // namespace capy | |||||
| 436 | } // namespace boost | 456 | } // namespace boost | |||||
| 437 | 457 | |||||||
| 438 | #endif | 458 | #endif | |||||