include/boost/capy/io/write_now.hpp

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