Lunchbox  1.12.0
Multi-threaded C++ toolbox library for all application developers creating high-performance multi-threaded programs.
perThread.h
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 
29 template< class T > void perThreadDelete( T* object ) { delete object; }
30 
32 template< class T > void perThreadNoDelete( T* ) {}
33 
45 template< class T, void (*D)( T* ) = &perThreadDelete< T > >
46 class PerThread : public boost::noncopyable
47 {
48 public:
50  PerThread();
52  ~PerThread();
53 
55  PerThread<T, D>& operator = ( const T* data );
58 
60  T* get();
62  const T* get() const;
64  T* operator->();
66  const T* operator->() const;
67 
69  T& operator*()
70  { LBASSERTINFO( get(), className( this )); return *get(); }
72  const T& operator*() const
73  { LBASSERTINFO( get(), className( this )); return *get(); }
74 
79  bool operator == ( const PerThread& rhs ) const
80  { return ( get() == rhs.get( )); }
81 
86  bool operator == ( const T* rhs ) const { return ( get()==rhs ); }
87 
92  bool operator != ( const T* rhs ) const { return ( get()!=rhs ); }
93 
98  bool operator ! () const;
99 
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
PerThread()
Construct a new per-thread variable.
Definition: perThread.ipp:20
const T & operator*() const
Definition: perThread.h:72
void perThreadNoDelete(T *)
Empty PerThread destructor.
Definition: perThread.h:32
bool isValid() const
Definition: perThread.ipp:67
bool operator!() const
Definition: perThread.ipp:62
std::string className(const T *object)
Print the RTTI name of the given class.
Definition: debug.h:74
PerThread< T, D > & operator=(const T *data)
Assign an object to the thread-local storage.
Definition: perThread.ipp:28
bool operator!=(const T *rhs) const
Definition: perThread.h:92
~PerThread()
Destruct the per-thread variable.
Definition: perThread.ipp:24
Implements thread-specific storage for C++ objects.
Definition: perThread.h:46
bool operator==(const PerThread &rhs) const
Definition: perThread.h:79
T * operator->()
Access the thread-local object.
Definition: perThread.ipp:51
Abstraction layer and common utilities for multi-threaded programming.
Definition: algorithm.h:32
Provides thread-local storage API used by PerThread and PerThreadRef.
Definition: tls.h:29
void perThreadDelete(T *object)
Default PerThread destructor deleting the object.
Definition: perThread.h:29