2 // detail/io_uring_socket_send_op.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5 // Copyright (c) 2003-2022 Christopher M. Kohlhoff (chris at kohlhoff dot com)
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)
11 #ifndef BOOST_ASIO_DETAIL_IO_URING_SOCKET_SEND_OP_HPP
12 #define BOOST_ASIO_DETAIL_IO_URING_SOCKET_SEND_OP_HPP
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
18 #include <boost/asio/detail/config.hpp>
20 #if defined(BOOST_ASIO_HAS_IO_URING)
22 #include <boost/asio/detail/bind_handler.hpp>
23 #include <boost/asio/detail/buffer_sequence_adapter.hpp>
24 #include <boost/asio/detail/socket_ops.hpp>
25 #include <boost/asio/detail/fenced_block.hpp>
26 #include <boost/asio/detail/handler_work.hpp>
27 #include <boost/asio/detail/io_uring_operation.hpp>
28 #include <boost/asio/detail/memory.hpp>
30 #include <boost/asio/detail/push_options.hpp>
36 template <typename ConstBufferSequence>
37 class io_uring_socket_send_op_base : public io_uring_operation
40 io_uring_socket_send_op_base(const boost::system::error_code& success_ec,
41 socket_type socket, socket_ops::state_type state,
42 const ConstBufferSequence& buffers,
43 socket_base::message_flags flags, func_type complete_func)
44 : io_uring_operation(success_ec,
45 &io_uring_socket_send_op_base::do_prepare,
46 &io_uring_socket_send_op_base::do_perform, complete_func),
54 msghdr_.msg_iov = bufs_.buffers();
55 msghdr_.msg_iovlen = static_cast<int>(bufs_.count());
58 static void do_prepare(io_uring_operation* base, ::io_uring_sqe* sqe)
60 io_uring_socket_send_op_base* o(
61 static_cast<io_uring_socket_send_op_base*>(base));
63 if ((o->state_ & socket_ops::internal_non_blocking) != 0)
65 ::io_uring_prep_poll_add(sqe, o->socket_, POLLOUT);
67 else if (o->bufs_.is_single_buffer
68 && o->bufs_.is_registered_buffer && o->flags_ == 0)
70 ::io_uring_prep_write_fixed(sqe, o->socket_,
71 o->bufs_.buffers()->iov_base, o->bufs_.buffers()->iov_len,
72 0, o->bufs_.registered_id().native_handle());
76 ::io_uring_prep_sendmsg(sqe, o->socket_, &o->msghdr_, o->flags_);
80 static bool do_perform(io_uring_operation* base, bool after_completion)
82 io_uring_socket_send_op_base* o(
83 static_cast<io_uring_socket_send_op_base*>(base));
85 if ((o->state_ & socket_ops::internal_non_blocking) != 0)
87 if (o->bufs_.is_single_buffer)
89 return socket_ops::non_blocking_send1(o->socket_,
90 o->bufs_.first(o->buffers_).data(),
91 o->bufs_.first(o->buffers_).size(), o->flags_,
92 o->ec_, o->bytes_transferred_);
96 return socket_ops::non_blocking_send(o->socket_,
97 o->bufs_.buffers(), o->bufs_.count(), o->flags_,
98 o->ec_, o->bytes_transferred_);
102 if (o->ec_ && o->ec_ == boost::asio::error::would_block)
104 o->state_ |= socket_ops::internal_non_blocking;
108 return after_completion;
113 socket_ops::state_type state_;
114 ConstBufferSequence buffers_;
115 socket_base::message_flags flags_;
116 buffer_sequence_adapter<boost::asio::const_buffer, ConstBufferSequence> bufs_;
120 template <typename ConstBufferSequence, typename Handler, typename IoExecutor>
121 class io_uring_socket_send_op
122 : public io_uring_socket_send_op_base<ConstBufferSequence>
125 BOOST_ASIO_DEFINE_HANDLER_PTR(io_uring_socket_send_op);
127 io_uring_socket_send_op(const boost::system::error_code& success_ec,
128 int socket, socket_ops::state_type state,
129 const ConstBufferSequence& buffers, socket_base::message_flags flags,
130 Handler& handler, const IoExecutor& io_ex)
131 : io_uring_socket_send_op_base<ConstBufferSequence>(success_ec,
132 socket, state, buffers, flags, &io_uring_socket_send_op::do_complete),
133 handler_(BOOST_ASIO_MOVE_CAST(Handler)(handler)),
134 work_(handler_, io_ex)
138 static void do_complete(void* owner, operation* base,
139 const boost::system::error_code& /*ec*/,
140 std::size_t /*bytes_transferred*/)
142 // Take ownership of the handler object.
143 io_uring_socket_send_op* o
144 (static_cast<io_uring_socket_send_op*>(base));
145 ptr p = { boost::asio::detail::addressof(o->handler_), o, o };
147 BOOST_ASIO_HANDLER_COMPLETION((*o));
149 // Take ownership of the operation's outstanding work.
150 handler_work<Handler, IoExecutor> w(
151 BOOST_ASIO_MOVE_CAST2(handler_work<Handler, IoExecutor>)(
154 // Make a copy of the handler so that the memory can be deallocated before
155 // the upcall is made. Even if we're not about to make an upcall, a
156 // sub-object of the handler may be the true owner of the memory associated
157 // with the handler. Consequently, a local copy of the handler is required
158 // to ensure that any owning sub-object remains valid until after we have
159 // deallocated the memory here.
160 detail::binder2<Handler, boost::system::error_code, std::size_t>
161 handler(o->handler_, o->ec_, o->bytes_transferred_);
162 p.h = boost::asio::detail::addressof(handler.handler_);
165 // Make the upcall if required.
168 fenced_block b(fenced_block::half);
169 BOOST_ASIO_HANDLER_INVOCATION_BEGIN((handler.arg1_, handler.arg2_));
170 w.complete(handler, handler.handler_);
171 BOOST_ASIO_HANDLER_INVOCATION_END;
177 handler_work<Handler, IoExecutor> work_;
180 } // namespace detail
184 #include <boost/asio/detail/pop_options.hpp>
186 #endif // defined(BOOST_ASIO_HAS_IO_URING)
188 #endif // BOOST_ASIO_DETAIL_IO_URING_SOCKET_SEND_OP_HPP