Lunchbox  1.13.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/debug.h> // LBASSERTINFO
23 #include <lunchbox/tls.h> // member
24 
25 namespace lunchbox
26 {
27 namespace detail { class PerThread; }
28 
30 template< class T > void perThreadDelete( T* object ) { delete object; }
31 
33 template< class T > void perThreadNoDelete( T* ) {}
34 
46 template< class T, void (*D)( T* ) = &perThreadDelete< T > >
47 class PerThread : public boost::noncopyable
48 {
49 public:
51  PerThread();
53  ~PerThread();
54 
56  PerThread<T, D>& operator = ( const T* data );
59 
61  T* get();
63  const T* get() const;
65  T* operator->();
67  const T* operator->() const;
68 
70  T& operator*()
71  { LBASSERTINFO( get(), className( this )); return *get(); }
73  const T& operator*() const
74  { LBASSERTINFO( get(), className( this )); return *get(); }
75 
80  bool operator == ( const PerThread& rhs ) const
81  { return ( get() == rhs.get( )); }
82 
87  bool operator == ( const T* rhs ) const { return ( get()==rhs ); }
88 
93  bool operator != ( const T* rhs ) const { return ( get()!=rhs ); }
94 
99  bool operator ! () const;
100 
105  bool isValid() const;
106 
107 private:
108  TLS tls_;
109 }; // LB_DEPRECATED;
110 
111 }
112 
113 #include "perThread.ipp" // template implementation
114 
115 #endif //LUNCHBOX_PERTHREAD_H
PerThread()
Construct a new per-thread variable.
Definition: perThread.ipp:20
const T & operator*() const
Definition: perThread.h:73
void perThreadNoDelete(T *)
Empty PerThread destructor.
Definition: perThread.h:33
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:93
~PerThread()
Destruct the per-thread variable.
Definition: perThread.ipp:24
Implements thread-specific storage for C++ objects.
Definition: perThread.h:47
bool operator==(const PerThread &rhs) const
Definition: perThread.h:80
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:30