Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_33_1/boost/thread/tss.hpp @ 28

Last change on this file since 28 was 12, checked in by landauf, 17 years ago

added boost

File size: 3.3 KB
Line 
1// Copyright (C) 2001-2003
2// William E. Kempf
3//
4// Permission to use, copy, modify, distribute and sell this software
5// and its documentation for any purpose is hereby granted without fee,
6// provided that the above copyright notice appear in all copies and
7// that both that copyright notice and this permission notice appear
8// in supporting documentation.  William E. Kempf makes no representations
9// about the suitability of this software for any purpose.
10// It is provided "as is" without express or implied warranty.
11
12#ifndef BOOST_TSS_WEK070601_HPP
13#define BOOST_TSS_WEK070601_HPP
14
15#include <boost/thread/detail/config.hpp>
16
17#include <boost/utility.hpp>
18#include <boost/function.hpp>
19#include <boost/thread/exceptions.hpp>
20
21#if defined(BOOST_HAS_PTHREADS)
22#   include <pthread.h>
23#elif defined(BOOST_HAS_MPTASKS)
24#   include <Multiprocessing.h>
25#endif
26
27namespace boost {
28
29namespace detail {
30
31class BOOST_THREAD_DECL tss : private noncopyable
32{
33public:
34    tss(boost::function1<void, void*>* pcleanup) {
35        if (pcleanup == 0) throw boost::thread_resource_error();
36        try
37        {
38            init(pcleanup);
39        }
40        catch (...)
41        {
42            delete pcleanup;
43            throw boost::thread_resource_error();
44        }
45    }
46
47    void* get() const;
48    void set(void* value);
49    void cleanup(void* p);
50
51private:
52    unsigned int m_slot; //This is a "pseudo-slot", not a native slot
53
54    void init(boost::function1<void, void*>* pcleanup);
55};
56
57#if defined(BOOST_HAS_MPTASKS)
58void thread_cleanup();
59#endif
60
61template <typename T>
62struct tss_adapter
63{
64    template <typename F>
65    tss_adapter(const F& cleanup) : m_cleanup(cleanup) { }
66    void operator()(void* p) { m_cleanup(static_cast<T*>(p)); }
67    boost::function1<void, T*> m_cleanup;
68};
69
70} // namespace detail
71
72template <typename T>
73class thread_specific_ptr : private noncopyable
74{
75public:
76    thread_specific_ptr()
77        : m_tss(new boost::function1<void, void*>(
78                    boost::detail::tss_adapter<T>(
79                        &thread_specific_ptr<T>::cleanup)))
80    {
81    }
82    thread_specific_ptr(void (*clean)(T*))
83        : m_tss(new boost::function1<void, void*>(
84                    boost::detail::tss_adapter<T>(clean)))
85    {
86    }
87    ~thread_specific_ptr() { reset(); }
88
89    T* get() const { return static_cast<T*>(m_tss.get()); }
90    T* operator->() const { return get(); }
91    T& operator*() const { return *get(); }
92    T* release() { T* temp = get(); if (temp) m_tss.set(0); return temp; }
93    void reset(T* p=0)
94    {
95        T* cur = get();
96        if (cur == p) return;
97        m_tss.set(p);
98        if (cur) m_tss.cleanup(cur);
99    }
100
101private:
102    static void cleanup(T* p) { delete p; }
103    detail::tss m_tss;
104};
105
106} // namespace boost
107
108#endif //BOOST_TSS_WEK070601_HPP
109
110// Change Log:
111//   6 Jun 01 
112//      WEKEMPF Initial version.
113//  30 May 02  WEKEMPF
114//      Added interface to set specific cleanup handlers.
115//      Removed TLS slot limits from most implementations.
116//  22 Mar 04 GlassfordM for WEKEMPF
117//      Fixed: thread_specific_ptr::reset() doesn't check error returned
118//          by tss::set(); tss::set() now throws if it fails.
119//      Fixed: calling thread_specific_ptr::reset() or
120//          thread_specific_ptr::release() causes double-delete: once on
121//          reset()/release() and once on ~thread_specific_ptr().
Note: See TracBrowser for help on using the repository browser.