TLA Line data 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_POSIX_POSIX_RANDOM_ACCESS_FILE_SERVICE_HPP
11 : #define BOOST_COROSIO_NATIVE_DETAIL_POSIX_POSIX_RANDOM_ACCESS_FILE_SERVICE_HPP
12 :
13 : #include <boost/corosio/detail/platform.hpp>
14 :
15 : #if BOOST_COROSIO_POSIX
16 :
17 : #include <boost/corosio/native/detail/posix/posix_random_access_file.hpp>
18 : #include <boost/corosio/native/detail/reactor/reactor_scheduler.hpp>
19 : #include <boost/corosio/detail/random_access_file_service.hpp>
20 : #include <boost/corosio/detail/thread_pool.hpp>
21 :
22 : #include <limits>
23 : #include <mutex>
24 : #include <unordered_map>
25 :
26 : namespace boost::corosio::detail {
27 :
28 : /** Random-access file service for POSIX backends. */
29 : class BOOST_COROSIO_DECL posix_random_access_file_service final
30 : : public random_access_file_service
31 : {
32 : public:
33 HIT 1232 : posix_random_access_file_service(
34 : capy::execution_context& ctx, scheduler& sched)
35 2464 : : sched_(&sched)
36 1232 : , pool_(get_or_create_pool(ctx))
37 : {
38 1232 : }
39 :
40 2464 : ~posix_random_access_file_service() override = default;
41 :
42 : posix_random_access_file_service(
43 : posix_random_access_file_service const&) = delete;
44 : posix_random_access_file_service& operator=(
45 : posix_random_access_file_service const&) = delete;
46 :
47 76 : io_object::implementation* construct() override
48 : {
49 76 : auto ptr = std::make_shared<posix_random_access_file>(*this);
50 76 : auto* impl = ptr.get();
51 :
52 : {
53 76 : std::lock_guard<std::mutex> lock(mutex_);
54 76 : file_list_.push_back(impl);
55 76 : file_ptrs_[impl] = std::move(ptr);
56 76 : }
57 :
58 76 : return impl;
59 76 : }
60 :
61 76 : void destroy(io_object::implementation* p) override
62 : {
63 76 : auto& impl = static_cast<posix_random_access_file&>(*p);
64 76 : impl.cancel();
65 76 : impl.close_file();
66 76 : destroy_impl(impl);
67 76 : }
68 :
69 136 : void close(io_object::handle& h) override
70 : {
71 136 : if (h.get())
72 : {
73 136 : auto& impl = static_cast<posix_random_access_file&>(*h.get());
74 136 : impl.cancel();
75 136 : impl.close_file();
76 : }
77 136 : }
78 :
79 64 : std::error_code open_file(
80 : random_access_file::implementation& impl,
81 : std::filesystem::path const& path,
82 : file_base::flags mode) override
83 : {
84 64 : if (sched_->is_single_threaded())
85 MIS 0 : return std::make_error_code(std::errc::operation_not_supported);
86 HIT 64 : return static_cast<posix_random_access_file&>(impl).open_file(
87 64 : path, mode);
88 : }
89 :
90 1232 : void shutdown() override
91 : {
92 1232 : std::lock_guard<std::mutex> lock(mutex_);
93 1232 : for (auto* impl = file_list_.pop_front(); impl != nullptr;
94 MIS 0 : impl = file_list_.pop_front())
95 : {
96 0 : impl->cancel();
97 0 : impl->close_file();
98 : }
99 HIT 1232 : file_ptrs_.clear();
100 1232 : }
101 :
102 76 : void destroy_impl(posix_random_access_file& impl)
103 : {
104 76 : std::lock_guard<std::mutex> lock(mutex_);
105 76 : file_list_.remove(&impl);
106 76 : file_ptrs_.erase(&impl);
107 76 : }
108 :
109 294 : void post(scheduler_op* op)
110 : {
111 294 : sched_->post(op);
112 294 : }
113 :
114 : void work_started() noexcept
115 : {
116 : sched_->work_started();
117 : }
118 :
119 : void work_finished() noexcept
120 : {
121 : sched_->work_finished();
122 : }
123 :
124 294 : thread_pool& pool() noexcept
125 : {
126 294 : return pool_;
127 : }
128 :
129 : private:
130 1232 : static thread_pool& get_or_create_pool(capy::execution_context& ctx)
131 : {
132 1232 : auto* p = ctx.find_service<thread_pool>();
133 1232 : if (p)
134 1232 : return *p;
135 MIS 0 : return ctx.make_service<thread_pool>();
136 : }
137 :
138 : scheduler* sched_;
139 : thread_pool& pool_;
140 : std::mutex mutex_;
141 : intrusive_list<posix_random_access_file> file_list_;
142 : std::unordered_map<
143 : posix_random_access_file*,
144 : std::shared_ptr<posix_random_access_file>>
145 : file_ptrs_;
146 : };
147 :
148 : /** Get or create the random-access file service for the given context. */
149 : inline posix_random_access_file_service&
150 HIT 1232 : get_random_access_file_service(capy::execution_context& ctx, scheduler& sched)
151 : {
152 1232 : return ctx.make_service<posix_random_access_file_service>(sched);
153 : }
154 :
155 : // ---------------------------------------------------------------------------
156 : // posix_random_access_file inline implementations (require complete service)
157 : // ---------------------------------------------------------------------------
158 :
159 : inline std::coroutine_handle<>
160 272 : posix_random_access_file::read_some_at(
161 : std::uint64_t offset,
162 : std::coroutine_handle<> h,
163 : capy::executor_ref ex,
164 : buffer_param param,
165 : std::stop_token token,
166 : std::error_code* ec,
167 : std::size_t* bytes_out)
168 : {
169 272 : capy::mutable_buffer bufs[max_buffers];
170 272 : auto count = param.copy_to(bufs, max_buffers);
171 :
172 272 : if (count == 0)
173 : {
174 2 : *ec = {};
175 2 : *bytes_out = 0;
176 2 : return h;
177 : }
178 :
179 270 : auto* op = new raf_op();
180 270 : op->is_read = true;
181 270 : op->offset = offset;
182 :
183 270 : op->iovec_count = static_cast<int>(count);
184 540 : for (int i = 0; i < op->iovec_count; ++i)
185 : {
186 270 : op->iovecs[i].iov_base = bufs[i].data();
187 270 : op->iovecs[i].iov_len = bufs[i].size();
188 : }
189 :
190 270 : op->h = h;
191 270 : op->ex = ex;
192 270 : op->ec_out = ec;
193 270 : op->bytes_out = bytes_out;
194 270 : op->file_ = this;
195 270 : op->file_ref = this->shared_from_this();
196 270 : op->start(token);
197 :
198 270 : op->ex.on_work_started();
199 :
200 : {
201 270 : std::lock_guard<std::mutex> lock(ops_mutex_);
202 270 : outstanding_ops_.push_back(op);
203 270 : }
204 :
205 270 : static_cast<pool_work_item*>(op)->func_ = &raf_op::do_work;
206 270 : if (!svc_.pool().post(static_cast<pool_work_item*>(op)))
207 : {
208 MIS 0 : op->cancelled.store(true, std::memory_order_release);
209 0 : svc_.post(static_cast<scheduler_op*>(op));
210 : }
211 HIT 270 : return std::noop_coroutine();
212 : }
213 :
214 : inline std::coroutine_handle<>
215 26 : posix_random_access_file::write_some_at(
216 : std::uint64_t offset,
217 : std::coroutine_handle<> h,
218 : capy::executor_ref ex,
219 : buffer_param param,
220 : std::stop_token token,
221 : std::error_code* ec,
222 : std::size_t* bytes_out)
223 : {
224 26 : capy::mutable_buffer bufs[max_buffers];
225 26 : auto count = param.copy_to(bufs, max_buffers);
226 :
227 26 : if (count == 0)
228 : {
229 2 : *ec = {};
230 2 : *bytes_out = 0;
231 2 : return h;
232 : }
233 :
234 24 : auto* op = new raf_op();
235 24 : op->is_read = false;
236 24 : op->offset = offset;
237 :
238 24 : op->iovec_count = static_cast<int>(count);
239 48 : for (int i = 0; i < op->iovec_count; ++i)
240 : {
241 24 : op->iovecs[i].iov_base = bufs[i].data();
242 24 : op->iovecs[i].iov_len = bufs[i].size();
243 : }
244 :
245 24 : op->h = h;
246 24 : op->ex = ex;
247 24 : op->ec_out = ec;
248 24 : op->bytes_out = bytes_out;
249 24 : op->file_ = this;
250 24 : op->file_ref = this->shared_from_this();
251 24 : op->start(token);
252 :
253 24 : op->ex.on_work_started();
254 :
255 : {
256 24 : std::lock_guard<std::mutex> lock(ops_mutex_);
257 24 : outstanding_ops_.push_back(op);
258 24 : }
259 :
260 24 : static_cast<pool_work_item*>(op)->func_ = &raf_op::do_work;
261 24 : if (!svc_.pool().post(static_cast<pool_work_item*>(op)))
262 : {
263 MIS 0 : op->cancelled.store(true, std::memory_order_release);
264 0 : svc_.post(static_cast<scheduler_op*>(op));
265 : }
266 HIT 24 : return std::noop_coroutine();
267 : }
268 :
269 : // -- raf_op thread-pool work function --
270 :
271 : inline void
272 294 : posix_random_access_file::raf_op::do_work(pool_work_item* w) noexcept
273 : {
274 294 : auto* op = static_cast<raf_op*>(w);
275 294 : auto* self = op->file_;
276 :
277 294 : if (op->cancelled.load(std::memory_order_acquire))
278 : {
279 2 : op->errn = ECANCELED;
280 2 : op->bytes_transferred = 0;
281 : }
282 584 : else if (op->offset >
283 292 : static_cast<std::uint64_t>(std::numeric_limits<off_t>::max()))
284 : {
285 MIS 0 : op->errn = EOVERFLOW;
286 0 : op->bytes_transferred = 0;
287 : }
288 : else
289 : {
290 : ssize_t n;
291 HIT 292 : if (op->is_read)
292 : {
293 : do
294 : {
295 536 : n = ::preadv(self->fd_, op->iovecs, op->iovec_count,
296 268 : static_cast<off_t>(op->offset));
297 : }
298 268 : while (n < 0 && errno == EINTR);
299 : }
300 : else
301 : {
302 : do
303 : {
304 48 : n = ::pwritev(self->fd_, op->iovecs, op->iovec_count,
305 24 : static_cast<off_t>(op->offset));
306 : }
307 24 : while (n < 0 && errno == EINTR);
308 : }
309 :
310 292 : if (n >= 0)
311 : {
312 292 : op->errn = 0;
313 292 : op->bytes_transferred = static_cast<std::size_t>(n);
314 : }
315 : else
316 : {
317 MIS 0 : op->errn = errno;
318 0 : op->bytes_transferred = 0;
319 : }
320 : }
321 :
322 HIT 294 : self->svc_.post(static_cast<scheduler_op*>(op));
323 294 : }
324 :
325 : } // namespace boost::corosio::detail
326 :
327 : #endif // BOOST_COROSIO_POSIX
328 :
329 : #endif // BOOST_COROSIO_NATIVE_DETAIL_POSIX_POSIX_RANDOM_ACCESS_FILE_SERVICE_HPP
|