Realistic 3D camera system
3D camera system components
kqueue_reactor.hpp
Go to the documentation of this file.
1 //
2 // detail/kqueue_reactor.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2015 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 ASIO_DETAIL_KQUEUE_REACTOR_HPP
13 #define 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 "asio/detail/config.hpp"
20 
21 #if defined(ASIO_HAS_KQUEUE)
22 
23 #include <cstddef>
24 #include <sys/types.h>
25 #include <sys/event.h>
26 #include <sys/time.h>
27 #include "asio/detail/limits.hpp"
28 #include "asio/detail/mutex.hpp"
30 #include "asio/detail/op_queue.hpp"
36 #include "asio/detail/wait_op.hpp"
37 #include "asio/error.hpp"
38 #include "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 
46 
47 namespace asio {
48 namespace detail {
49 
50 class kqueue_reactor
51  : public asio::detail::service_base<kqueue_reactor>
52 {
53 public:
54  enum op_types { read_op = 0, write_op = 1,
55  connect_op = 1, except_op = 2, max_ops = 3 };
56 
57  // Per-descriptor queues.
58  struct descriptor_state
59  {
60  friend class kqueue_reactor;
61  friend class object_pool_access;
62 
63  descriptor_state* next_;
64  descriptor_state* prev_;
65 
66  mutex mutex_;
67  int descriptor_;
68  int num_kevents_; // 1 == read only, 2 == read and write
69  op_queue<reactor_op> op_queue_[max_ops];
70  bool shutdown_;
71  };
72 
73  // Per-descriptor data.
74  typedef descriptor_state* per_descriptor_data;
75 
76  // Constructor.
77  ASIO_DECL kqueue_reactor(asio::io_service& io_service);
78 
79  // Destructor.
80  ASIO_DECL ~kqueue_reactor();
81 
82  // Destroy all user-defined handler objects owned by the service.
83  ASIO_DECL void shutdown_service();
84 
85  // Recreate internal descriptors following a fork.
86  ASIO_DECL void fork_service(
88 
89  // Initialise the task.
90  ASIO_DECL void init_task();
91 
92  // Register a socket with the reactor. Returns 0 on success, system error
93  // code on failure.
94  ASIO_DECL int register_descriptor(socket_type descriptor,
95  per_descriptor_data& descriptor_data);
96 
97  // Register a descriptor with an associated single operation. Returns 0 on
98  // success, system error code on failure.
99  ASIO_DECL int register_internal_descriptor(
100  int op_type, socket_type descriptor,
101  per_descriptor_data& descriptor_data, reactor_op* op);
102 
103  // Move descriptor registration from one descriptor_data object to another.
104  ASIO_DECL void move_descriptor(socket_type descriptor,
105  per_descriptor_data& target_descriptor_data,
106  per_descriptor_data& source_descriptor_data);
107 
108  // Post a reactor operation for immediate completion.
109  void post_immediate_completion(reactor_op* op, bool is_continuation)
110  {
111  io_service_.post_immediate_completion(op, is_continuation);
112  }
113 
114  // Start a new operation. The reactor operation will be performed when the
115  // given descriptor is flagged as ready, or an error has occurred.
116  ASIO_DECL void start_op(int op_type, socket_type descriptor,
117  per_descriptor_data& descriptor_data, reactor_op* op,
118  bool is_continuation, bool allow_speculative);
119 
120  // Cancel all operations associated with the given descriptor. The
121  // handlers associated with the descriptor will be invoked with the
122  // operation_aborted error.
123  ASIO_DECL void cancel_ops(socket_type descriptor,
124  per_descriptor_data& descriptor_data);
125 
126  // Cancel any operations that are running against the descriptor and remove
127  // its registration from the reactor.
128  ASIO_DECL void deregister_descriptor(socket_type descriptor,
129  per_descriptor_data& descriptor_data, bool closing);
130 
131  // Remote the descriptor's registration from the reactor.
132  ASIO_DECL void deregister_internal_descriptor(
133  socket_type descriptor, per_descriptor_data& descriptor_data);
134 
135  // Add a new timer queue to the reactor.
136  template <typename Time_Traits>
137  void add_timer_queue(timer_queue<Time_Traits>& queue);
138 
139  // Remove a timer queue from the reactor.
140  template <typename Time_Traits>
141  void remove_timer_queue(timer_queue<Time_Traits>& queue);
142 
143  // Schedule a new operation in the given timer queue to expire at the
144  // specified absolute time.
145  template <typename Time_Traits>
146  void schedule_timer(timer_queue<Time_Traits>& queue,
147  const typename Time_Traits::time_type& time,
148  typename timer_queue<Time_Traits>::per_timer_data& timer, wait_op* op);
149 
150  // Cancel the timer operations associated with the given token. Returns the
151  // number of operations that have been posted or dispatched.
152  template <typename Time_Traits>
153  std::size_t cancel_timer(timer_queue<Time_Traits>& queue,
154  typename timer_queue<Time_Traits>::per_timer_data& timer,
155  std::size_t max_cancelled = (std::numeric_limits<std::size_t>::max)());
156 
157  // Run the kqueue loop.
158  ASIO_DECL void run(bool block, op_queue<operation>& ops);
159 
160  // Interrupt the kqueue loop.
161  ASIO_DECL void interrupt();
162 
163 private:
164  // Create the kqueue file descriptor. Throws an exception if the descriptor
165  // cannot be created.
166  ASIO_DECL static int do_kqueue_create();
167 
168  // Allocate a new descriptor state object.
169  ASIO_DECL descriptor_state* allocate_descriptor_state();
170 
171  // Free an existing descriptor state object.
172  ASIO_DECL void free_descriptor_state(descriptor_state* s);
173 
174  // Helper function to add a new timer queue.
175  ASIO_DECL void do_add_timer_queue(timer_queue_base& queue);
176 
177  // Helper function to remove a timer queue.
178  ASIO_DECL void do_remove_timer_queue(timer_queue_base& queue);
179 
180  // Get the timeout value for the kevent call.
181  ASIO_DECL timespec* get_timeout(timespec& ts);
182 
183  // The io_service implementation used to post completions.
184  io_service_impl& io_service_;
185 
186  // Mutex to protect access to internal data.
187  mutex mutex_;
188 
189  // The kqueue file descriptor.
190  int kqueue_fd_;
191 
192  // The interrupter is used to break a blocking kevent call.
193  select_interrupter interrupter_;
194 
195  // The timer queues.
196  timer_queue_set timer_queues_;
197 
198  // Whether the service has been shut down.
199  bool shutdown_;
200 
201  // Mutex to protect access to the registered descriptors.
202  mutex registered_descriptors_mutex_;
203 
204  // Keep track of all registered descriptors.
205  object_pool<descriptor_state> registered_descriptors_;
206 };
207 
208 } // namespace detail
209 } // namespace asio
210 
212 
214 #if defined(ASIO_HEADER_ONLY)
216 #endif // defined(ASIO_HEADER_ONLY)
217 
218 #endif // defined(ASIO_HAS_KQUEUE)
219 
220 #endif // ASIO_DETAIL_KQUEUE_REACTOR_HPP
Provides core I/O functionality.
Definition: io_service.hpp:184
SocketService & s
Definition: connect.hpp:521
null_mutex mutex
Definition: mutex.hpp:36
class task_io_service io_service_impl
Definition: io_service.hpp:48
pipe_select_interrupter select_interrupter
fork_event
Fork-related event notifications.
Definition: io_service.hpp:501
bool is_continuation(Context &context)
#define ASIO_DECL
Definition: config.hpp:43