Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:28:40

0001 //
0002 // detail/io_uring_descriptor_write_at_op.hpp
0003 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
0004 //
0005 // Copyright (c) 2003-2023 Christopher M. Kohlhoff (chris at kohlhoff dot com)
0006 //
0007 // Distributed under the Boost Software License, Version 1.0. (See accompanying
0008 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
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 // defined(_MSC_VER) && (_MSC_VER >= 1200)
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& /*ec*/,
0140       std::size_t /*bytes_transferred*/)
0141   {
0142     // Take ownership of the handler object.
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     // Take ownership of the operation's outstanding work.
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     // Make a copy of the handler so that the memory can be deallocated before
0158     // the upcall is made. Even if we're not about to make an upcall, a
0159     // sub-object of the handler may be the true owner of the memory associated
0160     // with the handler. Consequently, a local copy of the handler is required
0161     // to ensure that any owning sub-object remains valid until after we have
0162     // deallocated the memory here.
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     // Make the upcall if required.
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 } // namespace detail
0184 } // namespace asio
0185 } // namespace boost
0186 
0187 #include <boost/asio/detail/pop_options.hpp>
0188 
0189 #endif // defined(BOOST_ASIO_HAS_IO_URING)
0190 
0191 #endif // BOOST_ASIO_DETAIL_IO_URING_DESCRIPTOR_WRITE_AT_OP_HPP