io_uring_socket_send_op.hpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. //
  2. // detail/io_uring_socket_send_op.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2024 Christopher M. Kohlhoff (chris at kohlhoff dot com)
  6. //
  7. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  8. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  9. //
  10. #ifndef BOOST_ASIO_DETAIL_IO_URING_SOCKET_SEND_OP_HPP
  11. #define BOOST_ASIO_DETAIL_IO_URING_SOCKET_SEND_OP_HPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include <boost/asio/detail/config.hpp>
  16. #if defined(BOOST_ASIO_HAS_IO_URING)
  17. #include <boost/asio/detail/bind_handler.hpp>
  18. #include <boost/asio/detail/buffer_sequence_adapter.hpp>
  19. #include <boost/asio/detail/socket_ops.hpp>
  20. #include <boost/asio/detail/fenced_block.hpp>
  21. #include <boost/asio/detail/handler_work.hpp>
  22. #include <boost/asio/detail/io_uring_operation.hpp>
  23. #include <boost/asio/detail/memory.hpp>
  24. #include <boost/asio/detail/push_options.hpp>
  25. namespace boost {
  26. namespace asio {
  27. namespace detail {
  28. template <typename ConstBufferSequence>
  29. class io_uring_socket_send_op_base : public io_uring_operation
  30. {
  31. public:
  32. io_uring_socket_send_op_base(const boost::system::error_code& success_ec,
  33. socket_type socket, socket_ops::state_type state,
  34. const ConstBufferSequence& buffers,
  35. socket_base::message_flags flags, func_type complete_func)
  36. : io_uring_operation(success_ec,
  37. &io_uring_socket_send_op_base::do_prepare,
  38. &io_uring_socket_send_op_base::do_perform, complete_func),
  39. socket_(socket),
  40. state_(state),
  41. buffers_(buffers),
  42. flags_(flags),
  43. bufs_(buffers),
  44. msghdr_()
  45. {
  46. msghdr_.msg_iov = bufs_.buffers();
  47. msghdr_.msg_iovlen = static_cast<int>(bufs_.count());
  48. }
  49. static void do_prepare(io_uring_operation* base, ::io_uring_sqe* sqe)
  50. {
  51. BOOST_ASIO_ASSUME(base != 0);
  52. io_uring_socket_send_op_base* o(
  53. static_cast<io_uring_socket_send_op_base*>(base));
  54. if ((o->state_ & socket_ops::internal_non_blocking) != 0)
  55. {
  56. ::io_uring_prep_poll_add(sqe, o->socket_, POLLOUT);
  57. }
  58. else if (o->bufs_.is_single_buffer
  59. && o->bufs_.is_registered_buffer && o->flags_ == 0)
  60. {
  61. ::io_uring_prep_write_fixed(sqe, o->socket_,
  62. o->bufs_.buffers()->iov_base, o->bufs_.buffers()->iov_len,
  63. 0, o->bufs_.registered_id().native_handle());
  64. }
  65. else
  66. {
  67. ::io_uring_prep_sendmsg(sqe, o->socket_, &o->msghdr_, o->flags_);
  68. }
  69. }
  70. static bool do_perform(io_uring_operation* base, bool after_completion)
  71. {
  72. BOOST_ASIO_ASSUME(base != 0);
  73. io_uring_socket_send_op_base* o(
  74. static_cast<io_uring_socket_send_op_base*>(base));
  75. if ((o->state_ & socket_ops::internal_non_blocking) != 0)
  76. {
  77. if (o->bufs_.is_single_buffer)
  78. {
  79. return socket_ops::non_blocking_send1(o->socket_,
  80. o->bufs_.first(o->buffers_).data(),
  81. o->bufs_.first(o->buffers_).size(), o->flags_,
  82. o->ec_, o->bytes_transferred_);
  83. }
  84. else
  85. {
  86. return socket_ops::non_blocking_send(o->socket_,
  87. o->bufs_.buffers(), o->bufs_.count(), o->flags_,
  88. o->ec_, o->bytes_transferred_);
  89. }
  90. }
  91. if (o->ec_ && o->ec_ == boost::asio::error::would_block)
  92. {
  93. o->state_ |= socket_ops::internal_non_blocking;
  94. return false;
  95. }
  96. return after_completion;
  97. }
  98. private:
  99. socket_type socket_;
  100. socket_ops::state_type state_;
  101. ConstBufferSequence buffers_;
  102. socket_base::message_flags flags_;
  103. buffer_sequence_adapter<boost::asio::const_buffer, ConstBufferSequence> bufs_;
  104. msghdr msghdr_;
  105. };
  106. template <typename ConstBufferSequence, typename Handler, typename IoExecutor>
  107. class io_uring_socket_send_op
  108. : public io_uring_socket_send_op_base<ConstBufferSequence>
  109. {
  110. public:
  111. BOOST_ASIO_DEFINE_HANDLER_PTR(io_uring_socket_send_op);
  112. io_uring_socket_send_op(const boost::system::error_code& success_ec,
  113. int socket, socket_ops::state_type state,
  114. const ConstBufferSequence& buffers, socket_base::message_flags flags,
  115. Handler& handler, const IoExecutor& io_ex)
  116. : io_uring_socket_send_op_base<ConstBufferSequence>(success_ec,
  117. socket, state, buffers, flags, &io_uring_socket_send_op::do_complete),
  118. handler_(static_cast<Handler&&>(handler)),
  119. work_(handler_, io_ex)
  120. {
  121. }
  122. static void do_complete(void* owner, operation* base,
  123. const boost::system::error_code& /*ec*/,
  124. std::size_t /*bytes_transferred*/)
  125. {
  126. // Take ownership of the handler object.
  127. BOOST_ASIO_ASSUME(base != 0);
  128. io_uring_socket_send_op* o
  129. (static_cast<io_uring_socket_send_op*>(base));
  130. ptr p = { boost::asio::detail::addressof(o->handler_), o, o };
  131. BOOST_ASIO_HANDLER_COMPLETION((*o));
  132. // Take ownership of the operation's outstanding work.
  133. handler_work<Handler, IoExecutor> w(
  134. static_cast<handler_work<Handler, IoExecutor>&&>(
  135. o->work_));
  136. BOOST_ASIO_ERROR_LOCATION(o->ec_);
  137. // Make a copy of the handler so that the memory can be deallocated before
  138. // the upcall is made. Even if we're not about to make an upcall, a
  139. // sub-object of the handler may be the true owner of the memory associated
  140. // with the handler. Consequently, a local copy of the handler is required
  141. // to ensure that any owning sub-object remains valid until after we have
  142. // deallocated the memory here.
  143. detail::binder2<Handler, boost::system::error_code, std::size_t>
  144. handler(o->handler_, o->ec_, o->bytes_transferred_);
  145. p.h = boost::asio::detail::addressof(handler.handler_);
  146. p.reset();
  147. // Make the upcall if required.
  148. if (owner)
  149. {
  150. fenced_block b(fenced_block::half);
  151. BOOST_ASIO_HANDLER_INVOCATION_BEGIN((handler.arg1_, handler.arg2_));
  152. w.complete(handler, handler.handler_);
  153. BOOST_ASIO_HANDLER_INVOCATION_END;
  154. }
  155. }
  156. private:
  157. Handler handler_;
  158. handler_work<Handler, IoExecutor> work_;
  159. };
  160. } // namespace detail
  161. } // namespace asio
  162. } // namespace boost
  163. #include <boost/asio/detail/pop_options.hpp>
  164. #endif // defined(BOOST_ASIO_HAS_IO_URING)
  165. #endif // BOOST_ASIO_DETAIL_IO_URING_SOCKET_SEND_OP_HPP