include/boost/corosio/native/detail/iocp/win_overlapped_op.hpp

91.8% Lines (45/49) 88.9% List of functions (8/9) 90.0% Branches (9/10)
win_overlapped_op.hpp
f(x) Functions (9)
Line Branch TLA Hits Source Code
1 //
2 // Copyright (c) 2025 Vinnie Falco ([email protected])
3 // Copyright (c) 2026 Steve Gerbino
4 // Copyright (c) 2026 Michael Vandeberg
5 //
6 // Distributed under the Boost Software License, Version 1.0. (See accompanying
7 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
8 //
9 // Official repository: https://github.com/cppalliance/corosio
10 //
11
12 #ifndef BOOST_COROSIO_NATIVE_DETAIL_IOCP_WIN_OVERLAPPED_OP_HPP
13 #define BOOST_COROSIO_NATIVE_DETAIL_IOCP_WIN_OVERLAPPED_OP_HPP
14
15 #include <boost/corosio/detail/platform.hpp>
16
17 #if BOOST_COROSIO_HAS_IOCP
18
19 #include <boost/corosio/detail/config.hpp>
20 #include <boost/capy/error.hpp>
21 #include <system_error>
22
23 #include <boost/corosio/native/detail/make_err.hpp>
24 #include <boost/corosio/detail/dispatch_coro.hpp>
25 #include <boost/corosio/native/detail/coro_op.hpp>
26 #include <boost/corosio/native/detail/coro_op_complete.hpp>
27
28 #include <atomic>
29 #include <coroutine>
30 #include <cstddef>
31
32 #include <boost/corosio/native/detail/iocp/win_windows.hpp>
33
34 namespace boost::corosio::detail {
35
36 /** Base class for IOCP overlapped operations.
37
38 Derives from both OVERLAPPED (for Windows IOCP) and scheduler_op
39 (for queueing). Uses function pointer dispatch inherited from
40 scheduler_op - no virtual functions.
41
42 The OVERLAPPED structure is at the start so we can static_cast
43 between OVERLAPPED* and overlapped_op*.
44 */
45 struct overlapped_op
46 : OVERLAPPED
47 , coro_op
48 {
49 /** Function pointer type for cancellation hook. */
50 using cancel_func_type = void (*)(overlapped_op*) noexcept;
51
52 long ready_ = 0;
53 DWORD dwError = 0;
54 DWORD bytes_transferred = 0;
55 cancel_func_type cancel_func_ = nullptr;
56
57 20519x explicit overlapped_op(func_type func) noexcept : coro_op(func)
58 {
59 20519x reset_overlapped();
60 20519x }
61
62 565172x void reset_overlapped() noexcept
63 {
64 565172x Internal = 0;
65 565172x InternalHigh = 0;
66 565172x Offset = 0;
67 565172x OffsetHigh = 0;
68 565172x hEvent = nullptr;
69 565172x }
70
71 544653x void reset() noexcept
72 {
73 544653x reset_overlapped();
74 544653x ready_ = 0;
75 544653x dwError = 0;
76 544653x bytes_transferred = 0;
77 544653x empty_buffer = false;
78 544653x is_read = false;
79 544653x cancelled.store(false, std::memory_order_relaxed);
80 544653x }
81
82 // coro_op::request_cancel() (set the cancelled flag) is inherited
83 // and used directly by close()/cancel() paths. The stop_token path
84 // additionally drives the kernel via on_cancel() below.
85
86 926x void do_cancel() noexcept
87 {
88
2/2
✓ Branch 2 → 3 taken 924 times.
✓ Branch 2 → 4 taken 2 times.
926x if (cancel_func_)
89 924x cancel_func_(this);
90 926x }
91
92 /** IOCP cancellation hook (stop_token path): set the flag, then issue
93 the registered CancelIoEx / wait-reactor deregister via cancel_func_. */
94 926x void on_cancel() noexcept override
95 {
96 926x request_cancel();
97 926x do_cancel();
98 926x }
99
100 544619x void store_result(DWORD bytes, DWORD err) noexcept
101 {
102 544619x bytes_transferred = bytes;
103 544619x dwError = err;
104 544619x }
105
106 /** Write results to output parameters and resume coroutine. */
107 543101x void invoke_handler()
108 {
109 543101x stop_cb.reset();
110
111 1084194x decode_io_result(
112 ec_out,
113 543101x cancelled.load(std::memory_order_acquire),
114 2008x dwError != 0 ? make_err(dwError) : std::error_code{},
115 543101x is_read, static_cast<std::size_t>(bytes_transferred),
116
2/2
✓ Branch 3 → 4 taken 2008 times.
✓ Branch 3 → 5 taken 541093 times.
543101x empty_buffer);
117
118
2/2
✓ Branch 8 → 9 taken 541705 times.
✓ Branch 8 → 10 taken 1396 times.
543101x if (bytes_out)
119 541705x *bytes_out = static_cast<std::size_t>(bytes_transferred);
120
121 543101x cont_op.cont.h = h;
122
2/2
✓ Branch 10 → 11 taken 543101 times.
✓ Branch 11 → 12 taken 543101 times.
543101x dispatch_coro(ex, cont_op.cont).resume();
123 543101x }
124
125 /** Disarm cancellation and abandon the coroutine handle. */
126 void cleanup_only()
127 {
128 stop_cb.reset();
129 h = {};
130 }
131 };
132
133 /** Cast OVERLAPPED* to overlapped_op*.
134
135 Safe because overlapped_op has OVERLAPPED as first base class.
136 */
137 inline overlapped_op*
138 544623x overlapped_to_op(LPOVERLAPPED ov) noexcept
139 {
140
1/2
✓ Branch 2 → 3 taken 544623 times.
✗ Branch 2 → 4 not taken.
544623x return static_cast<overlapped_op*>(ov);
141 }
142
143 } // namespace boost::corosio::detail
144
145 #endif // BOOST_COROSIO_HAS_IOCP
146
147 #endif // BOOST_COROSIO_NATIVE_DETAIL_IOCP_WIN_OVERLAPPED_OP_HPP
148