signal_set_service.hpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. //
  2. // signal_set_service.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2018 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. #ifndef BOOST_ASIO_SIGNAL_SET_SERVICE_HPP
  11. #define BOOST_ASIO_SIGNAL_SET_SERVICE_HPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include <boost/asio/detail/config.hpp>
  16. #if defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
  17. #include <boost/asio/async_result.hpp>
  18. #include <boost/asio/detail/signal_set_service.hpp>
  19. #include <boost/asio/error.hpp>
  20. #include <boost/asio/io_context.hpp>
  21. #include <boost/asio/detail/push_options.hpp>
  22. namespace boost {
  23. namespace asio {
  24. /// Default service implementation for a signal set.
  25. class signal_set_service
  26. #if defined(GENERATING_DOCUMENTATION)
  27. : public boost::asio::io_context::service
  28. #else
  29. : public boost::asio::detail::service_base<signal_set_service>
  30. #endif
  31. {
  32. public:
  33. #if defined(GENERATING_DOCUMENTATION)
  34. /// The unique service identifier.
  35. static boost::asio::io_context::id id;
  36. #endif
  37. public:
  38. /// The type of a signal set implementation.
  39. #if defined(GENERATING_DOCUMENTATION)
  40. typedef implementation_defined implementation_type;
  41. #else
  42. typedef detail::signal_set_service::implementation_type implementation_type;
  43. #endif
  44. /// Construct a new signal set service for the specified io_context.
  45. explicit signal_set_service(boost::asio::io_context& io_context)
  46. : boost::asio::detail::service_base<signal_set_service>(io_context),
  47. service_impl_(io_context)
  48. {
  49. }
  50. /// Construct a new signal set implementation.
  51. void construct(implementation_type& impl)
  52. {
  53. service_impl_.construct(impl);
  54. }
  55. /// Destroy a signal set implementation.
  56. void destroy(implementation_type& impl)
  57. {
  58. service_impl_.destroy(impl);
  59. }
  60. /// Add a signal to a signal_set.
  61. BOOST_ASIO_SYNC_OP_VOID add(implementation_type& impl,
  62. int signal_number, boost::system::error_code& ec)
  63. {
  64. service_impl_.add(impl, signal_number, ec);
  65. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  66. }
  67. /// Remove a signal to a signal_set.
  68. BOOST_ASIO_SYNC_OP_VOID remove(implementation_type& impl,
  69. int signal_number, boost::system::error_code& ec)
  70. {
  71. service_impl_.remove(impl, signal_number, ec);
  72. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  73. }
  74. /// Remove all signals from a signal_set.
  75. BOOST_ASIO_SYNC_OP_VOID clear(implementation_type& impl,
  76. boost::system::error_code& ec)
  77. {
  78. service_impl_.clear(impl, ec);
  79. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  80. }
  81. /// Cancel all operations associated with the signal set.
  82. BOOST_ASIO_SYNC_OP_VOID cancel(implementation_type& impl,
  83. boost::system::error_code& ec)
  84. {
  85. service_impl_.cancel(impl, ec);
  86. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  87. }
  88. // Start an asynchronous operation to wait for a signal to be delivered.
  89. template <typename SignalHandler>
  90. BOOST_ASIO_INITFN_RESULT_TYPE(SignalHandler,
  91. void (boost::system::error_code, int))
  92. async_wait(implementation_type& impl,
  93. BOOST_ASIO_MOVE_ARG(SignalHandler) handler)
  94. {
  95. async_completion<SignalHandler,
  96. void (boost::system::error_code, int)> init(handler);
  97. service_impl_.async_wait(impl, init.completion_handler);
  98. return init.result.get();
  99. }
  100. private:
  101. // Destroy all user-defined handler objects owned by the service.
  102. void shutdown()
  103. {
  104. service_impl_.shutdown();
  105. }
  106. // Perform any fork-related housekeeping.
  107. void notify_fork(boost::asio::io_context::fork_event event)
  108. {
  109. service_impl_.notify_fork(event);
  110. }
  111. // The platform-specific implementation.
  112. detail::signal_set_service service_impl_;
  113. };
  114. } // namespace asio
  115. } // namespace boost
  116. #include <boost/asio/detail/pop_options.hpp>
  117. #endif // defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
  118. #endif // BOOST_ASIO_SIGNAL_SET_SERVICE_HPP