Lunchbox  1.16.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
28 {
29 class PerThread;
30 }
31 
33 template <class T>
34 void perThreadDelete(T* object)
35 {
36  delete object;
37 }
38 
40 template <class T>
42 {
43 }
44 
56 template <class T, void (*D)(T*) = &perThreadDelete<T> >
57 class PerThread
58 {
59 public:
61  PerThread();
63  ~PerThread();
64 
66  PerThread<T, D>& operator=(const T* data);
68  PerThread<T, D>& operator=(const PerThread<T, D>& rhs);
69 
71  T* get();
73  const T* get() const;
75  T* operator->();
77  const T* operator->() const;
78 
80  T& operator*()
81  {
82  LBASSERTINFO(get(), className(this));
83  return *get();
84  }
86  const T& operator*() const
87  {
88  LBASSERTINFO(get(), className(this));
89  return *get();
90  }
91 
96  bool operator==(const PerThread& rhs) const { return (get() == rhs.get()); }
101  bool operator==(const T* rhs) const { return (get() == rhs); }
106  bool operator!=(const T* rhs) const { return (get() != rhs); }
111  bool operator!() const;
112 
117  bool isValid() const;
118 
119 private:
120  TLS tls_;
121  PerThread(const PerThread&) = delete;
122  PerThread(PerThread&&) = delete;
123  PerThread& operator=(const PerThread&&) = delete;
124 };
125 }
126 
127 #include "perThread.ipp" // template implementation
128 
129 #endif // LUNCHBOX_PERTHREAD_H
std::string className(const T &object)
Print the RTTI name of the given class.
Definition: debug.h:75
const T & operator*() const
Definition: perThread.h:86
void perThreadNoDelete(T *)
Empty PerThread destructor.
Definition: perThread.h:41
bool operator!=(const T *rhs) const
Definition: perThread.h:106
Implements thread-specific storage for C++ objects.
Definition: perThread.h:57
bool operator==(const PerThread &rhs) const
Definition: perThread.h:96
bool operator==(const T *rhs) const
Definition: perThread.h:101
Abstraction layer and common utilities for multi-threaded programming.
Definition: algorithm.h:29
Provides thread-local storage API used by PerThread and PerThreadRef.
Definition: tls.h:31
void perThreadDelete(T *object)
Default PerThread destructor deleting the object.
Definition: perThread.h:34