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

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