Reference documentation for deal.II version 8.5.1
thread_local_storage.h
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 2011 - 2016 by the deal.II authors
4 //
5 // This file is part of the deal.II library.
6 //
7 // The deal.II library is free software; you can use it, redistribute
8 // it, and/or modify it under the terms of the GNU Lesser General
9 // Public License as published by the Free Software Foundation; either
10 // version 2.1 of the License, or (at your option) any later version.
11 // The full text of the license can be found in the file LICENSE at
12 // the top level of the deal.II distribution.
13 //
14 // ---------------------------------------------------------------------
15 
16 #ifndef dealii__thread_local_storage_h
17 #define dealii__thread_local_storage_h
18 
19 
20 #include <deal.II/base/config.h>
21 
22 #ifdef DEAL_II_WITH_THREADS
23 # include <tbb/enumerable_thread_specific.h>
24 #endif
25 
26 
27 
28 DEAL_II_NAMESPACE_OPEN
29 
32 
33 
34 namespace Threads
35 {
69  template <typename T>
71  {
72  public:
78 
83  explicit ThreadLocalStorage (const T &t);
84 
90 
104  T &get ();
105 
110  T &get (bool &exists);
111 
118  operator T &();
119 
132  ThreadLocalStorage<T> &operator = (const T &t);
133 
153  void clear ();
154 
160 #ifdef DEAL_II_WITH_THREADS
161  tbb::enumerable_thread_specific<T> &
162 #else
163  T &
164 #endif
166 
167  private:
168 #ifdef DEAL_II_WITH_THREADS
169 
174  tbb::enumerable_thread_specific<T> data;
175 #else
176  T data;
177 #endif
178  };
179 
180 // ----------------- inline and template functions ----------------------------
181 
182  template <typename T>
183  inline
185  {}
186 
187 
188  template <typename T>
189  inline
191  :
192  data (t)
193  {}
194 
195 
196  template <typename T>
197  inline
199  :
200  data (t)
201  {}
202 
203 
204  template <typename T>
205  inline
206  T &
208  {
209 #ifdef DEAL_II_WITH_THREADS
210  return data.local();
211 #else
212  return data;
213 #endif
214  }
215 
216 
217  template <typename T>
218  inline
219  T &
221  {
222 #ifdef DEAL_II_WITH_THREADS
223  return data.local(exists);
224 #else
225  exists = true;
226  return data;
227 #endif
228  }
229 
230 
231  template <typename T>
232  inline
234  {
235  return get();
236  }
237 
238 
239  template <typename T>
240  inline
241  ThreadLocalStorage<T> &
243  {
244  get() = t;
245  return *this;
246  }
247 
248 
249  template <typename T>
250  inline
251 #ifdef DEAL_II_WITH_THREADS
252  tbb::enumerable_thread_specific<T> &
253 #else
254  T &
255 #endif
257  {
258  return data;
259  }
260 
261 
262 
263  template <typename T>
264  inline
265  void
267  {
268 #ifdef DEAL_II_WITH_THREADS
269  data.clear ();
270 #endif
271  }
272 } // end of implementation of namespace Threads
273 
279 //---------------------------------------------------------------------------
280 DEAL_II_NAMESPACE_CLOSE
281 // end of #ifndef dealii__thread_local_storage_h
282 #endif
283 //---------------------------------------------------------------------------
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)