call_stack.hpp 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. //
  2. // detail/call_stack.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2011 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_CALL_STACK_HPP
  11. #define 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 "asio/detail/config.hpp"
  16. #include "asio/detail/noncopyable.hpp"
  17. #include "asio/detail/tss_ptr.hpp"
  18. #include "asio/detail/push_options.hpp"
  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. #include "asio/detail/pop_options.hpp"
  74. #endif // ASIO_DETAIL_CALL_STACK_HPP