]>
Commit | Line | Data |
---|---|---|
1e59de90 TL |
1 | // |
2 | // detail/io_uring_null_buffers_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_NULL_BUFFERS_OP_HPP | |
12 | #define BOOST_ASIO_DETAIL_IO_URING_NULL_BUFFERS_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 | #include <boost/asio/detail/bind_handler.hpp> | |
20 | #include <boost/asio/detail/fenced_block.hpp> | |
21 | #include <boost/asio/detail/handler_alloc_helpers.hpp> | |
22 | #include <boost/asio/detail/handler_invoke_helpers.hpp> | |
23 | #include <boost/asio/detail/handler_work.hpp> | |
24 | #include <boost/asio/detail/io_uring_operation.hpp> | |
25 | #include <boost/asio/detail/memory.hpp> | |
26 | ||
27 | #include <boost/asio/detail/push_options.hpp> | |
28 | ||
29 | namespace boost { | |
30 | namespace asio { | |
31 | namespace detail { | |
32 | ||
33 | template <typename Handler, typename IoExecutor> | |
34 | class io_uring_null_buffers_op : public io_uring_operation | |
35 | { | |
36 | public: | |
37 | BOOST_ASIO_DEFINE_HANDLER_PTR(io_uring_null_buffers_op); | |
38 | ||
39 | io_uring_null_buffers_op(const boost::system::error_code& success_ec, | |
40 | int descriptor, int poll_flags, Handler& handler, const IoExecutor& io_ex) | |
41 | : io_uring_operation(success_ec, | |
42 | &io_uring_null_buffers_op::do_prepare, | |
43 | &io_uring_null_buffers_op::do_perform, | |
44 | &io_uring_null_buffers_op::do_complete), | |
45 | handler_(BOOST_ASIO_MOVE_CAST(Handler)(handler)), | |
46 | work_(handler_, io_ex), | |
47 | descriptor_(descriptor), | |
48 | poll_flags_(poll_flags) | |
49 | { | |
50 | } | |
51 | ||
52 | static void do_prepare(io_uring_operation* base, ::io_uring_sqe* sqe) | |
53 | { | |
54 | io_uring_null_buffers_op* o(static_cast<io_uring_null_buffers_op*>(base)); | |
55 | ||
56 | ::io_uring_prep_poll_add(sqe, o->descriptor_, o->poll_flags_); | |
57 | } | |
58 | ||
59 | static bool do_perform(io_uring_operation*, bool after_completion) | |
60 | { | |
61 | return after_completion; | |
62 | } | |
63 | ||
64 | static void do_complete(void* owner, operation* base, | |
65 | const boost::system::error_code& /*ec*/, | |
66 | std::size_t /*bytes_transferred*/) | |
67 | { | |
68 | // Take ownership of the handler object. | |
69 | io_uring_null_buffers_op* o(static_cast<io_uring_null_buffers_op*>(base)); | |
70 | ptr p = { boost::asio::detail::addressof(o->handler_), o, o }; | |
71 | ||
72 | BOOST_ASIO_HANDLER_COMPLETION((*o)); | |
73 | ||
74 | // Take ownership of the operation's outstanding work. | |
75 | handler_work<Handler, IoExecutor> w( | |
76 | BOOST_ASIO_MOVE_CAST2(handler_work<Handler, IoExecutor>)( | |
77 | o->work_)); | |
78 | ||
79 | // Make a copy of the handler so that the memory can be deallocated before | |
80 | // the upcall is made. Even if we're not about to make an upcall, a | |
81 | // sub-object of the handler may be the true owner of the memory associated | |
82 | // with the handler. Consequently, a local copy of the handler is required | |
83 | // to ensure that any owning sub-object remains valid until after we have | |
84 | // deallocated the memory here. | |
85 | detail::binder2<Handler, boost::system::error_code, std::size_t> | |
86 | handler(o->handler_, o->ec_, o->bytes_transferred_); | |
87 | p.h = boost::asio::detail::addressof(handler.handler_); | |
88 | p.reset(); | |
89 | ||
90 | // Make the upcall if required. | |
91 | if (owner) | |
92 | { | |
93 | fenced_block b(fenced_block::half); | |
94 | BOOST_ASIO_HANDLER_INVOCATION_BEGIN((handler.arg1_, handler.arg2_)); | |
95 | w.complete(handler, handler.handler_); | |
96 | BOOST_ASIO_HANDLER_INVOCATION_END; | |
97 | } | |
98 | } | |
99 | ||
100 | private: | |
101 | Handler handler_; | |
102 | handler_work<Handler, IoExecutor> work_; | |
103 | int descriptor_; | |
104 | int poll_flags_; | |
105 | }; | |
106 | ||
107 | } // namespace detail | |
108 | } // namespace asio | |
109 | } // namespace boost | |
110 | ||
111 | #include <boost/asio/detail/pop_options.hpp> | |
112 | ||
113 | #endif // BOOST_ASIO_DETAIL_IO_URING_NULL_BUFFERS_OP_HPP |