]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // |
2 | // detail/kqueue_reactor.hpp | |
3 | // ~~~~~~~~~~~~~~~~~~~~~~~~~ | |
4 | // | |
5 | // Copyright (c) 2003-2016 Christopher M. Kohlhoff (chris at kohlhoff dot com) | |
6 | // Copyright (c) 2005 Stefan Arentz (stefan at soze dot com) | |
7 | // | |
8 | // Distributed under the Boost Software License, Version 1.0. (See accompanying | |
9 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | |
10 | // | |
11 | ||
12 | #ifndef BOOST_ASIO_DETAIL_KQUEUE_REACTOR_HPP | |
13 | #define BOOST_ASIO_DETAIL_KQUEUE_REACTOR_HPP | |
14 | ||
15 | #if defined(_MSC_VER) && (_MSC_VER >= 1200) | |
16 | # pragma once | |
17 | #endif // defined(_MSC_VER) && (_MSC_VER >= 1200) | |
18 | ||
19 | #include <boost/asio/detail/config.hpp> | |
20 | ||
21 | #if defined(BOOST_ASIO_HAS_KQUEUE) | |
22 | ||
23 | #include <cstddef> | |
24 | #include <sys/types.h> | |
25 | #include <sys/event.h> | |
26 | #include <sys/time.h> | |
27 | #include <boost/asio/detail/limits.hpp> | |
28 | #include <boost/asio/detail/mutex.hpp> | |
29 | #include <boost/asio/detail/object_pool.hpp> | |
30 | #include <boost/asio/detail/op_queue.hpp> | |
31 | #include <boost/asio/detail/reactor_op.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/error.hpp> | |
38 | #include <boost/asio/io_service.hpp> | |
39 | ||
40 | // Older versions of Mac OS X may not define EV_OOBAND. | |
41 | #if !defined(EV_OOBAND) | |
42 | # define EV_OOBAND EV_FLAG1 | |
43 | #endif // !defined(EV_OOBAND) | |
44 | ||
45 | #include <boost/asio/detail/push_options.hpp> | |
46 | ||
47 | namespace boost { | |
48 | namespace asio { | |
49 | namespace detail { | |
50 | ||
51 | class kqueue_reactor | |
52 | : public boost::asio::detail::service_base<kqueue_reactor> | |
53 | { | |
54 | public: | |
55 | enum op_types { read_op = 0, write_op = 1, | |
56 | connect_op = 1, except_op = 2, max_ops = 3 }; | |
57 | ||
58 | // Per-descriptor queues. | |
59 | struct descriptor_state | |
60 | { | |
61 | friend class kqueue_reactor; | |
62 | friend class object_pool_access; | |
63 | ||
64 | descriptor_state* next_; | |
65 | descriptor_state* prev_; | |
66 | ||
67 | mutex mutex_; | |
68 | int descriptor_; | |
69 | int num_kevents_; // 1 == read only, 2 == read and write | |
70 | op_queue<reactor_op> op_queue_[max_ops]; | |
71 | bool shutdown_; | |
72 | }; | |
73 | ||
74 | // Per-descriptor data. | |
75 | typedef descriptor_state* per_descriptor_data; | |
76 | ||
77 | // Constructor. | |
78 | BOOST_ASIO_DECL kqueue_reactor(boost::asio::io_service& io_service); | |
79 | ||
80 | // Destructor. | |
81 | BOOST_ASIO_DECL ~kqueue_reactor(); | |
82 | ||
83 | // Destroy all user-defined handler objects owned by the service. | |
84 | BOOST_ASIO_DECL void shutdown_service(); | |
85 | ||
86 | // Recreate internal descriptors following a fork. | |
87 | BOOST_ASIO_DECL void fork_service( | |
88 | boost::asio::io_service::fork_event fork_ev); | |
89 | ||
90 | // Initialise the task. | |
91 | BOOST_ASIO_DECL void init_task(); | |
92 | ||
93 | // Register a socket with the reactor. Returns 0 on success, system error | |
94 | // code on failure. | |
95 | BOOST_ASIO_DECL int register_descriptor(socket_type descriptor, | |
96 | per_descriptor_data& descriptor_data); | |
97 | ||
98 | // Register a descriptor with an associated single operation. Returns 0 on | |
99 | // success, system error code on failure. | |
100 | BOOST_ASIO_DECL int register_internal_descriptor( | |
101 | int op_type, socket_type descriptor, | |
102 | per_descriptor_data& descriptor_data, reactor_op* op); | |
103 | ||
104 | // Move descriptor registration from one descriptor_data object to another. | |
105 | BOOST_ASIO_DECL void move_descriptor(socket_type descriptor, | |
106 | per_descriptor_data& target_descriptor_data, | |
107 | per_descriptor_data& source_descriptor_data); | |
108 | ||
109 | // Post a reactor operation for immediate completion. | |
110 | void post_immediate_completion(reactor_op* op, bool is_continuation) | |
111 | { | |
112 | io_service_.post_immediate_completion(op, is_continuation); | |
113 | } | |
114 | ||
115 | // Start a new operation. The reactor operation will be performed when the | |
116 | // given descriptor is flagged as ready, or an error has occurred. | |
117 | BOOST_ASIO_DECL void start_op(int op_type, socket_type descriptor, | |
118 | per_descriptor_data& descriptor_data, reactor_op* op, | |
119 | bool is_continuation, bool allow_speculative); | |
120 | ||
121 | // Cancel all operations associated with the given descriptor. The | |
122 | // handlers associated with the descriptor will be invoked with the | |
123 | // operation_aborted error. | |
124 | BOOST_ASIO_DECL void cancel_ops(socket_type descriptor, | |
125 | per_descriptor_data& descriptor_data); | |
126 | ||
127 | // Cancel any operations that are running against the descriptor and remove | |
128 | // its registration from the reactor. | |
129 | BOOST_ASIO_DECL void deregister_descriptor(socket_type descriptor, | |
130 | per_descriptor_data& descriptor_data, bool closing); | |
131 | ||
132 | // Remote the descriptor's registration from the reactor. | |
133 | BOOST_ASIO_DECL void deregister_internal_descriptor( | |
134 | socket_type descriptor, per_descriptor_data& descriptor_data); | |
135 | ||
136 | // Add a new timer queue to the reactor. | |
137 | template <typename Time_Traits> | |
138 | void add_timer_queue(timer_queue<Time_Traits>& queue); | |
139 | ||
140 | // Remove a timer queue from the reactor. | |
141 | template <typename Time_Traits> | |
142 | void remove_timer_queue(timer_queue<Time_Traits>& queue); | |
143 | ||
144 | // Schedule a new operation in the given timer queue to expire at the | |
145 | // specified absolute time. | |
146 | template <typename Time_Traits> | |
147 | void schedule_timer(timer_queue<Time_Traits>& queue, | |
148 | const typename Time_Traits::time_type& time, | |
149 | typename timer_queue<Time_Traits>::per_timer_data& timer, wait_op* op); | |
150 | ||
151 | // Cancel the timer operations associated with the given token. Returns the | |
152 | // number of operations that have been posted or dispatched. | |
153 | template <typename Time_Traits> | |
154 | std::size_t cancel_timer(timer_queue<Time_Traits>& queue, | |
155 | typename timer_queue<Time_Traits>::per_timer_data& timer, | |
156 | std::size_t max_cancelled = (std::numeric_limits<std::size_t>::max)()); | |
157 | ||
158 | // Run the kqueue loop. | |
159 | BOOST_ASIO_DECL void run(bool block, op_queue<operation>& ops); | |
160 | ||
161 | // Interrupt the kqueue loop. | |
162 | BOOST_ASIO_DECL void interrupt(); | |
163 | ||
164 | private: | |
165 | // Create the kqueue file descriptor. Throws an exception if the descriptor | |
166 | // cannot be created. | |
167 | BOOST_ASIO_DECL static int do_kqueue_create(); | |
168 | ||
169 | // Allocate a new descriptor state object. | |
170 | BOOST_ASIO_DECL descriptor_state* allocate_descriptor_state(); | |
171 | ||
172 | // Free an existing descriptor state object. | |
173 | BOOST_ASIO_DECL void free_descriptor_state(descriptor_state* s); | |
174 | ||
175 | // Helper function to add a new timer queue. | |
176 | BOOST_ASIO_DECL void do_add_timer_queue(timer_queue_base& queue); | |
177 | ||
178 | // Helper function to remove a timer queue. | |
179 | BOOST_ASIO_DECL void do_remove_timer_queue(timer_queue_base& queue); | |
180 | ||
181 | // Get the timeout value for the kevent call. | |
182 | BOOST_ASIO_DECL timespec* get_timeout(timespec& ts); | |
183 | ||
184 | // The io_service implementation used to post completions. | |
185 | io_service_impl& io_service_; | |
186 | ||
187 | // Mutex to protect access to internal data. | |
188 | mutex mutex_; | |
189 | ||
190 | // The kqueue file descriptor. | |
191 | int kqueue_fd_; | |
192 | ||
193 | // The interrupter is used to break a blocking kevent call. | |
194 | select_interrupter interrupter_; | |
195 | ||
196 | // The timer queues. | |
197 | timer_queue_set timer_queues_; | |
198 | ||
199 | // Whether the service has been shut down. | |
200 | bool shutdown_; | |
201 | ||
202 | // Mutex to protect access to the registered descriptors. | |
203 | mutex registered_descriptors_mutex_; | |
204 | ||
205 | // Keep track of all registered descriptors. | |
206 | object_pool<descriptor_state> registered_descriptors_; | |
207 | }; | |
208 | ||
209 | } // namespace detail | |
210 | } // namespace asio | |
211 | } // namespace boost | |
212 | ||
213 | #include <boost/asio/detail/pop_options.hpp> | |
214 | ||
215 | #include <boost/asio/detail/impl/kqueue_reactor.hpp> | |
216 | #if defined(BOOST_ASIO_HEADER_ONLY) | |
217 | # include <boost/asio/detail/impl/kqueue_reactor.ipp> | |
218 | #endif // defined(BOOST_ASIO_HEADER_ONLY) | |
219 | ||
220 | #endif // defined(BOOST_ASIO_HAS_KQUEUE) | |
221 | ||
222 | #endif // BOOST_ASIO_DETAIL_KQUEUE_REACTOR_HPP |