signal_set_service.hpp 3.6 KB

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