]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/asio/detail/dev_poll_reactor.hpp
bump version to 18.2.4-pve3
[ceph.git] / ceph / src / boost / boost / asio / detail / dev_poll_reactor.hpp
1 //
2 // detail/dev_poll_reactor.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_DEV_POLL_REACTOR_HPP
12 #define BOOST_ASIO_DETAIL_DEV_POLL_REACTOR_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_DEV_POLL)
21
22 #include <cstddef>
23 #include <vector>
24 #include <sys/devpoll.h>
25 #include <boost/asio/detail/hash_map.hpp>
26 #include <boost/asio/detail/limits.hpp>
27 #include <boost/asio/detail/mutex.hpp>
28 #include <boost/asio/detail/op_queue.hpp>
29 #include <boost/asio/detail/reactor_op.hpp>
30 #include <boost/asio/detail/reactor_op_queue.hpp>
31 #include <boost/asio/detail/scheduler_task.hpp>
32 #include <boost/asio/detail/select_interrupter.hpp>
33 #include <boost/asio/detail/socket_types.hpp>
34 #include <boost/asio/detail/timer_queue_base.hpp>
35 #include <boost/asio/detail/timer_queue_set.hpp>
36 #include <boost/asio/detail/wait_op.hpp>
37 #include <boost/asio/execution_context.hpp>
38
39 #include <boost/asio/detail/push_options.hpp>
40
41 namespace boost {
42 namespace asio {
43 namespace detail {
44
45 class dev_poll_reactor
46 : public execution_context_service_base<dev_poll_reactor>,
47 public scheduler_task
48 {
49 public:
50 enum op_types { read_op = 0, write_op = 1,
51 connect_op = 1, except_op = 2, max_ops = 3 };
52
53 // Per-descriptor data.
54 struct per_descriptor_data
55 {
56 };
57
58 // Constructor.
59 BOOST_ASIO_DECL dev_poll_reactor(boost::asio::execution_context& ctx);
60
61 // Destructor.
62 BOOST_ASIO_DECL ~dev_poll_reactor();
63
64 // Destroy all user-defined handler objects owned by the service.
65 BOOST_ASIO_DECL void shutdown();
66
67 // Recreate internal descriptors following a fork.
68 BOOST_ASIO_DECL void notify_fork(
69 boost::asio::execution_context::fork_event fork_ev);
70
71 // Initialise the task.
72 BOOST_ASIO_DECL void init_task();
73
74 // Register a socket with the reactor. Returns 0 on success, system error
75 // code on failure.
76 BOOST_ASIO_DECL int register_descriptor(socket_type, per_descriptor_data&);
77
78 // Register a descriptor with an associated single operation. Returns 0 on
79 // success, system error code on failure.
80 BOOST_ASIO_DECL int register_internal_descriptor(
81 int op_type, socket_type descriptor,
82 per_descriptor_data& descriptor_data, reactor_op* op);
83
84 // Move descriptor registration from one descriptor_data object to another.
85 BOOST_ASIO_DECL void move_descriptor(socket_type descriptor,
86 per_descriptor_data& target_descriptor_data,
87 per_descriptor_data& source_descriptor_data);
88
89 // Post a reactor operation for immediate completion.
90 void post_immediate_completion(reactor_op* op, bool is_continuation);
91
92 // Start a new operation. The reactor operation will be performed when the
93 // given descriptor is flagged as ready, or an error has occurred.
94 BOOST_ASIO_DECL void start_op(int op_type, socket_type descriptor,
95 per_descriptor_data&, reactor_op* op,
96 bool is_continuation, bool allow_speculative);
97
98 // Cancel all operations associated with the given descriptor. The
99 // handlers associated with the descriptor will be invoked with the
100 // operation_aborted error.
101 BOOST_ASIO_DECL void cancel_ops(socket_type descriptor, per_descriptor_data&);
102
103 // Cancel all operations associated with the given descriptor and key. The
104 // handlers associated with the descriptor will be invoked with the
105 // operation_aborted error.
106 BOOST_ASIO_DECL void cancel_ops_by_key(socket_type descriptor,
107 per_descriptor_data& descriptor_data,
108 int op_type, void* cancellation_key);
109
110 // Cancel any operations that are running against the descriptor and remove
111 // its registration from the reactor. The reactor resources associated with
112 // the descriptor must be released by calling cleanup_descriptor_data.
113 BOOST_ASIO_DECL void deregister_descriptor(socket_type descriptor,
114 per_descriptor_data&, bool closing);
115
116 // Remove the descriptor's registration from the reactor. The reactor
117 // resources associated with the descriptor must be released by calling
118 // cleanup_descriptor_data.
119 BOOST_ASIO_DECL void deregister_internal_descriptor(
120 socket_type descriptor, per_descriptor_data&);
121
122 // Perform any post-deregistration cleanup tasks associated with the
123 // descriptor data.
124 BOOST_ASIO_DECL void cleanup_descriptor_data(per_descriptor_data&);
125
126 // Add a new timer queue to the reactor.
127 template <typename Time_Traits>
128 void add_timer_queue(timer_queue<Time_Traits>& queue);
129
130 // Remove a timer queue from the reactor.
131 template <typename Time_Traits>
132 void remove_timer_queue(timer_queue<Time_Traits>& queue);
133
134 // Schedule a new operation in the given timer queue to expire at the
135 // specified absolute time.
136 template <typename Time_Traits>
137 void schedule_timer(timer_queue<Time_Traits>& queue,
138 const typename Time_Traits::time_type& time,
139 typename timer_queue<Time_Traits>::per_timer_data& timer, wait_op* op);
140
141 // Cancel the timer operations associated with the given token. Returns the
142 // number of operations that have been posted or dispatched.
143 template <typename Time_Traits>
144 std::size_t cancel_timer(timer_queue<Time_Traits>& queue,
145 typename timer_queue<Time_Traits>::per_timer_data& timer,
146 std::size_t max_cancelled = (std::numeric_limits<std::size_t>::max)());
147
148 // Cancel the timer operations associated with the given key.
149 template <typename Time_Traits>
150 void cancel_timer_by_key(timer_queue<Time_Traits>& queue,
151 typename timer_queue<Time_Traits>::per_timer_data* timer,
152 void* cancellation_key);
153
154 // Move the timer operations associated with the given timer.
155 template <typename Time_Traits>
156 void move_timer(timer_queue<Time_Traits>& queue,
157 typename timer_queue<Time_Traits>::per_timer_data& target,
158 typename timer_queue<Time_Traits>::per_timer_data& source);
159
160 // Run /dev/poll once until interrupted or events are ready to be dispatched.
161 BOOST_ASIO_DECL void run(long usec, op_queue<operation>& ops);
162
163 // Interrupt the select loop.
164 BOOST_ASIO_DECL void interrupt();
165
166 private:
167 // Create the /dev/poll file descriptor. Throws an exception if the descriptor
168 // cannot be created.
169 BOOST_ASIO_DECL static int do_dev_poll_create();
170
171 // Helper function to add a new timer queue.
172 BOOST_ASIO_DECL void do_add_timer_queue(timer_queue_base& queue);
173
174 // Helper function to remove a timer queue.
175 BOOST_ASIO_DECL void do_remove_timer_queue(timer_queue_base& queue);
176
177 // Get the timeout value for the /dev/poll DP_POLL operation. The timeout
178 // value is returned as a number of milliseconds. A return value of -1
179 // indicates that the poll should block indefinitely.
180 BOOST_ASIO_DECL int get_timeout(int msec);
181
182 // Cancel all operations associated with the given descriptor. The do_cancel
183 // function of the handler objects will be invoked. This function does not
184 // acquire the dev_poll_reactor's mutex.
185 BOOST_ASIO_DECL void cancel_ops_unlocked(socket_type descriptor,
186 const boost::system::error_code& ec);
187
188 // Add a pending event entry for the given descriptor.
189 BOOST_ASIO_DECL ::pollfd& add_pending_event_change(int descriptor);
190
191 // The scheduler implementation used to post completions.
192 scheduler& scheduler_;
193
194 // Mutex to protect access to internal data.
195 boost::asio::detail::mutex mutex_;
196
197 // The /dev/poll file descriptor.
198 int dev_poll_fd_;
199
200 // Vector of /dev/poll events waiting to be written to the descriptor.
201 std::vector< ::pollfd> pending_event_changes_;
202
203 // Hash map to associate a descriptor with a pending event change index.
204 hash_map<int, std::size_t> pending_event_change_index_;
205
206 // The interrupter is used to break a blocking DP_POLL operation.
207 select_interrupter interrupter_;
208
209 // The queues of read, write and except operations.
210 reactor_op_queue<socket_type> op_queue_[max_ops];
211
212 // The timer queues.
213 timer_queue_set timer_queues_;
214
215 // Whether the service has been shut down.
216 bool shutdown_;
217 };
218
219 } // namespace detail
220 } // namespace asio
221 } // namespace boost
222
223 #include <boost/asio/detail/pop_options.hpp>
224
225 #include <boost/asio/detail/impl/dev_poll_reactor.hpp>
226 #if defined(BOOST_ASIO_HEADER_ONLY)
227 # include <boost/asio/detail/impl/dev_poll_reactor.ipp>
228 #endif // defined(BOOST_ASIO_HEADER_ONLY)
229
230 #endif // defined(BOOST_ASIO_HAS_DEV_POLL)
231
232 #endif // BOOST_ASIO_DETAIL_DEV_POLL_REACTOR_HPP