TLA Line data Source code
1 : //
2 : // Copyright (c) 2026 Steve Gerbino
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_STREAM_SOCKET_HPP
11 : #define BOOST_COROSIO_NATIVE_DETAIL_REACTOR_REACTOR_STREAM_SOCKET_HPP
12 :
13 : #include <boost/corosio/tcp_socket.hpp>
14 : #include <boost/corosio/shutdown_type.hpp>
15 : #include <boost/corosio/wait_type.hpp>
16 : #include <boost/corosio/native/detail/reactor/reactor_basic_socket.hpp>
17 : #include <boost/corosio/native/detail/reactor/reactor_descriptor_state.hpp>
18 : #include <boost/corosio/detail/dispatch_coro.hpp>
19 : #include <boost/capy/buffers.hpp>
20 :
21 : #include <coroutine>
22 :
23 : #include <errno.h>
24 : #include <sys/socket.h>
25 : #include <sys/uio.h>
26 :
27 : namespace boost::corosio::detail {
28 :
29 : /** CRTP base for reactor-backed stream socket implementations.
30 :
31 : Inherits shared data members and cancel/close/register logic
32 : from reactor_basic_socket. Adds the stream-specific remote
33 : endpoint, shutdown, and I/O dispatch (connect, read, write, wait).
34 :
35 : @tparam Derived The concrete socket type (CRTP).
36 : @tparam Service The backend's socket service type.
37 : @tparam ConnOp The backend's connect op type.
38 : @tparam ReadOp The backend's read op type.
39 : @tparam WriteOp The backend's write op type.
40 : @tparam WaitOp The backend's wait op type.
41 : @tparam DescState The backend's descriptor_state type.
42 : @tparam ImplBase The public vtable base
43 : (tcp_socket::implementation or
44 : local_stream_socket::implementation).
45 : @tparam Endpoint The endpoint type (endpoint or local_endpoint).
46 : */
47 : template<
48 : class Derived,
49 : class Service,
50 : class ConnOp,
51 : class ReadOp,
52 : class WriteOp,
53 : class WaitOp,
54 : class DescState,
55 : class ImplBase = tcp_socket::implementation,
56 : class Endpoint = endpoint>
57 : class reactor_stream_socket
58 : : public reactor_basic_socket<
59 : Derived,
60 : ImplBase,
61 : Service,
62 : DescState,
63 : Endpoint>
64 : {
65 : using base_type = reactor_basic_socket<
66 : Derived,
67 : ImplBase,
68 : Service,
69 : DescState,
70 : Endpoint>;
71 : using self_type = reactor_stream_socket<
72 : Derived, Service, ConnOp, ReadOp, WriteOp, WaitOp,
73 : DescState, ImplBase, Endpoint>;
74 : friend base_type;
75 : friend Derived;
76 :
77 : protected:
78 : // NOLINTNEXTLINE(bugprone-crtp-constructor-accessibility)
79 HIT 13592 : explicit reactor_stream_socket(Service& svc) noexcept : base_type(svc) {}
80 :
81 : protected:
82 : Endpoint remote_endpoint_;
83 :
84 : public:
85 : /// Pending connect operation slot.
86 : ConnOp conn_;
87 :
88 : /// Pending read operation slot.
89 : ReadOp rd_;
90 :
91 : /// Pending write operation slot.
92 : WriteOp wr_;
93 :
94 : /// Pending wait-for-read operation slot.
95 : WaitOp wait_rd_;
96 :
97 : /// Pending wait-for-write operation slot.
98 : WaitOp wait_wr_;
99 :
100 : /// Pending wait-for-error operation slot.
101 : WaitOp wait_er_;
102 :
103 13592 : ~reactor_stream_socket() override = default;
104 :
105 : /// Return the cached remote endpoint.
106 60 : Endpoint remote_endpoint() const noexcept override
107 : {
108 60 : return remote_endpoint_;
109 : }
110 :
111 : // --- Virtual method overrides (satisfy ImplBase pure virtuals) ---
112 :
113 4418 : std::coroutine_handle<> connect(
114 : std::coroutine_handle<> h,
115 : capy::executor_ref ex,
116 : Endpoint ep,
117 : std::stop_token token,
118 : std::error_code* ec) override
119 : {
120 4418 : return do_connect(h, ex, ep, token, ec);
121 : }
122 :
123 197776 : std::coroutine_handle<> read_some(
124 : std::coroutine_handle<> h,
125 : capy::executor_ref ex,
126 : buffer_param param,
127 : std::stop_token token,
128 : std::error_code* ec,
129 : std::size_t* bytes_out) override
130 : {
131 197776 : return do_read_some(h, ex, param, token, ec, bytes_out);
132 : }
133 :
134 197131 : std::coroutine_handle<> write_some(
135 : std::coroutine_handle<> h,
136 : capy::executor_ref ex,
137 : buffer_param param,
138 : std::stop_token token,
139 : std::error_code* ec,
140 : std::size_t* bytes_out) override
141 : {
142 197131 : return do_write_some(h, ex, param, token, ec, bytes_out);
143 : }
144 :
145 51 : std::coroutine_handle<> wait(
146 : std::coroutine_handle<> h,
147 : capy::executor_ref ex,
148 : wait_type w,
149 : std::stop_token token,
150 : std::error_code* ec) override
151 : {
152 51 : return do_wait(h, ex, w, token, ec);
153 : }
154 :
155 : std::error_code
156 23 : shutdown(corosio::shutdown_type what) noexcept override
157 : {
158 23 : return do_shutdown(static_cast<int>(what));
159 : }
160 :
161 207 : void cancel() noexcept override
162 : {
163 207 : this->do_cancel();
164 207 : }
165 :
166 : // --- End virtual overrides ---
167 :
168 : /// Close the socket (non-virtual, called by the service).
169 : void close_socket() noexcept
170 : {
171 : this->do_close_socket();
172 : }
173 :
174 : /** Shut down part or all of the full-duplex connection.
175 :
176 : @param what 0 = receive, 1 = send, 2 = both.
177 : */
178 23 : std::error_code do_shutdown(int what) noexcept
179 : {
180 : int how;
181 23 : switch (what)
182 : {
183 4 : case 0: // shutdown_receive
184 4 : how = SHUT_RD;
185 4 : break;
186 15 : case 1: // shutdown_send
187 15 : how = SHUT_WR;
188 15 : break;
189 4 : case 2: // shutdown_both
190 4 : how = SHUT_RDWR;
191 4 : break;
192 MIS 0 : default:
193 0 : return make_err(EINVAL);
194 : }
195 HIT 23 : if (::shutdown(this->fd_, how) != 0)
196 MIS 0 : return make_err(errno);
197 HIT 23 : return {};
198 : }
199 :
200 : /// Cache local and remote endpoints.
201 8879 : void set_endpoints(Endpoint local, Endpoint remote) noexcept
202 : {
203 8879 : this->local_endpoint_ = std::move(local);
204 8879 : remote_endpoint_ = std::move(remote);
205 8879 : }
206 :
207 : /** Shared connect dispatch.
208 :
209 : Tries the connect syscall speculatively. On synchronous
210 : completion, returns via inline budget or posts through queue.
211 : On EINPROGRESS, registers with the reactor.
212 : */
213 : std::coroutine_handle<> do_connect(
214 : std::coroutine_handle<>,
215 : capy::executor_ref,
216 : Endpoint const&,
217 : std::stop_token const&,
218 : std::error_code*);
219 :
220 : /** Shared scatter-read dispatch.
221 :
222 : Tries readv() speculatively. On success or hard error,
223 : returns via inline budget or posts through queue.
224 : On EAGAIN, registers with the reactor.
225 : */
226 : std::coroutine_handle<> do_read_some(
227 : std::coroutine_handle<>,
228 : capy::executor_ref,
229 : buffer_param,
230 : std::stop_token const&,
231 : std::error_code*,
232 : std::size_t*);
233 :
234 : /** Shared gather-write dispatch.
235 :
236 : Tries the write via WriteOp::write_policy speculatively.
237 : On success or hard error, returns via inline budget or
238 : posts through queue. On EAGAIN, registers with the reactor.
239 : */
240 : std::coroutine_handle<> do_write_some(
241 : std::coroutine_handle<>,
242 : capy::executor_ref,
243 : buffer_param,
244 : std::stop_token const&,
245 : std::error_code*,
246 : std::size_t*);
247 :
248 : /** Shared readiness-wait dispatch.
249 :
250 : Every wait type probes the descriptor with a zero-timeout
251 : `poll()` and completes at once if the condition already
252 : holds; otherwise the op re-probes under the descriptor mutex
253 : and parks, completing when a reactor event arrives and a
254 : fresh probe confirms the condition. A write wait therefore
255 : completes only while a non-blocking write can make progress.
256 : */
257 : std::coroutine_handle<> do_wait(
258 : std::coroutine_handle<>,
259 : capy::executor_ref,
260 : wait_type,
261 : std::stop_token const&,
262 : std::error_code*);
263 :
264 : /** Close the socket and cancel pending operations.
265 :
266 : Extends the base do_close_socket() to also reset
267 : the remote endpoint.
268 : */
269 40764 : void do_close_socket() noexcept
270 : {
271 40764 : base_type::do_close_socket();
272 40764 : remote_endpoint_ = Endpoint{};
273 40764 : }
274 :
275 : /// Release ownership of the descriptor and drop the cached peer.
276 6 : native_handle_type do_release_socket() noexcept
277 : {
278 6 : auto fd = base_type::do_release_socket();
279 6 : remote_endpoint_ = Endpoint{};
280 6 : return fd;
281 : }
282 :
283 : private:
284 : // CRTP callbacks for reactor_basic_socket cancel/close
285 :
286 : template<class Op>
287 223 : reactor_op_base** op_to_desc_slot(Op& op) noexcept
288 : {
289 223 : if (&op == static_cast<void*>(&conn_))
290 5 : return &this->desc_state_.connect_op;
291 218 : if (&op == static_cast<void*>(&rd_))
292 203 : return &this->desc_state_.read_op;
293 15 : if (&op == static_cast<void*>(&wr_))
294 4 : return &this->desc_state_.write_op;
295 11 : if (&op == static_cast<void*>(&wait_rd_))
296 7 : return &this->desc_state_.wait_read_op;
297 4 : if (&op == static_cast<void*>(&wait_wr_))
298 2 : return &this->desc_state_.wait_write_op;
299 2 : if (&op == static_cast<void*>(&wait_er_))
300 2 : return &this->desc_state_.wait_error_op;
301 MIS 0 : return nullptr;
302 : }
303 :
304 : template<class Op>
305 : bool* op_to_cancel_flag(Op& op) noexcept
306 : {
307 : if (&op == static_cast<void*>(&conn_))
308 : return &this->desc_state_.connect_cancel_pending;
309 : if (&op == static_cast<void*>(&rd_))
310 : return &this->desc_state_.read_cancel_pending;
311 : if (&op == static_cast<void*>(&wr_))
312 : return &this->desc_state_.write_cancel_pending;
313 : if (&op == static_cast<void*>(&wait_rd_))
314 : return &this->desc_state_.wait_read_cancel_pending;
315 : if (&op == static_cast<void*>(&wait_wr_))
316 : return &this->desc_state_.wait_write_cancel_pending;
317 : if (&op == static_cast<void*>(&wait_er_))
318 : return &this->desc_state_.wait_error_cancel_pending;
319 : return nullptr;
320 : }
321 :
322 : template<class Fn>
323 HIT 40977 : void for_each_op(Fn fn) noexcept
324 : {
325 40977 : fn(conn_);
326 40977 : fn(rd_);
327 40977 : fn(wr_);
328 40977 : fn(wait_rd_);
329 40977 : fn(wait_wr_);
330 40977 : fn(wait_er_);
331 40977 : }
332 :
333 : template<class Fn>
334 40977 : void for_each_desc_entry(Fn fn) noexcept
335 : {
336 40977 : fn(conn_, this->desc_state_.connect_op);
337 40977 : fn(rd_, this->desc_state_.read_op);
338 40977 : fn(wr_, this->desc_state_.write_op);
339 40977 : fn(wait_rd_, this->desc_state_.wait_read_op);
340 40977 : fn(wait_wr_, this->desc_state_.wait_write_op);
341 40977 : fn(wait_er_, this->desc_state_.wait_error_op);
342 40977 : }
343 : };
344 :
345 : template<
346 : class Derived,
347 : class Service,
348 : class ConnOp,
349 : class ReadOp,
350 : class WriteOp,
351 : class WaitOp,
352 : class DescState,
353 : class ImplBase,
354 : class Endpoint>
355 : std::coroutine_handle<>
356 4418 : reactor_stream_socket<Derived, Service, ConnOp, ReadOp, WriteOp, WaitOp, DescState, ImplBase, Endpoint>::
357 : do_connect(
358 : std::coroutine_handle<> h,
359 : capy::executor_ref ex,
360 : Endpoint const& ep,
361 : std::stop_token const& token,
362 : std::error_code* ec)
363 : {
364 4418 : auto& op = conn_;
365 :
366 4418 : sockaddr_storage storage{};
367 4418 : socklen_t addrlen = to_sockaddr(ep, socket_family(this->fd_), storage);
368 : int result =
369 4418 : ::connect(this->fd_, reinterpret_cast<sockaddr*>(&storage), addrlen);
370 :
371 4418 : if (result == 0)
372 : {
373 27 : sockaddr_storage local_storage{};
374 27 : socklen_t local_len = sizeof(local_storage);
375 27 : if (::getsockname(
376 : this->fd_, reinterpret_cast<sockaddr*>(&local_storage),
377 27 : &local_len) == 0)
378 MIS 0 : this->local_endpoint_ =
379 HIT 27 : from_sockaddr_as(local_storage, local_len, Endpoint{});
380 27 : remote_endpoint_ = ep;
381 : }
382 :
383 4418 : if (result == 0 || errno != EINPROGRESS)
384 : {
385 31 : int err = (result < 0) ? errno : 0;
386 31 : if (this->svc_.scheduler().try_consume_inline_budget())
387 : {
388 MIS 0 : *ec = err ? make_err(err) : std::error_code{};
389 0 : op.cont.h = h;
390 0 : return dispatch_coro(ex, op.cont);
391 : }
392 HIT 31 : op.reset();
393 31 : op.h = h;
394 31 : op.ex = ex;
395 31 : op.ec_out = ec;
396 31 : op.fd = this->fd_;
397 31 : op.target_endpoint = ep;
398 31 : op.start(token, static_cast<Derived*>(this));
399 31 : op.impl_ptr = this->shared_from_this();
400 31 : op.complete(err, 0);
401 31 : this->svc_.post(&op);
402 31 : return std::noop_coroutine();
403 : }
404 :
405 : // EINPROGRESS — register with reactor
406 4387 : op.reset();
407 4387 : op.h = h;
408 4387 : op.ex = ex;
409 4387 : op.ec_out = ec;
410 4387 : op.fd = this->fd_;
411 4387 : op.target_endpoint = ep;
412 4387 : op.start(token, static_cast<Derived*>(this));
413 4387 : op.impl_ptr = this->shared_from_this();
414 :
415 4387 : this->register_op(
416 4387 : op, this->desc_state_.connect_op, this->desc_state_.write_ready,
417 4387 : this->desc_state_.connect_cancel_pending, true);
418 4387 : return std::noop_coroutine();
419 : }
420 :
421 : template<
422 : class Derived,
423 : class Service,
424 : class ConnOp,
425 : class ReadOp,
426 : class WriteOp,
427 : class WaitOp,
428 : class DescState,
429 : class ImplBase,
430 : class Endpoint>
431 : std::coroutine_handle<>
432 197776 : reactor_stream_socket<Derived, Service, ConnOp, ReadOp, WriteOp, WaitOp, DescState, ImplBase, Endpoint>::
433 : do_read_some(
434 : std::coroutine_handle<> h,
435 : capy::executor_ref ex,
436 : buffer_param param,
437 : std::stop_token const& token,
438 : std::error_code* ec,
439 : std::size_t* bytes_out)
440 : {
441 197776 : auto& op = rd_;
442 197776 : op.reset();
443 :
444 197776 : capy::mutable_buffer bufs[ReadOp::max_buffers];
445 197776 : op.iovec_count = static_cast<int>(param.copy_to(bufs, ReadOp::max_buffers));
446 :
447 197776 : if (op.iovec_count == 0 || (op.iovec_count == 1 && bufs[0].size() == 0))
448 : {
449 4 : op.empty_buffer_read = true;
450 4 : op.h = h;
451 4 : op.ex = ex;
452 4 : op.ec_out = ec;
453 4 : op.bytes_out = bytes_out;
454 4 : op.start(token, static_cast<Derived*>(this));
455 4 : op.impl_ptr = this->shared_from_this();
456 4 : op.complete(0, 0);
457 4 : this->svc_.post(&op);
458 4 : return std::noop_coroutine();
459 : }
460 :
461 395556 : for (int i = 0; i < op.iovec_count; ++i)
462 : {
463 197784 : op.iovecs[i].iov_base = bufs[i].data();
464 197784 : op.iovecs[i].iov_len = bufs[i].size();
465 : }
466 :
467 : // Speculative read; for the single-buffer case use recv() so the
468 : // kernel skips the readv iov_iter setup.
469 : ssize_t n;
470 197772 : if (op.iovec_count == 1)
471 : {
472 : do
473 : {
474 197764 : n = ::recv(this->fd_, bufs[0].data(), bufs[0].size(), 0);
475 : }
476 197764 : while (n < 0 && errno == EINTR);
477 : }
478 : else
479 : {
480 : do
481 : {
482 8 : n = ::readv(this->fd_, op.iovecs, op.iovec_count);
483 : }
484 8 : while (n < 0 && errno == EINTR);
485 : }
486 :
487 197772 : if (n >= 0 || (errno != EAGAIN && errno != EWOULDBLOCK))
488 : {
489 197031 : int err = (n < 0) ? errno : 0;
490 197031 : auto bytes = (n > 0) ? static_cast<std::size_t>(n) : std::size_t(0);
491 :
492 197031 : if (this->svc_.scheduler().try_consume_inline_budget())
493 : {
494 157662 : if (err)
495 MIS 0 : *ec = make_err(err);
496 HIT 157662 : else if (n == 0)
497 15 : *ec = capy::error::eof;
498 : else
499 157647 : *ec = {};
500 157662 : *bytes_out = bytes;
501 157662 : op.cont.h = h;
502 157662 : return dispatch_coro(ex, op.cont);
503 : }
504 39369 : op.h = h;
505 39369 : op.ex = ex;
506 39369 : op.ec_out = ec;
507 39369 : op.bytes_out = bytes_out;
508 39369 : op.start(token, static_cast<Derived*>(this));
509 39369 : op.impl_ptr = this->shared_from_this();
510 39369 : op.complete(err, bytes);
511 39369 : this->svc_.post(&op);
512 39369 : return std::noop_coroutine();
513 : }
514 :
515 : // EAGAIN — register with reactor
516 741 : op.h = h;
517 741 : op.ex = ex;
518 741 : op.ec_out = ec;
519 741 : op.bytes_out = bytes_out;
520 741 : op.fd = this->fd_;
521 741 : op.start(token, static_cast<Derived*>(this));
522 741 : op.impl_ptr = this->shared_from_this();
523 :
524 741 : this->register_op(
525 741 : op, this->desc_state_.read_op, this->desc_state_.read_ready,
526 741 : this->desc_state_.read_cancel_pending);
527 741 : return std::noop_coroutine();
528 : }
529 :
530 : template<
531 : class Derived,
532 : class Service,
533 : class ConnOp,
534 : class ReadOp,
535 : class WriteOp,
536 : class WaitOp,
537 : class DescState,
538 : class ImplBase,
539 : class Endpoint>
540 : std::coroutine_handle<>
541 197131 : reactor_stream_socket<Derived, Service, ConnOp, ReadOp, WriteOp, WaitOp, DescState, ImplBase, Endpoint>::
542 : do_write_some(
543 : std::coroutine_handle<> h,
544 : capy::executor_ref ex,
545 : buffer_param param,
546 : std::stop_token const& token,
547 : std::error_code* ec,
548 : std::size_t* bytes_out)
549 : {
550 197131 : auto& op = wr_;
551 197131 : op.reset();
552 :
553 197131 : capy::mutable_buffer bufs[WriteOp::max_buffers];
554 197131 : op.iovec_count =
555 197131 : static_cast<int>(param.copy_to(bufs, WriteOp::max_buffers));
556 :
557 197131 : if (op.iovec_count == 0 || (op.iovec_count == 1 && bufs[0].size() == 0))
558 : {
559 4 : op.h = h;
560 4 : op.ex = ex;
561 4 : op.ec_out = ec;
562 4 : op.bytes_out = bytes_out;
563 4 : op.start(token, static_cast<Derived*>(this));
564 4 : op.impl_ptr = this->shared_from_this();
565 4 : op.complete(0, 0);
566 4 : this->svc_.post(&op);
567 4 : return std::noop_coroutine();
568 : }
569 :
570 394264 : for (int i = 0; i < op.iovec_count; ++i)
571 : {
572 197137 : op.iovecs[i].iov_base = bufs[i].data();
573 197137 : op.iovecs[i].iov_len = bufs[i].size();
574 : }
575 :
576 : // Speculative write; the single-buffer case dispatches to a
577 : // backend-specific fast path so the kernel skips msghdr/iov_iter
578 : // setup (and so each backend can pick the right SIGPIPE strategy).
579 : ssize_t n;
580 197127 : if (op.iovec_count == 1)
581 : {
582 394242 : n = WriteOp::write_policy::write_one(
583 197121 : this->fd_, bufs[0].data(), bufs[0].size());
584 : }
585 : else
586 : {
587 6 : n = WriteOp::write_policy::write(
588 6 : this->fd_, op.iovecs, op.iovec_count);
589 : }
590 :
591 197127 : if (n >= 0 || (errno != EAGAIN && errno != EWOULDBLOCK))
592 : {
593 196998 : int err = (n < 0) ? errno : 0;
594 196998 : auto bytes = (n > 0) ? static_cast<std::size_t>(n) : std::size_t(0);
595 :
596 196998 : if (this->svc_.scheduler().try_consume_inline_budget())
597 : {
598 157565 : *ec = err ? make_err(err) : std::error_code{};
599 157565 : *bytes_out = bytes;
600 157565 : op.cont.h = h;
601 157565 : return dispatch_coro(ex, op.cont);
602 : }
603 39433 : op.h = h;
604 39433 : op.ex = ex;
605 39433 : op.ec_out = ec;
606 39433 : op.bytes_out = bytes_out;
607 39433 : op.start(token, static_cast<Derived*>(this));
608 39433 : op.impl_ptr = this->shared_from_this();
609 39433 : op.complete(err, bytes);
610 39433 : this->svc_.post(&op);
611 39433 : return std::noop_coroutine();
612 : }
613 :
614 : // EAGAIN — register with reactor
615 129 : op.h = h;
616 129 : op.ex = ex;
617 129 : op.ec_out = ec;
618 129 : op.bytes_out = bytes_out;
619 129 : op.fd = this->fd_;
620 129 : op.start(token, static_cast<Derived*>(this));
621 129 : op.impl_ptr = this->shared_from_this();
622 :
623 129 : this->register_op(
624 129 : op, this->desc_state_.write_op, this->desc_state_.write_ready,
625 129 : this->desc_state_.write_cancel_pending, true);
626 129 : return std::noop_coroutine();
627 : }
628 :
629 : template<
630 : class Derived,
631 : class Service,
632 : class ConnOp,
633 : class ReadOp,
634 : class WriteOp,
635 : class WaitOp,
636 : class DescState,
637 : class ImplBase,
638 : class Endpoint>
639 : std::coroutine_handle<>
640 51 : reactor_stream_socket<Derived, Service, ConnOp, ReadOp, WriteOp, WaitOp, DescState, ImplBase, Endpoint>::
641 : do_wait(
642 : std::coroutine_handle<> h,
643 : capy::executor_ref ex,
644 : wait_type w,
645 : std::stop_token const& token,
646 : std::error_code* ec)
647 : {
648 : // Pick refs up-front to avoid duplicating the register_op call.
649 : WaitOp* op_ptr;
650 : reactor_op_base** desc_slot_ptr;
651 : bool* cancel_flag_ptr;
652 : std::uint32_t event;
653 :
654 51 : if (w == wait_type::read)
655 : {
656 21 : op_ptr = &wait_rd_;
657 21 : desc_slot_ptr = &this->desc_state_.wait_read_op;
658 21 : cancel_flag_ptr = &this->desc_state_.wait_read_cancel_pending;
659 21 : event = reactor_event_read;
660 : }
661 30 : else if (w == wait_type::write)
662 : {
663 16 : op_ptr = &wait_wr_;
664 16 : desc_slot_ptr = &this->desc_state_.wait_write_op;
665 16 : cancel_flag_ptr = &this->desc_state_.wait_write_cancel_pending;
666 16 : event = reactor_event_write;
667 : }
668 : else // wait_type::error
669 : {
670 14 : op_ptr = &wait_er_;
671 14 : desc_slot_ptr = &this->desc_state_.wait_error_op;
672 14 : cancel_flag_ptr = &this->desc_state_.wait_error_cancel_pending;
673 14 : event = reactor_event_error;
674 : }
675 :
676 51 : auto& op = *op_ptr;
677 :
678 : // Speculative probe, mirroring the speculative read: an
679 : // edge-triggered reactor cannot report a condition that already
680 : // holds, so a wait initiated on an already-ready socket would
681 : // otherwise park forever.
682 51 : int perr = 0;
683 51 : if (WaitOp::probe(this->fd_, event, perr))
684 : {
685 18 : if (this->svc_.scheduler().try_consume_inline_budget())
686 : {
687 4 : *ec = perr ? make_err(perr) : std::error_code{};
688 4 : op.cont.h = h;
689 4 : return dispatch_coro(ex, op.cont);
690 : }
691 14 : op.reset();
692 14 : op.wait_event = event;
693 14 : op.h = h;
694 14 : op.ex = ex;
695 14 : op.ec_out = ec;
696 14 : op.fd = this->fd_;
697 14 : op.start(token, static_cast<Derived*>(this));
698 14 : op.impl_ptr = this->shared_from_this();
699 14 : op.complete(perr, 0);
700 14 : this->svc_.post(&op);
701 14 : return std::noop_coroutine();
702 : }
703 :
704 33 : op.reset();
705 33 : op.wait_event = event;
706 33 : op.h = h;
707 33 : op.ex = ex;
708 33 : op.ec_out = ec;
709 33 : op.fd = this->fd_;
710 33 : op.start(token, static_cast<Derived*>(this));
711 33 : op.impl_ptr = this->shared_from_this();
712 :
713 : // Force register_op's ready path so the wait op re-probes under
714 : // the descriptor mutex before parking. An edge consumed between
715 : // the speculative probe above and the park (a concurrent short
716 : // read, or an error event dispatched to an empty slot) would
717 : // otherwise leave the wait parked on a ready socket.
718 33 : bool force_probe = true;
719 33 : this->register_op(op, *desc_slot_ptr, force_probe, *cancel_flag_ptr,
720 : event == reactor_event_write);
721 33 : return std::noop_coroutine();
722 : }
723 :
724 : } // namespace boost::corosio::detail
725 :
726 : #endif // BOOST_COROSIO_NATIVE_DETAIL_REACTOR_REACTOR_STREAM_SOCKET_HPP
|