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