include/boost/corosio/native/detail/reactor/reactor_backend.hpp

84.3% Lines (70/83) 100.0% List of functions (4/4) 23.6% Branches (35/148)
reactor_backend.hpp
f(x) Functions (4)
Function Calls Lines Branches Blocks
boost::corosio::detail::reactor_acceptor_impl<boost::corosio::detail::kqueue_local_stream_acceptor, boost::corosio::detail::kqueue_traits, boost::corosio::detail::kqueue_local_stream_acceptor_service, boost::corosio::detail::kqueue_local_stream_socket, boost::corosio::local_stream_acceptor::implementation, boost::corosio::local_endpoint>::accept(std::__1::coroutine_handle<void>, boost::capy::executor_ref, std::__1::stop_token, std::__1::error_code*, boost::corosio::io_object::implementation**) :49 18x 100.0% – 37.0% boost::corosio::detail::reactor_acceptor_impl<boost::corosio::detail::kqueue_tcp_acceptor, boost::corosio::detail::kqueue_traits, boost::corosio::detail::kqueue_tcp_acceptor_service, boost::corosio::detail::kqueue_tcp_socket, boost::corosio::tcp_acceptor::implementation, boost::corosio::endpoint>::accept(std::__1::coroutine_handle<void>, boost::capy::executor_ref, std::__1::stop_token, std::__1::error_code*, boost::corosio::io_object::implementation**) :49 6530x 64.6% 30.6% 57.0% boost::corosio::detail::reactor_acceptor_impl<boost::corosio::detail::select_local_stream_acceptor, boost::corosio::detail::select_traits, boost::corosio::detail::select_local_stream_acceptor_service, boost::corosio::detail::select_local_stream_socket, boost::corosio::local_stream_acceptor::implementation, boost::corosio::local_endpoint>::accept(std::__1::coroutine_handle<void>, boost::capy::executor_ref, std::__1::stop_token, std::__1::error_code*, boost::corosio::io_object::implementation**) :49 17x 49.4% 16.9% 39.0% boost::corosio::detail::reactor_acceptor_impl<boost::corosio::detail::select_tcp_acceptor, boost::corosio::detail::select_traits, boost::corosio::detail::select_tcp_acceptor_service, boost::corosio::detail::select_tcp_socket, boost::corosio::tcp_acceptor::implementation, boost::corosio::endpoint>::accept(std::__1::coroutine_handle<void>, boost::capy::executor_ref, std::__1::stop_token, std::__1::error_code*, boost::corosio::io_object::implementation**) :49 1656x 84.3% 47.3% 56.0%
Line Branch TLA Hits Source Code
1 //
2 // Copyright (c) 2026 Michael Vandeberg
3 //
4 // Distributed under the Boost Software License, Version 1.0. (See accompanying
5 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 //
7 // Official repository: https://github.com/cppalliance/corosio
8 //
9
10 #ifndef BOOST_COROSIO_NATIVE_DETAIL_REACTOR_REACTOR_BACKEND_HPP
11 #define BOOST_COROSIO_NATIVE_DETAIL_REACTOR_REACTOR_BACKEND_HPP
12
13 /* Reactor backend: acceptor accept() implementation.
14
15 Contains the accept() method body for reactor_acceptor_impl,
16 which needs all socket/service types to be complete. Included
17 by per-backend type files (epoll_types.hpp, etc.) after all
18 named types are defined.
19 */
20
21 #include <boost/corosio/native/detail/reactor/reactor_service_finals.hpp>
22 #include <boost/corosio/native/detail/reactor/reactor_op_complete.hpp>
23 #include <boost/corosio/native/detail/endpoint_convert.hpp>
24 #include <boost/corosio/detail/dispatch_coro.hpp>
25
26 #include <mutex>
27
28 namespace boost::corosio::detail {
29
30 // ============================================================
31 // Acceptor accept() implementation
32 // ============================================================
33
34 template<
35 class Derived,
36 class Traits,
37 class Service,
38 class SocketFinal,
39 class AccImplBase,
40 class Endpoint>
41 std::coroutine_handle<>
42 reactor_acceptor_impl<
43 Derived,
44 Traits,
45 Service,
46 SocketFinal,
47 AccImplBase,
48 Endpoint>::
49 8221x accept(
50 std::coroutine_handle<> h,
51 capy::executor_ref ex,
52 std::stop_token token,
53 std::error_code* ec,
54 io_object::implementation** impl_out)
55 {
56 8221x auto& op = this->acc_;
57 8221x op.reset();
58 8221x op.h = h;
59 8221x op.ex = ex;
60 8221x op.ec_out = ec;
61 8221x op.impl_out = impl_out;
62 8221x op.fd = this->fd_;
63 8221x op.start(token, static_cast<Derived*>(this));
64
65 8221x sockaddr_storage peer_storage{};
66 8221x socklen_t peer_addrlen = 0;
67
68 8221x int accepted =
69 8221x Traits::accept_policy::do_accept(this->fd_, peer_storage, peer_addrlen);
70
71
4/8
✓ Branch 0 taken 6530 times.
✓ Branch 1 taken 18 times.
✓ Branch 2 taken 1657 times.
✓ Branch 3 taken 16 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
8221x if (accepted >= 0)
72 {
73 {
74 34x std::lock_guard lock(this->desc_state_.mutex);
75 34x this->desc_state_.read_ready = false;
76 34x }
77
78
4/8
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 11 times.
✓ Branch 2 taken 5 times.
✓ Branch 3 taken 11 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
34x if (this->svc_.scheduler().try_consume_inline_budget())
79 {
80 12x auto* socket_svc = this->svc_.stream_service();
81
2/8
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
12x if (socket_svc)
82 {
83 12x auto& impl =
84 12x static_cast<SocketFinal&>(*socket_svc->construct());
85 12x impl.set_socket(accepted);
86
87 12x impl.desc_state_.fd = accepted;
88 {
89 12x std::lock_guard lock(impl.desc_state_.mutex);
90 12x impl.desc_state_.read_op = nullptr;
91 12x impl.desc_state_.write_op = nullptr;
92 12x impl.desc_state_.connect_op = nullptr;
93 12x }
94 24x auto reg_ec = socket_svc->scheduler().register_descriptor(
95 12x accepted, &impl.desc_state_);
96
3/8
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 5 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
12x if (reg_ec)
97 {
98 // destroy() closes the fd the impl already owns.
99 1x socket_svc->destroy(&impl);
100 1x *ec = reg_ec;
101
1/8
✗ Branch 0 not taken.
✓ Branch 1 taken 1 time.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
1x if (impl_out)
102 1x *impl_out = nullptr;
103 1x }
104 else
105 {
106 22x impl.set_endpoints(
107 11x this->local_endpoint_,
108 11x from_sockaddr_as(
109 11x peer_storage, peer_addrlen, Endpoint{}));
110
111 11x *ec = {};
112
2/8
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 5 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
11x if (impl_out)
113 11x *impl_out = &impl;
114 }
115 12x }
116 else
117 {
118 ✗ ::close(accepted);
119 ✗ *ec = make_err(ENOENT);
120 ✗ if (impl_out)
121 ✗ *impl_out = nullptr;
122 }
123 12x op.cont.h = h;
124 12x return dispatch_coro(ex, op.cont);
125 }
126
127 22x op.accepted_fd = accepted;
128 22x op.peer_storage = peer_storage;
129 22x op.peer_addrlen = peer_addrlen;
130 22x op.complete(0, 0);
131
2/8
✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 11 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
22x op.impl_ptr = this->shared_from_this();
132 22x this->svc_.post(&op);
133 22x return std::noop_coroutine();
134 }
135
136
6/16
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 6523 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 7 times.
✓ Branch 4 taken 8 times.
✓ Branch 5 taken 1649 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 8 times.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
8187x if (errno == EAGAIN || errno == EWOULDBLOCK)
137 {
138
2/8
✓ Branch 0 taken 6523 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1649 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
8172x op.impl_ptr = this->shared_from_this();
139 8172x this->svc_.work_started();
140
141 8172x std::lock_guard lock(this->desc_state_.mutex);
142 8172x bool io_done = false;
143
2/8
✓ Branch 0 taken 6523 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1649 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
8172x if (this->desc_state_.read_ready)
144 {
145 ✗ this->desc_state_.read_ready = false;
146 ✗ op.perform_io();
147 ✗ io_done = (op.errn != EAGAIN && op.errn != EWOULDBLOCK);
148 ✗ if (!io_done)
149 ✗ op.errn = 0;
150 ✗ }
151
152
4/16
✓ Branch 0 taken 6523 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 6523 times.
✓ Branch 4 taken 1649 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 1649 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
8172x if (io_done || op.cancelled.load(std::memory_order_acquire))
153 {
154 ✗ this->svc_.post(&op);
155 ✗ this->svc_.work_finished();
156 ✗ }
157 else
158 {
159 8172x this->desc_state_.read_op = &op;
160
161 // Select watches reads only for parked ops; see register_op.
162 if constexpr (Traits::needs_park_notification)
163
1/4
✓ Branch 0 taken 1649 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
1649x this->svc_.scheduler().notify_reactor();
164 }
165 8172x return std::noop_coroutine();
166 8172x }
167
168 15x op.complete(errno, 0);
169
2/8
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
15x op.impl_ptr = this->shared_from_this();
170 15x this->svc_.post(&op);
171 15x return std::noop_coroutine();
172 8221x }
173
174 } // namespace boost::corosio::detail
175
176 #endif // BOOST_COROSIO_NATIVE_DETAIL_REACTOR_REACTOR_BACKEND_HPP
177