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&) :117 149x 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 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&) :192 38x 100.0% 100.0% boost::corosio::detail::win_random_access_file_internal::~win_random_access_file_internal() :199 38x 100.0% 100.0% boost::corosio::detail::win_random_access_file_internal::native_handle() const :205 259x 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 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 :223 0 33.3% 0.0% boost::corosio::detail::win_random_access_file_internal::close_handle() :229 107x 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 136x 94.4% 73.7% 84.6% 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 13x 91.4% 68.4% 80.8% boost::corosio::detail::win_random_access_file::win_random_access_file(std::shared_ptr<boost::corosio::detail::win_random_access_file_internal>) :446 38x 100.0% 100.0% boost::corosio::detail::win_random_access_file::close_internal() :453 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*) :463 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*) :476 13x 100.0% 100.0% 80.0% boost::corosio::detail::win_random_access_file::native_handle() const :489 258x 100.0% 100.0% boost::corosio::detail::win_random_access_file::cancel() :495 2x 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 98x 100.0% 100.0% boost::corosio::detail::win_random_access_file_service::win_random_access_file_service(boost::capy::execution_context&) :547 567x 100.0% 83.3% 80.0% boost::corosio::detail::win_random_access_file_service::~win_random_access_file_service() :562 1134x 100.0% 100.0% boost::corosio::detail::win_random_access_file_service::construct() :570 38x 100.0% 100.0% 89.5% boost::corosio::detail::win_random_access_file_service::destroy(boost::corosio::io_object::implementation*) :591 38x 100.0% 50.0% 100.0% boost::corosio::detail::win_random_access_file_service::close(boost::corosio::io_object::handle&) :602 68x 100.0% 100.0% boost::corosio::detail::win_random_access_file_service::shutdown() :609 567x 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 32x 78.4% 83.3% 85.7% boost::corosio::detail::win_random_access_file_service::destroy_impl(boost::corosio::detail::win_random_access_file&) :695 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&) :705 38x 100.0% 100.0% boost::corosio::detail::win_random_access_file_service::on_pending(boost::corosio::detail::overlapped_op*) :719 147x 100.0% 100.0% boost::corosio::detail::win_random_access_file_service::on_completion(boost::corosio::detail::overlapped_op*, unsigned long, unsigned long) :725 2x 100.0% 100.0% boost::corosio::detail::win_random_access_file_service::work_started() :732 149x 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 149x inline raf_concurrent_op::raf_concurrent_op(
118 149x win_random_access_file_internal& f) noexcept
119 : overlapped_op(&do_complete)
120 149x , file_(&f)
121 {
122 149x cancel_func_ = &do_cancel_impl;
123 149x }
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 149x raf_concurrent_op::do_complete(
136 void* owner,
137 scheduler_op* base,
138 std::uint32_t /*bytes*/,
139 std::uint32_t /*error*/)
140 {
141 149x auto* op = static_cast<raf_concurrent_op*>(base);
142
143
1/2
✗ Branch 2 → 3 not taken.
✓ Branch 2 → 12 taken 149 times.
149x 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 149x op->stop_cb.reset();
159
160
1/2
✓ Branch 13 → 14 taken 149 times.
✗ Branch 13 → 28 not taken.
149x if (op->ec_out)
161 {
162
2/2
✓ Branch 15 → 16 taken 1 time.
✓ Branch 15 → 18 taken 148 times.
149x if (op->cancelled.load(std::memory_order_acquire))
163 1x *op->ec_out = capy::error::canceled;
164
2/2
✓ Branch 18 → 19 taken 2 times.
✓ Branch 18 → 20 taken 146 times.
148x else if (op->dwError != 0)
165 2x *op->ec_out = make_err(op->dwError);
166
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)
167 *op->ec_out = capy::error::eof;
168 else
169 146x *op->ec_out = {};
170 }
171
172
1/2
✓ Branch 28 → 29 taken 149 times.
✗ Branch 28 → 30 not taken.
149x if (op->bytes_out)
173 149x *op->bytes_out = static_cast<std::size_t>(op->bytes_transferred);
174
175 {
176 149x std::lock_guard<win_mutex> lock(op->file_->ops_mutex_);
177 149x op->file_->outstanding_ops_.remove(op);
178 149x }
179
180 149x op->file_ref.reset();
181
182 149x auto coro = op->h;
183
1/2
✓ Branch 34 → 35 taken 149 times.
✗ Branch 34 → 36 not taken.
149x delete op;
184
1/1
✓ Branch 36 → 37 taken 149 times.
149x coro.resume();
185 }
186
187 // ---------------------------------------------------------------------------
188 // win_random_access_file_internal
189 // ---------------------------------------------------------------------------
190
191 inline
192 38x win_random_access_file_internal::win_random_access_file_internal(
193 38x win_random_access_file_service& svc) noexcept
194 38x : svc_(svc)
195 {
196 38x }
197
198 inline
199 38x win_random_access_file_internal::~win_random_access_file_internal()
200 {
201 38x svc_.unregister_impl(*this);
202 38x }
203
204 inline HANDLE
205 259x win_random_access_file_internal::native_handle() const noexcept
206 {
207 259x 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 2x win_random_access_file_internal::cancel() noexcept
218 {
219
1/2
✓ Branch 2 → 3 taken 2 times.
✗ Branch 2 → 4 not taken.
2x if (handle_ != INVALID_HANDLE_VALUE)
220 2x ::CancelIoEx(handle_, nullptr);
221
222 2x std::lock_guard<win_mutex> lock(ops_mutex_);
223 2x outstanding_ops_.for_each([](raf_concurrent_op* op) {
224 op->request_cancel();
225 });
226 2x }
227
228 inline void
229 107x win_random_access_file_internal::close_handle() noexcept
230 {
231
2/2
✓ Branch 2 → 3 taken 30 times.
✓ Branch 2 → 6 taken 77 times.
107x if (handle_ != INVALID_HANDLE_VALUE)
232 {
233 30x ::CancelIoEx(handle_, nullptr);
234 30x ::CloseHandle(handle_);
235 30x handle_ = INVALID_HANDLE_VALUE;
236 }
237 107x }
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 136x 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 136 times.
136x auto* op = new raf_concurrent_op(*this);
317
1/1
✓ Branch 4 → 5 taken 136 times.
136x op->file_ref = shared_from_this();
318
319 136x op->reset();
320 136x op->is_read_ = true;
321 136x op->h = h;
322 136x op->ex = ex;
323 136x op->ec_out = ec;
324 136x op->bytes_out = bytes_out;
325 136x op->start(token);
326
327 136x svc_.work_started();
328
329 136x capy::mutable_buffer bufs[max_buffers];
330 136x auto count = param.copy_to(bufs, max_buffers);
331
332
2/2
✓ Branch 13 → 14 taken 1 time.
✓ Branch 13 → 21 taken 135 times.
136x if (count == 0)
333 {
334 1x op->empty_buffer = true;
335 {
336 1x std::lock_guard<win_mutex> lock(ops_mutex_);
337 1x outstanding_ops_.push_back(op);
338 1x }
339 1x svc_.on_completion(op, 0, 0);
340 1x return std::noop_coroutine();
341 }
342
343 135x op->buf = bufs[0].data();
344 135x op->buf_len = static_cast<DWORD>(bufs[0].size());
345
346 // Set caller-provided offset in OVERLAPPED
347 135x op->Offset = static_cast<DWORD>(offset & 0xFFFFFFFF);
348 135x op->OffsetHigh = static_cast<DWORD>(offset >> 32);
349
350 {
351 135x std::lock_guard<win_mutex> lock(ops_mutex_);
352 135x outstanding_ops_.push_back(op);
353 135x }
354
355
2/3
✓ Branch 26 → 27 taken 135 times.
✗ Branch 26 → 28 not taken.
✓ Branch 29 → 30 taken 135 times.
135x BOOL ok = ::ReadFile(handle_, op->buf, op->buf_len, nullptr, op);
356
2/3
✓ Branch 30 → 31 taken 135 times.
✗ Branch 30 → 33 not taken.
✓ Branch 31 → 32 taken 135 times.
135x DWORD err = ok ? 0 : ::GetLastError();
357
358
2/4
✓ Branch 34 → 35 taken 135 times.
✗ Branch 34 → 40 not taken.
✗ Branch 35 → 36 not taken.
✓ Branch 35 → 40 taken 135 times.
135x if (err != 0 && err != ERROR_IO_PENDING)
359 {
360 svc_.on_completion(op, err, 0);
361 return std::noop_coroutine();
362 }
363
364 135x 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 134 times.
135x 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 135x return std::noop_coroutine();
371 }
372
373 inline std::coroutine_handle<>
374 13x 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 13 times.
13x auto* op = new raf_concurrent_op(*this);
386
1/1
✓ Branch 4 → 5 taken 13 times.
13x op->file_ref = shared_from_this();
387
388 13x op->reset();
389 13x op->is_read_ = false;
390 13x op->h = h;
391 13x op->ex = ex;
392 13x op->ec_out = ec;
393 13x op->bytes_out = bytes_out;
394 13x op->start(token);
395
396 13x svc_.work_started();
397
398 13x capy::mutable_buffer bufs[max_buffers];
399 13x auto count = param.copy_to(bufs, max_buffers);
400
401
2/2
✓ Branch 13 → 14 taken 1 time.
✓ Branch 13 → 21 taken 12 times.
13x if (count == 0)
402 {
403 {
404 1x std::lock_guard<win_mutex> lock(ops_mutex_);
405 1x outstanding_ops_.push_back(op);
406 1x }
407 1x svc_.on_completion(op, 0, 0);
408 1x return std::noop_coroutine();
409 }
410
411 12x op->buf = bufs[0].data();
412 12x op->buf_len = static_cast<DWORD>(bufs[0].size());
413
414 // Set caller-provided offset in OVERLAPPED
415 12x op->Offset = static_cast<DWORD>(offset & 0xFFFFFFFF);
416 12x op->OffsetHigh = static_cast<DWORD>(offset >> 32);
417
418 {
419 12x std::lock_guard<win_mutex> lock(ops_mutex_);
420 12x outstanding_ops_.push_back(op);
421 12x }
422
423
2/3
✓ Branch 26 → 27 taken 12 times.
✗ Branch 26 → 28 not taken.
✓ Branch 29 → 30 taken 12 times.
12x BOOL ok = ::WriteFile(handle_, op->buf, op->buf_len, nullptr, op);
424
3/3
✓ Branch 30 → 31 taken 3 times.
✓ Branch 30 → 33 taken 9 times.
✓ Branch 31 → 32 taken 3 times.
12x DWORD err = ok ? 0 : ::GetLastError();
425
426
3/4
✓ Branch 34 → 35 taken 3 times.
✓ Branch 34 → 40 taken 9 times.
✗ Branch 35 → 36 not taken.
✓ Branch 35 → 40 taken 3 times.
12x if (err != 0 && err != ERROR_IO_PENDING)
427 {
428 svc_.on_completion(op, err, 0);
429 return std::noop_coroutine();
430 }
431
432 12x 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 12 times.
12x if (op->cancelled.load(std::memory_order_acquire))
436 ::CancelIoEx(handle_, op);
437
438 12x return std::noop_coroutine();
439 }
440
441 // ---------------------------------------------------------------------------
442 // win_random_access_file wrapper
443 // ---------------------------------------------------------------------------
444
445 inline
446 38x win_random_access_file::win_random_access_file(
447 38x std::shared_ptr<win_random_access_file_internal> internal) noexcept
448 38x : internal_(std::move(internal))
449 {
450 38x }
451
452 inline void
453 38x win_random_access_file::close_internal() noexcept
454 {
455
1/2
✓ Branch 3 → 4 taken 38 times.
✗ Branch 3 → 7 not taken.
38x if (internal_)
456 {
457 38x internal_->close_handle();
458 38x internal_.reset();
459 }
460 38x }
461
462 inline std::coroutine_handle<>
463 136x 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 136 times.
136x return internal_->read_some_at(offset, h, d, buf, token, ec, bytes);
473 }
474
475 inline std::coroutine_handle<>
476 13x 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 13 times.
13x return internal_->write_some_at(offset, h, d, buf, token, ec, bytes);
486 }
487
488 inline native_handle_type
489 258x win_random_access_file::native_handle() const noexcept
490 {
491 258x return reinterpret_cast<native_handle_type>(internal_->native_handle());
492 }
493
494 inline void
495 2x win_random_access_file::cancel() noexcept
496 {
497 2x internal_->cancel();
498 2x }
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 98x win_random_access_file::get_internal() const noexcept
538 {
539 98x return internal_.get();
540 }
541
542 // ---------------------------------------------------------------------------
543 // win_random_access_file_service
544 // ---------------------------------------------------------------------------
545
546 inline
547 567x win_random_access_file_service::win_random_access_file_service(
548 567x capy::execution_context& ctx)
549 1134x : sched_(ctx.use_service<win_scheduler>())
550 567x , iocp_(sched_.native_handle())
551
2/2
✓ Branch 3 → 4 taken 567 times.
✓ Branch 4 → 5 taken 567 times.
567x , nt_flush_buffers_file_ex_(nullptr)
552 {
553
3/4
✓ Branch 8 → 9 taken 567 times.
✓ Branch 9 → 10 taken 567 times.
✓ Branch 10 → 11 taken 567 times.
✗ Branch 10 → 12 not taken.
567x if (FARPROC p = ::GetProcAddress(
554 ::GetModuleHandleA("NTDLL"), "NtFlushBuffersFileEx"))
555 {
556 567x nt_flush_buffers_file_ex_ = reinterpret_cast<nt_flush_fn>(
557 reinterpret_cast<void*>(p));
558 }
559 567x }
560
561 inline
562 1134x win_random_access_file_service::~win_random_access_file_service()
563 {
564
1/2
✗ Branch 6 → 3 not taken.
✓ Branch 6 → 7 taken 567 times.
567x for (auto* w = wrapper_list_.pop_front(); w != nullptr;
565 w = wrapper_list_.pop_front())
566 delete w;
567 1134x }
568
569 inline io_object::implementation*
570 38x win_random_access_file_service::construct()
571 {
572 auto internal =
573
1/1
✓ Branch 2 → 3 taken 38 times.
38x std::make_shared<win_random_access_file_internal>(*this);
574
575 {
576 38x std::lock_guard<win_mutex> lock(mutex_);
577 38x file_list_.push_back(internal.get());
578 38x }
579
580
1/1
✓ Branch 7 → 8 taken 38 times.
38x auto* wrapper = new win_random_access_file(std::move(internal));
581
582 {
583 38x std::lock_guard<win_mutex> lock(mutex_);
584 38x wrapper_list_.push_back(wrapper);
585 38x }
586
587 38x return wrapper;
588 38x }
589
590 inline void
591 38x win_random_access_file_service::destroy(io_object::implementation* p)
592 {
593
1/2
✓ Branch 2 → 3 taken 38 times.
✗ Branch 2 → 5 not taken.
38x if (p)
594 {
595 38x auto& wrapper = static_cast<win_random_access_file&>(*p);
596 38x wrapper.close_internal();
597 38x destroy_impl(wrapper);
598 }
599 38x }
600
601 inline void
602 68x win_random_access_file_service::close(io_object::handle& h)
603 {
604 68x auto& wrapper = static_cast<win_random_access_file&>(*h.get());
605 68x wrapper.get_internal()->close_handle();
606 68x }
607
608 inline void
609 567x win_random_access_file_service::shutdown()
610 {
611 567x std::lock_guard<win_mutex> lock(mutex_);
612
613
1/2
✗ Branch 6 → 4 not taken.
✓ Branch 6 → 7 taken 567 times.
567x for (auto* impl = file_list_.pop_front(); impl != nullptr;
614 impl = file_list_.pop_front())
615 {
616 impl->close_handle();
617 }
618 567x }
619
620 inline std::error_code
621 32x 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 32x DWORD access = 0;
628 32x unsigned a = static_cast<unsigned>(mode) & 3u;
629
2/2
✓ Branch 2 → 3 taken 7 times.
✓ Branch 2 → 4 taken 25 times.
32x if (a == 3)
630 7x access = GENERIC_READ | GENERIC_WRITE;
631
2/2
✓ Branch 4 → 5 taken 5 times.
✓ Branch 4 → 6 taken 20 times.
25x else if (a == 2)
632 5x access = GENERIC_WRITE;
633 else
634 20x access = GENERIC_READ;
635
636 // Build creation disposition
637 32x DWORD disposition = OPEN_EXISTING;
638
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))
639 2x disposition = CREATE_NEW;
640
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))
641 7x disposition = OPEN_ALWAYS;
642
1/2
✗ Branch 24 → 25 not taken.
✓ Branch 24 → 26 taken 23 times.
23x else if (mode & file_base::create)
643 disposition = OPEN_ALWAYS;
644
1/2
✗ Branch 27 → 28 not taken.
✓ Branch 27 → 29 taken 23 times.
23x else if (mode & file_base::truncate)
645 disposition = TRUNCATE_EXISTING;
646
647 // Build flags — FILE_FLAG_OVERLAPPED + FILE_FLAG_RANDOM_ACCESS
648 32x DWORD flags = FILE_ATTRIBUTE_NORMAL
649 | FILE_FLAG_OVERLAPPED
650 | FILE_FLAG_RANDOM_ACCESS;
651
2/2
✓ Branch 30 → 31 taken 1 time.
✓ Branch 30 → 32 taken 31 times.
32x if (mode & file_base::sync_all_on_write)
652 1x flags |= FILE_FLAG_WRITE_THROUGH;
653
654 32x 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 2 times.
✓ Branch 34 → 37 taken 30 times.
32x if (h == INVALID_HANDLE_VALUE)
664 2x return make_err(::GetLastError());
665
666 // Register with IOCP
667
1/2
✗ Branch 38 → 39 not taken.
✓ Branch 38 → 42 taken 30 times.
30x if (!::CreateIoCompletionPort(
668 30x 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
2/2
✓ Branch 45 → 46 taken 7 times.
✓ Branch 45 → 48 taken 1 time.
38x if ((mode & file_base::create) && (mode & file_base::truncate)
677
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)
678 {
679
1/2
✗ Branch 51 → 52 not taken.
✓ Branch 51 → 55 taken 7 times.
7x if (!::SetEndOfFile(h))
680 {
681 DWORD err = ::GetLastError();
682 ::CloseHandle(h);
683 return make_err(err);
684 }
685 }
686
687 auto& internal =
688 30x *static_cast<win_random_access_file&>(impl).get_internal();
689 30x internal.handle_ = h;
690
691 30x return {};
692 }
693
694 inline void
695 38x win_random_access_file_service::destroy_impl(win_random_access_file& impl)
696 {
697 {
698 38x std::lock_guard<win_mutex> lock(mutex_);
699 38x wrapper_list_.remove(&impl);
700 38x }
701
1/2
✓ Branch 5 → 6 taken 38 times.
✗ Branch 5 → 7 not taken.
38x delete &impl;
702 38x }
703
704 inline void
705 38x win_random_access_file_service::unregister_impl(
706 win_random_access_file_internal& impl)
707 {
708 38x std::lock_guard<win_mutex> lock(mutex_);
709 38x file_list_.remove(&impl);
710 38x }
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 147x win_random_access_file_service::on_pending(overlapped_op* op) noexcept
720 {
721 147x sched_.on_pending(op);
722 147x }
723
724 inline void
725 2x win_random_access_file_service::on_completion(
726 overlapped_op* op, DWORD error, DWORD bytes) noexcept
727 {
728 2x sched_.on_completion(op, error, bytes);
729 2x }
730
731 inline void
732 149x win_random_access_file_service::work_started() noexcept
733 {
734 149x sched_.work_started();
735 149x }
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