Realistic 3D camera system
3D camera system components
signal_set_service.hpp
Go to the documentation of this file.
1 //
2 // detail/signal_set_service.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2015 Christopher M. Kohlhoff (chris at kohlhoff dot com)
6 //
7 // Distributed under the Boost Software License, Version 1.0. (See accompanying
8 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
9 //
10 
11 #ifndef ASIO_DETAIL_SIGNAL_SET_SERVICE_HPP
12 #define ASIO_DETAIL_SIGNAL_SET_SERVICE_HPP
13 
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
15 # pragma once
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
17 
18 #include "asio/detail/config.hpp"
19 
20 #include <cstddef>
21 #include <signal.h>
22 #include "asio/error.hpp"
23 #include "asio/io_service.hpp"
26 #include "asio/detail/op_queue.hpp"
30 
31 #if !defined(ASIO_WINDOWS) && !defined(__CYGWIN__)
32 # include "asio/detail/reactor.hpp"
33 #endif // !defined(ASIO_WINDOWS) && !defined(__CYGWIN__)
34 
36 
37 namespace asio {
38 namespace detail {
39 
40 #if defined(NSIG) && (NSIG > 0)
41 enum { max_signal_number = NSIG };
42 #else
43 enum { max_signal_number = 128 };
44 #endif
45 
46 extern ASIO_DECL struct signal_state* get_signal_state();
47 
48 extern "C" ASIO_DECL void asio_signal_handler(int signal_number);
49 
51 {
52 public:
53  // Type used for tracking an individual signal registration.
55  {
56  public:
57  // Default constructor.
59  : signal_number_(0),
60  queue_(0),
61  undelivered_(0),
62  next_in_table_(0),
63  prev_in_table_(0),
64  next_in_set_(0)
65  {
66  }
67 
68  private:
69  // Only this service will have access to the internal values.
70  friend class signal_set_service;
71 
72  // The signal number that is registered.
73  int signal_number_;
74 
75  // The waiting signal handlers.
76  op_queue<signal_op>* queue_;
77 
78  // The number of undelivered signals.
79  std::size_t undelivered_;
80 
81  // Pointers to adjacent registrations in the registrations_ table.
82  registration* next_in_table_;
83  registration* prev_in_table_;
84 
85  // Link to next registration in the signal set.
86  registration* next_in_set_;
87  };
88 
89  // The implementation type of the signal_set.
91  {
92  public:
93  // Default constructor.
95  : signals_(0)
96  {
97  }
98 
99  private:
100  // Only this service will have access to the internal values.
101  friend class signal_set_service;
102 
103  // The pending signal handlers.
104  op_queue<signal_op> queue_;
105 
106  // Linked list of registered signals.
107  registration* signals_;
108  };
109 
110  // Constructor.
112 
113  // Destructor.
115 
116  // Destroy all user-defined handler objects owned by the service.
118 
119  // Perform fork-related housekeeping.
120  ASIO_DECL void fork_service(
122 
123  // Construct a new signal_set implementation.
125 
126  // Destroy a signal_set implementation.
128 
129  // Add a signal to a signal_set.
131  int signal_number, asio::error_code& ec);
132 
133  // Remove a signal to a signal_set.
135  int signal_number, asio::error_code& ec);
136 
137  // Remove all signals from a signal_set.
139  asio::error_code& ec);
140 
141  // Cancel all operations associated with the signal set.
143  asio::error_code& ec);
144 
145  // Start an asynchronous operation to wait for a signal to be delivered.
146  template <typename Handler>
147  void async_wait(implementation_type& impl, Handler& handler)
148  {
149  // Allocate and construct an operation to wrap the handler.
150  typedef signal_handler<Handler> op;
151  typename op::ptr p = { asio::detail::addressof(handler),
153  sizeof(op), handler), 0 };
154  p.p = new (p.v) op(handler);
155 
156  ASIO_HANDLER_CREATION((p.p, "signal_set", &impl, "async_wait"));
157 
158  start_wait_op(impl, p.p);
159  p.v = p.p = 0;
160  }
161 
162  // Deliver notification that a particular signal occurred.
163  ASIO_DECL static void deliver_signal(int signal_number);
164 
165 private:
166  // Helper function to add a service to the global signal state.
167  ASIO_DECL static void add_service(signal_set_service* service);
168 
169  // Helper function to remove a service from the global signal state.
170  ASIO_DECL static void remove_service(signal_set_service* service);
171 
172  // Helper function to create the pipe descriptors.
173  ASIO_DECL static void open_descriptors();
174 
175  // Helper function to close the pipe descriptors.
176  ASIO_DECL static void close_descriptors();
177 
178  // Helper function to start a wait operation.
179  ASIO_DECL void start_wait_op(implementation_type& impl, signal_op* op);
180 
181  // The io_service instance used for dispatching handlers.
182  io_service_impl& io_service_;
183 
184 #if !defined(ASIO_WINDOWS) \
185  && !defined(ASIO_WINDOWS_RUNTIME) \
186  && !defined(__CYGWIN__)
187  // The type used for registering for pipe reactor notifications.
188  class pipe_read_op;
189 
190  // The reactor used for waiting for pipe readiness.
191  reactor& reactor_;
192 
193  // The per-descriptor reactor data used for the pipe.
194  reactor::per_descriptor_data reactor_data_;
195 #endif // !defined(ASIO_WINDOWS)
196  // && !defined(ASIO_WINDOWS_RUNTIME)
197  // && !defined(__CYGWIN__)
198 
199  // A mapping from signal number to the registered signal sets.
200  registration* registrations_[max_signal_number];
201 
202  // Pointers to adjacent services in linked list.
203  signal_set_service* next_;
204  signal_set_service* prev_;
205 };
206 
207 } // namespace detail
208 } // namespace asio
209 
211 
212 #if defined(ASIO_HEADER_ONLY)
214 #endif // defined(ASIO_HEADER_ONLY)
215 
216 #endif // ASIO_DETAIL_SIGNAL_SET_SERVICE_HPP
signal_state * get_signal_state()
Provides core I/O functionality.
Definition: io_service.hpp:184
static ASIO_DECL void deliver_signal(int signal_number)
ASIO_DECL asio::error_code add(implementation_type &impl, int signal_number, asio::error_code &ec)
class task_io_service io_service_impl
Definition: io_service.hpp:48
void add_service(io_service &ios, Service *svc)
Definition: io_service.hpp:43
class select_reactor reactor
Definition: reactor_fwd.hpp:34
fork_event
Fork-related event notifications.
Definition: io_service.hpp:501
ASIO_DECL void destroy(implementation_type &impl)
Class to represent an error code value.
Definition: error_code.hpp:80
#define ASIO_DECL
Definition: config.hpp:43
ASIO_DECL void fork_service(asio::io_service::fork_event fork_ev)
ASIO_DECL asio::error_code clear(implementation_type &impl, asio::error_code &ec)
void async_wait(implementation_type &impl, Handler &handler)
void * allocate(std::size_t s, Handler &h)
void asio_signal_handler(int signal_number)
ASIO_DECL asio::error_code cancel(implementation_type &impl, asio::error_code &ec)
#define ASIO_HANDLER_CREATION(args)
ASIO_DECL void construct(implementation_type &impl)