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