Lunchbox  1.15.0
Multi-threaded C++ toolbox library for all application developers creating high-performance multi-threaded programs.
perThread.h
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 { 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
48 {
49 public:
51  PerThread();
53  ~PerThread();
54 
56  PerThread<T, D>& operator = ( const T* data );
58  PerThread<T, D>& operator = ( const PerThread<T, D>& rhs );
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  PerThread( const PerThread& ) = delete;
110  PerThread( PerThread&& ) = delete;
111  PerThread& operator = ( const PerThread&& ) = delete;
112 };
113 
114 }
115 
116 #include "perThread.ipp" // template implementation
117 
118 #endif //LUNCHBOX_PERTHREAD_H
std::string className(const T &object)
Print the RTTI name of the given class.
Definition: debug.h:74
const T & operator*() const
Definition: perThread.h:73
void perThreadNoDelete(T *)
Empty PerThread destructor.
Definition: perThread.h:33
Implements thread-specific storage for C++ objects.
Definition: perThread.h:47
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:28
void perThreadDelete(T *object)
Default PerThread destructor deleting the object.
Definition: perThread.h:30