2 // detail/kqueue_reactor.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~
5 // Copyright (c) 2003-2016 Christopher M. Kohlhoff (chris at kohlhoff dot com)
6 // Copyright (c) 2005 Stefan Arentz (stefan at soze dot com)
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)
12 #ifndef BOOST_ASIO_DETAIL_KQUEUE_REACTOR_HPP
13 #define BOOST_ASIO_DETAIL_KQUEUE_REACTOR_HPP
15 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
17 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
19 #include <boost/asio/detail/config.hpp>
21 #if defined(BOOST_ASIO_HAS_KQUEUE)
24 #include <sys/types.h>
25 #include <sys/event.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>
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)
45 #include <boost/asio/detail/push_options.hpp>
52 : public boost::asio::detail::service_base<kqueue_reactor>
55 enum op_types { read_op = 0, write_op = 1,
56 connect_op = 1, except_op = 2, max_ops = 3 };
58 // Per-descriptor queues.
59 struct descriptor_state
61 friend class kqueue_reactor;
62 friend class object_pool_access;
64 descriptor_state* next_;
65 descriptor_state* prev_;
69 int num_kevents_; // 1 == read only, 2 == read and write
70 op_queue<reactor_op> op_queue_[max_ops];
74 // Per-descriptor data.
75 typedef descriptor_state* per_descriptor_data;
78 BOOST_ASIO_DECL kqueue_reactor(boost::asio::io_service& io_service);
81 BOOST_ASIO_DECL ~kqueue_reactor();
83 // Destroy all user-defined handler objects owned by the service.
84 BOOST_ASIO_DECL void shutdown_service();
86 // Recreate internal descriptors following a fork.
87 BOOST_ASIO_DECL void fork_service(
88 boost::asio::io_service::fork_event fork_ev);
90 // Initialise the task.
91 BOOST_ASIO_DECL void init_task();
93 // Register a socket with the reactor. Returns 0 on success, system error
95 BOOST_ASIO_DECL int register_descriptor(socket_type descriptor,
96 per_descriptor_data& descriptor_data);
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);
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);
109 // Post a reactor operation for immediate completion.
110 void post_immediate_completion(reactor_op* op, bool is_continuation)
112 io_service_.post_immediate_completion(op, is_continuation);
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);
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);
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);
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);
136 // Add a new timer queue to the reactor.
137 template <typename Time_Traits>
138 void add_timer_queue(timer_queue<Time_Traits>& queue);
140 // Remove a timer queue from the reactor.
141 template <typename Time_Traits>
142 void remove_timer_queue(timer_queue<Time_Traits>& queue);
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);
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)());
158 // Run the kqueue loop.
159 BOOST_ASIO_DECL void run(bool block, op_queue<operation>& ops);
161 // Interrupt the kqueue loop.
162 BOOST_ASIO_DECL void interrupt();
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();
169 // Allocate a new descriptor state object.
170 BOOST_ASIO_DECL descriptor_state* allocate_descriptor_state();
172 // Free an existing descriptor state object.
173 BOOST_ASIO_DECL void free_descriptor_state(descriptor_state* s);
175 // Helper function to add a new timer queue.
176 BOOST_ASIO_DECL void do_add_timer_queue(timer_queue_base& queue);
178 // Helper function to remove a timer queue.
179 BOOST_ASIO_DECL void do_remove_timer_queue(timer_queue_base& queue);
181 // Get the timeout value for the kevent call.
182 BOOST_ASIO_DECL timespec* get_timeout(timespec& ts);
184 // The io_service implementation used to post completions.
185 io_service_impl& io_service_;
187 // Mutex to protect access to internal data.
190 // The kqueue file descriptor.
193 // The interrupter is used to break a blocking kevent call.
194 select_interrupter interrupter_;
197 timer_queue_set timer_queues_;
199 // Whether the service has been shut down.
202 // Mutex to protect access to the registered descriptors.
203 mutex registered_descriptors_mutex_;
205 // Keep track of all registered descriptors.
206 object_pool<descriptor_state> registered_descriptors_;
209 } // namespace detail
213 #include <boost/asio/detail/pop_options.hpp>
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)
220 #endif // defined(BOOST_ASIO_HAS_KQUEUE)
222 #endif // BOOST_ASIO_DETAIL_KQUEUE_REACTOR_HPP