Lunchbox  1.16.0
Multi-threaded C++ toolbox library for all application developers creating high-performance multi-threaded programs.
perThreadRef.h
1 
2 /* Copyright (c) 2008-2013, 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_PERTHREADREF_H
19 #define LUNCHBOX_PERTHREADREF_H
20 
21 #include <lunchbox/tls.h> // member
22 
23 namespace lunchbox
24 {
26 template <typename T>
27 class PerThreadRef : public boost::noncopyable
28 {
29 public:
31  PerThreadRef();
33  ~PerThreadRef();
34 
37 
40 
42  RefPtr<const T> get() const;
44  RefPtr<T> get();
45 
50  T* getPointer();
51 
56  T* operator->();
57 
62  const T* operator->() const;
63 
68  bool operator==(const PerThreadRef& rhs) const
69  {
70  return (get() == rhs.get());
71  }
72 
77  bool operator==(const RefPtr<T>& rhs) const { return (get() == rhs); }
82  bool operator!=(const RefPtr<T>& rhs) const { return (get() != rhs); }
87  bool operator!() const;
88 
93  bool isValid() const;
94 
95 private:
96  TLS tls_;
97 };
98 
99 template <typename T>
101  : tls_(0)
102 {
103 }
104 
105 template <typename T>
107 {
108  RefPtr<T> object = get();
109  object.unref();
110 }
111 
112 template <typename T>
114 {
115  data.ref(); // ref new
116 
117  RefPtr<T> object = get();
118  tls_.set(static_cast<const void*>(data.get()));
119 
120  object.unref(); // unref old
121  return *this;
122 }
123 
124 template <typename T>
126 {
127  RefPtr<T> newObject = rhs.get();
128  newObject.ref(); // ref new
129 
130  RefPtr<T> object = get();
131  tls_.set(rhs.tls_.get());
132 
133  object.unref(); // unref old
134  return *this;
135 }
136 
137 template <typename T>
139 {
140  return static_cast<const T*>(tls_.get());
141 }
142 
143 template <typename T>
145 {
146  return static_cast<T*>(tls_.get());
147 }
148 
149 template <typename T>
151 {
152  return static_cast<T*>(tls_.get());
153 }
154 
155 template <typename T>
157 {
158  LBASSERT(tls_.get());
159  return static_cast<T*>(tls_.get());
160 }
161 
162 template <typename T>
164 {
165  LBASSERT(tls_.get());
166  return static_cast<const T*>(tls_.get());
167 }
168 
169 template <typename T>
171 {
172  return tls_.get() == 0;
173 }
174 
175 template <typename T>
177 {
178  return tls_.get() != 0;
179 }
180 }
181 #endif // LUNCHBOX_PERTHREADREF_H
void * get()
bool isValid() const
Definition: perThreadRef.h:176
~PerThreadRef()
Destruct a per-thread RefPtr.
Definition: perThreadRef.h:106
bool operator!=(const RefPtr< T > &rhs) const
Definition: perThreadRef.h:82
PerThreadRef< T > & operator=(RefPtr< T > data)
Assign a RefPtr to the thread-local storage.
Definition: perThreadRef.h:113
void set(const void *data)
Set the data for this thread-local storage.
Abstraction layer and common utilities for multi-threaded programming.
Definition: algorithm.h:29
bool operator==(const RefPtr< T > &rhs) const
Definition: perThreadRef.h:77
A smart reference pointer, aka boost::intrusive_ptr.
Definition: refPtr.h:40
Thread-specific storage for a RefPtr.
Definition: perThreadRef.h:27
RefPtr< const T > get() const
Definition: perThreadRef.h:138
Provides thread-local storage API used by PerThread and PerThreadRef.
Definition: tls.h:31
PerThreadRef()
Construct a new per-thread RefPtr.
Definition: perThreadRef.h:100
bool operator!() const
Definition: perThreadRef.h:170
bool operator==(const PerThreadRef &rhs) const
Definition: perThreadRef.h:68