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