Lunchbox  1.13.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 {
25 
27 template< typename T > 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  { return ( get() == rhs.get( )); }
70 
75  bool operator == ( const RefPtr< T >& rhs ) const
76  { return ( get()==rhs ); }
77 
82  bool operator != ( const RefPtr< T >& rhs ) const
83  { return ( get()!=rhs ); }
84 
89  bool operator ! () const;
90 
95  bool isValid() const;
96 
97 private:
98  TLS tls_;
99 };
100 
101 template< typename T > PerThreadRef<T>::PerThreadRef()
102  : tls_( 0 )
103 {}
104 
105 template< typename T > PerThreadRef<T>::~PerThreadRef()
106 {
107  RefPtr< T > object = get();
108  object.unref();
109 }
110 
111 template< typename T >
113 {
114  data.ref(); // ref new
115 
116  RefPtr< T > object = get();
117  tls_.set( static_cast<const void*>( data.get( )));
118 
119  object.unref(); // unref old
120  return *this;
121 }
122 
123 template< typename T >
125 {
126  RefPtr< T > newObject = rhs.get();
127  newObject.ref(); // ref new
128 
129  RefPtr< T > object = get();
130  tls_.set( rhs.tls_.get( ));
131 
132  object.unref(); // unref old
133  return *this;
134 }
135 
136 template< typename T > RefPtr< const T > PerThreadRef<T>::get() const
137 {
138  return static_cast< const T* >( tls_.get( ));
139 }
140 
141 template< typename T > RefPtr< T > PerThreadRef<T>::get()
142 {
143  return static_cast< T* >( tls_.get( ));
144 }
145 
146 template< typename T >
148 {
149  return static_cast< T* >( tls_.get( ));
150 }
151 
152 template< typename T >
154 {
155  LBASSERT( tls_.get( ));
156  return static_cast< T* >( tls_.get( ));
157 }
158 
159 template< typename T >
161 {
162  LBASSERT( tls_.get( ));
163  return static_cast< const T* >( tls_.get( ));
164 }
165 
166 template< typename T >
168 {
169  return tls_.get() == 0;
170 }
171 
172 template< typename T >
174 {
175  return tls_.get() != 0;
176 }
177 
178 }
179 #endif //LUNCHBOX_PERTHREADREF_H
void * get()
bool isValid() const
Definition: perThreadRef.h:173
~PerThreadRef()
Destruct a per-thread RefPtr.
Definition: perThreadRef.h:105
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:112
Abstraction layer and common utilities for multi-threaded programming.
Definition: algorithm.h:32
A smart reference pointer, aka boost::intrusive_ptr.
Definition: refPtr.h:39
Thread-specific storage for a RefPtr.
Definition: perThreadRef.h:27
RefPtr< const T > get() const
Definition: perThreadRef.h:136
Provides thread-local storage API used by PerThread and PerThreadRef.
Definition: tls.h:29
PerThreadRef()
Construct a new per-thread RefPtr.
Definition: perThreadRef.h:101
bool operator!() const
Definition: perThreadRef.h:167
bool operator==(const PerThreadRef &rhs) const
Definition: perThreadRef.h:68