$search
00001 // 00002 // win_tss_ptr.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_WIN_TSS_PTR_HPP 00012 #define ASIO_DETAIL_WIN_TSS_PTR_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/push_options.hpp" 00021 #include <boost/config.hpp> 00022 #include "asio/detail/pop_options.hpp" 00023 00024 #if defined(BOOST_WINDOWS) 00025 00026 #include "asio/error.hpp" 00027 #include "asio/system_error.hpp" 00028 #include "asio/detail/noncopyable.hpp" 00029 #include "asio/detail/socket_types.hpp" 00030 00031 #include "asio/detail/push_options.hpp" 00032 #include <boost/throw_exception.hpp> 00033 #include "asio/detail/pop_options.hpp" 00034 00035 namespace asio { 00036 namespace detail { 00037 00038 template <typename T> 00039 class win_tss_ptr 00040 : private noncopyable 00041 { 00042 public: 00043 #if defined(UNDER_CE) 00044 enum { out_of_indexes = 0xFFFFFFFF }; 00045 #else 00046 enum { out_of_indexes = TLS_OUT_OF_INDEXES }; 00047 #endif 00048 00049 // Constructor. 00050 win_tss_ptr() 00051 { 00052 tss_key_ = ::TlsAlloc(); 00053 if (tss_key_ == out_of_indexes) 00054 { 00055 DWORD last_error = ::GetLastError(); 00056 asio::system_error e( 00057 asio::error_code(last_error, 00058 asio::error::get_system_category()), 00059 "tss"); 00060 boost::throw_exception(e); 00061 } 00062 } 00063 00064 // Destructor. 00065 ~win_tss_ptr() 00066 { 00067 ::TlsFree(tss_key_); 00068 } 00069 00070 // Get the value. 00071 operator T*() const 00072 { 00073 return static_cast<T*>(::TlsGetValue(tss_key_)); 00074 } 00075 00076 // Set the value. 00077 void operator=(T* value) 00078 { 00079 ::TlsSetValue(tss_key_, value); 00080 } 00081 00082 private: 00083 // Thread-specific storage to allow unlocked access to determine whether a 00084 // thread is a member of the pool. 00085 DWORD tss_key_; 00086 }; 00087 00088 } // namespace detail 00089 } // namespace asio 00090 00091 #endif // defined(BOOST_WINDOWS) 00092 00093 #include "asio/detail/pop_options.hpp" 00094 00095 #endif // ASIO_DETAIL_WIN_TSS_PTR_HPP