handler_tracking.hpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. //
  2. // detail/handler_tracking.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2015 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. #if defined(ASIO_ENABLE_HANDLER_TRACKING)
  17. # include "asio/error_code.hpp"
  18. # include "asio/detail/cstdint.hpp"
  19. # include "asio/detail/static_mutex.hpp"
  20. # include "asio/detail/tss_ptr.hpp"
  21. #endif // defined(ASIO_ENABLE_HANDLER_TRACKING)
  22. #include "asio/detail/push_options.hpp"
  23. namespace asio {
  24. namespace detail {
  25. #if defined(ASIO_ENABLE_HANDLER_TRACKING)
  26. class handler_tracking
  27. {
  28. public:
  29. class completion;
  30. // Base class for objects containing tracked handlers.
  31. class tracked_handler
  32. {
  33. private:
  34. // Only the handler_tracking class will have access to the id.
  35. friend class handler_tracking;
  36. friend class completion;
  37. uint64_t id_;
  38. protected:
  39. // Constructor initialises with no id.
  40. tracked_handler() : id_(0) {}
  41. // Prevent deletion through this type.
  42. ~tracked_handler() {}
  43. };
  44. // Initialise the tracking system.
  45. ASIO_DECL static void init();
  46. // Record the creation of a tracked handler.
  47. ASIO_DECL static void creation(tracked_handler* h,
  48. const char* object_type, void* object, const char* op_name);
  49. class completion
  50. {
  51. public:
  52. // Constructor records that handler is to be invoked with no arguments.
  53. ASIO_DECL explicit completion(tracked_handler* h);
  54. // Destructor records only when an exception is thrown from the handler, or
  55. // if the memory is being freed without the handler having been invoked.
  56. ASIO_DECL ~completion();
  57. // Records that handler is to be invoked with no arguments.
  58. ASIO_DECL void invocation_begin();
  59. // Records that handler is to be invoked with one arguments.
  60. ASIO_DECL void invocation_begin(const asio::error_code& ec);
  61. // Constructor records that handler is to be invoked with two arguments.
  62. ASIO_DECL void invocation_begin(
  63. const asio::error_code& ec, std::size_t bytes_transferred);
  64. // Constructor records that handler is to be invoked with two arguments.
  65. ASIO_DECL void invocation_begin(
  66. const asio::error_code& ec, int signal_number);
  67. // Constructor records that handler is to be invoked with two arguments.
  68. ASIO_DECL void invocation_begin(
  69. const asio::error_code& ec, const char* arg);
  70. // Record that handler invocation has ended.
  71. ASIO_DECL void invocation_end();
  72. private:
  73. friend class handler_tracking;
  74. uint64_t id_;
  75. bool invoked_;
  76. completion* next_;
  77. };
  78. // Record an operation that affects pending handlers.
  79. ASIO_DECL static void operation(const char* object_type,
  80. void* object, const char* op_name);
  81. // Write a line of output.
  82. ASIO_DECL static void write_line(const char* format, ...);
  83. private:
  84. struct tracking_state;
  85. ASIO_DECL static tracking_state* get_state();
  86. };
  87. # define ASIO_INHERIT_TRACKED_HANDLER \
  88. : public asio::detail::handler_tracking::tracked_handler
  89. # define ASIO_ALSO_INHERIT_TRACKED_HANDLER \
  90. , public asio::detail::handler_tracking::tracked_handler
  91. # define ASIO_HANDLER_TRACKING_INIT \
  92. asio::detail::handler_tracking::init()
  93. # define ASIO_HANDLER_CREATION(args) \
  94. asio::detail::handler_tracking::creation args
  95. # define ASIO_HANDLER_COMPLETION(args) \
  96. asio::detail::handler_tracking::completion tracked_completion args
  97. # define ASIO_HANDLER_INVOCATION_BEGIN(args) \
  98. tracked_completion.invocation_begin args
  99. # define ASIO_HANDLER_INVOCATION_END \
  100. tracked_completion.invocation_end()
  101. # define ASIO_HANDLER_OPERATION(args) \
  102. asio::detail::handler_tracking::operation args
  103. #else // defined(ASIO_ENABLE_HANDLER_TRACKING)
  104. # define ASIO_INHERIT_TRACKED_HANDLER
  105. # define ASIO_ALSO_INHERIT_TRACKED_HANDLER
  106. # define ASIO_HANDLER_TRACKING_INIT (void)0
  107. # define ASIO_HANDLER_CREATION(args) (void)0
  108. # define ASIO_HANDLER_COMPLETION(args) (void)0
  109. # define ASIO_HANDLER_INVOCATION_BEGIN(args) (void)0
  110. # define ASIO_HANDLER_INVOCATION_END (void)0
  111. # define ASIO_HANDLER_OPERATION(args) (void)0
  112. #endif // defined(ASIO_ENABLE_HANDLER_TRACKING)
  113. } // namespace detail
  114. } // namespace asio
  115. #include "asio/detail/pop_options.hpp"
  116. #if defined(ASIO_HEADER_ONLY)
  117. # include "asio/detail/impl/handler_tracking.ipp"
  118. #endif // defined(ASIO_HEADER_ONLY)
  119. #endif // ASIO_DETAIL_HANDLER_TRACKING_HPP