handler_tracking.hpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. //
  2. // detail/handler_tracking.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2023 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_DETAIL_HANDLER_TRACKING_HPP
  11. #define 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 "asio/detail/config.hpp"
  16. namespace asio {
  17. class execution_context;
  18. } // namespace asio
  19. #if defined(ASIO_CUSTOM_HANDLER_TRACKING)
  20. # include ASIO_CUSTOM_HANDLER_TRACKING
  21. #elif defined(ASIO_ENABLE_HANDLER_TRACKING)
  22. # include "asio/error_code.hpp"
  23. # include "asio/detail/cstdint.hpp"
  24. # include "asio/detail/static_mutex.hpp"
  25. # include "asio/detail/tss_ptr.hpp"
  26. #endif // defined(ASIO_ENABLE_HANDLER_TRACKING)
  27. #include "asio/detail/push_options.hpp"
  28. namespace asio {
  29. namespace detail {
  30. #if defined(ASIO_CUSTOM_HANDLER_TRACKING)
  31. // The user-specified header must define the following macros:
  32. // - ASIO_INHERIT_TRACKED_HANDLER
  33. // - ASIO_ALSO_INHERIT_TRACKED_HANDLER
  34. // - ASIO_HANDLER_TRACKING_INIT
  35. // - ASIO_HANDLER_CREATION(args)
  36. // - ASIO_HANDLER_COMPLETION(args)
  37. // - ASIO_HANDLER_INVOCATION_BEGIN(args)
  38. // - ASIO_HANDLER_INVOCATION_END
  39. // - ASIO_HANDLER_OPERATION(args)
  40. // - ASIO_HANDLER_REACTOR_REGISTRATION(args)
  41. // - ASIO_HANDLER_REACTOR_DEREGISTRATION(args)
  42. // - ASIO_HANDLER_REACTOR_READ_EVENT
  43. // - ASIO_HANDLER_REACTOR_WRITE_EVENT
  44. // - ASIO_HANDLER_REACTOR_ERROR_EVENT
  45. // - ASIO_HANDLER_REACTOR_EVENTS(args)
  46. // - ASIO_HANDLER_REACTOR_OPERATION(args)
  47. # if !defined(ASIO_ENABLE_HANDLER_TRACKING)
  48. # define ASIO_ENABLE_HANDLER_TRACKING 1
  49. # endif /// !defined(ASIO_ENABLE_HANDLER_TRACKING)
  50. #elif defined(ASIO_ENABLE_HANDLER_TRACKING)
  51. class handler_tracking
  52. {
  53. public:
  54. class completion;
  55. // Base class for objects containing tracked handlers.
  56. class tracked_handler
  57. {
  58. private:
  59. // Only the handler_tracking class will have access to the id.
  60. friend class handler_tracking;
  61. friend class completion;
  62. uint64_t id_;
  63. protected:
  64. // Constructor initialises with no id.
  65. tracked_handler() : id_(0) {}
  66. // Prevent deletion through this type.
  67. ~tracked_handler() {}
  68. };
  69. // Initialise the tracking system.
  70. ASIO_DECL static void init();
  71. class location
  72. {
  73. public:
  74. // Constructor adds a location to the stack.
  75. ASIO_DECL explicit location(const char* file,
  76. int line, const char* func);
  77. // Destructor removes a location from the stack.
  78. ASIO_DECL ~location();
  79. private:
  80. // Disallow copying and assignment.
  81. location(const location&) = delete;
  82. location& operator=(const location&) = delete;
  83. friend class handler_tracking;
  84. const char* file_;
  85. int line_;
  86. const char* func_;
  87. location* next_;
  88. };
  89. // Record the creation of a tracked handler.
  90. ASIO_DECL static void creation(
  91. execution_context& context, tracked_handler& h,
  92. const char* object_type, void* object,
  93. uintmax_t native_handle, const char* op_name);
  94. class completion
  95. {
  96. public:
  97. // Constructor records that handler is to be invoked with no arguments.
  98. ASIO_DECL explicit completion(const tracked_handler& h);
  99. // Destructor records only when an exception is thrown from the handler, or
  100. // if the memory is being freed without the handler having been invoked.
  101. ASIO_DECL ~completion();
  102. // Records that handler is to be invoked with no arguments.
  103. ASIO_DECL void invocation_begin();
  104. // Records that handler is to be invoked with one arguments.
  105. ASIO_DECL void invocation_begin(const asio::error_code& ec);
  106. // Constructor records that handler is to be invoked with two arguments.
  107. ASIO_DECL void invocation_begin(
  108. const asio::error_code& ec, std::size_t bytes_transferred);
  109. // Constructor records that handler is to be invoked with two arguments.
  110. ASIO_DECL void invocation_begin(
  111. const asio::error_code& ec, int signal_number);
  112. // Constructor records that handler is to be invoked with two arguments.
  113. ASIO_DECL void invocation_begin(
  114. const asio::error_code& ec, const char* arg);
  115. // Record that handler invocation has ended.
  116. ASIO_DECL void invocation_end();
  117. private:
  118. friend class handler_tracking;
  119. uint64_t id_;
  120. bool invoked_;
  121. completion* next_;
  122. };
  123. // Record an operation that is not directly associated with a handler.
  124. ASIO_DECL static void operation(execution_context& context,
  125. const char* object_type, void* object,
  126. uintmax_t native_handle, const char* op_name);
  127. // Record that a descriptor has been registered with the reactor.
  128. ASIO_DECL static void reactor_registration(execution_context& context,
  129. uintmax_t native_handle, uintmax_t registration);
  130. // Record that a descriptor has been deregistered from the reactor.
  131. ASIO_DECL static void reactor_deregistration(execution_context& context,
  132. uintmax_t native_handle, uintmax_t registration);
  133. // Record a reactor-based operation that is associated with a handler.
  134. ASIO_DECL static void reactor_events(execution_context& context,
  135. uintmax_t registration, unsigned events);
  136. // Record a reactor-based operation that is associated with a handler.
  137. ASIO_DECL static void reactor_operation(
  138. const tracked_handler& h, const char* op_name,
  139. const asio::error_code& ec);
  140. // Record a reactor-based operation that is associated with a handler.
  141. ASIO_DECL static void reactor_operation(
  142. const tracked_handler& h, const char* op_name,
  143. const asio::error_code& ec, std::size_t bytes_transferred);
  144. // Write a line of output.
  145. ASIO_DECL static void write_line(const char* format, ...);
  146. private:
  147. struct tracking_state;
  148. ASIO_DECL static tracking_state* get_state();
  149. };
  150. # define ASIO_INHERIT_TRACKED_HANDLER \
  151. : public asio::detail::handler_tracking::tracked_handler
  152. # define ASIO_ALSO_INHERIT_TRACKED_HANDLER \
  153. , public asio::detail::handler_tracking::tracked_handler
  154. # define ASIO_HANDLER_TRACKING_INIT \
  155. asio::detail::handler_tracking::init()
  156. # define ASIO_HANDLER_LOCATION(args) \
  157. asio::detail::handler_tracking::location tracked_location args
  158. # define ASIO_HANDLER_CREATION(args) \
  159. asio::detail::handler_tracking::creation args
  160. # define ASIO_HANDLER_COMPLETION(args) \
  161. asio::detail::handler_tracking::completion tracked_completion args
  162. # define ASIO_HANDLER_INVOCATION_BEGIN(args) \
  163. tracked_completion.invocation_begin args
  164. # define ASIO_HANDLER_INVOCATION_END \
  165. tracked_completion.invocation_end()
  166. # define ASIO_HANDLER_OPERATION(args) \
  167. asio::detail::handler_tracking::operation args
  168. # define ASIO_HANDLER_REACTOR_REGISTRATION(args) \
  169. asio::detail::handler_tracking::reactor_registration args
  170. # define ASIO_HANDLER_REACTOR_DEREGISTRATION(args) \
  171. asio::detail::handler_tracking::reactor_deregistration args
  172. # define ASIO_HANDLER_REACTOR_READ_EVENT 1
  173. # define ASIO_HANDLER_REACTOR_WRITE_EVENT 2
  174. # define ASIO_HANDLER_REACTOR_ERROR_EVENT 4
  175. # define ASIO_HANDLER_REACTOR_EVENTS(args) \
  176. asio::detail::handler_tracking::reactor_events args
  177. # define ASIO_HANDLER_REACTOR_OPERATION(args) \
  178. asio::detail::handler_tracking::reactor_operation args
  179. #else // defined(ASIO_ENABLE_HANDLER_TRACKING)
  180. # define ASIO_INHERIT_TRACKED_HANDLER
  181. # define ASIO_ALSO_INHERIT_TRACKED_HANDLER
  182. # define ASIO_HANDLER_TRACKING_INIT (void)0
  183. # define ASIO_HANDLER_LOCATION(loc) (void)0
  184. # define ASIO_HANDLER_CREATION(args) (void)0
  185. # define ASIO_HANDLER_COMPLETION(args) (void)0
  186. # define ASIO_HANDLER_INVOCATION_BEGIN(args) (void)0
  187. # define ASIO_HANDLER_INVOCATION_END (void)0
  188. # define ASIO_HANDLER_OPERATION(args) (void)0
  189. # define ASIO_HANDLER_REACTOR_REGISTRATION(args) (void)0
  190. # define ASIO_HANDLER_REACTOR_DEREGISTRATION(args) (void)0
  191. # define ASIO_HANDLER_REACTOR_READ_EVENT 0
  192. # define ASIO_HANDLER_REACTOR_WRITE_EVENT 0
  193. # define ASIO_HANDLER_REACTOR_ERROR_EVENT 0
  194. # define ASIO_HANDLER_REACTOR_EVENTS(args) (void)0
  195. # define ASIO_HANDLER_REACTOR_OPERATION(args) (void)0
  196. #endif // defined(ASIO_ENABLE_HANDLER_TRACKING)
  197. } // namespace detail
  198. } // namespace asio
  199. #include "asio/detail/pop_options.hpp"
  200. #if defined(ASIO_HEADER_ONLY)
  201. # include "asio/detail/impl/handler_tracking.ipp"
  202. #endif // defined(ASIO_HEADER_ONLY)
  203. #endif // ASIO_DETAIL_HANDLER_TRACKING_HPP