]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/asio/detail/io_uring_socket_send_op.hpp
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / boost / boost / asio / detail / io_uring_socket_send_op.hpp
1 //
2 // detail/io_uring_socket_send_op.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2022 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
11 #ifndef BOOST_ASIO_DETAIL_IO_URING_SOCKET_SEND_OP_HPP
12 #define BOOST_ASIO_DETAIL_IO_URING_SOCKET_SEND_OP_HPP
13
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
15 # pragma once
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
17
18 #include <boost/asio/detail/config.hpp>
19
20 #if defined(BOOST_ASIO_HAS_IO_URING)
21
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>
29
30 #include <boost/asio/detail/push_options.hpp>
31
32 namespace boost {
33 namespace asio {
34 namespace detail {
35
36 template <typename ConstBufferSequence>
37 class io_uring_socket_send_op_base : public io_uring_operation
38 {
39 public:
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),
47 socket_(socket),
48 state_(state),
49 buffers_(buffers),
50 flags_(flags),
51 bufs_(buffers),
52 msghdr_()
53 {
54 msghdr_.msg_iov = bufs_.buffers();
55 msghdr_.msg_iovlen = static_cast<int>(bufs_.count());
56 }
57
58 static void do_prepare(io_uring_operation* base, ::io_uring_sqe* sqe)
59 {
60 io_uring_socket_send_op_base* o(
61 static_cast<io_uring_socket_send_op_base*>(base));
62
63 if ((o->state_ & socket_ops::internal_non_blocking) != 0)
64 {
65 ::io_uring_prep_poll_add(sqe, o->socket_, POLLOUT);
66 }
67 else if (o->bufs_.is_single_buffer
68 && o->bufs_.is_registered_buffer && o->flags_ == 0)
69 {
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());
73 }
74 else
75 {
76 ::io_uring_prep_sendmsg(sqe, o->socket_, &o->msghdr_, o->flags_);
77 }
78 }
79
80 static bool do_perform(io_uring_operation* base, bool after_completion)
81 {
82 io_uring_socket_send_op_base* o(
83 static_cast<io_uring_socket_send_op_base*>(base));
84
85 if ((o->state_ & socket_ops::internal_non_blocking) != 0)
86 {
87 if (o->bufs_.is_single_buffer)
88 {
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_);
93 }
94 else
95 {
96 return socket_ops::non_blocking_send(o->socket_,
97 o->bufs_.buffers(), o->bufs_.count(), o->flags_,
98 o->ec_, o->bytes_transferred_);
99 }
100 }
101
102 if (o->ec_ && o->ec_ == boost::asio::error::would_block)
103 {
104 o->state_ |= socket_ops::internal_non_blocking;
105 return false;
106 }
107
108 return after_completion;
109 }
110
111 private:
112 socket_type socket_;
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_;
117 msghdr msghdr_;
118 };
119
120 template <typename ConstBufferSequence, typename Handler, typename IoExecutor>
121 class io_uring_socket_send_op
122 : public io_uring_socket_send_op_base<ConstBufferSequence>
123 {
124 public:
125 BOOST_ASIO_DEFINE_HANDLER_PTR(io_uring_socket_send_op);
126
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)
135 {
136 }
137
138 static void do_complete(void* owner, operation* base,
139 const boost::system::error_code& /*ec*/,
140 std::size_t /*bytes_transferred*/)
141 {
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 };
146
147 BOOST_ASIO_HANDLER_COMPLETION((*o));
148
149 // Take ownership of the operation's outstanding work.
150 handler_work<Handler, IoExecutor> w(
151 BOOST_ASIO_MOVE_CAST2(handler_work<Handler, IoExecutor>)(
152 o->work_));
153
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_);
163 p.reset();
164
165 // Make the upcall if required.
166 if (owner)
167 {
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;
172 }
173 }
174
175 private:
176 Handler handler_;
177 handler_work<Handler, IoExecutor> work_;
178 };
179
180 } // namespace detail
181 } // namespace asio
182 } // namespace boost
183
184 #include <boost/asio/detail/pop_options.hpp>
185
186 #endif // defined(BOOST_ASIO_HAS_IO_URING)
187
188 #endif // BOOST_ASIO_DETAIL_IO_URING_SOCKET_SEND_OP_HPP