fix doc example typo
[boost.git] / boost / thread / tss.hpp
blob73248239b19b822cf7cd0ddad97b2a55d23b4deb
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
8 #include <boost/thread/detail/config.hpp>
9 #include <boost/shared_ptr.hpp>
10 #include <boost/thread/detail/thread_heap_alloc.hpp>
12 #include <boost/config/abi_prefix.hpp>
14 namespace boost
16 namespace detail
18 struct tss_cleanup_function
20 virtual ~tss_cleanup_function()
23 virtual void operator()(void* data)=0;
26 BOOST_THREAD_DECL void set_tss_data(void const* key,boost::shared_ptr<tss_cleanup_function> func,void* tss_data,bool cleanup_existing);
27 BOOST_THREAD_DECL void* get_tss_data(void const* key);
30 template <typename T>
31 class thread_specific_ptr
33 private:
34 thread_specific_ptr(thread_specific_ptr&);
35 thread_specific_ptr& operator=(thread_specific_ptr&);
37 struct delete_data:
38 detail::tss_cleanup_function
40 void operator()(void* data)
42 delete static_cast<T*>(data);
46 struct run_custom_cleanup_function:
47 detail::tss_cleanup_function
49 void (*cleanup_function)(T*);
51 explicit run_custom_cleanup_function(void (*cleanup_function_)(T*)):
52 cleanup_function(cleanup_function_)
55 void operator()(void* data)
57 cleanup_function(static_cast<T*>(data));
62 boost::shared_ptr<detail::tss_cleanup_function> cleanup;
64 public:
65 thread_specific_ptr():
66 cleanup(detail::heap_new<delete_data>(),detail::do_heap_delete<delete_data>())
68 explicit thread_specific_ptr(void (*func_)(T*))
70 if(func_)
72 cleanup.reset(detail::heap_new<run_custom_cleanup_function>(func_),detail::do_heap_delete<run_custom_cleanup_function>());
75 ~thread_specific_ptr()
77 reset();
80 T* get() const
82 return static_cast<T*>(detail::get_tss_data(this));
84 T* operator->() const
86 return get();
88 T& operator*() const
90 return *get();
92 T* release()
94 T* const temp=get();
95 detail::set_tss_data(this,boost::shared_ptr<detail::tss_cleanup_function>(),0,false);
96 return temp;
98 void reset(T* new_value=0)
100 T* const current_value=get();
101 if(current_value!=new_value)
103 detail::set_tss_data(this,cleanup,new_value,true);
109 #include <boost/config/abi_suffix.hpp>
111 #endif