handler_tracking.hpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. //
  2. // detail/handler_tracking.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_DETAIL_HANDLER_TRACKING_HPP
  11. #define BOOST_ASIO_DETAIL_HANDLER_TRACKING_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. namespace boost {
  17. namespace asio {
  18. class execution_context;
  19. } // namespace asio
  20. } // namespace boost
  21. #if defined(BOOST_ASIO_CUSTOM_HANDLER_TRACKING)
  22. # include BOOST_ASIO_CUSTOM_HANDLER_TRACKING
  23. #elif defined(BOOST_ASIO_ENABLE_HANDLER_TRACKING)
  24. # include <boost/system/error_code.hpp>
  25. # include <boost/asio/detail/cstdint.hpp>
  26. # include <boost/asio/detail/static_mutex.hpp>
  27. # include <boost/asio/detail/tss_ptr.hpp>
  28. #endif // defined(BOOST_ASIO_ENABLE_HANDLER_TRACKING)
  29. #include <boost/asio/detail/push_options.hpp>
  30. namespace boost {
  31. namespace asio {
  32. namespace detail {
  33. #if defined(BOOST_ASIO_CUSTOM_HANDLER_TRACKING)
  34. // The user-specified header must define the following macros:
  35. // - BOOST_ASIO_INHERIT_TRACKED_HANDLER
  36. // - BOOST_ASIO_ALSO_INHERIT_TRACKED_HANDLER
  37. // - BOOST_ASIO_HANDLER_TRACKING_INIT
  38. // - BOOST_ASIO_HANDLER_CREATION(args)
  39. // - BOOST_ASIO_HANDLER_COMPLETION(args)
  40. // - BOOST_ASIO_HANDLER_INVOCATION_BEGIN(args)
  41. // - BOOST_ASIO_HANDLER_INVOCATION_END
  42. // - BOOST_ASIO_HANDLER_OPERATION(args)
  43. // - BOOST_ASIO_HANDLER_REACTOR_REGISTRATION(args)
  44. // - BOOST_ASIO_HANDLER_REACTOR_DEREGISTRATION(args)
  45. // - BOOST_ASIO_HANDLER_REACTOR_READ_EVENT
  46. // - BOOST_ASIO_HANDLER_REACTOR_WRITE_EVENT
  47. // - BOOST_ASIO_HANDLER_REACTOR_ERROR_EVENT
  48. // - BOOST_ASIO_HANDLER_REACTOR_EVENTS(args)
  49. // - BOOST_ASIO_HANDLER_REACTOR_OPERATION(args)
  50. # if !defined(BOOST_ASIO_ENABLE_HANDLER_TRACKING)
  51. # define BOOST_ASIO_ENABLE_HANDLER_TRACKING 1
  52. # endif /// !defined(BOOST_ASIO_ENABLE_HANDLER_TRACKING)
  53. #elif defined(BOOST_ASIO_ENABLE_HANDLER_TRACKING)
  54. class handler_tracking
  55. {
  56. public:
  57. class completion;
  58. // Base class for objects containing tracked handlers.
  59. class tracked_handler
  60. {
  61. private:
  62. // Only the handler_tracking class will have access to the id.
  63. friend class handler_tracking;
  64. friend class completion;
  65. uint64_t id_;
  66. protected:
  67. // Constructor initialises with no id.
  68. tracked_handler() : id_(0) {}
  69. // Prevent deletion through this type.
  70. ~tracked_handler() {}
  71. };
  72. // Initialise the tracking system.
  73. BOOST_ASIO_DECL static void init();
  74. // Record the creation of a tracked handler.
  75. BOOST_ASIO_DECL static void creation(
  76. execution_context& context, tracked_handler& h,
  77. const char* object_type, void* object,
  78. uintmax_t native_handle, const char* op_name);
  79. class completion
  80. {
  81. public:
  82. // Constructor records that handler is to be invoked with no arguments.
  83. BOOST_ASIO_DECL explicit completion(const tracked_handler& h);
  84. // Destructor records only when an exception is thrown from the handler, or
  85. // if the memory is being freed without the handler having been invoked.
  86. BOOST_ASIO_DECL ~completion();
  87. // Records that handler is to be invoked with no arguments.
  88. BOOST_ASIO_DECL void invocation_begin();
  89. // Records that handler is to be invoked with one arguments.
  90. BOOST_ASIO_DECL void invocation_begin(const boost::system::error_code& ec);
  91. // Constructor records that handler is to be invoked with two arguments.
  92. BOOST_ASIO_DECL void invocation_begin(
  93. const boost::system::error_code& ec, std::size_t bytes_transferred);
  94. // Constructor records that handler is to be invoked with two arguments.
  95. BOOST_ASIO_DECL void invocation_begin(
  96. const boost::system::error_code& ec, int signal_number);
  97. // Constructor records that handler is to be invoked with two arguments.
  98. BOOST_ASIO_DECL void invocation_begin(
  99. const boost::system::error_code& ec, const char* arg);
  100. // Record that handler invocation has ended.
  101. BOOST_ASIO_DECL void invocation_end();
  102. private:
  103. friend class handler_tracking;
  104. uint64_t id_;
  105. bool invoked_;
  106. completion* next_;
  107. };
  108. // Record an operation that is not directly associated with a handler.
  109. BOOST_ASIO_DECL static void operation(execution_context& context,
  110. const char* object_type, void* object,
  111. uintmax_t native_handle, const char* op_name);
  112. // Record that a descriptor has been registered with the reactor.
  113. BOOST_ASIO_DECL static void reactor_registration(execution_context& context,
  114. uintmax_t native_handle, uintmax_t registration);
  115. // Record that a descriptor has been deregistered from the reactor.
  116. BOOST_ASIO_DECL static void reactor_deregistration(execution_context& context,
  117. uintmax_t native_handle, uintmax_t registration);
  118. // Record a reactor-based operation that is associated with a handler.
  119. BOOST_ASIO_DECL static void reactor_events(execution_context& context,
  120. uintmax_t registration, unsigned events);
  121. // Record a reactor-based operation that is associated with a handler.
  122. BOOST_ASIO_DECL static void reactor_operation(
  123. const tracked_handler& h, const char* op_name,
  124. const boost::system::error_code& ec);
  125. // Record a reactor-based operation that is associated with a handler.
  126. BOOST_ASIO_DECL static void reactor_operation(
  127. const tracked_handler& h, const char* op_name,
  128. const boost::system::error_code& ec, std::size_t bytes_transferred);
  129. // Write a line of output.
  130. BOOST_ASIO_DECL static void write_line(const char* format, ...);
  131. private:
  132. struct tracking_state;
  133. BOOST_ASIO_DECL static tracking_state* get_state();
  134. };
  135. # define BOOST_ASIO_INHERIT_TRACKED_HANDLER \
  136. : public boost::asio::detail::handler_tracking::tracked_handler
  137. # define BOOST_ASIO_ALSO_INHERIT_TRACKED_HANDLER \
  138. , public boost::asio::detail::handler_tracking::tracked_handler
  139. # define BOOST_ASIO_HANDLER_TRACKING_INIT \
  140. boost::asio::detail::handler_tracking::init()
  141. # define BOOST_ASIO_HANDLER_CREATION(args) \
  142. boost::asio::detail::handler_tracking::creation args
  143. # define BOOST_ASIO_HANDLER_COMPLETION(args) \
  144. boost::asio::detail::handler_tracking::completion tracked_completion args
  145. # define BOOST_ASIO_HANDLER_INVOCATION_BEGIN(args) \
  146. tracked_completion.invocation_begin args
  147. # define BOOST_ASIO_HANDLER_INVOCATION_END \
  148. tracked_completion.invocation_end()
  149. # define BOOST_ASIO_HANDLER_OPERATION(args) \
  150. boost::asio::detail::handler_tracking::operation args
  151. # define BOOST_ASIO_HANDLER_REACTOR_REGISTRATION(args) \
  152. boost::asio::detail::handler_tracking::reactor_registration args
  153. # define BOOST_ASIO_HANDLER_REACTOR_DEREGISTRATION(args) \
  154. boost::asio::detail::handler_tracking::reactor_deregistration args
  155. # define BOOST_ASIO_HANDLER_REACTOR_READ_EVENT 1
  156. # define BOOST_ASIO_HANDLER_REACTOR_WRITE_EVENT 2
  157. # define BOOST_ASIO_HANDLER_REACTOR_ERROR_EVENT 4
  158. # define BOOST_ASIO_HANDLER_REACTOR_EVENTS(args) \
  159. boost::asio::detail::handler_tracking::reactor_events args
  160. # define BOOST_ASIO_HANDLER_REACTOR_OPERATION(args) \
  161. boost::asio::detail::handler_tracking::reactor_operation args
  162. #else // defined(BOOST_ASIO_ENABLE_HANDLER_TRACKING)
  163. # define BOOST_ASIO_INHERIT_TRACKED_HANDLER
  164. # define BOOST_ASIO_ALSO_INHERIT_TRACKED_HANDLER
  165. # define BOOST_ASIO_HANDLER_TRACKING_INIT (void)0
  166. # define BOOST_ASIO_HANDLER_CREATION(args) (void)0
  167. # define BOOST_ASIO_HANDLER_COMPLETION(args) (void)0
  168. # define BOOST_ASIO_HANDLER_INVOCATION_BEGIN(args) (void)0
  169. # define BOOST_ASIO_HANDLER_INVOCATION_END (void)0
  170. # define BOOST_ASIO_HANDLER_OPERATION(args) (void)0
  171. # define BOOST_ASIO_HANDLER_REACTOR_REGISTRATION(args) (void)0
  172. # define BOOST_ASIO_HANDLER_REACTOR_DEREGISTRATION(args) (void)0
  173. # define BOOST_ASIO_HANDLER_REACTOR_READ_EVENT 0
  174. # define BOOST_ASIO_HANDLER_REACTOR_WRITE_EVENT 0
  175. # define BOOST_ASIO_HANDLER_REACTOR_ERROR_EVENT 0
  176. # define BOOST_ASIO_HANDLER_REACTOR_EVENTS(args) (void)0
  177. # define BOOST_ASIO_HANDLER_REACTOR_OPERATION(args) (void)0
  178. #endif // defined(BOOST_ASIO_ENABLE_HANDLER_TRACKING)
  179. } // namespace detail
  180. } // namespace asio
  181. } // namespace boost
  182. #include <boost/asio/detail/pop_options.hpp>
  183. #if defined(BOOST_ASIO_HEADER_ONLY)
  184. # include <boost/asio/detail/impl/handler_tracking.ipp>
  185. #endif // defined(BOOST_ASIO_HEADER_ONLY)
  186. #endif // BOOST_ASIO_DETAIL_HANDLER_TRACKING_HPP