1 //
2 // detail/kqueue_reactor.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2019 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/execution_context.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 scheduler;
52 
53 class kqueue_reactor
54   : public execution_context_service_base<kqueue_reactor>
55 {
56 private:
57   // The mutex type used by this reactor.
58   typedef conditionally_enabled_mutex mutex;
59 
60 public:
61   enum op_types { read_op = 0, write_op = 1,
62     connect_op = 1, except_op = 2, max_ops = 3 };
63 
64   // Per-descriptor queues.
65   struct descriptor_state
66   {
descriptor_stateboost::asio::detail::kqueue_reactor::descriptor_state67     descriptor_state(bool locking) : mutex_(locking) {}
68 
69     friend class kqueue_reactor;
70     friend class object_pool_access;
71 
72     descriptor_state* next_;
73     descriptor_state* prev_;
74 
75     mutex mutex_;
76     int descriptor_;
77     int num_kevents_; // 1 == read only, 2 == read and write
78     op_queue<reactor_op> op_queue_[max_ops];
79     bool shutdown_;
80   };
81 
82   // Per-descriptor data.
83   typedef descriptor_state* per_descriptor_data;
84 
85   // Constructor.
86   BOOST_ASIO_DECL kqueue_reactor(boost::asio::execution_context& ctx);
87 
88   // Destructor.
89   BOOST_ASIO_DECL ~kqueue_reactor();
90 
91   // Destroy all user-defined handler objects owned by the service.
92   BOOST_ASIO_DECL void shutdown();
93 
94   // Recreate internal descriptors following a fork.
95   BOOST_ASIO_DECL void notify_fork(
96       boost::asio::execution_context::fork_event fork_ev);
97 
98   // Initialise the task.
99   BOOST_ASIO_DECL void init_task();
100 
101   // Register a socket with the reactor. Returns 0 on success, system error
102   // code on failure.
103   BOOST_ASIO_DECL int register_descriptor(socket_type descriptor,
104       per_descriptor_data& descriptor_data);
105 
106   // Register a descriptor with an associated single operation. Returns 0 on
107   // success, system error code on failure.
108   BOOST_ASIO_DECL int register_internal_descriptor(
109       int op_type, socket_type descriptor,
110       per_descriptor_data& descriptor_data, reactor_op* op);
111 
112   // Move descriptor registration from one descriptor_data object to another.
113   BOOST_ASIO_DECL void move_descriptor(socket_type descriptor,
114       per_descriptor_data& target_descriptor_data,
115       per_descriptor_data& source_descriptor_data);
116 
117   // Post a reactor operation for immediate completion.
post_immediate_completion(reactor_op * op,bool is_continuation)118   void post_immediate_completion(reactor_op* op, bool is_continuation)
119   {
120     scheduler_.post_immediate_completion(op, is_continuation);
121   }
122 
123   // Start a new operation. The reactor operation will be performed when the
124   // given descriptor is flagged as ready, or an error has occurred.
125   BOOST_ASIO_DECL void start_op(int op_type, socket_type descriptor,
126       per_descriptor_data& descriptor_data, reactor_op* op,
127       bool is_continuation, bool allow_speculative);
128 
129   // Cancel all operations associated with the given descriptor. The
130   // handlers associated with the descriptor will be invoked with the
131   // operation_aborted error.
132   BOOST_ASIO_DECL void cancel_ops(socket_type descriptor,
133       per_descriptor_data& descriptor_data);
134 
135   // Cancel any operations that are running against the descriptor and remove
136   // its registration from the reactor. The reactor resources associated with
137   // the descriptor must be released by calling cleanup_descriptor_data.
138   BOOST_ASIO_DECL void deregister_descriptor(socket_type descriptor,
139       per_descriptor_data& descriptor_data, bool closing);
140 
141   // Remove the descriptor's registration from the reactor. The reactor
142   // resources associated with the descriptor must be released by calling
143   // cleanup_descriptor_data.
144   BOOST_ASIO_DECL void deregister_internal_descriptor(
145       socket_type descriptor, per_descriptor_data& descriptor_data);
146 
147   // Perform any post-deregistration cleanup tasks associated with the
148   // descriptor data.
149   BOOST_ASIO_DECL void cleanup_descriptor_data(
150       per_descriptor_data& descriptor_data);
151 
152   // Add a new timer queue to the reactor.
153   template <typename Time_Traits>
154   void add_timer_queue(timer_queue<Time_Traits>& queue);
155 
156   // Remove a timer queue from the reactor.
157   template <typename Time_Traits>
158   void remove_timer_queue(timer_queue<Time_Traits>& queue);
159 
160   // Schedule a new operation in the given timer queue to expire at the
161   // specified absolute time.
162   template <typename Time_Traits>
163   void schedule_timer(timer_queue<Time_Traits>& queue,
164       const typename Time_Traits::time_type& time,
165       typename timer_queue<Time_Traits>::per_timer_data& timer, wait_op* op);
166 
167   // Cancel the timer operations associated with the given token. Returns the
168   // number of operations that have been posted or dispatched.
169   template <typename Time_Traits>
170   std::size_t cancel_timer(timer_queue<Time_Traits>& queue,
171       typename timer_queue<Time_Traits>::per_timer_data& timer,
172       std::size_t max_cancelled = (std::numeric_limits<std::size_t>::max)());
173 
174   // Move the timer operations associated with the given timer.
175   template <typename Time_Traits>
176   void move_timer(timer_queue<Time_Traits>& queue,
177       typename timer_queue<Time_Traits>::per_timer_data& target,
178       typename timer_queue<Time_Traits>::per_timer_data& source);
179 
180   // Run the kqueue loop.
181   BOOST_ASIO_DECL void run(long usec, op_queue<operation>& ops);
182 
183   // Interrupt the kqueue loop.
184   BOOST_ASIO_DECL void interrupt();
185 
186 private:
187   // Create the kqueue file descriptor. Throws an exception if the descriptor
188   // cannot be created.
189   BOOST_ASIO_DECL static int do_kqueue_create();
190 
191   // Allocate a new descriptor state object.
192   BOOST_ASIO_DECL descriptor_state* allocate_descriptor_state();
193 
194   // Free an existing descriptor state object.
195   BOOST_ASIO_DECL void free_descriptor_state(descriptor_state* s);
196 
197   // Helper function to add a new timer queue.
198   BOOST_ASIO_DECL void do_add_timer_queue(timer_queue_base& queue);
199 
200   // Helper function to remove a timer queue.
201   BOOST_ASIO_DECL void do_remove_timer_queue(timer_queue_base& queue);
202 
203   // Get the timeout value for the kevent call.
204   BOOST_ASIO_DECL timespec* get_timeout(long usec, timespec& ts);
205 
206   // The scheduler used to post completions.
207   scheduler& scheduler_;
208 
209   // Mutex to protect access to internal data.
210   mutex mutex_;
211 
212   // The kqueue file descriptor.
213   int kqueue_fd_;
214 
215   // The interrupter is used to break a blocking kevent call.
216   select_interrupter interrupter_;
217 
218   // The timer queues.
219   timer_queue_set timer_queues_;
220 
221   // Whether the service has been shut down.
222   bool shutdown_;
223 
224   // Mutex to protect access to the registered descriptors.
225   mutex registered_descriptors_mutex_;
226 
227   // Keep track of all registered descriptors.
228   object_pool<descriptor_state> registered_descriptors_;
229 };
230 
231 } // namespace detail
232 } // namespace asio
233 } // namespace boost
234 
235 #include <boost/asio/detail/pop_options.hpp>
236 
237 #include <boost/asio/detail/impl/kqueue_reactor.hpp>
238 #if defined(BOOST_ASIO_HEADER_ONLY)
239 # include <boost/asio/detail/impl/kqueue_reactor.ipp>
240 #endif // defined(BOOST_ASIO_HEADER_ONLY)
241 
242 #endif // defined(BOOST_ASIO_HAS_KQUEUE)
243 
244 #endif // BOOST_ASIO_DETAIL_KQUEUE_REACTOR_HPP
245