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 10367x 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 10367x auto& op = this->acc_;
57 10367x op.reset();
58 10367x op.cont = &cont;
59 10367x op.ex = ex;
60 10367x op.ec_out = ec;
61 10367x op.impl_out = impl_out;
62 10367x op.fd = this->fd_;
63 10367x op.start(token, static_cast<Derived*>(this));
64
65 10367x sockaddr_storage peer_storage{};
66 10367x socklen_t peer_addrlen = 0;
67
68 int accepted =
69 10367x Traits::accept_policy::do_accept(this->fd_, peer_storage, peer_addrlen);
70
71 10367x if (accepted >= 0)
72 {
73 {
74 783x std::lock_guard lock(this->desc_state_.mutex);
75 783x this->desc_state_.read_ready = false;
76 783x }
77
78 783x if (this->svc_.scheduler().try_consume_inline_budget())
79 {
80 621x auto* socket_svc = this->svc_.stream_service();
81 621x if (socket_svc)
82 {
83 auto& impl =
84 621x static_cast<SocketFinal&>(*socket_svc->construct());
85 621x impl.set_socket(
86 accepted,
87 621x peer_addrlen > 0 ? peer_storage.ss_family : AF_UNSPEC);
88
89 621x impl.desc_state_.fd = accepted;
90 {
91 621x std::lock_guard lock(impl.desc_state_.mutex);
92 621x impl.desc_state_.read_op = nullptr;
93 621x impl.desc_state_.write_op = nullptr;
94 621x impl.desc_state_.connect_op = nullptr;
95 621x }
96 621x auto reg_ec = socket_svc->scheduler().register_descriptor(
97 accepted, &impl.desc_state_);
98 621x 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 620x impl.set_endpoints(
109 this->local_endpoint_,
110 620x from_sockaddr_as(
111 peer_storage, peer_addrlen, Endpoint{}));
112
113 620x *ec = {};
114 620x if (impl_out)
115 620x *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 621x op.stop_cb.reset();
127 621x return dispatch_coro(ex, cont);
128 }
129
130 162x op.accepted_fd = accepted;
131 162x op.peer_storage = peer_storage;
132 162x op.peer_addrlen = peer_addrlen;
133 162x op.complete(0, 0);
134 162x op.object_ref_ = detail::object_ref(this);
135 162x this->svc_.post(&op);
136 162x return std::noop_coroutine();
137 }
138
139 9584x if (errno == EAGAIN || errno == EWOULDBLOCK)
140 {
141 9574x op.object_ref_ = detail::object_ref(this);
142 9574x this->svc_.work_started();
143
144 // Posted after the descriptor lock is released: the scheduler
145 // takes its own lock before descriptor locks.
146 9574x bool post_now = false;
147 {
148 9574x std::lock_guard lock(this->desc_state_.mutex);
149 9574x bool io_done = false;
150 9574x if (this->desc_state_.read_ready)
151 {
152 3x this->desc_state_.read_ready = false;
153 3x op.perform_io();
154 3x io_done = (op.errn != EAGAIN && op.errn != EWOULDBLOCK);
155 3x if (!io_done)
156 3x op.errn = 0;
157 }
158
159 9574x if (io_done || op.cancelled.load(std::memory_order_acquire))
160 {
161 ✗ post_now = true;
162 }
163 else
164 {
165 9574x 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 4214x this->svc_.scheduler().notify_reactor();
170 }
171 9574x }
172 9574x if (post_now)
173 ✗ this->svc_.scheduler().post_counted(&op);
174 9574x 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