Line data Source code
1 :
2 : /* Copyright (c) 2005-2016, 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/debug.h> // LBASSERTINFO
23 : #include <lunchbox/tls.h> // member
24 :
25 : namespace lunchbox
26 : {
27 : namespace detail { class PerThread; }
28 :
29 : /** Default PerThread destructor deleting the object. @version 1.1.2 */
30 1049 : template< class T > void perThreadDelete( T* object ) { delete object; }
31 :
32 : /** Empty PerThread destructor. @version 1.1.2 */
33 : template< class T > void perThreadNoDelete( T* ) {}
34 :
35 : /**
36 : * Implements thread-specific storage for C++ objects.
37 : *
38 : * The default destructor function deletes the object on thread exit.
39 : *
40 : * @param T the type of data to store in thread-local storage
41 : * @param D the destructor callback function.
42 : * @deprecated Use boost::thread_specific_ptr
43 : *
44 : * Example: @include tests/perThread.cpp
45 : */
46 : template< class T, void (*D)( T* ) = &perThreadDelete< T > >
47 : class PerThread
48 : {
49 : public:
50 : /** Construct a new per-thread variable. @version 1.0 */
51 : PerThread();
52 : /** Destruct the per-thread variable. @version 1.0 */
53 : ~PerThread();
54 :
55 : /** Assign an object to the thread-local storage. @version 1.0 */
56 : PerThread<T, D>& operator = ( const T* data );
57 : /** Assign an object from another thread-local storage. @version 1.0 */
58 : PerThread<T, D>& operator = ( const PerThread<T, D>& rhs );
59 :
60 : /** @return the held object pointer. @version 1.0 */
61 : T* get();
62 : /** @return the held object pointer. @version 1.0 */
63 : const T* get() const;
64 : /** Access the thread-local object. @version 1.0 */
65 : T* operator->();
66 : /** Access the thread-local object. @version 1.0 */
67 : const T* operator->() const;
68 :
69 : /** @return the held object reference. @version 1.0 */
70 6144 : T& operator*()
71 6144 : { LBASSERTINFO( get(), className( this )); return *get(); }
72 : /** @return the held object reference. @version 1.0 */
73 : const T& operator*() const
74 : { LBASSERTINFO( get(), className( this )); return *get(); }
75 :
76 : /**
77 : * @return true if the thread-local variables hold the same object.
78 : * @version 1.0
79 : */
80 : bool operator == ( const PerThread& rhs ) const
81 : { return ( get() == rhs.get( )); }
82 :
83 : /**
84 : * @return true if the thread-local variable holds the same object.
85 : * @version 1.0
86 : */
87 1024 : bool operator == ( const T* rhs ) const { return ( get()==rhs ); }
88 :
89 : /**
90 : * @return true if the thread-local variable holds another object.
91 : * @version 1.0
92 : */
93 : bool operator != ( const T* rhs ) const { return ( get()!=rhs ); }
94 :
95 : /**
96 : * @return true if the thread-local storage holds a 0 pointer.
97 : * @version 1.0
98 : */
99 : bool operator ! () const;
100 :
101 : /**
102 : * @return true if the thread-local storage holds a non-0 pointer.
103 : * @version 1.0
104 : */
105 : bool isValid() const;
106 :
107 : private:
108 : TLS tls_;
109 : PerThread( const PerThread& ) = delete;
110 : PerThread( PerThread&& ) = delete;
111 : PerThread& operator = ( const PerThread&& ) = delete;
112 : };
113 :
114 : }
115 :
116 : #include "perThread.ipp" // template implementation
117 :
118 : #endif //LUNCHBOX_PERTHREAD_H
|