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

86.7% Lines (273/315) 97.8% List of functions (44/45) 66.4% Branches (101/152)
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 149x 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 149x 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&) :195 38x 100.0% 100.0% boost::corosio::detail::win_random_access_file_internal::~win_random_access_file_internal() :202 38x 100.0% 100.0% boost::corosio::detail::win_random_access_file_internal::native_handle() const :208 259x 100.0% 100.0% boost::corosio::detail::win_random_access_file_internal::is_open() const :214 1x 100.0% 100.0% boost::corosio::detail::win_random_access_file_internal::cancel() :220 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 :226 0 33.3% 0.0% boost::corosio::detail::win_random_access_file_internal::close_handle() :232 107x 100.0% 100.0% 100.0% boost::corosio::detail::win_random_access_file_internal::size() const :243 2x 60.0% 50.0% 55.6% boost::corosio::detail::win_random_access_file_internal::resize(unsigned long long) :253 2x 55.6% 50.0% 42.9% boost::corosio::detail::win_random_access_file_internal::sync_data() :266 1x 50.0% 20.0% 50.0% boost::corosio::detail::win_random_access_file_internal::sync_all() :277 1x 60.0% 33.3% 57.1% boost::corosio::detail::win_random_access_file_internal::release() :285 1x 100.0% 100.0% boost::corosio::detail::win_random_access_file_internal::assign(unsigned long long) :293 1x 77.8% 33.3% 66.7% 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*) :308 136x 94.4% 73.7% 84.0% 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*) :377 13x 91.4% 68.4% 80.0% boost::corosio::detail::win_random_access_file::win_random_access_file(std::shared_ptr<boost::corosio::detail::win_random_access_file_internal>) :449 38x 100.0% 100.0% boost::corosio::detail::win_random_access_file::close_internal() :456 38x 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*) :466 136x 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*) :479 13x 100.0% 100.0% 80.0% boost::corosio::detail::win_random_access_file::native_handle() const :492 258x 100.0% 100.0% boost::corosio::detail::win_random_access_file::cancel() :498 2x 100.0% 100.0% boost::corosio::detail::win_random_access_file::size() const :504 2x 100.0% 100.0% boost::corosio::detail::win_random_access_file::resize(unsigned long long) :510 2x 100.0% 100.0% boost::corosio::detail::win_random_access_file::sync_data() :516 1x 100.0% 100.0% boost::corosio::detail::win_random_access_file::sync_all() :522 1x 100.0% 100.0% boost::corosio::detail::win_random_access_file::release() :528 1x 100.0% 100.0% boost::corosio::detail::win_random_access_file::assign(unsigned long long) :534 1x 100.0% 100.0% boost::corosio::detail::win_random_access_file::get_internal() const :540 98x 100.0% 100.0% boost::corosio::detail::win_random_access_file_service::win_random_access_file_service(boost::capy::execution_context&) :550 615x 100.0% 83.3% 80.0% boost::corosio::detail::win_random_access_file_service::~win_random_access_file_service() :565 1230x 100.0% 100.0% boost::corosio::detail::win_random_access_file_service::construct() :573 38x 100.0% 100.0% 89.5% boost::corosio::detail::win_random_access_file_service::destroy(boost::corosio::io_object::implementation*) :594 38x 100.0% 50.0% 100.0% boost::corosio::detail::win_random_access_file_service::close(boost::corosio::io_object::handle&) :605 68x 100.0% 100.0% boost::corosio::detail::win_random_access_file_service::shutdown() :612 615x 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) :624 32x 78.4% 83.3% 85.7% boost::corosio::detail::win_random_access_file_service::destroy_impl(boost::corosio::detail::win_random_access_file&) :698 38x 100.0% 50.0% 100.0% boost::corosio::detail::win_random_access_file_service::unregister_impl(boost::corosio::detail::win_random_access_file_internal&) :708 38x 100.0% 100.0% boost::corosio::detail::win_random_access_file_service::on_pending(boost::corosio::detail::overlapped_op*) :722 147x 100.0% 100.0% boost::corosio::detail::win_random_access_file_service::on_completion(boost::corosio::detail::overlapped_op*, unsigned long, unsigned long) :728 2x 100.0% 100.0% boost::corosio::detail::win_random_access_file_service::work_started() :735 149x 100.0% 100.0% boost::corosio::detail::win_random_access_file_service::iocp_handle() const :747 1x 100.0% 100.0% boost::corosio::detail::win_random_access_file_service::try_flush_data(void*) :753 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 149x inline raf_concurrent_op::raf_concurrent_op(
121 149x win_random_access_file_internal& f) noexcept
122 : overlapped_op(&do_complete)
123 149x , file_(&f)
124 {
125 149x cancel_func_ = &do_cancel_impl;
126 149x }
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 149x raf_concurrent_op::do_complete(
139 void* owner,
140 scheduler_op* base,
141 std::uint32_t /*bytes*/,
142 std::uint32_t /*error*/)
143 {
144 149x auto* op = static_cast<raf_concurrent_op*>(base);
145
146
1/2
✗ Branch 2 → 3 not taken.
✓ Branch 2 → 12 taken 149 times.
149x 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 149x op->stop_cb.reset();
162
163
1/2
✓ Branch 13 → 14 taken 149 times.
✗ Branch 13 → 28 not taken.
149x if (op->ec_out)
164 {
165
2/2
✓ Branch 15 → 16 taken 1 time.
✓ Branch 15 → 18 taken 148 times.
149x if (op->cancelled.load(std::memory_order_acquire))
166 1x *op->ec_out = capy::error::canceled;
167
2/2
✓ Branch 18 → 19 taken 2 times.
✓ Branch 18 → 20 taken 146 times.
148x else if (op->dwError != 0)
168 2x *op->ec_out = make_err(op->dwError);
169
5/6
✓ Branch 20 → 21 taken 133 times.
✓ Branch 20 → 25 taken 13 times.
✓ Branch 21 → 22 taken 1 time.
✓ Branch 21 → 25 taken 132 times.
✗ Branch 22 → 23 not taken.
✓ Branch 22 → 25 taken 1 time.
146x else if (op->is_read && op->bytes_transferred == 0 && !op->empty_buffer)
170 *op->ec_out = capy::error::eof;
171 else
172 146x *op->ec_out = {};
173 }
174
175
1/2
✓ Branch 28 → 29 taken 149 times.
✗ Branch 28 → 30 not taken.
149x if (op->bytes_out)
176 149x *op->bytes_out = static_cast<std::size_t>(op->bytes_transferred);
177
178 {
179 149x std::lock_guard<win_mutex> lock(op->file_->ops_mutex_);
180 149x op->file_->outstanding_ops_.remove(op);
181 149x }
182
183 149x op->file_ref.reset();
184
185 149x auto coro = op->h;
186
1/2
✓ Branch 34 → 35 taken 149 times.
✗ Branch 34 → 36 not taken.
149x delete op;
187
1/1
✓ Branch 36 → 37 taken 149 times.
149x coro.resume();
188 }
189
190 // ---------------------------------------------------------------------------
191 // win_random_access_file_internal
192 // ---------------------------------------------------------------------------
193
194 inline
195 38x win_random_access_file_internal::win_random_access_file_internal(
196 38x win_random_access_file_service& svc) noexcept
197 38x : svc_(svc)
198 {
199 38x }
200
201 inline
202 38x win_random_access_file_internal::~win_random_access_file_internal()
203 {
204 38x svc_.unregister_impl(*this);
205 38x }
206
207 inline HANDLE
208 259x win_random_access_file_internal::native_handle() const noexcept
209 {
210 259x return handle_;
211 }
212
213 inline bool
214 1x win_random_access_file_internal::is_open() const noexcept
215 {
216 1x return handle_ != INVALID_HANDLE_VALUE;
217 }
218
219 inline void
220 2x win_random_access_file_internal::cancel() noexcept
221 {
222
1/2
✓ Branch 2 → 3 taken 2 times.
✗ Branch 2 → 4 not taken.
2x if (handle_ != INVALID_HANDLE_VALUE)
223 2x ::CancelIoEx(handle_, nullptr);
224
225 2x std::lock_guard<win_mutex> lock(ops_mutex_);
226 2x outstanding_ops_.for_each([](raf_concurrent_op* op) {
227 op->request_cancel();
228 });
229 2x }
230
231 inline void
232 107x win_random_access_file_internal::close_handle() noexcept
233 {
234
2/2
✓ Branch 2 → 3 taken 30 times.
✓ Branch 2 → 6 taken 77 times.
107x if (handle_ != INVALID_HANDLE_VALUE)
235 {
236 30x ::CancelIoEx(handle_, nullptr);
237 30x ::CloseHandle(handle_);
238 30x handle_ = INVALID_HANDLE_VALUE;
239 }
240 107x }
241
242 inline std::uint64_t
243 2x win_random_access_file_internal::size() const
244 {
245 LARGE_INTEGER li;
246
2/3
✓ Branch 2 → 3 taken 2 times.
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 7 taken 2 times.
2x if (!::GetFileSizeEx(handle_, &li))
247 throw_system_error(
248 make_err(::GetLastError()), "random_access_file::size");
249 2x return static_cast<std::uint64_t>(li.QuadPart);
250 }
251
252 inline void
253 2x win_random_access_file_internal::resize(std::uint64_t new_size)
254 {
255 LARGE_INTEGER li;
256 2x li.QuadPart = static_cast<LONGLONG>(new_size);
257
2/3
✓ Branch 2 → 3 taken 2 times.
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 7 taken 2 times.
2x if (!::SetFilePointerEx(handle_, li, nullptr, FILE_BEGIN))
258 throw_system_error(
259 make_err(::GetLastError()), "random_access_file::resize");
260
2/3
✓ Branch 7 → 8 taken 2 times.
✗ Branch 8 → 9 not taken.
✓ Branch 8 → 12 taken 2 times.
2x if (!::SetEndOfFile(handle_))
261 throw_system_error(
262 make_err(::GetLastError()), "random_access_file::resize");
263 2x }
264
265 inline void
266 1x win_random_access_file_internal::sync_data()
267 {
268 // Attempt data-only flush; fall back to full flush
269
1/2
✓ Branch 3 → 4 taken 1 time.
✗ Branch 3 → 5 not taken.
1x if (svc_.try_flush_data(handle_))
270 1x return;
271 if (!::FlushFileBuffers(handle_))
272 throw_system_error(
273 make_err(::GetLastError()), "random_access_file::sync_data");
274 }
275
276 inline void
277 1x win_random_access_file_internal::sync_all()
278 {
279
1/2
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 7 taken 1 time.
1x if (!::FlushFileBuffers(handle_))
280 throw_system_error(
281 make_err(::GetLastError()), "random_access_file::sync_all");
282 1x }
283
284 inline native_handle_type
285 1x win_random_access_file_internal::release()
286 {
287 1x HANDLE h = handle_;
288 1x handle_ = INVALID_HANDLE_VALUE;
289 1x return reinterpret_cast<native_handle_type>(h);
290 }
291
292 inline void
293 1x win_random_access_file_internal::assign(native_handle_type handle)
294 {
295 1x close_handle();
296 1x HANDLE h = reinterpret_cast<HANDLE>(handle);
297 // Register with IOCP so overlapped I/O works
298
1/2
✗ Branch 5 → 6 not taken.
✓ Branch 5 → 9 taken 1 time.
1x if (!::CreateIoCompletionPort(
299 1x h, static_cast<HANDLE>(svc_.iocp_handle()), key_io, 0))
300 {
301 throw_system_error(
302 make_err(::GetLastError()), "random_access_file::assign");
303 }
304 1x handle_ = h;
305 1x }
306
307 inline std::coroutine_handle<>
308 136x win_random_access_file_internal::read_some_at(
309 std::uint64_t offset,
310 std::coroutine_handle<> h,
311 capy::executor_ref ex,
312 buffer_param param,
313 std::stop_token token,
314 std::error_code* ec,
315 std::size_t* bytes_out)
316 {
317 static constexpr std::size_t max_buffers = 16;
318
319
1/1
✓ Branch 2 → 3 taken 136 times.
136x auto* op = new raf_concurrent_op(*this);
320
1/1
✓ Branch 4 → 5 taken 136 times.
136x op->file_ref = shared_from_this();
321
322 136x op->reset();
323 136x op->is_read = true;
324 136x op->h = h;
325 136x op->ex = ex;
326 136x op->ec_out = ec;
327 136x op->bytes_out = bytes_out;
328 136x op->start(token);
329
330 136x svc_.work_started();
331
332 136x capy::mutable_buffer bufs[max_buffers];
333 136x auto count = param.copy_to(bufs, max_buffers);
334
335
2/2
✓ Branch 11 → 12 taken 1 time.
✓ Branch 11 → 19 taken 135 times.
136x if (count == 0)
336 {
337 1x op->empty_buffer = true;
338 {
339 1x std::lock_guard<win_mutex> lock(ops_mutex_);
340 1x outstanding_ops_.push_back(op);
341 1x }
342 1x svc_.on_completion(op, 0, 0);
343 1x return std::noop_coroutine();
344 }
345
346 135x op->buf = bufs[0].data();
347 135x op->buf_len = static_cast<DWORD>(bufs[0].size());
348
349 // Set caller-provided offset in OVERLAPPED
350 135x op->Offset = static_cast<DWORD>(offset & 0xFFFFFFFF);
351 135x op->OffsetHigh = static_cast<DWORD>(offset >> 32);
352
353 {
354 135x std::lock_guard<win_mutex> lock(ops_mutex_);
355 135x outstanding_ops_.push_back(op);
356 135x }
357
358
2/3
✓ Branch 24 → 25 taken 135 times.
✗ Branch 24 → 26 not taken.
✓ Branch 27 → 28 taken 135 times.
135x BOOL ok = ::ReadFile(handle_, op->buf, op->buf_len, nullptr, op);
359
2/3
✓ Branch 28 → 29 taken 135 times.
✗ Branch 28 → 31 not taken.
✓ Branch 29 → 30 taken 135 times.
135x DWORD err = ok ? 0 : ::GetLastError();
360
361
2/4
✓ Branch 32 → 33 taken 135 times.
✗ Branch 32 → 38 not taken.
✗ Branch 33 → 34 not taken.
✓ Branch 33 → 38 taken 135 times.
135x if (err != 0 && err != ERROR_IO_PENDING)
362 {
363 svc_.on_completion(op, err, 0);
364 return std::noop_coroutine();
365 }
366
367 135x svc_.on_pending(op);
368
369 // Re-check cancellation after I/O is pending
370
2/2
✓ Branch 40 → 41 taken 1 time.
✓ Branch 40 → 45 taken 134 times.
135x if (op->cancelled.load(std::memory_order_acquire))
371
2/3
✓ Branch 41 → 42 taken 1 time.
✗ Branch 41 → 43 not taken.
✓ Branch 44 → 45 taken 1 time.
1x ::CancelIoEx(handle_, op);
372
373 135x return std::noop_coroutine();
374 }
375
376 inline std::coroutine_handle<>
377 13x win_random_access_file_internal::write_some_at(
378 std::uint64_t offset,
379 std::coroutine_handle<> h,
380 capy::executor_ref ex,
381 buffer_param param,
382 std::stop_token token,
383 std::error_code* ec,
384 std::size_t* bytes_out)
385 {
386 static constexpr std::size_t max_buffers = 16;
387
388
1/1
✓ Branch 2 → 3 taken 13 times.
13x auto* op = new raf_concurrent_op(*this);
389
1/1
✓ Branch 4 → 5 taken 13 times.
13x op->file_ref = shared_from_this();
390
391 13x op->reset();
392 13x op->is_read = false;
393 13x op->h = h;
394 13x op->ex = ex;
395 13x op->ec_out = ec;
396 13x op->bytes_out = bytes_out;
397 13x op->start(token);
398
399 13x svc_.work_started();
400
401 13x capy::mutable_buffer bufs[max_buffers];
402 13x auto count = param.copy_to(bufs, max_buffers);
403
404
2/2
✓ Branch 11 → 12 taken 1 time.
✓ Branch 11 → 19 taken 12 times.
13x if (count == 0)
405 {
406 {
407 1x std::lock_guard<win_mutex> lock(ops_mutex_);
408 1x outstanding_ops_.push_back(op);
409 1x }
410 1x svc_.on_completion(op, 0, 0);
411 1x return std::noop_coroutine();
412 }
413
414 12x op->buf = bufs[0].data();
415 12x op->buf_len = static_cast<DWORD>(bufs[0].size());
416
417 // Set caller-provided offset in OVERLAPPED
418 12x op->Offset = static_cast<DWORD>(offset & 0xFFFFFFFF);
419 12x op->OffsetHigh = static_cast<DWORD>(offset >> 32);
420
421 {
422 12x std::lock_guard<win_mutex> lock(ops_mutex_);
423 12x outstanding_ops_.push_back(op);
424 12x }
425
426
2/3
✓ Branch 24 → 25 taken 12 times.
✗ Branch 24 → 26 not taken.
✓ Branch 27 → 28 taken 12 times.
12x BOOL ok = ::WriteFile(handle_, op->buf, op->buf_len, nullptr, op);
427
3/3
✓ Branch 28 → 29 taken 3 times.
✓ Branch 28 → 31 taken 9 times.
✓ Branch 29 → 30 taken 3 times.
12x DWORD err = ok ? 0 : ::GetLastError();
428
429
3/4
✓ Branch 32 → 33 taken 3 times.
✓ Branch 32 → 38 taken 9 times.
✗ Branch 33 → 34 not taken.
✓ Branch 33 → 38 taken 3 times.
12x if (err != 0 && err != ERROR_IO_PENDING)
430 {
431 svc_.on_completion(op, err, 0);
432 return std::noop_coroutine();
433 }
434
435 12x svc_.on_pending(op);
436
437 // Re-check cancellation after I/O is pending
438
1/2
✗ Branch 40 → 41 not taken.
✓ Branch 40 → 45 taken 12 times.
12x if (op->cancelled.load(std::memory_order_acquire))
439 ::CancelIoEx(handle_, op);
440
441 12x return std::noop_coroutine();
442 }
443
444 // ---------------------------------------------------------------------------
445 // win_random_access_file wrapper
446 // ---------------------------------------------------------------------------
447
448 inline
449 38x win_random_access_file::win_random_access_file(
450 38x std::shared_ptr<win_random_access_file_internal> internal) noexcept
451 38x : internal_(std::move(internal))
452 {
453 38x }
454
455 inline void
456 38x win_random_access_file::close_internal() noexcept
457 {
458
1/2
✓ Branch 3 → 4 taken 38 times.
✗ Branch 3 → 7 not taken.
38x if (internal_)
459 {
460 38x internal_->close_handle();
461 38x internal_.reset();
462 }
463 38x }
464
465 inline std::coroutine_handle<>
466 136x win_random_access_file::read_some_at(
467 std::uint64_t offset,
468 std::coroutine_handle<> h,
469 capy::executor_ref d,
470 buffer_param buf,
471 std::stop_token token,
472 std::error_code* ec,
473 std::size_t* bytes)
474 {
475
1/1
✓ Branch 4 → 5 taken 136 times.
136x return internal_->read_some_at(offset, h, d, buf, token, ec, bytes);
476 }
477
478 inline std::coroutine_handle<>
479 13x win_random_access_file::write_some_at(
480 std::uint64_t offset,
481 std::coroutine_handle<> h,
482 capy::executor_ref d,
483 buffer_param buf,
484 std::stop_token token,
485 std::error_code* ec,
486 std::size_t* bytes)
487 {
488
1/1
✓ Branch 4 → 5 taken 13 times.
13x return internal_->write_some_at(offset, h, d, buf, token, ec, bytes);
489 }
490
491 inline native_handle_type
492 258x win_random_access_file::native_handle() const noexcept
493 {
494 258x return reinterpret_cast<native_handle_type>(internal_->native_handle());
495 }
496
497 inline void
498 2x win_random_access_file::cancel() noexcept
499 {
500 2x internal_->cancel();
501 2x }
502
503 inline std::uint64_t
504 2x win_random_access_file::size() const
505 {
506 2x return internal_->size();
507 }
508
509 inline void
510 2x win_random_access_file::resize(std::uint64_t new_size)
511 {
512 2x internal_->resize(new_size);
513 2x }
514
515 inline void
516 1x win_random_access_file::sync_data()
517 {
518 1x internal_->sync_data();
519 1x }
520
521 inline void
522 1x win_random_access_file::sync_all()
523 {
524 1x internal_->sync_all();
525 1x }
526
527 inline native_handle_type
528 1x win_random_access_file::release()
529 {
530 1x return internal_->release();
531 }
532
533 inline void
534 1x win_random_access_file::assign(native_handle_type handle)
535 {
536 1x internal_->assign(handle);
537 1x }
538
539 inline win_random_access_file_internal*
540 98x win_random_access_file::get_internal() const noexcept
541 {
542 98x return internal_.get();
543 }
544
545 // ---------------------------------------------------------------------------
546 // win_random_access_file_service
547 // ---------------------------------------------------------------------------
548
549 inline
550 615x win_random_access_file_service::win_random_access_file_service(
551 615x capy::execution_context& ctx)
552 1230x : sched_(ctx.use_service<win_scheduler>())
553 615x , iocp_(sched_.native_handle())
554
2/2
✓ Branch 3 → 4 taken 615 times.
✓ Branch 4 → 5 taken 615 times.
615x , nt_flush_buffers_file_ex_(nullptr)
555 {
556
3/4
✓ Branch 8 → 9 taken 615 times.
✓ Branch 9 → 10 taken 615 times.
✓ Branch 10 → 11 taken 615 times.
✗ Branch 10 → 12 not taken.
615x if (FARPROC p = ::GetProcAddress(
557 ::GetModuleHandleA("NTDLL"), "NtFlushBuffersFileEx"))
558 {
559 615x nt_flush_buffers_file_ex_ = reinterpret_cast<nt_flush_fn>(
560 reinterpret_cast<void*>(p));
561 }
562 615x }
563
564 inline
565 1230x win_random_access_file_service::~win_random_access_file_service()
566 {
567
1/2
✗ Branch 6 → 3 not taken.
✓ Branch 6 → 7 taken 615 times.
615x for (auto* w = wrapper_list_.pop_front(); w != nullptr;
568 w = wrapper_list_.pop_front())
569 delete w;
570 1230x }
571
572 inline io_object::implementation*
573 38x win_random_access_file_service::construct()
574 {
575 auto internal =
576
1/1
✓ Branch 2 → 3 taken 38 times.
38x std::make_shared<win_random_access_file_internal>(*this);
577
578 {
579 38x std::lock_guard<win_mutex> lock(mutex_);
580 38x file_list_.push_back(internal.get());
581 38x }
582
583
1/1
✓ Branch 7 → 8 taken 38 times.
38x auto* wrapper = new win_random_access_file(std::move(internal));
584
585 {
586 38x std::lock_guard<win_mutex> lock(mutex_);
587 38x wrapper_list_.push_back(wrapper);
588 38x }
589
590 38x return wrapper;
591 38x }
592
593 inline void
594 38x win_random_access_file_service::destroy(io_object::implementation* p)
595 {
596
1/2
✓ Branch 2 → 3 taken 38 times.
✗ Branch 2 → 5 not taken.
38x if (p)
597 {
598 38x auto& wrapper = static_cast<win_random_access_file&>(*p);
599 38x wrapper.close_internal();
600 38x destroy_impl(wrapper);
601 }
602 38x }
603
604 inline void
605 68x win_random_access_file_service::close(io_object::handle& h)
606 {
607 68x auto& wrapper = static_cast<win_random_access_file&>(*h.get());
608 68x wrapper.get_internal()->close_handle();
609 68x }
610
611 inline void
612 615x win_random_access_file_service::shutdown()
613 {
614 615x std::lock_guard<win_mutex> lock(mutex_);
615
616
1/2
✗ Branch 6 → 4 not taken.
✓ Branch 6 → 7 taken 615 times.
615x for (auto* impl = file_list_.pop_front(); impl != nullptr;
617 impl = file_list_.pop_front())
618 {
619 impl->close_handle();
620 }
621 615x }
622
623 inline std::error_code
624 32x win_random_access_file_service::open_file(
625 random_access_file::implementation& impl,
626 std::filesystem::path const& path,
627 file_base::flags mode)
628 {
629 // Build access mask
630 32x DWORD access = 0;
631 32x unsigned a = static_cast<unsigned>(mode) & 3u;
632
2/2
✓ Branch 2 → 3 taken 7 times.
✓ Branch 2 → 4 taken 25 times.
32x if (a == 3)
633 7x access = GENERIC_READ | GENERIC_WRITE;
634
2/2
✓ Branch 4 → 5 taken 5 times.
✓ Branch 4 → 6 taken 20 times.
25x else if (a == 2)
635 5x access = GENERIC_WRITE;
636 else
637 20x access = GENERIC_READ;
638
639 // Build creation disposition
640 32x DWORD disposition = OPEN_EXISTING;
641
6/6
✓ Branch 8 → 9 taken 9 times.
✓ Branch 8 → 12 taken 23 times.
✓ Branch 10 → 11 taken 2 times.
✓ Branch 10 → 12 taken 7 times.
✓ Branch 13 → 14 taken 2 times.
✓ Branch 13 → 15 taken 30 times.
32x if ((mode & file_base::create) && (mode & file_base::exclusive))
642 2x disposition = CREATE_NEW;
643
5/6
✓ Branch 16 → 17 taken 7 times.
✓ Branch 16 → 20 taken 23 times.
✓ Branch 18 → 19 taken 7 times.
✗ Branch 18 → 20 not taken.
✓ Branch 21 → 22 taken 7 times.
✓ Branch 21 → 23 taken 23 times.
30x else if ((mode & file_base::create) && (mode & file_base::truncate))
644 7x disposition = OPEN_ALWAYS;
645
1/2
✗ Branch 24 → 25 not taken.
✓ Branch 24 → 26 taken 23 times.
23x else if (mode & file_base::create)
646 disposition = OPEN_ALWAYS;
647
1/2
✗ Branch 27 → 28 not taken.
✓ Branch 27 → 29 taken 23 times.
23x else if (mode & file_base::truncate)
648 disposition = TRUNCATE_EXISTING;
649
650 // Build flags — FILE_FLAG_OVERLAPPED + FILE_FLAG_RANDOM_ACCESS
651 32x DWORD flags = FILE_ATTRIBUTE_NORMAL
652 | FILE_FLAG_OVERLAPPED
653 | FILE_FLAG_RANDOM_ACCESS;
654
2/2
✓ Branch 30 → 31 taken 1 time.
✓ Branch 30 → 32 taken 31 times.
32x if (mode & file_base::sync_all_on_write)
655 1x flags |= FILE_FLAG_WRITE_THROUGH;
656
657 32x HANDLE h = ::CreateFileW(
658 path.c_str(),
659 access,
660 FILE_SHARE_READ | FILE_SHARE_WRITE,
661 nullptr,
662 disposition,
663 flags,
664 nullptr);
665
666
2/2
✓ Branch 34 → 35 taken 2 times.
✓ Branch 34 → 37 taken 30 times.
32x if (h == INVALID_HANDLE_VALUE)
667 2x return make_err(::GetLastError());
668
669 // Register with IOCP
670
1/2
✗ Branch 38 → 39 not taken.
✓ Branch 38 → 42 taken 30 times.
30x if (!::CreateIoCompletionPort(
671 30x h, static_cast<HANDLE>(iocp_), key_io, 0))
672 {
673 DWORD err = ::GetLastError();
674 ::CloseHandle(h);
675 return make_err(err);
676 }
677
678 // Handle truncation for create|truncate combo
679
2/2
✓ Branch 45 → 46 taken 7 times.
✓ Branch 45 → 48 taken 1 time.
38x if ((mode & file_base::create) && (mode & file_base::truncate)
680
5/6
✓ Branch 43 → 44 taken 8 times.
✓ Branch 43 → 48 taken 22 times.
✓ Branch 46 → 47 taken 7 times.
✗ Branch 46 → 48 not taken.
✓ Branch 49 → 50 taken 7 times.
✓ Branch 49 → 55 taken 23 times.
38x && disposition == OPEN_ALWAYS)
681 {
682
1/2
✗ Branch 51 → 52 not taken.
✓ Branch 51 → 55 taken 7 times.
7x if (!::SetEndOfFile(h))
683 {
684 DWORD err = ::GetLastError();
685 ::CloseHandle(h);
686 return make_err(err);
687 }
688 }
689
690 auto& internal =
691 30x *static_cast<win_random_access_file&>(impl).get_internal();
692 30x internal.handle_ = h;
693
694 30x return {};
695 }
696
697 inline void
698 38x win_random_access_file_service::destroy_impl(win_random_access_file& impl)
699 {
700 {
701 38x std::lock_guard<win_mutex> lock(mutex_);
702 38x wrapper_list_.remove(&impl);
703 38x }
704
1/2
✓ Branch 5 → 6 taken 38 times.
✗ Branch 5 → 7 not taken.
38x delete &impl;
705 38x }
706
707 inline void
708 38x win_random_access_file_service::unregister_impl(
709 win_random_access_file_internal& impl)
710 {
711 38x std::lock_guard<win_mutex> lock(mutex_);
712 38x file_list_.remove(&impl);
713 38x }
714
715 inline void
716 win_random_access_file_service::post(overlapped_op* op)
717 {
718 sched_.post(op);
719 }
720
721 inline void
722 147x win_random_access_file_service::on_pending(overlapped_op* op) noexcept
723 {
724 147x sched_.on_pending(op);
725 147x }
726
727 inline void
728 2x win_random_access_file_service::on_completion(
729 overlapped_op* op, DWORD error, DWORD bytes) noexcept
730 {
731 2x sched_.on_completion(op, error, bytes);
732 2x }
733
734 inline void
735 149x win_random_access_file_service::work_started() noexcept
736 {
737 149x sched_.work_started();
738 149x }
739
740 inline void
741 win_random_access_file_service::work_finished() noexcept
742 {
743 sched_.work_finished();
744 }
745
746 inline void*
747 1x win_random_access_file_service::iocp_handle() const noexcept
748 {
749 1x return iocp_;
750 }
751
752 inline bool
753 1x win_random_access_file_service::try_flush_data(HANDLE h) noexcept
754 {
755
1/2
✓ Branch 2 → 3 taken 1 time.
✗ Branch 2 → 7 not taken.
1x if (nt_flush_buffers_file_ex_)
756 {
757 1x io_status_block status = {};
758 1x if (nt_flush_buffers_file_ex_(
759 h, flush_flags_file_data_sync_only,
760
1/2
✓ Branch 4 → 5 taken 1 time.
✗ Branch 4 → 6 not taken.
1x nullptr, 0, &status) == 0)
761 1x return true;
762 }
763 return false;
764 }
765
766 } // namespace boost::corosio::detail
767
768 #endif // BOOST_COROSIO_HAS_IOCP
769
770 #endif // BOOST_COROSIO_NATIVE_DETAIL_IOCP_WIN_RANDOM_ACCESS_FILE_SERVICE_HPP
771