handler_tracking.hpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. //
  2. // detail/handler_tracking.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2021 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. class location
  75. {
  76. public:
  77. // Constructor adds a location to the stack.
  78. BOOST_ASIO_DECL explicit location(const char* file,
  79. int line, const char* func);
  80. // Destructor removes a location from the stack.
  81. BOOST_ASIO_DECL ~location();
  82. private:
  83. // Disallow copying and assignment.
  84. location(const location&) BOOST_ASIO_DELETED;
  85. location& operator=(const location&) BOOST_ASIO_DELETED;
  86. friend class handler_tracking;
  87. const char* file_;
  88. int line_;
  89. const char* func_;
  90. location* next_;
  91. };
  92. // Record the creation of a tracked handler.
  93. BOOST_ASIO_DECL static void creation(
  94. execution_context& context, tracked_handler& h,
  95. const char* object_type, void* object,
  96. uintmax_t native_handle, const char* op_name);
  97. class completion
  98. {
  99. public:
  100. // Constructor records that handler is to be invoked with no arguments.
  101. BOOST_ASIO_DECL explicit completion(const tracked_handler& h);
  102. // Destructor records only when an exception is thrown from the handler, or
  103. // if the memory is being freed without the handler having been invoked.
  104. BOOST_ASIO_DECL ~completion();
  105. // Records that handler is to be invoked with no arguments.
  106. BOOST_ASIO_DECL void invocation_begin();
  107. // Records that handler is to be invoked with one arguments.
  108. BOOST_ASIO_DECL void invocation_begin(const boost::system::error_code& ec);
  109. // Constructor records that handler is to be invoked with two arguments.
  110. BOOST_ASIO_DECL void invocation_begin(
  111. const boost::system::error_code& ec, std::size_t bytes_transferred);
  112. // Constructor records that handler is to be invoked with two arguments.
  113. BOOST_ASIO_DECL void invocation_begin(
  114. const boost::system::error_code& ec, int signal_number);
  115. // Constructor records that handler is to be invoked with two arguments.
  116. BOOST_ASIO_DECL void invocation_begin(
  117. const boost::system::error_code& ec, const char* arg);
  118. // Record that handler invocation has ended.
  119. BOOST_ASIO_DECL void invocation_end();
  120. private:
  121. friend class handler_tracking;
  122. uint64_t id_;
  123. bool invoked_;
  124. completion* next_;
  125. };
  126. // Record an operation that is not directly associated with a handler.
  127. BOOST_ASIO_DECL static void operation(execution_context& context,
  128. const char* object_type, void* object,
  129. uintmax_t native_handle, const char* op_name);
  130. // Record that a descriptor has been registered with the reactor.
  131. BOOST_ASIO_DECL static void reactor_registration(execution_context& context,
  132. uintmax_t native_handle, uintmax_t registration);
  133. // Record that a descriptor has been deregistered from the reactor.
  134. BOOST_ASIO_DECL static void reactor_deregistration(execution_context& context,
  135. uintmax_t native_handle, uintmax_t registration);
  136. // Record a reactor-based operation that is associated with a handler.
  137. BOOST_ASIO_DECL static void reactor_events(execution_context& context,
  138. uintmax_t registration, unsigned events);
  139. // Record a reactor-based operation that is associated with a handler.
  140. BOOST_ASIO_DECL static void reactor_operation(
  141. const tracked_handler& h, const char* op_name,
  142. const boost::system::error_code& ec);
  143. // Record a reactor-based operation that is associated with a handler.
  144. BOOST_ASIO_DECL static void reactor_operation(
  145. const tracked_handler& h, const char* op_name,
  146. const boost::system::error_code& ec, std::size_t bytes_transferred);
  147. // Write a line of output.
  148. BOOST_ASIO_DECL static void write_line(const char* format, ...);
  149. private:
  150. struct tracking_state;
  151. BOOST_ASIO_DECL static tracking_state* get_state();
  152. };
  153. # define BOOST_ASIO_INHERIT_TRACKED_HANDLER \
  154. : public boost::asio::detail::handler_tracking::tracked_handler
  155. # define BOOST_ASIO_ALSO_INHERIT_TRACKED_HANDLER \
  156. , public boost::asio::detail::handler_tracking::tracked_handler
  157. # define BOOST_ASIO_HANDLER_TRACKING_INIT \
  158. boost::asio::detail::handler_tracking::init()
  159. # define BOOST_ASIO_HANDLER_LOCATION(args) \
  160. boost::asio::detail::handler_tracking::location tracked_location args
  161. # define BOOST_ASIO_HANDLER_CREATION(args) \
  162. boost::asio::detail::handler_tracking::creation args
  163. # define BOOST_ASIO_HANDLER_COMPLETION(args) \
  164. boost::asio::detail::handler_tracking::completion tracked_completion args
  165. # define BOOST_ASIO_HANDLER_INVOCATION_BEGIN(args) \
  166. tracked_completion.invocation_begin args
  167. # define BOOST_ASIO_HANDLER_INVOCATION_END \
  168. tracked_completion.invocation_end()
  169. # define BOOST_ASIO_HANDLER_OPERATION(args) \
  170. boost::asio::detail::handler_tracking::operation args
  171. # define BOOST_ASIO_HANDLER_REACTOR_REGISTRATION(args) \
  172. boost::asio::detail::handler_tracking::reactor_registration args
  173. # define BOOST_ASIO_HANDLER_REACTOR_DEREGISTRATION(args) \
  174. boost::asio::detail::handler_tracking::reactor_deregistration args
  175. # define BOOST_ASIO_HANDLER_REACTOR_READ_EVENT 1
  176. # define BOOST_ASIO_HANDLER_REACTOR_WRITE_EVENT 2
  177. # define BOOST_ASIO_HANDLER_REACTOR_ERROR_EVENT 4
  178. # define BOOST_ASIO_HANDLER_REACTOR_EVENTS(args) \
  179. boost::asio::detail::handler_tracking::reactor_events args
  180. # define BOOST_ASIO_HANDLER_REACTOR_OPERATION(args) \
  181. boost::asio::detail::handler_tracking::reactor_operation args
  182. #else // defined(BOOST_ASIO_ENABLE_HANDLER_TRACKING)
  183. # define BOOST_ASIO_INHERIT_TRACKED_HANDLER
  184. # define BOOST_ASIO_ALSO_INHERIT_TRACKED_HANDLER
  185. # define BOOST_ASIO_HANDLER_TRACKING_INIT (void)0
  186. # define BOOST_ASIO_HANDLER_LOCATION(loc) (void)0
  187. # define BOOST_ASIO_HANDLER_CREATION(args) (void)0
  188. # define BOOST_ASIO_HANDLER_COMPLETION(args) (void)0
  189. # define BOOST_ASIO_HANDLER_INVOCATION_BEGIN(args) (void)0
  190. # define BOOST_ASIO_HANDLER_INVOCATION_END (void)0
  191. # define BOOST_ASIO_HANDLER_OPERATION(args) (void)0
  192. # define BOOST_ASIO_HANDLER_REACTOR_REGISTRATION(args) (void)0
  193. # define BOOST_ASIO_HANDLER_REACTOR_DEREGISTRATION(args) (void)0
  194. # define BOOST_ASIO_HANDLER_REACTOR_READ_EVENT 0
  195. # define BOOST_ASIO_HANDLER_REACTOR_WRITE_EVENT 0
  196. # define BOOST_ASIO_HANDLER_REACTOR_ERROR_EVENT 0
  197. # define BOOST_ASIO_HANDLER_REACTOR_EVENTS(args) (void)0
  198. # define BOOST_ASIO_HANDLER_REACTOR_OPERATION(args) (void)0
  199. #endif // defined(BOOST_ASIO_ENABLE_HANDLER_TRACKING)
  200. } // namespace detail
  201. } // namespace asio
  202. } // namespace boost
  203. #include <boost/asio/detail/pop_options.hpp>
  204. #if defined(BOOST_ASIO_HEADER_ONLY)
  205. # include <boost/asio/detail/impl/handler_tracking.ipp>
  206. #endif // defined(BOOST_ASIO_HEADER_ONLY)
  207. #endif // BOOST_ASIO_DETAIL_HANDLER_TRACKING_HPP