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)
Functions (9)
Function
Calls
Lines
Branches
Blocks
boost::corosio::detail::overlapped_op::overlapped_op(void (*)(void*, boost::corosio::detail::scheduler_op*, unsigned int, unsigned int))
:57
20855x
100.0%
–
100.0%
boost::corosio::detail::overlapped_op::reset_overlapped()
:62
611892x
100.0%
–
100.0%
boost::corosio::detail::overlapped_op::reset()
:71
591037x
100.0%
–
100.0%
boost::corosio::detail::overlapped_op::do_cancel()
:86
942x
100.0%
100.0%
100.0%
boost::corosio::detail::overlapped_op::on_cancel()
:94
942x
100.0%
–
100.0%
boost::corosio::detail::overlapped_op::store_result(unsigned long, unsigned long)
:100
591003x
100.0%
–
100.0%
boost::corosio::detail::overlapped_op::invoke_handler()
:107
589461x
100.0%
100.0%
100.0%
boost::corosio::detail::overlapped_op::cleanup_only()
:126
0
0.0%
–
0.0%
boost::corosio::detail::overlapped_to_op(_OVERLAPPED*)
:138
591007x
100.0%
50.0%
80.0%
| 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 | 20855x | explicit overlapped_op(func_type func) noexcept : coro_op(func) | ||
| 58 | { | |||
| 59 | 20855x | reset_overlapped(); | ||
| 60 | 20855x | } | ||
| 61 | ||||
| 62 | 611892x | void reset_overlapped() noexcept | ||
| 63 | { | |||
| 64 | 611892x | Internal = 0; | ||
| 65 | 611892x | InternalHigh = 0; | ||
| 66 | 611892x | Offset = 0; | ||
| 67 | 611892x | OffsetHigh = 0; | ||
| 68 | 611892x | hEvent = nullptr; | ||
| 69 | 611892x | } | ||
| 70 | ||||
| 71 | 591037x | void reset() noexcept | ||
| 72 | { | |||
| 73 | 591037x | reset_overlapped(); | ||
| 74 | 591037x | ready_ = 0; | ||
| 75 | 591037x | dwError = 0; | ||
| 76 | 591037x | bytes_transferred = 0; | ||
| 77 | 591037x | empty_buffer = false; | ||
| 78 | 591037x | is_read = false; | ||
| 79 | 591037x | cancelled.store(false, std::memory_order_relaxed); | ||
| 80 | 591037x | } | ||
| 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 | 942x | void do_cancel() noexcept | ||
| 87 | { | |||
| 88 |
2/2✓ Branch 2 → 3 taken 940 times.
✓ Branch 2 → 4 taken 2 times.
|
942x | if (cancel_func_) | |
| 89 | 940x | cancel_func_(this); | ||
| 90 | 942x | } | ||
| 91 | ||||
| 92 | /** IOCP cancellation hook (stop_token path): set the flag, then issue | |||
| 93 | the registered CancelIoEx / wait-reactor deregister via cancel_func_. */ | |||
| 94 | 942x | void on_cancel() noexcept override | ||
| 95 | { | |||
| 96 | 942x | request_cancel(); | ||
| 97 | 942x | do_cancel(); | ||
| 98 | 942x | } | ||
| 99 | ||||
| 100 | 591003x | void store_result(DWORD bytes, DWORD err) noexcept | ||
| 101 | { | |||
| 102 | 591003x | bytes_transferred = bytes; | ||
| 103 | 591003x | dwError = err; | ||
| 104 | 591003x | } | ||
| 105 | ||||
| 106 | /** Write results to output parameters and resume coroutine. */ | |||
| 107 | 589461x | void invoke_handler() | ||
| 108 | { | |||
| 109 | 589461x | stop_cb.reset(); | ||
| 110 | ||||
| 111 | 1176881x | decode_io_result( | ||
| 112 | ec_out, | |||
| 113 | 589461x | cancelled.load(std::memory_order_acquire), | ||
| 114 | 2041x | dwError != 0 ? make_err(dwError) : std::error_code{}, | ||
| 115 | 589461x | is_read, static_cast<std::size_t>(bytes_transferred), | ||
| 116 |
2/2✓ Branch 3 → 4 taken 2041 times.
✓ Branch 3 → 5 taken 587420 times.
|
589461x | empty_buffer); | |
| 117 | ||||
| 118 |
2/2✓ Branch 8 → 9 taken 588041 times.
✓ Branch 8 → 10 taken 1420 times.
|
589461x | if (bytes_out) | |
| 119 | 588041x | *bytes_out = static_cast<std::size_t>(bytes_transferred); | ||
| 120 | ||||
| 121 | 589461x | cont_op.cont.h = h; | ||
| 122 |
2/2✓ Branch 10 → 11 taken 589461 times.
✓ Branch 11 → 12 taken 589461 times.
|
589461x | dispatch_coro(ex, cont_op.cont).resume(); | |
| 123 | 589461x | } | ||
| 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 | 591007x | overlapped_to_op(LPOVERLAPPED ov) noexcept | ||
| 139 | { | |||
| 140 |
1/2✓ Branch 2 → 3 taken 591007 times.
✗ Branch 2 → 4 not taken.
|
591007x | 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 |