2 // detail/call_stack.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~
5 // Copyright (c) 2003-2019 Christopher M. Kohlhoff (chris at kohlhoff dot com)
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)
11 #ifndef BOOST_ASIO_DETAIL_CALL_STACK_HPP
12 #define BOOST_ASIO_DETAIL_CALL_STACK_HPP
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
18 #include <boost/asio/detail/config.hpp>
19 #include <boost/asio/detail/noncopyable.hpp>
20 #include <boost/asio/detail/tss_ptr.hpp>
22 #include <boost/asio/detail/push_options.hpp>
28 // Helper class to determine whether or not the current thread is inside an
29 // invocation of io_context::run() for a specified io_context object.
30 template <typename Key, typename Value = unsigned char>
34 // Context class automatically pushes the key/value pair on to the stack.
39 // Push the key on to the stack.
40 explicit context(Key* k)
42 next_(call_stack<Key, Value>::top_)
44 value_ = reinterpret_cast<unsigned char*>(this);
45 call_stack<Key, Value>::top_ = this;
48 // Push the key/value pair on to the stack.
49 context(Key* k, Value& v)
52 next_(call_stack<Key, Value>::top_)
54 call_stack<Key, Value>::top_ = this;
57 // Pop the key/value pair from the stack.
60 call_stack<Key, Value>::top_ = next_;
63 // Find the next context with the same key.
64 Value* next_by_key() const
66 context* elem = next_;
69 if (elem->key_ == key_)
77 friend class call_stack<Key, Value>;
79 // The key associated with the context.
82 // The value associated with the context.
85 // The next element in the stack.
91 // Determine whether the specified owner is on the stack. Returns address of
92 // key if present, 0 otherwise.
93 static Value* contains(Key* k)
105 // Obtain the value at the top of the stack.
108 context* elem = top_;
109 return elem ? elem->value_ : 0;
113 // The top of the stack of calls for the current thread.
114 static tss_ptr<context> top_;
117 template <typename Key, typename Value>
118 tss_ptr<typename call_stack<Key, Value>::context>
119 call_stack<Key, Value>::top_;
121 } // namespace detail
125 #include <boost/asio/detail/pop_options.hpp>
127 #endif // BOOST_ASIO_DETAIL_CALL_STACK_HPP