PageRenderTime 12ms CodeModel.GetById 1ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/Src/Dependencies/Boost/boost/asio/detail/signal_set_service.hpp

http://hadesmem.googlecode.com/
C++ Header | 213 lines | 117 code | 50 blank | 46 comment | 4 complexity | 3b08e401bdd46031a666d9c56ebd6ac2 MD5 | raw file
  1//
  2// detail/signal_set_service.hpp
  3// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4//
  5// Copyright (c) 2003-2011 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 BOOST_ASIO_DETAIL_SIGNAL_SET_SERVICE_HPP
 12#define BOOST_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 <boost/asio/detail/config.hpp>
 19
 20#include <csignal>
 21#include <cstddef>
 22#include <boost/asio/error.hpp>
 23#include <boost/asio/io_service.hpp>
 24#include <boost/asio/detail/handler_alloc_helpers.hpp>
 25#include <boost/asio/detail/op_queue.hpp>
 26#include <boost/asio/detail/signal_handler.hpp>
 27#include <boost/asio/detail/signal_op.hpp>
 28#include <boost/asio/detail/socket_types.hpp>
 29
 30#if !defined(BOOST_WINDOWS) && !defined(__CYGWIN__)
 31# include <boost/asio/detail/reactor.hpp>
 32#endif // !defined(BOOST_WINDOWS) && !defined(__CYGWIN__)
 33
 34#include <boost/asio/detail/push_options.hpp>
 35
 36namespace boost {
 37namespace asio {
 38namespace detail {
 39
 40#if defined(NSIG) && (NSIG > 0)
 41enum { max_signal_number = NSIG };
 42#else
 43enum { max_signal_number = 128 };
 44#endif
 45
 46extern BOOST_ASIO_DECL struct signal_state* get_signal_state();
 47
 48extern "C" BOOST_ASIO_DECL void asio_signal_handler(int signal_number);
 49
 50class signal_set_service
 51{
 52public:
 53  // Type used for tracking an individual signal registration.
 54  class registration
 55  {
 56  public:
 57    // Default constructor.
 58    registration()
 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.
 90  class implementation_type
 91  {
 92  public:
 93    // Default constructor.
 94    implementation_type()
 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.
111  BOOST_ASIO_DECL signal_set_service(boost::asio::io_service& io_service);
112
113  // Destructor.
114  BOOST_ASIO_DECL ~signal_set_service();
115
116  // Destroy all user-defined handler objects owned by the service.
117  BOOST_ASIO_DECL void shutdown_service();
118
119  // Perform fork-related housekeeping.
120  BOOST_ASIO_DECL void fork_service(
121      boost::asio::io_service::fork_event fork_ev);
122
123  // Construct a new signal_set implementation.
124  BOOST_ASIO_DECL void construct(implementation_type& impl);
125
126  // Destroy a signal_set implementation.
127  BOOST_ASIO_DECL void destroy(implementation_type& impl);
128
129  // Add a signal to a signal_set.
130  BOOST_ASIO_DECL boost::system::error_code add(implementation_type& impl,
131      int signal_number, boost::system::error_code& ec);
132
133  // Remove a signal to a signal_set.
134  BOOST_ASIO_DECL boost::system::error_code remove(implementation_type& impl,
135      int signal_number, boost::system::error_code& ec);
136
137  // Remove all signals from a signal_set.
138  BOOST_ASIO_DECL boost::system::error_code clear(implementation_type& impl,
139      boost::system::error_code& ec);
140
141  // Cancel all operations associated with the signal set.
142  BOOST_ASIO_DECL boost::system::error_code cancel(implementation_type& impl,
143      boost::system::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 = { boost::addressof(handler),
152      boost_asio_handler_alloc_helpers::allocate(
153        sizeof(op), handler), 0 };
154    p.p = new (p.v) op(handler);
155
156    BOOST_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  BOOST_ASIO_DECL static void deliver_signal(int signal_number);
164
165private:
166  // Helper function to add a service to the global signal state.
167  BOOST_ASIO_DECL static void add_service(signal_set_service* service);
168
169  // Helper function to remove a service from the global signal state.
170  BOOST_ASIO_DECL static void remove_service(signal_set_service* service);
171
172  // Helper function to create the pipe descriptors.
173  BOOST_ASIO_DECL static void open_descriptors();
174
175  // Helper function to close the pipe descriptors.
176  BOOST_ASIO_DECL static void close_descriptors();
177
178  // Helper function to start a wait operation.
179  BOOST_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(BOOST_WINDOWS) && !defined(__CYGWIN__)
185  // The type used for registering for pipe reactor notifications.
186  class pipe_read_op;
187
188  // The reactor used for waiting for pipe readiness.
189  reactor& reactor_;
190
191  // The per-descriptor reactor data used for the pipe.
192  reactor::per_descriptor_data reactor_data_;
193#endif // !defined(BOOST_WINDOWS) && !defined(__CYGWIN__)
194
195  // A mapping from signal number to the registered signal sets.
196  registration* registrations_[max_signal_number];
197
198  // Pointers to adjacent services in linked list.
199  signal_set_service* next_;
200  signal_set_service* prev_;
201};
202
203} // namespace detail
204} // namespace asio
205} // namespace boost
206
207#include <boost/asio/detail/pop_options.hpp>
208
209#if defined(BOOST_ASIO_HEADER_ONLY)
210# include <boost/asio/detail/impl/signal_set_service.ipp>
211#endif // defined(BOOST_ASIO_HEADER_ONLY)
212
213#endif // BOOST_ASIO_DETAIL_SIGNAL_SET_SERVICE_HPP