call_stack.hpp 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. //
  2. // call_stack.hpp
  3. // ~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2008 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_CALL_STACK_HPP
  11. #define BOOST_ASIO_DETAIL_CALL_STACK_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/push_options.hpp>
  16. #include <boost/asio/detail/noncopyable.hpp>
  17. #include <boost/asio/detail/tss_ptr.hpp>
  18. namespace boost {
  19. namespace asio {
  20. namespace detail {
  21. // Helper class to determine whether or not the current thread is inside an
  22. // invocation of io_service::run() for a specified io_service object.
  23. template <typename Owner>
  24. class call_stack
  25. {
  26. public:
  27. // Context class automatically pushes an owner on to the stack.
  28. class context
  29. : private noncopyable
  30. {
  31. public:
  32. // Push the owner on to the stack.
  33. explicit context(Owner* d)
  34. : owner_(d),
  35. next_(call_stack<Owner>::top_)
  36. {
  37. call_stack<Owner>::top_ = this;
  38. }
  39. // Pop the owner from the stack.
  40. ~context()
  41. {
  42. call_stack<Owner>::top_ = next_;
  43. }
  44. private:
  45. friend class call_stack<Owner>;
  46. // The owner associated with the context.
  47. Owner* owner_;
  48. // The next element in the stack.
  49. context* next_;
  50. };
  51. friend class context;
  52. // Determine whether the specified owner is on the stack.
  53. static bool contains(Owner* d)
  54. {
  55. context* elem = top_;
  56. while (elem)
  57. {
  58. if (elem->owner_ == d)
  59. return true;
  60. elem = elem->next_;
  61. }
  62. return false;
  63. }
  64. private:
  65. // The top of the stack of calls for the current thread.
  66. static tss_ptr<context> top_;
  67. };
  68. template <typename Owner>
  69. tss_ptr<typename call_stack<Owner>::context>
  70. call_stack<Owner>::top_;
  71. } // namespace detail
  72. } // namespace asio
  73. } // namespace boost
  74. #include <boost/asio/detail/pop_options.hpp>
  75. #endif // BOOST_ASIO_DETAIL_CALL_STACK_HPP