Reference documentation for deal.II version 9.0.0
thread_local_storage.h
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 2011 - 2017 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:
77  ThreadLocalStorage () = default;
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  data (t)
187  {}
188 
189 
190  template <typename T>
191  inline
193  :
194  data (t)
195  {}
196 
197 
198  template <typename T>
199  inline
200  T &
202  {
203 #ifdef DEAL_II_WITH_THREADS
204  return data.local();
205 #else
206  return data;
207 #endif
208  }
209 
210 
211  template <typename T>
212  inline
213  T &
215  {
216 #ifdef DEAL_II_WITH_THREADS
217  return data.local(exists);
218 #else
219  exists = true;
220  return data;
221 #endif
222  }
223 
224 
225  template <typename T>
226  inline
228  {
229  return get();
230  }
231 
232 
233  template <typename T>
234  inline
235  ThreadLocalStorage<T> &
237  {
238  get() = t;
239  return *this;
240  }
241 
242 
243  template <typename T>
244  inline
245 #ifdef DEAL_II_WITH_THREADS
246  tbb::enumerable_thread_specific<T> &
247 #else
248  T &
249 #endif
251  {
252  return data;
253  }
254 
255 
256 
257  template <typename T>
258  inline
259  void
261  {
262 #ifdef DEAL_II_WITH_THREADS
263  data.clear ();
264 #else
265  data = T {};
266 #endif
267  }
268 } // end of implementation of namespace Threads
269 
275 //---------------------------------------------------------------------------
276 DEAL_II_NAMESPACE_CLOSE
277 // end of #ifndef dealii_thread_local_storage_h
278 #endif
279 //---------------------------------------------------------------------------
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)