handler_tracking.hpp 5.0 KB

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