Reference documentation for deal.II version 9.1.0-pre
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.md at
12 // the top level directory of deal.II.
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 &
105  get();
106 
111  T &
112  get(bool &exists);
113 
120  operator T &();
121 
135  operator=(const T &t);
136 
156  void
157  clear();
158 
164 # ifdef DEAL_II_WITH_THREADS
165  tbb::enumerable_thread_specific<T> &
166 # else
167  T &
168 # endif
170 
171  private:
172 # ifdef DEAL_II_WITH_THREADS
173 
178  tbb::enumerable_thread_specific<T> data;
179 # else
180  T data;
181 # endif
182  };
183 
184  // ----------------- inline and template functions --------------------------
185 
186  template <typename T>
188  : data(t)
189  {}
190 
191 
192  template <typename T>
194  const ThreadLocalStorage<T> &t)
195  : data(t)
196  {}
197 
198 
199  template <typename T>
200  inline 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 T &
214  {
215 # ifdef DEAL_II_WITH_THREADS
216  return data.local(exists);
217 # else
218  exists = true;
219  return data;
220 # endif
221  }
222 
223 
224  template <typename T>
226  {
227  return get();
228  }
229 
230 
231  template <typename T>
232  inline ThreadLocalStorage<T> &
234  {
235  get() = t;
236  return *this;
237  }
238 
239 
240  template <typename T>
241  inline
242 # ifdef DEAL_II_WITH_THREADS
243  tbb::enumerable_thread_specific<T> &
244 # else
245  T &
246 # endif
248  {
249  return data;
250  }
251 
252 
253 
254  template <typename T>
255  inline void
257  {
258 # ifdef DEAL_II_WITH_THREADS
259  data.clear();
260 # else
261  data = T{};
262 # endif
263  }
264 } // namespace Threads
265 
271 //---------------------------------------------------------------------------
272 DEAL_II_NAMESPACE_CLOSE
273 // end of #ifndef dealii_thread_local_storage_h
274 #endif
275 //---------------------------------------------------------------------------
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)