Line data Source code
1 :
2 : /* Copyright (c) 2005-2014, Stefan Eilemann <eile@equalizergraphics.com>
3 : *
4 : * This library is free software; you can redistribute it and/or modify it under
5 : * the terms of the GNU Lesser General Public License version 2.1 as published
6 : * by the Free Software Foundation.
7 : *
8 : * This library is distributed in the hope that it will be useful, but WITHOUT
9 : * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
10 : * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
11 : * details.
12 : *
13 : * You should have received a copy of the GNU Lesser General Public License
14 : * along with this library; if not, write to the Free Software Foundation, Inc.,
15 : * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16 : */
17 :
18 : #ifndef LUNCHBOX_PERTHREAD_H
19 : #define LUNCHBOX_PERTHREAD_H
20 :
21 : #include <lunchbox/compiler.h> // deprecated macro
22 : #include <lunchbox/tls.h> // member
23 :
24 : namespace lunchbox
25 : {
26 : namespace detail { class PerThread; }
27 :
28 : /** Default PerThread destructor deleting the object. @version 1.1.2 */
29 1059 : template< class T > void perThreadDelete( T* object ) { delete object; }
30 :
31 : /** Empty PerThread destructor. @version 1.1.2 */
32 : template< class T > void perThreadNoDelete( T* ) {}
33 :
34 : /**
35 : * Implements thread-specific storage for C++ objects.
36 : *
37 : * The default destructor function deletes the object on thread exit.
38 : *
39 : * @param T the type of data to store in thread-local storage
40 : * @param D the destructor callback function.
41 : * @deprecated Use boost::thread_specific_ptr
42 : *
43 : * Example: @include tests/perThread.cpp
44 : */
45 : template< class T, void (*D)( T* ) = &perThreadDelete< T > >
46 : class PerThread : public boost::noncopyable
47 : {
48 : public:
49 : /** Construct a new per-thread variable. @version 1.0 */
50 : PerThread();
51 : /** Destruct the per-thread variable. @version 1.0 */
52 : ~PerThread();
53 :
54 : /** Assign an object to the thread-local storage. @version 1.0 */
55 : PerThread<T, D>& operator = ( const T* data );
56 : /** Assign an object from another thread-local storage. @version 1.0 */
57 : PerThread<T, D>& operator = ( const PerThread<T, D>& rhs );
58 :
59 : /** @return the held object pointer. @version 1.0 */
60 : T* get();
61 : /** @return the held object pointer. @version 1.0 */
62 : const T* get() const;
63 : /** Access the thread-local object. @version 1.0 */
64 : T* operator->();
65 : /** Access the thread-local object. @version 1.0 */
66 : const T* operator->() const;
67 :
68 : /** @return the held object reference. @version 1.0 */
69 6144 : T& operator*()
70 6144 : { LBASSERTINFO( get(), className( this )); return *get(); }
71 : /** @return the held object reference. @version 1.0 */
72 : const T& operator*() const
73 : { LBASSERTINFO( get(), className( this )); return *get(); }
74 :
75 : /**
76 : * @return true if the thread-local variables hold the same object.
77 : * @version 1.0
78 : */
79 : bool operator == ( const PerThread& rhs ) const
80 : { return ( get() == rhs.get( )); }
81 :
82 : /**
83 : * @return true if the thread-local variable holds the same object.
84 : * @version 1.0
85 : */
86 1024 : bool operator == ( const T* rhs ) const { return ( get()==rhs ); }
87 :
88 : /**
89 : * @return true if the thread-local variable holds another object.
90 : * @version 1.0
91 : */
92 : bool operator != ( const T* rhs ) const { return ( get()!=rhs ); }
93 :
94 : /**
95 : * @return true if the thread-local storage holds a 0 pointer.
96 : * @version 1.0
97 : */
98 : bool operator ! () const;
99 :
100 : /**
101 : * @return true if the thread-local storage holds a non-0 pointer.
102 : * @version 1.0
103 : */
104 : bool isValid() const;
105 :
106 : private:
107 : TLS tls_;
108 : } LB_DEPRECATED;
109 :
110 : }
111 :
112 : #include "perThread.ipp" // template implementation
113 :
114 : #endif //LUNCHBOX_PERTHREAD_H
|