include/boost/corosio/native/detail/posix/posix_stream_file_service.hpp

100.0% Lines (158 / 158) 100.0% Functions (14 / 14)
posix_stream_file_service.hpp
f(x) Functions (14)
Function Calls Lines Blocks
boost::corosio::detail::posix_stream_file_service::posix_stream_file_service(boost::capy::execution_context&) :35 216x 100.0% 89.0% boost::corosio::detail::posix_stream_file_service::~posix_stream_file_service() :41 432x 100.0% 100.0% boost::corosio::detail::posix_stream_file_service::construct() :47 272x 100.0% 71.0% boost::corosio::detail::posix_stream_file_service::destroy(boost::corosio::io_object::implementation*) :61 270x 100.0% 100.0% boost::corosio::detail::posix_stream_file_service::close(boost::corosio::io_object::handle&) :69 507x 100.0% 100.0% boost::corosio::detail::posix_stream_file_service::open_file(boost::corosio::stream_file::implementation&, std::filesystem::__cxx11::path const&, boost::corosio::file_base::flags) :79 255x 100.0% 100.0% boost::corosio::detail::posix_stream_file_service::shutdown() :91 216x 100.0% 100.0% boost::corosio::detail::posix_stream_file_service::destroy_impl(boost::corosio::detail::posix_stream_file&) :103 270x 100.0% 67.0% boost::corosio::detail::posix_stream_file_service::post(boost::corosio::detail::scheduler_op*) :110 217x 100.0% 100.0% boost::corosio::detail::posix_stream_file_service::pool() :138 225x 100.0% 100.0% boost::corosio::detail::posix_stream_file::read_some(std::__n4861::coroutine_handle<void>, boost::capy::executor_ref, boost::corosio::buffer_param, std::stop_token, std::error_code*, unsigned long*) :157 152x 100.0% 95.0% boost::corosio::detail::posix_stream_file::do_read_work(boost::corosio::detail::pool_work_item*) :224 138x 100.0% 100.0% boost::corosio::detail::posix_stream_file::write_some(std::__n4861::coroutine_handle<void>, boost::capy::executor_ref, boost::corosio::buffer_param, std::stop_token, std::error_code*, unsigned long*) :259 89x 100.0% 95.0% boost::corosio::detail::posix_stream_file::do_write_work(boost::corosio::detail::pool_work_item*) :326 79x 100.0% 100.0%
Line 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_POSIX_POSIX_STREAM_FILE_SERVICE_HPP
11 #define BOOST_COROSIO_NATIVE_DETAIL_POSIX_POSIX_STREAM_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_stream_file.hpp>
18 #include <boost/corosio/native/detail/reactor/reactor_scheduler.hpp>
19 #include <boost/corosio/detail/file_service.hpp>
20 #include <boost/corosio/detail/thread_pool.hpp>
21
22 #include <mutex>
23 #include <unordered_map>
24
25 namespace boost::corosio::detail {
26
27 /** Stream file service for POSIX backends.
28
29 Owns all posix_stream_file instances. Thread lifecycle is
30 managed by the thread_pool service (shared with resolver).
31 */
32 class BOOST_COROSIO_DECL posix_stream_file_service final : public file_service
33 {
34 public:
35 216x explicit posix_stream_file_service(capy::execution_context& ctx)
36 648x : sched_(&get_scheduler(ctx))
37 216x , pool_(ctx)
38 {
39 216x }
40
41 432x ~posix_stream_file_service() override = default;
42
43 posix_stream_file_service(posix_stream_file_service const&) = delete;
44 posix_stream_file_service&
45 operator=(posix_stream_file_service const&) = delete;
46
47 272x io_object::implementation* construct() override
48 {
49 272x auto ptr = std::make_shared<posix_stream_file>(*this);
50 272x auto* impl = ptr.get();
51
52 {
53 272x std::lock_guard<std::mutex> lock(mutex_);
54 272x file_list_.push_back(impl);
55 272x file_ptrs_[impl] = std::move(ptr);
56 272x }
57
58 272x return impl;
59 272x }
60
61 270x void destroy(io_object::implementation* p) override
62 {
63 270x auto& impl = static_cast<posix_stream_file&>(*p);
64 270x impl.cancel();
65 270x impl.close_file();
66 270x destroy_impl(impl);
67 270x }
68
69 507x void close(io_object::handle& h) override
70 {
71 507x if (h.get())
72 {
73 507x auto& impl = static_cast<posix_stream_file&>(*h.get());
74 507x impl.cancel();
75 507x impl.close_file();
76 }
77 507x }
78
79 255x std::error_code open_file(
80 stream_file::implementation& impl,
81 std::filesystem::path const& path,
82 file_base::flags mode) override
83 {
84 // Unavailable in the unsafe tier: the file thread pool completes
85 // cross-thread, which the lockless scheduler cannot accept.
86 255x if (sched_->scheduler_locking_disabled())
87 2x return std::make_error_code(std::errc::operation_not_supported);
88 253x return static_cast<posix_stream_file&>(impl).open_file(path, mode);
89 }
90
91 216x void shutdown() override
92 {
93 216x std::lock_guard<std::mutex> lock(mutex_);
94 218x for (auto* impl = file_list_.pop_front(); impl != nullptr;
95 2x impl = file_list_.pop_front())
96 {
97 2x impl->cancel();
98 2x impl->close_file();
99 }
100 216x file_ptrs_.clear();
101 216x }
102
103 270x void destroy_impl(posix_stream_file& impl)
104 {
105 270x std::lock_guard<std::mutex> lock(mutex_);
106 270x file_list_.remove(&impl);
107 270x file_ptrs_.erase(&impl);
108 270x }
109
110 217x void post(scheduler_op* op)
111 {
112 217x sched_->post(op);
113 217x }
114
115 void work_started() noexcept
116 {
117 sched_->work_started();
118 }
119
120 void work_finished() noexcept
121 {
122 sched_->work_finished();
123 }
124
125 /** Return the thread pool that runs this service's file work.
126
127 The pool's service is created on first use, so this can fail
128 where a plain accessor could not. Its workers start later, on
129 the first post, and a thread the system refuses there is
130 reported by that post rather than thrown here.
131
132 @throws std::bad_alloc If the service cannot be allocated.
133
134 @return The context's shared blocking-I/O pool.
135
136 @see thread_pool_ref::get
137 */
138 225x thread_pool& pool()
139 {
140 225x return pool_.get();
141 }
142
143 private:
144 scheduler* sched_;
145 thread_pool_ref pool_;
146 std::mutex mutex_;
147 intrusive_list<posix_stream_file> file_list_;
148 std::unordered_map<posix_stream_file*, std::shared_ptr<posix_stream_file>>
149 file_ptrs_;
150 };
151
152 // ---------------------------------------------------------------------------
153 // posix_stream_file inline implementations (require complete service type)
154 // ---------------------------------------------------------------------------
155
156 inline std::coroutine_handle<>
157 152x posix_stream_file::read_some(
158 std::coroutine_handle<> h,
159 capy::executor_ref ex,
160 buffer_param param,
161 std::stop_token token,
162 std::error_code* ec,
163 std::size_t* bytes_out)
164 {
165 152x auto& op = read_op_;
166 152x op.reset();
167 152x op.is_read = true;
168
169 // Closed-object contract outranks the zero-length no-op.
170 152x if (fd_ < 0)
171 {
172 6x *ec = make_error_code(std::errc::bad_file_descriptor);
173 6x *bytes_out = 0;
174 6x op.cont.h = h;
175 6x return dispatch_coro(ex, op.cont);
176 }
177
178 146x capy::mutable_buffer bufs[max_buffers];
179 146x op.iovec_count = static_cast<int>(param.copy_to(bufs, max_buffers));
180
181 146x if (op.iovec_count == 0)
182 {
183 2x *ec = {};
184 2x *bytes_out = 0;
185 2x op.cont.h = h;
186 2x return dispatch_coro(ex, op.cont);
187 }
188
189 288x for (int i = 0; i < op.iovec_count; ++i)
190 {
191 144x op.iovecs[i].iov_base = bufs[i].data();
192 144x op.iovecs[i].iov_len = bufs[i].size();
193 }
194
195 144x op.h = h;
196 144x op.ex = ex;
197 144x op.ec_out = ec;
198 144x op.bytes_out = bytes_out;
199 144x op.start(token);
200
201 144x op.ex.on_work_started();
202
203 144x read_pool_op_.file_ = this;
204 144x read_pool_op_.ref_ = this->shared_from_this();
205 144x read_pool_op_.func_ = &posix_stream_file::do_read_work;
206 144x if (auto pec = svc_.pool().post(&read_pool_op_))
207 {
208 // The pool is shutting down, or the system refused it a thread.
209 // Nothing of this read went cross-thread, so it answers here
210 // like the closed-descriptor and zero-length exits above rather
211 // than through a completion the scheduler has to carry back.
212 6x read_pool_op_.ref_.reset();
213 6x op.stop_cb.reset();
214 6x op.ex.on_work_finished();
215 6x *ec = pec;
216 6x *bytes_out = 0;
217 6x op.cont.h = h;
218 6x return dispatch_coro(ex, op.cont);
219 }
220 138x return std::noop_coroutine();
221 }
222
223 inline void
224 138x posix_stream_file::do_read_work(pool_work_item* w) noexcept
225 {
226 138x auto* pw = static_cast<pool_op*>(w);
227 138x auto* self = pw->file_;
228 138x auto& op = self->read_op_;
229
230 138x if (!op.cancelled.load(std::memory_order_acquire))
231 {
232 ssize_t n;
233 do
234 {
235 240x n = ::preadv(
236 120x self->fd_, op.iovecs, op.iovec_count,
237 120x static_cast<off_t>(self->offset_));
238 }
239 120x while (n < 0 && errno == EINTR);
240
241 120x if (n >= 0)
242 {
243 113x op.errn = 0;
244 113x op.bytes_transferred = static_cast<std::size_t>(n);
245 113x self->offset_ += static_cast<std::uint64_t>(n);
246 }
247 else
248 {
249 7x op.errn = errno;
250 7x op.bytes_transferred = 0;
251 }
252 }
253
254 138x op.impl_ptr = std::move(pw->ref_);
255 138x self->svc_.post(&op);
256 138x }
257
258 inline std::coroutine_handle<>
259 89x posix_stream_file::write_some(
260 std::coroutine_handle<> h,
261 capy::executor_ref ex,
262 buffer_param param,
263 std::stop_token token,
264 std::error_code* ec,
265 std::size_t* bytes_out)
266 {
267 89x auto& op = write_op_;
268 89x op.reset();
269 89x op.is_read = false;
270
271 // Closed-object contract outranks the zero-length no-op.
272 89x if (fd_ < 0)
273 {
274 6x *ec = make_error_code(std::errc::bad_file_descriptor);
275 6x *bytes_out = 0;
276 6x op.cont.h = h;
277 6x return dispatch_coro(ex, op.cont);
278 }
279
280 83x capy::mutable_buffer bufs[max_buffers];
281 83x op.iovec_count = static_cast<int>(param.copy_to(bufs, max_buffers));
282
283 83x if (op.iovec_count == 0)
284 {
285 2x *ec = {};
286 2x *bytes_out = 0;
287 2x op.cont.h = h;
288 2x return dispatch_coro(ex, op.cont);
289 }
290
291 162x for (int i = 0; i < op.iovec_count; ++i)
292 {
293 81x op.iovecs[i].iov_base = bufs[i].data();
294 81x op.iovecs[i].iov_len = bufs[i].size();
295 }
296
297 81x op.h = h;
298 81x op.ex = ex;
299 81x op.ec_out = ec;
300 81x op.bytes_out = bytes_out;
301 81x op.start(token);
302
303 81x op.ex.on_work_started();
304
305 81x write_pool_op_.file_ = this;
306 81x write_pool_op_.ref_ = this->shared_from_this();
307 81x write_pool_op_.func_ = &posix_stream_file::do_write_work;
308 81x if (auto pec = svc_.pool().post(&write_pool_op_))
309 {
310 // The pool is shutting down, or the system refused it a thread.
311 // Nothing of this write went cross-thread, so it answers here
312 // like the closed-descriptor and zero-length exits above rather
313 // than through a completion the scheduler has to carry back.
314 2x write_pool_op_.ref_.reset();
315 2x op.stop_cb.reset();
316 2x op.ex.on_work_finished();
317 2x *ec = pec;
318 2x *bytes_out = 0;
319 2x op.cont.h = h;
320 2x return dispatch_coro(ex, op.cont);
321 }
322 79x return std::noop_coroutine();
323 }
324
325 inline void
326 79x posix_stream_file::do_write_work(pool_work_item* w) noexcept
327 {
328 79x auto* pw = static_cast<pool_op*>(w);
329 79x auto* self = pw->file_;
330 79x auto& op = self->write_op_;
331
332 79x if (!op.cancelled.load(std::memory_order_acquire))
333 {
334 ssize_t n;
335 do
336 {
337 58x n = ::pwritev(
338 29x self->fd_, op.iovecs, op.iovec_count,
339 29x static_cast<off_t>(self->offset_));
340 }
341 29x while (n < 0 && errno == EINTR);
342
343 29x if (n >= 0)
344 {
345 22x op.errn = 0;
346 22x op.bytes_transferred = static_cast<std::size_t>(n);
347 22x self->offset_ += static_cast<std::uint64_t>(n);
348 }
349 else
350 {
351 7x op.errn = errno;
352 7x op.bytes_transferred = 0;
353 }
354 }
355
356 79x op.impl_ptr = std::move(pw->ref_);
357 79x self->svc_.post(&op);
358 79x }
359
360 } // namespace boost::corosio::detail
361
362 #endif // BOOST_COROSIO_POSIX
363
364 #endif // BOOST_COROSIO_NATIVE_DETAIL_POSIX_POSIX_STREAM_FILE_SERVICE_HPP
365