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

81.7% Lines (67/82) 100.0% List of functions (4/4) 29.2% Branches (42/144)
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**) :37 18x 30.5% 4.2% 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**) :37 5863x 64.6% 30.6% 60.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**) :37 17x 48.8% 33.3% 37.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**) :37 1699x 81.7% 50.0% 60.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<class Derived, class Traits, class Service,
35 class SocketFinal, class AccImplBase, class Endpoint>
36 std::coroutine_handle<>
37 7597x reactor_acceptor_impl<Derived, Traits, Service, SocketFinal, AccImplBase, Endpoint>::accept(
38 std::coroutine_handle<> h,
39 capy::executor_ref ex,
40 std::stop_token token,
41 std::error_code* ec,
42 io_object::implementation** impl_out)
43 {
44 7597x auto& op = this->acc_;
45 7597x op.reset();
46 7597x op.h = h;
47 7597x op.ex = ex;
48 7597x op.ec_out = ec;
49 7597x op.impl_out = impl_out;
50 7597x op.fd = this->fd_;
51 7597x op.start(token, static_cast<Derived*>(this));
52
53 7597x sockaddr_storage peer_storage{};
54 7597x socklen_t peer_addrlen = 0;
55
56 7597x int accepted = Traits::accept_policy::do_accept(
57 7597x this->fd_, peer_storage, peer_addrlen);
58
59
5/8
✓ Branch 0 taken 5868 times.
✓ Branch 1 taken 12 times.
✓ Branch 2 taken 1705 times.
✓ Branch 3 taken 11 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✓ Branch 6 taken 1 time.
✗ Branch 7 not taken.
7597x if (accepted >= 0)
60 {
61 {
62 23x std::lock_guard lock(this->desc_state_.mutex);
63 23x this->desc_state_.read_ready = false;
64 23x }
65
66
4/8
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 10 times.
✓ Branch 2 taken 1 time.
✓ Branch 3 taken 10 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
23x if (this->svc_.scheduler().try_consume_inline_budget())
67 {
68 3x auto* socket_svc = this->svc_.stream_service();
69
2/8
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
3x if (socket_svc)
70 {
71 3x auto& impl =
72 3x static_cast<SocketFinal&>(*socket_svc->construct());
73 3x impl.set_socket(accepted);
74
75 3x impl.desc_state_.fd = accepted;
76 {
77 3x std::lock_guard lock(impl.desc_state_.mutex);
78 3x impl.desc_state_.read_op = nullptr;
79 3x impl.desc_state_.write_op = nullptr;
80 3x impl.desc_state_.connect_op = nullptr;
81 3x }
82 6x auto reg_ec = socket_svc->scheduler().register_descriptor(
83 3x accepted, &impl.desc_state_);
84
2/8
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 time.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
3x if (reg_ec)
85 {
86 // destroy() closes the fd the impl already owns.
87 socket_svc->destroy(&impl);
88 *ec = reg_ec;
89 if (impl_out)
90 *impl_out = nullptr;
91 }
92 else
93 {
94 6x impl.set_endpoints(
95 3x this->local_endpoint_,
96 3x from_sockaddr_as(
97 3x peer_storage, peer_addrlen, Endpoint{}));
98
99 3x *ec = {};
100
2/8
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 time.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
3x if (impl_out)
101 3x *impl_out = &impl;
102 }
103 3x }
104 else
105 {
106 ::close(accepted);
107 *ec = make_err(ENOENT);
108 if (impl_out)
109 *impl_out = nullptr;
110 }
111 3x op.cont.h = h;
112 3x return dispatch_coro(ex, op.cont);
113 }
114
115 20x op.accepted_fd = accepted;
116 20x op.peer_storage = peer_storage;
117 20x op.peer_addrlen = peer_addrlen;
118 20x op.complete(0, 0);
119
2/8
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 10 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
20x op.impl_ptr = this->shared_from_this();
120 20x this->svc_.post(&op);
121 20x return std::noop_coroutine();
122 }
123
124
7/16
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 5866 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
✓ Branch 4 taken 2 times.
✓ Branch 5 taken 1703 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 2 times.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✓ Branch 13 taken 1 time.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
7574x if (errno == EAGAIN || errno == EWOULDBLOCK)
125 {
126
3/8
✓ Branch 0 taken 5866 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1703 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✓ Branch 6 taken 1 time.
✗ Branch 7 not taken.
7570x op.impl_ptr = this->shared_from_this();
127 7570x this->svc_.work_started();
128
129 7570x std::lock_guard lock(this->desc_state_.mutex);
130 7570x bool io_done = false;
131
3/8
✓ Branch 0 taken 5866 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1703 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✓ Branch 6 taken 1 time.
✗ Branch 7 not taken.
7570x if (this->desc_state_.read_ready)
132 {
133 this->desc_state_.read_ready = false;
134 op.perform_io();
135 io_done = (op.errn != EAGAIN && op.errn != EWOULDBLOCK);
136 if (!io_done)
137 op.errn = 0;
138 }
139
140
8/16
✓ Branch 0 taken 5866 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✓ Branch 3 taken 5865 times.
✓ Branch 4 taken 1703 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 1 time.
✓ Branch 7 taken 1702 times.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✓ Branch 12 taken 1 time.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✓ Branch 15 taken 1 time.
7570x if (io_done || op.cancelled.load(std::memory_order_acquire))
141 {
142
2/8
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
2x this->svc_.post(&op);
143 2x this->svc_.work_finished();
144 2x }
145 else
146 {
147 7568x this->desc_state_.read_op = &op;
148 }
149 7570x return std::noop_coroutine();
150 7570x }
151
152 4x op.complete(errno, 0);
153
2/8
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
4x op.impl_ptr = this->shared_from_this();
154 4x this->svc_.post(&op);
155 4x return std::noop_coroutine();
156 7597x }
157
158 } // namespace boost::corosio::detail
159
160 #endif // BOOST_COROSIO_NATIVE_DETAIL_REACTOR_REACTOR_BACKEND_HPP
161