File indexing completed on 2025-12-16 10:10:08
0001 #ifndef BOOST_THREAD_TSS_HPP
0002 #define BOOST_THREAD_TSS_HPP
0003
0004
0005
0006
0007
0008 #include <boost/thread/detail/config.hpp>
0009
0010 #include <boost/type_traits/add_reference.hpp>
0011
0012 #include <boost/config/abi_prefix.hpp>
0013
0014 namespace boost
0015 {
0016 namespace detail
0017 {
0018 namespace thread
0019 {
0020 typedef void(*cleanup_func_t)(void*);
0021 typedef void(*cleanup_caller_t)(cleanup_func_t, void*);
0022 }
0023
0024 BOOST_THREAD_DECL void set_tss_data(void const* key,detail::thread::cleanup_caller_t caller,detail::thread::cleanup_func_t func,void* tss_data,bool cleanup_existing);
0025 BOOST_THREAD_DECL void* get_tss_data(void const* key);
0026 }
0027
0028 template <typename T>
0029 class thread_specific_ptr
0030 {
0031 private:
0032 thread_specific_ptr(thread_specific_ptr&);
0033 thread_specific_ptr& operator=(thread_specific_ptr&);
0034
0035 typedef void(*original_cleanup_func_t)(T*);
0036
0037 static void default_deleter(T* data)
0038 {
0039 delete data;
0040 }
0041
0042 static void cleanup_caller(detail::thread::cleanup_func_t cleanup_function,void* data)
0043 {
0044 reinterpret_cast<original_cleanup_func_t>(cleanup_function)(static_cast<T*>(data));
0045 }
0046
0047
0048 detail::thread::cleanup_func_t cleanup;
0049
0050 public:
0051 typedef T element_type;
0052
0053 thread_specific_ptr():
0054 cleanup(reinterpret_cast<detail::thread::cleanup_func_t>(&default_deleter))
0055 {}
0056 explicit thread_specific_ptr(void (*func_)(T*))
0057 : cleanup(reinterpret_cast<detail::thread::cleanup_func_t>(func_))
0058 {}
0059 ~thread_specific_ptr()
0060 {
0061 detail::set_tss_data(this,0,0,0,true);
0062 }
0063
0064 T* get() const
0065 {
0066 return static_cast<T*>(detail::get_tss_data(this));
0067 }
0068 T* operator->() const
0069 {
0070 return get();
0071 }
0072 typename add_reference<T>::type operator*() const
0073 {
0074 return *get();
0075 }
0076 T* release()
0077 {
0078 T* const temp=get();
0079 detail::set_tss_data(this,0,0,0,false);
0080 return temp;
0081 }
0082 void reset(T* new_value=0)
0083 {
0084 T* const current_value=get();
0085 if(current_value!=new_value)
0086 {
0087 detail::set_tss_data(this,&cleanup_caller,cleanup,new_value,true);
0088 }
0089 }
0090 };
0091 }
0092
0093 #include <boost/config/abi_suffix.hpp>
0094
0095 #endif