Chris@16
|
1 //
|
Chris@16
|
2 // detail/signal_handler.hpp
|
Chris@16
|
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~
|
Chris@16
|
4 //
|
Chris@101
|
5 // Copyright (c) 2003-2015 Christopher M. Kohlhoff (chris at kohlhoff dot com)
|
Chris@16
|
6 //
|
Chris@16
|
7 // Distributed under the Boost Software License, Version 1.0. (See accompanying
|
Chris@16
|
8 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
9 //
|
Chris@16
|
10
|
Chris@16
|
11 #ifndef BOOST_ASIO_DETAIL_SIGNAL_HANDLER_HPP
|
Chris@16
|
12 #define BOOST_ASIO_DETAIL_SIGNAL_HANDLER_HPP
|
Chris@16
|
13
|
Chris@16
|
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
|
Chris@16
|
15 # pragma once
|
Chris@16
|
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
|
Chris@16
|
17
|
Chris@16
|
18 #include <boost/asio/detail/config.hpp>
|
Chris@16
|
19 #include <boost/asio/detail/addressof.hpp>
|
Chris@16
|
20 #include <boost/asio/detail/fenced_block.hpp>
|
Chris@16
|
21 #include <boost/asio/detail/handler_alloc_helpers.hpp>
|
Chris@16
|
22 #include <boost/asio/detail/handler_invoke_helpers.hpp>
|
Chris@16
|
23 #include <boost/asio/detail/signal_op.hpp>
|
Chris@16
|
24
|
Chris@16
|
25 #include <boost/asio/detail/push_options.hpp>
|
Chris@16
|
26
|
Chris@16
|
27 namespace boost {
|
Chris@16
|
28 namespace asio {
|
Chris@16
|
29 namespace detail {
|
Chris@16
|
30
|
Chris@16
|
31 template <typename Handler>
|
Chris@16
|
32 class signal_handler : public signal_op
|
Chris@16
|
33 {
|
Chris@16
|
34 public:
|
Chris@16
|
35 BOOST_ASIO_DEFINE_HANDLER_PTR(signal_handler);
|
Chris@16
|
36
|
Chris@16
|
37 signal_handler(Handler& h)
|
Chris@16
|
38 : signal_op(&signal_handler::do_complete),
|
Chris@16
|
39 handler_(BOOST_ASIO_MOVE_CAST(Handler)(h))
|
Chris@16
|
40 {
|
Chris@16
|
41 }
|
Chris@16
|
42
|
Chris@16
|
43 static void do_complete(io_service_impl* owner, operation* base,
|
Chris@16
|
44 const boost::system::error_code& /*ec*/,
|
Chris@16
|
45 std::size_t /*bytes_transferred*/)
|
Chris@16
|
46 {
|
Chris@16
|
47 // Take ownership of the handler object.
|
Chris@16
|
48 signal_handler* h(static_cast<signal_handler*>(base));
|
Chris@16
|
49 ptr p = { boost::asio::detail::addressof(h->handler_), h, h };
|
Chris@16
|
50
|
Chris@16
|
51 BOOST_ASIO_HANDLER_COMPLETION((h));
|
Chris@16
|
52
|
Chris@16
|
53 // Make a copy of the handler so that the memory can be deallocated before
|
Chris@16
|
54 // the upcall is made. Even if we're not about to make an upcall, a
|
Chris@16
|
55 // sub-object of the handler may be the true owner of the memory associated
|
Chris@16
|
56 // with the handler. Consequently, a local copy of the handler is required
|
Chris@16
|
57 // to ensure that any owning sub-object remains valid until after we have
|
Chris@16
|
58 // deallocated the memory here.
|
Chris@16
|
59 detail::binder2<Handler, boost::system::error_code, int>
|
Chris@16
|
60 handler(h->handler_, h->ec_, h->signal_number_);
|
Chris@16
|
61 p.h = boost::asio::detail::addressof(handler.handler_);
|
Chris@16
|
62 p.reset();
|
Chris@16
|
63
|
Chris@16
|
64 // Make the upcall if required.
|
Chris@16
|
65 if (owner)
|
Chris@16
|
66 {
|
Chris@16
|
67 fenced_block b(fenced_block::half);
|
Chris@16
|
68 BOOST_ASIO_HANDLER_INVOCATION_BEGIN((handler.arg1_, handler.arg2_));
|
Chris@16
|
69 boost_asio_handler_invoke_helpers::invoke(handler, handler.handler_);
|
Chris@16
|
70 BOOST_ASIO_HANDLER_INVOCATION_END;
|
Chris@16
|
71 }
|
Chris@16
|
72 }
|
Chris@16
|
73
|
Chris@16
|
74 private:
|
Chris@16
|
75 Handler handler_;
|
Chris@16
|
76 };
|
Chris@16
|
77
|
Chris@16
|
78 } // namespace detail
|
Chris@16
|
79 } // namespace asio
|
Chris@16
|
80 } // namespace boost
|
Chris@16
|
81
|
Chris@16
|
82 #include <boost/asio/detail/pop_options.hpp>
|
Chris@16
|
83
|
Chris@16
|
84 #endif // BOOST_ASIO_DETAIL_SIGNAL_HANDLER_HPP
|