Reference documentation for deal.II version 9.1.1
\(\newcommand{\dealcoloneq}{\mathrel{\vcenter{:}}=}\)
thread_local_storage.h
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 2011 - 2018 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 DEAL_II_DISABLE_EXTRA_DIAGNOSTICS
23 # ifdef DEAL_II_WITH_THREADS
24 # include <tbb/enumerable_thread_specific.h>
25 # endif
26 DEAL_II_ENABLE_EXTRA_DIAGNOSTICS
27 
28 
29 
30 DEAL_II_NAMESPACE_OPEN
31 
34 
35 
36 namespace Threads
37 {
71  template <typename T>
73  {
74  public:
79  ThreadLocalStorage() = default;
80 
85  explicit ThreadLocalStorage(const T &t);
86 
92 
106  T &
107  get();
108 
113  T &
114  get(bool &exists);
115 
122  operator T &();
123 
137  operator=(const T &t);
138 
158  void
159  clear();
160 
166 # ifdef DEAL_II_WITH_THREADS
167  tbb::enumerable_thread_specific<T> &
168 # else
169  T &
170 # endif
172 
173  private:
174 # ifdef DEAL_II_WITH_THREADS
175 
180  tbb::enumerable_thread_specific<T> data;
181 # else
182  T data;
183 # endif
184  };
185 
186  // ----------------- inline and template functions --------------------------
187 
188  template <typename T>
190  : data(t)
191  {}
192 
193 
194  template <typename T>
196  const ThreadLocalStorage<T> &t)
197  : data(t)
198  {}
199 
200 
201  template <typename T>
202  inline T &
204  {
205 # ifdef DEAL_II_WITH_THREADS
206  return data.local();
207 # else
208  return data;
209 # endif
210  }
211 
212 
213  template <typename T>
214  inline T &
216  {
217 # ifdef DEAL_II_WITH_THREADS
218  return data.local(exists);
219 # else
220  exists = true;
221  return data;
222 # endif
223  }
224 
225 
226  template <typename T>
228  {
229  return get();
230  }
231 
232 
233  template <typename T>
234  inline ThreadLocalStorage<T> &
236  {
237  get() = t;
238  return *this;
239  }
240 
241 
242  template <typename T>
243  inline
244 # ifdef DEAL_II_WITH_THREADS
245  tbb::enumerable_thread_specific<T> &
246 # else
247  T &
248 # endif
250  {
251  return data;
252  }
253 
254 
255 
256  template <typename T>
257  inline void
259  {
260 # ifdef DEAL_II_WITH_THREADS
261  data.clear();
262 # else
263  data = T{};
264 # endif
265  }
266 } // namespace Threads
267 
273 //---------------------------------------------------------------------------
274 DEAL_II_NAMESPACE_CLOSE
275 // end of #ifndef dealii_thread_local_storage_h
276 #endif
277 //---------------------------------------------------------------------------
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)