Back to home page

EIC code displayed by LXR

 
 

    


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

0001 //
0002 // detail/io_uring_socket_send_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_SOCKET_SEND_OP_HPP
0012 #define BOOST_ASIO_DETAIL_IO_URING_SOCKET_SEND_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/socket_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_socket_send_op_base : public io_uring_operation
0038 {
0039 public:
0040   io_uring_socket_send_op_base(const boost::system::error_code& success_ec,
0041       socket_type socket, socket_ops::state_type state,
0042       const ConstBufferSequence& buffers,
0043       socket_base::message_flags flags, func_type complete_func)
0044     : io_uring_operation(success_ec,
0045         &io_uring_socket_send_op_base::do_prepare,
0046         &io_uring_socket_send_op_base::do_perform, complete_func),
0047       socket_(socket),
0048       state_(state),
0049       buffers_(buffers),
0050       flags_(flags),
0051       bufs_(buffers),
0052       msghdr_()
0053   {
0054     msghdr_.msg_iov = bufs_.buffers();
0055     msghdr_.msg_iovlen = static_cast<int>(bufs_.count());
0056   }
0057 
0058   static void do_prepare(io_uring_operation* base, ::io_uring_sqe* sqe)
0059   {
0060     BOOST_ASIO_ASSUME(base != 0);
0061     io_uring_socket_send_op_base* o(
0062         static_cast<io_uring_socket_send_op_base*>(base));
0063 
0064     if ((o->state_ & socket_ops::internal_non_blocking) != 0)
0065     {
0066       ::io_uring_prep_poll_add(sqe, o->socket_, POLLOUT);
0067     }
0068     else if (o->bufs_.is_single_buffer
0069         && o->bufs_.is_registered_buffer && o->flags_ == 0)
0070     {
0071       ::io_uring_prep_write_fixed(sqe, o->socket_,
0072           o->bufs_.buffers()->iov_base, o->bufs_.buffers()->iov_len,
0073           0, o->bufs_.registered_id().native_handle());
0074     }
0075     else
0076     {
0077       ::io_uring_prep_sendmsg(sqe, o->socket_, &o->msghdr_, o->flags_);
0078     }
0079   }
0080 
0081   static bool do_perform(io_uring_operation* base, bool after_completion)
0082   {
0083     BOOST_ASIO_ASSUME(base != 0);
0084     io_uring_socket_send_op_base* o(
0085         static_cast<io_uring_socket_send_op_base*>(base));
0086 
0087     if ((o->state_ & socket_ops::internal_non_blocking) != 0)
0088     {
0089       if (o->bufs_.is_single_buffer)
0090       {
0091         return socket_ops::non_blocking_send1(o->socket_,
0092             o->bufs_.first(o->buffers_).data(),
0093             o->bufs_.first(o->buffers_).size(), o->flags_,
0094             o->ec_, o->bytes_transferred_);
0095       }
0096       else
0097       {
0098         return socket_ops::non_blocking_send(o->socket_,
0099             o->bufs_.buffers(), o->bufs_.count(), o->flags_,
0100             o->ec_, o->bytes_transferred_);
0101       }
0102     }
0103 
0104     if (o->ec_ && o->ec_ == boost::asio::error::would_block)
0105     {
0106       o->state_ |= socket_ops::internal_non_blocking;
0107       return false;
0108     }
0109 
0110     return after_completion;
0111   }
0112 
0113 private:
0114   socket_type socket_;
0115   socket_ops::state_type state_;
0116   ConstBufferSequence buffers_;
0117   socket_base::message_flags flags_;
0118   buffer_sequence_adapter<boost::asio::const_buffer, ConstBufferSequence> bufs_;
0119   msghdr msghdr_;
0120 };
0121 
0122 template <typename ConstBufferSequence, typename Handler, typename IoExecutor>
0123 class io_uring_socket_send_op
0124   : public io_uring_socket_send_op_base<ConstBufferSequence>
0125 {
0126 public:
0127   BOOST_ASIO_DEFINE_HANDLER_PTR(io_uring_socket_send_op);
0128 
0129   io_uring_socket_send_op(const boost::system::error_code& success_ec,
0130       int socket, socket_ops::state_type state,
0131       const ConstBufferSequence& buffers, socket_base::message_flags flags,
0132       Handler& handler, const IoExecutor& io_ex)
0133     : io_uring_socket_send_op_base<ConstBufferSequence>(success_ec,
0134         socket, state, buffers, flags, &io_uring_socket_send_op::do_complete),
0135       handler_(static_cast<Handler&&>(handler)),
0136       work_(handler_, io_ex)
0137   {
0138   }
0139 
0140   static void do_complete(void* owner, operation* base,
0141       const boost::system::error_code& /*ec*/,
0142       std::size_t /*bytes_transferred*/)
0143   {
0144     // Take ownership of the handler object.
0145     BOOST_ASIO_ASSUME(base != 0);
0146     io_uring_socket_send_op* o
0147       (static_cast<io_uring_socket_send_op*>(base));
0148     ptr p = { boost::asio::detail::addressof(o->handler_), o, o };
0149 
0150     BOOST_ASIO_HANDLER_COMPLETION((*o));
0151 
0152     // Take ownership of the operation's outstanding work.
0153     handler_work<Handler, IoExecutor> w(
0154         static_cast<handler_work<Handler, IoExecutor>&&>(
0155           o->work_));
0156 
0157     BOOST_ASIO_ERROR_LOCATION(o->ec_);
0158 
0159     // Make a copy of the handler so that the memory can be deallocated before
0160     // the upcall is made. Even if we're not about to make an upcall, a
0161     // sub-object of the handler may be the true owner of the memory associated
0162     // with the handler. Consequently, a local copy of the handler is required
0163     // to ensure that any owning sub-object remains valid until after we have
0164     // deallocated the memory here.
0165     detail::binder2<Handler, boost::system::error_code, std::size_t>
0166       handler(o->handler_, o->ec_, o->bytes_transferred_);
0167     p.h = boost::asio::detail::addressof(handler.handler_);
0168     p.reset();
0169 
0170     // Make the upcall if required.
0171     if (owner)
0172     {
0173       fenced_block b(fenced_block::half);
0174       BOOST_ASIO_HANDLER_INVOCATION_BEGIN((handler.arg1_, handler.arg2_));
0175       w.complete(handler, handler.handler_);
0176       BOOST_ASIO_HANDLER_INVOCATION_END;
0177     }
0178   }
0179 
0180 private:
0181   Handler handler_;
0182   handler_work<Handler, IoExecutor> work_;
0183 };
0184 
0185 } // namespace detail
0186 } // namespace asio
0187 } // namespace boost
0188 
0189 #include <boost/asio/detail/pop_options.hpp>
0190 
0191 #endif // defined(BOOST_ASIO_HAS_IO_URING)
0192 
0193 #endif // BOOST_ASIO_DETAIL_IO_URING_SOCKET_SEND_OP_HPP