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

92.1% Lines (70 / 76) 100.0% Functions (1 / 1, 1 excl)
reactor_backend.hpp
f(x) Functions (2)
Line 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 7336x reactor_acceptor_impl<
43 Derived,
44 Traits,
45 Service,
46 SocketFinal,
47 AccImplBase,
48 Endpoint>::
49 accept(
50 capy::continuation& cont,
51 capy::executor_ref ex,
52 std::stop_token token,
53 std::error_code* ec,
54 io_object::implementation** impl_out)
55 {
56 7336x auto& op = this->acc_;
57 7336x op.reset();
58 7336x op.cont = &cont;
59 7336x op.ex = ex;
60 7336x op.ec_out = ec;
61 7336x op.impl_out = impl_out;
62 7336x op.fd = this->fd_;
63 7336x op.start(token, static_cast<Derived*>(this));
64
65 7336x sockaddr_storage peer_storage{};
66 7336x socklen_t peer_addrlen = 0;
67
68 int accepted =
69 7336x Traits::accept_policy::do_accept(this->fd_, peer_storage, peer_addrlen);
70
71 7336x if (accepted >= 0)
72 {
73 {
74 626x std::lock_guard lock(this->desc_state_.mutex);
75 626x this->desc_state_.read_ready = false;
76 626x }
77
78 626x if (this->svc_.scheduler().try_consume_inline_budget())
79 {
80 506x auto* socket_svc = this->svc_.stream_service();
81 506x if (socket_svc)
82 {
83 auto& impl =
84 506x static_cast<SocketFinal&>(*socket_svc->construct());
85 506x impl.set_socket(
86 accepted,
87 506x peer_addrlen > 0 ? peer_storage.ss_family : AF_UNSPEC);
88
89 506x impl.desc_state_.fd = accepted;
90 {
91 506x std::lock_guard lock(impl.desc_state_.mutex);
92 506x impl.desc_state_.read_op = nullptr;
93 506x impl.desc_state_.write_op = nullptr;
94 506x impl.desc_state_.connect_op = nullptr;
95 506x }
96 506x auto reg_ec = socket_svc->scheduler().register_descriptor(
97 accepted, &impl.desc_state_);
98 506x if (reg_ec)
99 {
100 // destroy() closes the fd the impl already owns.
101 1x socket_svc->destroy(&impl);
102 1x *ec = reg_ec;
103 1x if (impl_out)
104 1x *impl_out = nullptr;
105 }
106 else
107 {
108 505x impl.set_endpoints(
109 this->local_endpoint_,
110 505x from_sockaddr_as(
111 peer_storage, peer_addrlen, Endpoint{}));
112
113 505x *ec = {};
114 505x if (impl_out)
115 505x *impl_out = &impl;
116 }
117 }
118 else
119 {
120 ✗ ::close(accepted);
121 ✗ *ec = make_err(ENOENT);
122 ✗ if (impl_out)
123 ✗ *impl_out = nullptr;
124 }
125 // Completed without parking: nothing left for a stop to cancel.
126 506x op.stop_cb.reset();
127 506x return dispatch_coro(ex, cont);
128 }
129
130 120x op.accepted_fd = accepted;
131 120x op.peer_storage = peer_storage;
132 120x op.peer_addrlen = peer_addrlen;
133 120x op.complete(0, 0);
134 120x op.object_ref_ = detail::object_ref(this);
135 120x this->svc_.post(&op);
136 120x return std::noop_coroutine();
137 }
138
139 6710x if (errno == EAGAIN || errno == EWOULDBLOCK)
140 {
141 6700x op.object_ref_ = detail::object_ref(this);
142 6700x this->svc_.work_started();
143
144 // Posted after the descriptor lock is released: the scheduler
145 // takes its own lock before descriptor locks.
146 6700x bool post_now = false;
147 {
148 6700x std::lock_guard lock(this->desc_state_.mutex);
149 6700x bool io_done = false;
150 6700x if (this->desc_state_.read_ready)
151 {
152 2x this->desc_state_.read_ready = false;
153 2x op.perform_io();
154 2x io_done = (op.errn != EAGAIN && op.errn != EWOULDBLOCK);
155 2x if (!io_done)
156 2x op.errn = 0;
157 }
158
159 6700x if (io_done || op.cancelled.load(std::memory_order_acquire))
160 {
161 ✗ post_now = true;
162 }
163 else
164 {
165 6700x this->desc_state_.read_op = &op;
166 // Select watches reads only for parked ops; see
167 // park_op.
168 if constexpr (Traits::needs_park_notification)
169 3019x this->svc_.scheduler().notify_reactor();
170 }
171 6700x }
172 6700x if (post_now)
173 ✗ this->svc_.scheduler().post_counted(&op);
174 6700x return std::noop_coroutine();
175 }
176
177 10x op.complete(errno, 0);
178 10x op.object_ref_ = detail::object_ref(this);
179 10x this->svc_.post(&op);
180 10x return std::noop_coroutine();
181 }
182
183 } // namespace boost::corosio::detail
184
185 #endif // BOOST_COROSIO_NATIVE_DETAIL_REACTOR_REACTOR_BACKEND_HPP
186