Reference documentation for deal.II version 8.1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
thread_local_storage.h
1 // ---------------------------------------------------------------------
2 // @f$Id: thread_local_storage.h 30036 2013-07-18 16:55:32Z maier @f$
3 //
4 // Copyright (C) 2011 - 2013 by the deal.II authors
5 //
6 // This file is part of the deal.II library.
7 //
8 // The deal.II library is free software; you can use it, redistribute
9 // it, and/or modify it under the terms of the GNU Lesser General
10 // Public License as published by the Free Software Foundation; either
11 // version 2.1 of the License, or (at your option) any later version.
12 // The full text of the license can be found in the file LICENSE at
13 // the top level of the deal.II distribution.
14 //
15 // ---------------------------------------------------------------------
16 
17 #ifndef __deal2__thread_local_storage_h
18 #define __deal2__thread_local_storage_h
19 
20 
21 #include <deal.II/base/config.h>
22 
23 #ifdef DEAL_II_WITH_THREADS
24 # include <tbb/enumerable_thread_specific.h>
25 #endif
26 
27 
28 
29 DEAL_II_NAMESPACE_OPEN
30 
33 
34 
35 namespace Threads
36 {
68  template <typename T>
70  {
71  public:
77 
82  explicit ThreadLocalStorage (const T &t);
83 
89 
104  T &get ();
105 
110  T &get (bool &exists);
111 
118  operator T &();
119 
132  ThreadLocalStorage<T> &operator = (const T &t);
133 
137 #ifdef DEAL_II_WITH_THREADS
138  tbb::enumerable_thread_specific<T> &
139 #else
140  T &
141 #endif
143  {
144  return data;
145  }
146 
147  private:
148 #ifdef DEAL_II_WITH_THREADS
149 
155  tbb::enumerable_thread_specific<T> data;
156 #else
157  T data;
158 #endif
159  };
160 
161 // ----------------- inline and template functions ----------------------------
162 
163  template <typename T>
164  inline
166  {}
167 
168 
169  template <typename T>
170  inline
172  :
173  data (t)
174  {}
175 
176 
177  template <typename T>
178  inline
180  :
181  data (t)
182  {}
183 
184 
185  template <typename T>
186  inline
187  T &
189  {
190 #ifdef DEAL_II_WITH_THREADS
191  return data.local();
192 #else
193  return data;
194 #endif
195  }
196 
197 
198  template <typename T>
199  inline
200  T &
202  {
203 #ifdef DEAL_II_WITH_THREADS
204  return data.local(exists);
205 #else
206  exists = true;
207  return data;
208 #endif
209  }
210 
211 
212  template <typename T>
213  inline
215  {
216  return get();
217  }
218 
219 
220  template <typename T>
221  inline
224  {
225  get() = t;
226  return *this;
227  }
228 } // end of implementation of namespace Threads
229 
235 //---------------------------------------------------------------------------
236 DEAL_II_NAMESPACE_CLOSE
237 // end of #ifndef __deal2__thread_local_storage_h
238 #endif
239 //---------------------------------------------------------------------------
A class that provides a separate storage location on each thread that accesses the object...
tbb::enumerable_thread_specific< T > & get_implementation()
tbb::enumerable_thread_specific< T > data
ThreadLocalStorage< T > & operator=(const T &t)