00001 // 00002 // call_stack.hpp 00003 // ~~~~~~~~~~~~~~ 00004 // 00005 // Copyright (c) 2003-2008 Christopher M. Kohlhoff (chris at kohlhoff dot com) 00006 // 00007 // Distributed under the Boost Software License, Version 1.0. (See accompanying 00008 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 00009 // 00010 00011 #ifndef ASIO_DETAIL_CALL_STACK_HPP 00012 #define ASIO_DETAIL_CALL_STACK_HPP 00013 00014 #if defined(_MSC_VER) && (_MSC_VER >= 1200) 00015 # pragma once 00016 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200) 00017 00018 #include "asio/detail/push_options.hpp" 00019 00020 #include "asio/detail/noncopyable.hpp" 00021 #include "asio/detail/tss_ptr.hpp" 00022 00023 namespace asio { 00024 namespace detail { 00025 00026 // Helper class to determine whether or not the current thread is inside an 00027 // invocation of io_service::run() for a specified io_service object. 00028 template <typename Owner> 00029 class call_stack 00030 { 00031 public: 00032 // Context class automatically pushes an owner on to the stack. 00033 class context 00034 : private noncopyable 00035 { 00036 public: 00037 // Push the owner on to the stack. 00038 explicit context(Owner* d) 00039 : owner_(d), 00040 next_(call_stack<Owner>::top_) 00041 { 00042 call_stack<Owner>::top_ = this; 00043 } 00044 00045 // Pop the owner from the stack. 00046 ~context() 00047 { 00048 call_stack<Owner>::top_ = next_; 00049 } 00050 00051 private: 00052 friend class call_stack<Owner>; 00053 00054 // The owner associated with the context. 00055 Owner* owner_; 00056 00057 // The next element in the stack. 00058 context* next_; 00059 }; 00060 00061 friend class context; 00062 00063 // Determine whether the specified owner is on the stack. 00064 static bool contains(Owner* d) 00065 { 00066 context* elem = top_; 00067 while (elem) 00068 { 00069 if (elem->owner_ == d) 00070 return true; 00071 elem = elem->next_; 00072 } 00073 return false; 00074 } 00075 00076 private: 00077 // The top of the stack of calls for the current thread. 00078 static tss_ptr<context> top_; 00079 }; 00080 00081 template <typename Owner> 00082 tss_ptr<typename call_stack<Owner>::context> 00083 call_stack<Owner>::top_; 00084 00085 } // namespace detail 00086 } // namespace asio 00087 00088 #include "asio/detail/pop_options.hpp" 00089 00090 #endif // ASIO_DETAIL_CALL_STACK_HPP