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

89.7% Lines (286/319) 97.8% List of functions (44/45) 71.1% Branches (106/149)
win_random_access_file_service.hpp
f(x) Functions (45)
Function Calls Lines Branches Blocks
boost::corosio::detail::raf_concurrent_op::raf_concurrent_op(boost::corosio::detail::win_random_access_file_internal&) :120 156x 100.0% 100.0% boost::corosio::detail::raf_concurrent_op::do_cancel_impl(boost::corosio::detail::overlapped_op*) :129 1x 100.0% 50.0% 88.9% boost::corosio::detail::raf_concurrent_op::do_complete(void*, boost::corosio::detail::scheduler_op*, unsigned int, unsigned int) :138 156x 69.0% 66.7% 71.1% boost::corosio::detail::win_random_access_file_internal::win_random_access_file_internal(boost::corosio::detail::win_random_access_file_service&) :198 48x 100.0% 100.0% boost::corosio::detail::win_random_access_file_internal::~win_random_access_file_internal() :205 48x 100.0% 100.0% boost::corosio::detail::win_random_access_file_internal::native_handle() const :211 294x 100.0% 100.0% boost::corosio::detail::win_random_access_file_internal::is_open() const :217 1x 100.0% 100.0% boost::corosio::detail::win_random_access_file_internal::cancel() :223 2x 100.0% 50.0% 100.0% boost::corosio::detail::win_random_access_file_internal::cancel()::{lambda(boost::corosio::detail::raf_concurrent_op*)#1}::operator()(boost::corosio::detail::raf_concurrent_op*) const :229 0 33.3% 0.0% boost::corosio::detail::win_random_access_file_internal::close_handle() :235 137x 100.0% 100.0% 100.0% boost::corosio::detail::win_random_access_file_internal::size() const :246 4x 60.0% 50.0% 55.6% boost::corosio::detail::win_random_access_file_internal::resize(unsigned long long) :256 3x 85.7% 75.0% 81.8% boost::corosio::detail::win_random_access_file_internal::sync_data() :268 1x 50.0% 25.0% 44.4% boost::corosio::detail::win_random_access_file_internal::sync_all() :279 1x 75.0% 50.0% 66.7% boost::corosio::detail::win_random_access_file_internal::release() :287 2x 100.0% 100.0% boost::corosio::detail::win_random_access_file_internal::assign(unsigned long long) :295 3x 87.5% 50.0% 75.0% boost::corosio::detail::win_random_access_file_internal::read_some_at(unsigned long long, std::__n4861::coroutine_handle<void>, boost::capy::executor_ref, boost::corosio::buffer_param, std::stop_token, std::error_code*, unsigned long long*) :310 140x 100.0% 81.0% 93.1% boost::corosio::detail::win_random_access_file_internal::write_some_at(unsigned long long, std::__n4861::coroutine_handle<void>, boost::capy::executor_ref, boost::corosio::buffer_param, std::stop_token, std::error_code*, unsigned long long*) :391 16x 97.6% 76.2% 89.7% boost::corosio::detail::win_random_access_file::win_random_access_file(std::shared_ptr<boost::corosio::detail::win_random_access_file_internal>) :475 48x 100.0% 100.0% boost::corosio::detail::win_random_access_file::close_internal() :482 48x 100.0% 50.0% 100.0% boost::corosio::detail::win_random_access_file::read_some_at(unsigned long long, std::__n4861::coroutine_handle<void>, boost::capy::executor_ref, boost::corosio::buffer_param, std::stop_token, std::error_code*, unsigned long long*) :492 140x 100.0% 100.0% 80.0% boost::corosio::detail::win_random_access_file::write_some_at(unsigned long long, std::__n4861::coroutine_handle<void>, boost::capy::executor_ref, boost::corosio::buffer_param, std::stop_token, std::error_code*, unsigned long long*) :505 16x 100.0% 100.0% 80.0% boost::corosio::detail::win_random_access_file::native_handle() const :518 293x 100.0% 100.0% boost::corosio::detail::win_random_access_file::cancel() :524 2x 100.0% 100.0% boost::corosio::detail::win_random_access_file::size() const :530 4x 100.0% 100.0% boost::corosio::detail::win_random_access_file::resize(unsigned long long) :536 3x 100.0% 100.0% boost::corosio::detail::win_random_access_file::sync_data() :542 1x 100.0% 100.0% boost::corosio::detail::win_random_access_file::sync_all() :548 1x 100.0% 100.0% boost::corosio::detail::win_random_access_file::release() :554 2x 100.0% 100.0% boost::corosio::detail::win_random_access_file::assign(unsigned long long) :560 3x 100.0% 100.0% boost::corosio::detail::win_random_access_file::get_internal() const :566 123x 100.0% 100.0% boost::corosio::detail::win_random_access_file_service::win_random_access_file_service(boost::capy::execution_context&) :576 1121x 100.0% 83.3% 80.0% boost::corosio::detail::win_random_access_file_service::~win_random_access_file_service() :591 2242x 100.0% 100.0% boost::corosio::detail::win_random_access_file_service::construct() :599 48x 100.0% 100.0% 89.5% boost::corosio::detail::win_random_access_file_service::destroy(boost::corosio::io_object::implementation*) :620 48x 100.0% 50.0% 100.0% boost::corosio::detail::win_random_access_file_service::close(boost::corosio::io_object::handle&) :631 86x 100.0% 100.0% boost::corosio::detail::win_random_access_file_service::shutdown() :638 1121x 66.7% 50.0% 71.4% boost::corosio::detail::win_random_access_file_service::open_file(boost::corosio::random_access_file::implementation&, std::filesystem::__cxx11::path const&, boost::corosio::file_base::flags) :650 39x 78.4% 83.3% 85.7% boost::corosio::detail::win_random_access_file_service::destroy_impl(boost::corosio::detail::win_random_access_file&) :724 48x 100.0% 50.0% 100.0% boost::corosio::detail::win_random_access_file_service::unregister_impl(boost::corosio::detail::win_random_access_file_internal&) :734 48x 100.0% 100.0% boost::corosio::detail::win_random_access_file_service::on_pending(boost::corosio::detail::overlapped_op*) :748 149x 100.0% 100.0% boost::corosio::detail::win_random_access_file_service::on_completion(boost::corosio::detail::overlapped_op*, unsigned long, unsigned long) :754 7x 100.0% 100.0% boost::corosio::detail::win_random_access_file_service::work_started() :761 156x 100.0% 100.0% boost::corosio::detail::win_random_access_file_service::iocp_handle() const :773 3x 100.0% 100.0% boost::corosio::detail::win_random_access_file_service::try_flush_data(void*) :779 1x 85.7% 50.0% 71.4%
Line Branch 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_IOCP_WIN_RANDOM_ACCESS_FILE_SERVICE_HPP
11 #define BOOST_COROSIO_NATIVE_DETAIL_IOCP_WIN_RANDOM_ACCESS_FILE_SERVICE_HPP
12
13 #include <boost/corosio/detail/platform.hpp>
14
15 #if BOOST_COROSIO_HAS_IOCP
16
17 #include <boost/corosio/detail/config.hpp>
18 #include <boost/corosio/detail/except.hpp>
19 #include <boost/corosio/detail/random_access_file_service.hpp>
20 #include <boost/capy/ex/execution_context.hpp>
21 #include <boost/corosio/detail/intrusive.hpp>
22 #include <boost/corosio/native/detail/iocp/win_mutex.hpp>
23 #include <boost/corosio/native/detail/iocp/win_random_access_file.hpp>
24 #include <boost/corosio/native/detail/iocp/win_scheduler.hpp>
25 #include <boost/corosio/native/detail/iocp/win_completion_key.hpp>
26 #include <boost/corosio/native/detail/make_err.hpp>
27 #include <boost/corosio/detail/buffer_param.hpp>
28 #include <boost/capy/buffers.hpp>
29 #include <boost/capy/error.hpp>
30
31 #include <filesystem>
32
33 namespace boost::corosio::detail {
34
35 /** Windows IOCP random-access file management service.
36
37 Owns all random-access file implementations and coordinates
38 their lifecycle with the IOCP.
39
40 @par Thread Safety
41 All public member functions are thread-safe.
42 */
43 class BOOST_COROSIO_DECL win_random_access_file_service final
44 : public random_access_file_service
45 {
46 public:
47 using key_type = win_random_access_file_service;
48
49 explicit win_random_access_file_service(capy::execution_context& ctx);
50 ~win_random_access_file_service();
51
52 win_random_access_file_service(
53 win_random_access_file_service const&) = delete;
54 win_random_access_file_service& operator=(
55 win_random_access_file_service const&) = delete;
56
57 io_object::implementation* construct() override;
58 void destroy(io_object::implementation* p) override;
59 void close(io_object::handle& h) override;
60 void shutdown() override;
61
62 std::error_code open_file(
63 random_access_file::implementation& impl,
64 std::filesystem::path const& path,
65 file_base::flags mode) override;
66
67 void destroy_impl(win_random_access_file& impl);
68 void unregister_impl(win_random_access_file_internal& impl);
69
70 void post(overlapped_op* op);
71 void on_pending(overlapped_op* op) noexcept;
72 void on_completion(overlapped_op* op, DWORD error, DWORD bytes) noexcept;
73 void work_started() noexcept;
74 void work_finished() noexcept;
75
76 void* iocp_handle() const noexcept;
77
78 /** Attempt data-only flush via NtFlushBuffersFileEx.
79
80 @return true if data-only flush succeeded, false if
81 caller should fall back to FlushFileBuffers.
82 */
83 bool try_flush_data(HANDLE h) noexcept;
84
85 private:
86 // NtFlushBuffersFileEx support for data-only sync
87 struct io_status_block
88 {
89 union { LONG Status; void* Pointer; };
90 ULONG_PTR Information;
91 };
92
93 enum { flush_flags_file_data_sync_only = 4 };
94
95 using nt_flush_fn = LONG(NTAPI*)(
96 HANDLE, ULONG, void*, ULONG, io_status_block*);
97
98 win_scheduler& sched_;
99 BOOST_COROSIO_MSVC_WARNING_PUSH
100 BOOST_COROSIO_MSVC_WARNING_DISABLE(4251) // detail:: members, dll-interface
101 win_mutex mutex_;
102 intrusive_list<win_random_access_file_internal> file_list_;
103 intrusive_list<win_random_access_file> wrapper_list_;
104 BOOST_COROSIO_MSVC_WARNING_POP
105 void* iocp_;
106 nt_flush_fn nt_flush_buffers_file_ex_;
107 };
108
109 /** Get or create the random-access file service for the given context. */
110 inline win_random_access_file_service&
111 get_random_access_file_service(capy::execution_context& ctx, win_scheduler&)
112 {
113 return ctx.make_service<win_random_access_file_service>();
114 }
115
116 // ---------------------------------------------------------------------------
117 // raf_concurrent_op
118 // ---------------------------------------------------------------------------
119
120 156x inline raf_concurrent_op::raf_concurrent_op(
121 156x win_random_access_file_internal& f) noexcept
122 : overlapped_op(&do_complete)
123 156x , file_(&f)
124 {
125 156x cancel_func_ = &do_cancel_impl;
126 156x }
127
128 inline void
129 1x raf_concurrent_op::do_cancel_impl(overlapped_op* base) noexcept
130 {
131 1x auto* op = static_cast<raf_concurrent_op*>(base);
132 1x op->cancelled.store(true, std::memory_order_release);
133
1/2
✓ Branch 4 → 5 taken 1 time.
✗ Branch 4 → 10 not taken.
1x if (op->file_->is_open())
134
1/2
✓ Branch 5 → 6 taken 1 time.
✗ Branch 5 → 7 not taken.
1x ::CancelIoEx(op->file_->native_handle(), op);
135 1x }
136
137 inline void
138 156x raf_concurrent_op::do_complete(
139 void* owner,
140 scheduler_op* base,
141 std::uint32_t /*bytes*/,
142 std::uint32_t /*error*/)
143 {
144 156x auto* op = static_cast<raf_concurrent_op*>(base);
145
146
1/2
✗ Branch 2 → 3 not taken.
✓ Branch 2 → 12 taken 156 times.
156x if (!owner)
147 {
148 // Shutdown path: clean up without invoking
149 op->stop_cb.reset();
150 op->h = {};
151 {
152 std::lock_guard<win_mutex> lock(op->file_->ops_mutex_);
153 op->file_->outstanding_ops_.remove(op);
154 }
155 op->file_ref.reset();
156 delete op;
157 return;
158 }
159
160 // Normal completion
161 156x op->stop_cb.reset();
162
163
1/2
✓ Branch 13 → 14 taken 156 times.
✗ Branch 13 → 28 not taken.
156x if (op->ec_out)
164 {
165
2/2
✓ Branch 15 → 16 taken 1 time.
✓ Branch 15 → 18 taken 155 times.
156x if (op->cancelled.load(std::memory_order_acquire))
166 1x *op->ec_out = capy::error::canceled;
167
2/2
✓ Branch 18 → 19 taken 7 times.
✓ Branch 18 → 20 taken 148 times.
155x else if (op->dwError != 0)
168 // Through the IOCP normalization point, like every other
169 // overlapped op — plain make_err would leave codes such
170 // as ERROR_INVALID_HANDLE to toolchain-dependent mappings.
171 7x *op->ec_out = iocp_make_err(op->dwError, /*accept_path=*/false);
172
5/6
✓ Branch 20 → 21 taken 134 times.
✓ Branch 20 → 25 taken 14 times.
✓ Branch 21 → 22 taken 1 time.
✓ Branch 21 → 25 taken 133 times.
✗ Branch 22 → 23 not taken.
✓ Branch 22 → 25 taken 1 time.
148x else if (op->is_read && op->bytes_transferred == 0 && !op->empty_buffer)
173 *op->ec_out = capy::error::eof;
174 else
175 148x *op->ec_out = {};
176 }
177
178
1/2
✓ Branch 28 → 29 taken 156 times.
✗ Branch 28 → 30 not taken.
156x if (op->bytes_out)
179 156x *op->bytes_out = static_cast<std::size_t>(op->bytes_transferred);
180
181 {
182 156x std::lock_guard<win_mutex> lock(op->file_->ops_mutex_);
183 156x op->file_->outstanding_ops_.remove(op);
184 156x }
185
186 156x op->file_ref.reset();
187
188 156x auto coro = op->h;
189
1/2
✓ Branch 34 → 35 taken 156 times.
✗ Branch 34 → 36 not taken.
156x delete op;
190
1/1
✓ Branch 36 → 37 taken 156 times.
156x coro.resume();
191 }
192
193 // ---------------------------------------------------------------------------
194 // win_random_access_file_internal
195 // ---------------------------------------------------------------------------
196
197 inline
198 48x win_random_access_file_internal::win_random_access_file_internal(
199 48x win_random_access_file_service& svc) noexcept
200 48x : svc_(svc)
201 {
202 48x }
203
204 inline
205 48x win_random_access_file_internal::~win_random_access_file_internal()
206 {
207 48x svc_.unregister_impl(*this);
208 48x }
209
210 inline HANDLE
211 294x win_random_access_file_internal::native_handle() const noexcept
212 {
213 294x return handle_;
214 }
215
216 inline bool
217 1x win_random_access_file_internal::is_open() const noexcept
218 {
219 1x return handle_ != INVALID_HANDLE_VALUE;
220 }
221
222 inline void
223 2x win_random_access_file_internal::cancel() noexcept
224 {
225
1/2
✓ Branch 2 → 3 taken 2 times.
✗ Branch 2 → 4 not taken.
2x if (handle_ != INVALID_HANDLE_VALUE)
226 2x ::CancelIoEx(handle_, nullptr);
227
228 2x std::lock_guard<win_mutex> lock(ops_mutex_);
229 2x outstanding_ops_.for_each([](raf_concurrent_op* op) {
230 op->request_cancel();
231 });
232 2x }
233
234 inline void
235 137x win_random_access_file_internal::close_handle() noexcept
236 {
237
2/2
✓ Branch 2 → 3 taken 38 times.
✓ Branch 2 → 6 taken 99 times.
137x if (handle_ != INVALID_HANDLE_VALUE)
238 {
239 38x ::CancelIoEx(handle_, nullptr);
240 38x ::CloseHandle(handle_);
241 38x handle_ = INVALID_HANDLE_VALUE;
242 }
243 137x }
244
245 inline std::uint64_t
246 4x win_random_access_file_internal::size() const
247 {
248 LARGE_INTEGER li;
249
2/3
✓ Branch 2 → 3 taken 4 times.
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 7 taken 4 times.
4x if (!::GetFileSizeEx(handle_, &li))
250 throw_system_error(
251 make_err(::GetLastError()), "random_access_file::size");
252 4x return static_cast<std::uint64_t>(li.QuadPart);
253 }
254
255 inline std::error_code
256 3x win_random_access_file_internal::resize(std::uint64_t new_size) noexcept
257 {
258 LARGE_INTEGER li;
259 3x li.QuadPart = static_cast<LONGLONG>(new_size);
260
1/2
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 6 taken 3 times.
3x if (!::SetFilePointerEx(handle_, li, nullptr, FILE_BEGIN))
261 return make_err(::GetLastError());
262
2/2
✓ Branch 7 → 8 taken 1 time.
✓ Branch 7 → 10 taken 2 times.
3x if (!::SetEndOfFile(handle_))
263 1x return make_err(::GetLastError());
264 2x return {};
265 }
266
267 inline std::error_code
268 1x win_random_access_file_internal::sync_data() noexcept
269 {
270 // Attempt data-only flush; fall back to full flush
271
1/2
✓ Branch 3 → 4 taken 1 time.
✗ Branch 3 → 5 not taken.
1x if (svc_.try_flush_data(handle_))
272 1x return {};
273 if (!::FlushFileBuffers(handle_))
274 return make_err(::GetLastError());
275 return {};
276 }
277
278 inline std::error_code
279 1x win_random_access_file_internal::sync_all() noexcept
280 {
281
1/2
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 6 taken 1 time.
1x if (!::FlushFileBuffers(handle_))
282 return make_err(::GetLastError());
283 1x return {};
284 }
285
286 inline native_handle_type
287 2x win_random_access_file_internal::release()
288 {
289 2x HANDLE h = handle_;
290 2x handle_ = INVALID_HANDLE_VALUE;
291 2x return reinterpret_cast<native_handle_type>(h);
292 }
293
294 inline std::error_code
295 3x win_random_access_file_internal::assign(native_handle_type handle) noexcept
296 {
297 3x close_handle();
298 3x HANDLE h = reinterpret_cast<HANDLE>(handle);
299 // Register with IOCP so overlapped I/O works
300
1/2
✗ Branch 5 → 6 not taken.
✓ Branch 5 → 8 taken 3 times.
3x if (!::CreateIoCompletionPort(
301 3x h, static_cast<HANDLE>(svc_.iocp_handle()), key_io, 0))
302 {
303 return make_err(::GetLastError());
304 }
305 3x handle_ = h;
306 3x return {};
307 }
308
309 inline std::coroutine_handle<>
310 140x win_random_access_file_internal::read_some_at(
311 std::uint64_t offset,
312 std::coroutine_handle<> h,
313 capy::executor_ref ex,
314 buffer_param param,
315 std::stop_token token,
316 std::error_code* ec,
317 std::size_t* bytes_out)
318 {
319 static constexpr std::size_t max_buffers = 16;
320
321
1/1
✓ Branch 2 → 3 taken 140 times.
140x auto* op = new raf_concurrent_op(*this);
322
1/1
✓ Branch 4 → 5 taken 140 times.
140x op->file_ref = shared_from_this();
323
324 140x op->reset();
325 140x op->is_read = true;
326 140x op->h = h;
327 140x op->ex = ex;
328 140x op->ec_out = ec;
329 140x op->bytes_out = bytes_out;
330 140x op->start(token);
331
332 140x svc_.work_started();
333
334 // Closed-object contract: complete with bad_file_descriptor
335 // without touching the kernel.
336
2/2
✓ Branch 10 → 11 taken 2 times.
✓ Branch 10 → 18 taken 138 times.
140x if (handle_ == INVALID_HANDLE_VALUE)
337 {
338 {
339 2x std::lock_guard<win_mutex> lock(ops_mutex_);
340 2x outstanding_ops_.push_back(op);
341 2x }
342 2x svc_.on_completion(op, ERROR_INVALID_HANDLE, 0);
343 2x return std::noop_coroutine();
344 }
345
346 138x capy::mutable_buffer bufs[max_buffers];
347 138x auto count = param.copy_to(bufs, max_buffers);
348
349
2/2
✓ Branch 19 → 20 taken 1 time.
✓ Branch 19 → 27 taken 137 times.
138x if (count == 0)
350 {
351 1x op->empty_buffer = true;
352 {
353 1x std::lock_guard<win_mutex> lock(ops_mutex_);
354 1x outstanding_ops_.push_back(op);
355 1x }
356 1x svc_.on_completion(op, 0, 0);
357 1x return std::noop_coroutine();
358 }
359
360 137x op->buf = bufs[0].data();
361 137x op->buf_len = static_cast<DWORD>(bufs[0].size());
362
363 // Set caller-provided offset in OVERLAPPED
364 137x op->Offset = static_cast<DWORD>(offset & 0xFFFFFFFF);
365 137x op->OffsetHigh = static_cast<DWORD>(offset >> 32);
366
367 {
368 137x std::lock_guard<win_mutex> lock(ops_mutex_);
369 137x outstanding_ops_.push_back(op);
370 137x }
371
372
2/3
✓ Branch 32 → 33 taken 137 times.
✗ Branch 32 → 34 not taken.
✓ Branch 35 → 36 taken 137 times.
137x BOOL ok = ::ReadFile(handle_, op->buf, op->buf_len, nullptr, op);
373
2/3
✓ Branch 36 → 37 taken 137 times.
✗ Branch 36 → 39 not taken.
✓ Branch 37 → 38 taken 137 times.
137x DWORD err = ok ? 0 : ::GetLastError();
374
375
3/4
✓ Branch 40 → 41 taken 137 times.
✗ Branch 40 → 46 not taken.
✓ Branch 41 → 42 taken 1 time.
✓ Branch 41 → 46 taken 136 times.
137x if (err != 0 && err != ERROR_IO_PENDING)
376 {
377 1x svc_.on_completion(op, err, 0);
378 1x return std::noop_coroutine();
379 }
380
381 136x svc_.on_pending(op);
382
383 // Re-check cancellation after I/O is pending
384
2/2
✓ Branch 48 → 49 taken 1 time.
✓ Branch 48 → 53 taken 135 times.
136x if (op->cancelled.load(std::memory_order_acquire))
385
2/3
✓ Branch 49 → 50 taken 1 time.
✗ Branch 49 → 51 not taken.
✓ Branch 52 → 53 taken 1 time.
1x ::CancelIoEx(handle_, op);
386
387 136x return std::noop_coroutine();
388 }
389
390 inline std::coroutine_handle<>
391 16x win_random_access_file_internal::write_some_at(
392 std::uint64_t offset,
393 std::coroutine_handle<> h,
394 capy::executor_ref ex,
395 buffer_param param,
396 std::stop_token token,
397 std::error_code* ec,
398 std::size_t* bytes_out)
399 {
400 static constexpr std::size_t max_buffers = 16;
401
402
1/1
✓ Branch 2 → 3 taken 16 times.
16x auto* op = new raf_concurrent_op(*this);
403
1/1
✓ Branch 4 → 5 taken 16 times.
16x op->file_ref = shared_from_this();
404
405 16x op->reset();
406 16x op->is_read = false;
407 16x op->h = h;
408 16x op->ex = ex;
409 16x op->ec_out = ec;
410 16x op->bytes_out = bytes_out;
411 16x op->start(token);
412
413 16x svc_.work_started();
414
415 // Closed-object contract: complete with bad_file_descriptor
416 // without touching the kernel.
417
2/2
✓ Branch 10 → 11 taken 1 time.
✓ Branch 10 → 18 taken 15 times.
16x if (handle_ == INVALID_HANDLE_VALUE)
418 {
419 {
420 1x std::lock_guard<win_mutex> lock(ops_mutex_);
421 1x outstanding_ops_.push_back(op);
422 1x }
423 1x svc_.on_completion(op, ERROR_INVALID_HANDLE, 0);
424 1x return std::noop_coroutine();
425 }
426
427 15x capy::mutable_buffer bufs[max_buffers];
428 15x auto count = param.copy_to(bufs, max_buffers);
429
430
2/2
✓ Branch 19 → 20 taken 1 time.
✓ Branch 19 → 27 taken 14 times.
15x if (count == 0)
431 {
432 {
433 1x std::lock_guard<win_mutex> lock(ops_mutex_);
434 1x outstanding_ops_.push_back(op);
435 1x }
436 1x svc_.on_completion(op, 0, 0);
437 1x return std::noop_coroutine();
438 }
439
440 14x op->buf = bufs[0].data();
441 14x op->buf_len = static_cast<DWORD>(bufs[0].size());
442
443 // Set caller-provided offset in OVERLAPPED
444 14x op->Offset = static_cast<DWORD>(offset & 0xFFFFFFFF);
445 14x op->OffsetHigh = static_cast<DWORD>(offset >> 32);
446
447 {
448 14x std::lock_guard<win_mutex> lock(ops_mutex_);
449 14x outstanding_ops_.push_back(op);
450 14x }
451
452
2/3
✓ Branch 32 → 33 taken 14 times.
✗ Branch 32 → 34 not taken.
✓ Branch 35 → 36 taken 14 times.
14x BOOL ok = ::WriteFile(handle_, op->buf, op->buf_len, nullptr, op);
453
3/3
✓ Branch 36 → 37 taken 4 times.
✓ Branch 36 → 39 taken 10 times.
✓ Branch 37 → 38 taken 4 times.
14x DWORD err = ok ? 0 : ::GetLastError();
454
455
4/4
✓ Branch 40 → 41 taken 4 times.
✓ Branch 40 → 46 taken 10 times.
✓ Branch 41 → 42 taken 1 time.
✓ Branch 41 → 46 taken 3 times.
14x if (err != 0 && err != ERROR_IO_PENDING)
456 {
457 1x svc_.on_completion(op, err, 0);
458 1x return std::noop_coroutine();
459 }
460
461 13x svc_.on_pending(op);
462
463 // Re-check cancellation after I/O is pending
464
1/2
✗ Branch 48 → 49 not taken.
✓ Branch 48 → 53 taken 13 times.
13x if (op->cancelled.load(std::memory_order_acquire))
465 ::CancelIoEx(handle_, op);
466
467 13x return std::noop_coroutine();
468 }
469
470 // ---------------------------------------------------------------------------
471 // win_random_access_file wrapper
472 // ---------------------------------------------------------------------------
473
474 inline
475 48x win_random_access_file::win_random_access_file(
476 48x std::shared_ptr<win_random_access_file_internal> internal) noexcept
477 48x : internal_(std::move(internal))
478 {
479 48x }
480
481 inline void
482 48x win_random_access_file::close_internal() noexcept
483 {
484
1/2
✓ Branch 3 → 4 taken 48 times.
✗ Branch 3 → 7 not taken.
48x if (internal_)
485 {
486 48x internal_->close_handle();
487 48x internal_.reset();
488 }
489 48x }
490
491 inline std::coroutine_handle<>
492 140x win_random_access_file::read_some_at(
493 std::uint64_t offset,
494 std::coroutine_handle<> h,
495 capy::executor_ref d,
496 buffer_param buf,
497 std::stop_token token,
498 std::error_code* ec,
499 std::size_t* bytes)
500 {
501
1/1
✓ Branch 4 → 5 taken 140 times.
140x return internal_->read_some_at(offset, h, d, buf, token, ec, bytes);
502 }
503
504 inline std::coroutine_handle<>
505 16x win_random_access_file::write_some_at(
506 std::uint64_t offset,
507 std::coroutine_handle<> h,
508 capy::executor_ref d,
509 buffer_param buf,
510 std::stop_token token,
511 std::error_code* ec,
512 std::size_t* bytes)
513 {
514
1/1
✓ Branch 4 → 5 taken 16 times.
16x return internal_->write_some_at(offset, h, d, buf, token, ec, bytes);
515 }
516
517 inline native_handle_type
518 293x win_random_access_file::native_handle() const noexcept
519 {
520 293x return reinterpret_cast<native_handle_type>(internal_->native_handle());
521 }
522
523 inline void
524 2x win_random_access_file::cancel() noexcept
525 {
526 2x internal_->cancel();
527 2x }
528
529 inline std::uint64_t
530 4x win_random_access_file::size() const
531 {
532 4x return internal_->size();
533 }
534
535 inline std::error_code
536 3x win_random_access_file::resize(std::uint64_t new_size) noexcept
537 {
538 3x return internal_->resize(new_size);
539 }
540
541 inline std::error_code
542 1x win_random_access_file::sync_data() noexcept
543 {
544 1x return internal_->sync_data();
545 }
546
547 inline std::error_code
548 1x win_random_access_file::sync_all() noexcept
549 {
550 1x return internal_->sync_all();
551 }
552
553 inline native_handle_type
554 2x win_random_access_file::release()
555 {
556 2x return internal_->release();
557 }
558
559 inline std::error_code
560 3x win_random_access_file::assign(native_handle_type handle) noexcept
561 {
562 3x return internal_->assign(handle);
563 }
564
565 inline win_random_access_file_internal*
566 123x win_random_access_file::get_internal() const noexcept
567 {
568 123x return internal_.get();
569 }
570
571 // ---------------------------------------------------------------------------
572 // win_random_access_file_service
573 // ---------------------------------------------------------------------------
574
575 inline
576 1121x win_random_access_file_service::win_random_access_file_service(
577 1121x capy::execution_context& ctx)
578 2242x : sched_(ctx.use_service<win_scheduler>())
579 1121x , iocp_(sched_.native_handle())
580
2/2
✓ Branch 3 → 4 taken 1121 times.
✓ Branch 4 → 5 taken 1121 times.
1121x , nt_flush_buffers_file_ex_(nullptr)
581 {
582
3/4
✓ Branch 8 → 9 taken 1121 times.
✓ Branch 9 → 10 taken 1121 times.
✓ Branch 10 → 11 taken 1121 times.
✗ Branch 10 → 12 not taken.
1121x if (FARPROC p = ::GetProcAddress(
583 ::GetModuleHandleA("NTDLL"), "NtFlushBuffersFileEx"))
584 {
585 1121x nt_flush_buffers_file_ex_ = reinterpret_cast<nt_flush_fn>(
586 reinterpret_cast<void*>(p));
587 }
588 1121x }
589
590 inline
591 2242x win_random_access_file_service::~win_random_access_file_service()
592 {
593
1/2
✗ Branch 6 → 3 not taken.
✓ Branch 6 → 7 taken 1121 times.
1121x for (auto* w = wrapper_list_.pop_front(); w != nullptr;
594 w = wrapper_list_.pop_front())
595 delete w;
596 2242x }
597
598 inline io_object::implementation*
599 48x win_random_access_file_service::construct()
600 {
601 auto internal =
602
1/1
✓ Branch 2 → 3 taken 48 times.
48x std::make_shared<win_random_access_file_internal>(*this);
603
604 {
605 48x std::lock_guard<win_mutex> lock(mutex_);
606 48x file_list_.push_back(internal.get());
607 48x }
608
609
1/1
✓ Branch 7 → 8 taken 48 times.
48x auto* wrapper = new win_random_access_file(std::move(internal));
610
611 {
612 48x std::lock_guard<win_mutex> lock(mutex_);
613 48x wrapper_list_.push_back(wrapper);
614 48x }
615
616 48x return wrapper;
617 48x }
618
619 inline void
620 48x win_random_access_file_service::destroy(io_object::implementation* p)
621 {
622
1/2
✓ Branch 2 → 3 taken 48 times.
✗ Branch 2 → 5 not taken.
48x if (p)
623 {
624 48x auto& wrapper = static_cast<win_random_access_file&>(*p);
625 48x wrapper.close_internal();
626 48x destroy_impl(wrapper);
627 }
628 48x }
629
630 inline void
631 86x win_random_access_file_service::close(io_object::handle& h)
632 {
633 86x auto& wrapper = static_cast<win_random_access_file&>(*h.get());
634 86x wrapper.get_internal()->close_handle();
635 86x }
636
637 inline void
638 1121x win_random_access_file_service::shutdown()
639 {
640 1121x std::lock_guard<win_mutex> lock(mutex_);
641
642
1/2
✗ Branch 6 → 4 not taken.
✓ Branch 6 → 7 taken 1121 times.
1121x for (auto* impl = file_list_.pop_front(); impl != nullptr;
643 impl = file_list_.pop_front())
644 {
645 impl->close_handle();
646 }
647 1121x }
648
649 inline std::error_code
650 39x win_random_access_file_service::open_file(
651 random_access_file::implementation& impl,
652 std::filesystem::path const& path,
653 file_base::flags mode)
654 {
655 // Build access mask
656 39x DWORD access = 0;
657 39x unsigned a = static_cast<unsigned>(mode) & 3u;
658
2/2
✓ Branch 2 → 3 taken 8 times.
✓ Branch 2 → 4 taken 31 times.
39x if (a == 3)
659 8x access = GENERIC_READ | GENERIC_WRITE;
660
2/2
✓ Branch 4 → 5 taken 6 times.
✓ Branch 4 → 6 taken 25 times.
31x else if (a == 2)
661 6x access = GENERIC_WRITE;
662 else
663 25x access = GENERIC_READ;
664
665 // Build creation disposition
666 39x DWORD disposition = OPEN_EXISTING;
667
6/6
✓ Branch 8 → 9 taken 9 times.
✓ Branch 8 → 12 taken 30 times.
✓ Branch 10 → 11 taken 2 times.
✓ Branch 10 → 12 taken 7 times.
✓ Branch 13 → 14 taken 2 times.
✓ Branch 13 → 15 taken 37 times.
39x if ((mode & file_base::create) && (mode & file_base::exclusive))
668 2x disposition = CREATE_NEW;
669
5/6
✓ Branch 16 → 17 taken 7 times.
✓ Branch 16 → 20 taken 30 times.
✓ Branch 18 → 19 taken 7 times.
✗ Branch 18 → 20 not taken.
✓ Branch 21 → 22 taken 7 times.
✓ Branch 21 → 23 taken 30 times.
37x else if ((mode & file_base::create) && (mode & file_base::truncate))
670 7x disposition = OPEN_ALWAYS;
671
1/2
✗ Branch 24 → 25 not taken.
✓ Branch 24 → 26 taken 30 times.
30x else if (mode & file_base::create)
672 disposition = OPEN_ALWAYS;
673
1/2
✗ Branch 27 → 28 not taken.
✓ Branch 27 → 29 taken 30 times.
30x else if (mode & file_base::truncate)
674 disposition = TRUNCATE_EXISTING;
675
676 // Build flags — FILE_FLAG_OVERLAPPED + FILE_FLAG_RANDOM_ACCESS
677 39x DWORD flags = FILE_ATTRIBUTE_NORMAL
678 | FILE_FLAG_OVERLAPPED
679 | FILE_FLAG_RANDOM_ACCESS;
680
2/2
✓ Branch 30 → 31 taken 1 time.
✓ Branch 30 → 32 taken 38 times.
39x if (mode & file_base::sync_all_on_write)
681 1x flags |= FILE_FLAG_WRITE_THROUGH;
682
683 39x HANDLE h = ::CreateFileW(
684 path.c_str(),
685 access,
686 FILE_SHARE_READ | FILE_SHARE_WRITE,
687 nullptr,
688 disposition,
689 flags,
690 nullptr);
691
692
2/2
✓ Branch 34 → 35 taken 2 times.
✓ Branch 34 → 37 taken 37 times.
39x if (h == INVALID_HANDLE_VALUE)
693 2x return make_err(::GetLastError());
694
695 // Register with IOCP
696
1/2
✗ Branch 38 → 39 not taken.
✓ Branch 38 → 42 taken 37 times.
37x if (!::CreateIoCompletionPort(
697 37x h, static_cast<HANDLE>(iocp_), key_io, 0))
698 {
699 DWORD err = ::GetLastError();
700 ::CloseHandle(h);
701 return make_err(err);
702 }
703
704 // Handle truncation for create|truncate combo
705
2/2
✓ Branch 45 → 46 taken 7 times.
✓ Branch 45 → 48 taken 1 time.
45x if ((mode & file_base::create) && (mode & file_base::truncate)
706
5/6
✓ Branch 43 → 44 taken 8 times.
✓ Branch 43 → 48 taken 29 times.
✓ Branch 46 → 47 taken 7 times.
✗ Branch 46 → 48 not taken.
✓ Branch 49 → 50 taken 7 times.
✓ Branch 49 → 55 taken 30 times.
45x && disposition == OPEN_ALWAYS)
707 {
708
1/2
✗ Branch 51 → 52 not taken.
✓ Branch 51 → 55 taken 7 times.
7x if (!::SetEndOfFile(h))
709 {
710 DWORD err = ::GetLastError();
711 ::CloseHandle(h);
712 return make_err(err);
713 }
714 }
715
716 auto& internal =
717 37x *static_cast<win_random_access_file&>(impl).get_internal();
718 37x internal.handle_ = h;
719
720 37x return {};
721 }
722
723 inline void
724 48x win_random_access_file_service::destroy_impl(win_random_access_file& impl)
725 {
726 {
727 48x std::lock_guard<win_mutex> lock(mutex_);
728 48x wrapper_list_.remove(&impl);
729 48x }
730
1/2
✓ Branch 5 → 6 taken 48 times.
✗ Branch 5 → 7 not taken.
48x delete &impl;
731 48x }
732
733 inline void
734 48x win_random_access_file_service::unregister_impl(
735 win_random_access_file_internal& impl)
736 {
737 48x std::lock_guard<win_mutex> lock(mutex_);
738 48x file_list_.remove(&impl);
739 48x }
740
741 inline void
742 win_random_access_file_service::post(overlapped_op* op)
743 {
744 sched_.post(op);
745 }
746
747 inline void
748 149x win_random_access_file_service::on_pending(overlapped_op* op) noexcept
749 {
750 149x sched_.on_pending(op);
751 149x }
752
753 inline void
754 7x win_random_access_file_service::on_completion(
755 overlapped_op* op, DWORD error, DWORD bytes) noexcept
756 {
757 7x sched_.on_completion(op, error, bytes);
758 7x }
759
760 inline void
761 156x win_random_access_file_service::work_started() noexcept
762 {
763 156x sched_.work_started();
764 156x }
765
766 inline void
767 win_random_access_file_service::work_finished() noexcept
768 {
769 sched_.work_finished();
770 }
771
772 inline void*
773 3x win_random_access_file_service::iocp_handle() const noexcept
774 {
775 3x return iocp_;
776 }
777
778 inline bool
779 1x win_random_access_file_service::try_flush_data(HANDLE h) noexcept
780 {
781
1/2
✓ Branch 2 → 3 taken 1 time.
✗ Branch 2 → 7 not taken.
1x if (nt_flush_buffers_file_ex_)
782 {
783 1x io_status_block status = {};
784 1x if (nt_flush_buffers_file_ex_(
785 h, flush_flags_file_data_sync_only,
786
1/2
✓ Branch 4 → 5 taken 1 time.
✗ Branch 4 → 6 not taken.
1x nullptr, 0, &status) == 0)
787 1x return true;
788 }
789 return false;
790 }
791
792 } // namespace boost::corosio::detail
793
794 #endif // BOOST_COROSIO_HAS_IOCP
795
796 #endif // BOOST_COROSIO_NATIVE_DETAIL_IOCP_WIN_RANDOM_ACCESS_FILE_SERVICE_HPP
797