• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #ifndef BOOST_THREAD_TSS_HPP
2 #define BOOST_THREAD_TSS_HPP
3 // Distributed under the Boost Software License, Version 1.0. (See
4 // accompanying file LICENSE_1_0.txt or copy at
5 // http://www.boost.org/LICENSE_1_0.txt)
6 // (C) Copyright 2007-8 Anthony Williams
7 
8 #include <boost/thread/detail/config.hpp>
9 
10 #include <boost/type_traits/add_reference.hpp>
11 
12 #include <boost/config/abi_prefix.hpp>
13 
14 namespace boost
15 {
16     namespace detail
17     {
18         namespace thread
19         {
20             typedef void(*cleanup_func_t)(void*);
21             typedef void(*cleanup_caller_t)(cleanup_func_t, void*);
22         }
23 
24         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);
25         BOOST_THREAD_DECL void* get_tss_data(void const* key);
26     }
27 
28     template <typename T>
29     class thread_specific_ptr
30     {
31     private:
32         thread_specific_ptr(thread_specific_ptr&);
33         thread_specific_ptr& operator=(thread_specific_ptr&);
34 
35         typedef void(*original_cleanup_func_t)(T*);
36 
default_deleter(T * data)37         static void default_deleter(T* data)
38         {
39             delete data;
40         }
41 
cleanup_caller(detail::thread::cleanup_func_t cleanup_function,void * data)42         static void cleanup_caller(detail::thread::cleanup_func_t cleanup_function,void* data)
43         {
44             reinterpret_cast<original_cleanup_func_t>(cleanup_function)(static_cast<T*>(data));
45         }
46 
47 
48         detail::thread::cleanup_func_t cleanup;
49 
50     public:
51         typedef T element_type;
52 
thread_specific_ptr()53         thread_specific_ptr():
54             cleanup(reinterpret_cast<detail::thread::cleanup_func_t>(&default_deleter))
55         {}
thread_specific_ptr(void (* func_)(T *))56         explicit thread_specific_ptr(void (*func_)(T*))
57           : cleanup(reinterpret_cast<detail::thread::cleanup_func_t>(func_))
58         {}
~thread_specific_ptr()59         ~thread_specific_ptr()
60         {
61             detail::set_tss_data(this,0,0,0,true);
62         }
63 
get() const64         T* get() const
65         {
66             return static_cast<T*>(detail::get_tss_data(this));
67         }
operator ->() const68         T* operator->() const
69         {
70             return get();
71         }
operator *() const72         typename add_reference<T>::type operator*() const
73         {
74             return *get();
75         }
release()76         T* release()
77         {
78             T* const temp=get();
79             detail::set_tss_data(this,0,0,0,false);
80             return temp;
81         }
reset(T * new_value=0)82         void reset(T* new_value=0)
83         {
84             T* const current_value=get();
85             if(current_value!=new_value)
86             {
87                 detail::set_tss_data(this,&cleanup_caller,cleanup,new_value,true);
88             }
89         }
90     };
91 }
92 
93 #include <boost/config/abi_suffix.hpp>
94 
95 #endif
96