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