File indexing completed on 2025-01-18 09:28:40
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011 #ifndef BOOST_ASIO_DETAIL_IO_URING_DESCRIPTOR_WRITE_AT_OP_HPP
0012 #define BOOST_ASIO_DETAIL_IO_URING_DESCRIPTOR_WRITE_AT_OP_HPP
0013
0014 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
0015 # pragma once
0016 #endif
0017
0018 #include <boost/asio/detail/config.hpp>
0019
0020 #if defined(BOOST_ASIO_HAS_IO_URING)
0021
0022 #include <boost/asio/detail/bind_handler.hpp>
0023 #include <boost/asio/detail/buffer_sequence_adapter.hpp>
0024 #include <boost/asio/detail/descriptor_ops.hpp>
0025 #include <boost/asio/detail/fenced_block.hpp>
0026 #include <boost/asio/detail/handler_work.hpp>
0027 #include <boost/asio/detail/io_uring_operation.hpp>
0028 #include <boost/asio/detail/memory.hpp>
0029
0030 #include <boost/asio/detail/push_options.hpp>
0031
0032 namespace boost {
0033 namespace asio {
0034 namespace detail {
0035
0036 template <typename ConstBufferSequence>
0037 class io_uring_descriptor_write_at_op_base : public io_uring_operation
0038 {
0039 public:
0040 io_uring_descriptor_write_at_op_base(
0041 const boost::system::error_code& success_ec, int descriptor,
0042 descriptor_ops::state_type state, uint64_t offset,
0043 const ConstBufferSequence& buffers, func_type complete_func)
0044 : io_uring_operation(success_ec,
0045 &io_uring_descriptor_write_at_op_base::do_prepare,
0046 &io_uring_descriptor_write_at_op_base::do_perform, complete_func),
0047 descriptor_(descriptor),
0048 state_(state),
0049 offset_(offset),
0050 buffers_(buffers),
0051 bufs_(buffers)
0052 {
0053 }
0054
0055 static void do_prepare(io_uring_operation* base, ::io_uring_sqe* sqe)
0056 {
0057 BOOST_ASIO_ASSUME(base != 0);
0058 io_uring_descriptor_write_at_op_base* o(
0059 static_cast<io_uring_descriptor_write_at_op_base*>(base));
0060
0061 if ((o->state_ & descriptor_ops::internal_non_blocking) != 0)
0062 {
0063 ::io_uring_prep_poll_add(sqe, o->descriptor_, POLLOUT);
0064 }
0065 else if (o->bufs_.is_single_buffer && o->bufs_.is_registered_buffer)
0066 {
0067 ::io_uring_prep_write_fixed(sqe, o->descriptor_,
0068 o->bufs_.buffers()->iov_base, o->bufs_.buffers()->iov_len,
0069 o->offset_, o->bufs_.registered_id().native_handle());
0070 }
0071 else
0072 {
0073 ::io_uring_prep_writev(sqe, o->descriptor_,
0074 o->bufs_.buffers(), o->bufs_.count(), o->offset_);
0075 }
0076 }
0077
0078 static bool do_perform(io_uring_operation* base, bool after_completion)
0079 {
0080 BOOST_ASIO_ASSUME(base != 0);
0081 io_uring_descriptor_write_at_op_base* o(
0082 static_cast<io_uring_descriptor_write_at_op_base*>(base));
0083
0084 if ((o->state_ & descriptor_ops::internal_non_blocking) != 0)
0085 {
0086 if (o->bufs_.is_single_buffer)
0087 {
0088 return descriptor_ops::non_blocking_write_at1(o->descriptor_,
0089 o->offset_, o->bufs_.first(o->buffers_).data(),
0090 o->bufs_.first(o->buffers_).size(), o->ec_,
0091 o->bytes_transferred_);
0092 }
0093 else
0094 {
0095 return descriptor_ops::non_blocking_write_at(o->descriptor_,
0096 o->offset_, o->bufs_.buffers(), o->bufs_.count(),
0097 o->ec_, o->bytes_transferred_);
0098 }
0099 }
0100
0101 if (o->ec_ && o->ec_ == boost::asio::error::would_block)
0102 {
0103 o->state_ |= descriptor_ops::internal_non_blocking;
0104 return false;
0105 }
0106
0107 return after_completion;
0108 }
0109
0110 private:
0111 int descriptor_;
0112 descriptor_ops::state_type state_;
0113 uint64_t offset_;
0114 ConstBufferSequence buffers_;
0115 buffer_sequence_adapter<boost::asio::const_buffer,
0116 ConstBufferSequence> bufs_;
0117 };
0118
0119 template <typename ConstBufferSequence, typename Handler, typename IoExecutor>
0120 class io_uring_descriptor_write_at_op
0121 : public io_uring_descriptor_write_at_op_base<ConstBufferSequence>
0122 {
0123 public:
0124 BOOST_ASIO_DEFINE_HANDLER_PTR(io_uring_descriptor_write_at_op);
0125
0126 io_uring_descriptor_write_at_op(const boost::system::error_code& success_ec,
0127 int descriptor, descriptor_ops::state_type state, uint64_t offset,
0128 const ConstBufferSequence& buffers, Handler& handler,
0129 const IoExecutor& io_ex)
0130 : io_uring_descriptor_write_at_op_base<ConstBufferSequence>(
0131 success_ec, descriptor, state, offset, buffers,
0132 &io_uring_descriptor_write_at_op::do_complete),
0133 handler_(static_cast<Handler&&>(handler)),
0134 work_(handler_, io_ex)
0135 {
0136 }
0137
0138 static void do_complete(void* owner, operation* base,
0139 const boost::system::error_code& ,
0140 std::size_t )
0141 {
0142
0143 BOOST_ASIO_ASSUME(base != 0);
0144 io_uring_descriptor_write_at_op* o
0145 (static_cast<io_uring_descriptor_write_at_op*>(base));
0146 ptr p = { boost::asio::detail::addressof(o->handler_), o, o };
0147
0148 BOOST_ASIO_HANDLER_COMPLETION((*o));
0149
0150
0151 handler_work<Handler, IoExecutor> w(
0152 static_cast<handler_work<Handler, IoExecutor>&&>(
0153 o->work_));
0154
0155 BOOST_ASIO_ERROR_LOCATION(o->ec_);
0156
0157
0158
0159
0160
0161
0162
0163 detail::binder2<Handler, boost::system::error_code, std::size_t>
0164 handler(o->handler_, o->ec_, o->bytes_transferred_);
0165 p.h = boost::asio::detail::addressof(handler.handler_);
0166 p.reset();
0167
0168
0169 if (owner)
0170 {
0171 fenced_block b(fenced_block::half);
0172 BOOST_ASIO_HANDLER_INVOCATION_BEGIN((handler.arg1_, handler.arg2_));
0173 w.complete(handler, handler.handler_);
0174 BOOST_ASIO_HANDLER_INVOCATION_END;
0175 }
0176 }
0177
0178 private:
0179 Handler handler_;
0180 handler_work<Handler, IoExecutor> work_;
0181 };
0182
0183 }
0184 }
0185 }
0186
0187 #include <boost/asio/detail/pop_options.hpp>
0188
0189 #endif
0190
0191 #endif