16#ifndef dealii_thread_local_storage_h
17# define dealii_thread_local_storage_h
28# include <shared_mutex>
57 struct unpack_container
62 template <
typename T,
typename A>
63 struct unpack_container<
std::vector<T, A>>
68 template <
typename T,
typename A>
69 struct unpack_container<
std::list<T, A>>
103 template <
typename T>
107 std::is_copy_constructible<
108 typename internal::unpack_container<T>::type>::value ||
109 std::is_default_constructible<T>::value,
110 "The stored type must be either copyable, or default constructible");
244 std::map<std::thread::id, T>
data;
254# ifdef DEAL_II_HAVE_CXX17
265 friend class ::LogStream;
278 template <
typename T>
280 : exemplar(t.exemplar)
284 std::shared_lock<
decltype(insertion_mutex)> lock(t.insertion_mutex);
290 template <
typename T>
292 : exemplar(std::move(t.exemplar))
312 std::unique_lock<
decltype(insertion_mutex)> lock(t.insertion_mutex);
313 data = std::move(t.data);
318 template <
typename T>
320 : exemplar(
std::make_shared<const T>(t))
325 template <
typename T>
327 : exemplar(
std::make_shared<T>(
std::forward<T>(t)))
332 template <
typename T>
333 inline ThreadLocalStorage<T> &
338 std::shared_lock<
decltype(insertion_mutex)> reader_lock(t.insertion_mutex);
339 std::unique_lock<
decltype(insertion_mutex)> writer_lock(insertion_mutex);
342 exemplar = t.exemplar;
349 template <
typename T>
350 inline ThreadLocalStorage<T> &
359 std::unique_lock<
decltype(insertion_mutex)> reader_lock(t.insertion_mutex);
360 std::unique_lock<
decltype(insertion_mutex)> writer_lock(insertion_mutex);
362 data = std::move(t.data);
363 exemplar = std::move(t.exemplar);
378 template <
typename T>
380 std::is_copy_constructible<typename unpack_container<T>::type>::value,
382 construct_element(std::map<std::thread::id, T> & data,
383 const std::thread::id &
id,
384 const std::shared_ptr<const T> &exemplar)
388 const auto it = data.emplace(
id, *exemplar).first;
394 template <
typename T>
396 !std::is_copy_constructible<typename unpack_container<T>::type>::value,
398 construct_element(std::map<std::thread::id, T> &data,
399 const std::thread::id &
id,
400 const std::shared_ptr<const T> &)
408 template <
typename T>
412 const std::thread::id my_id = std::this_thread::get_id();
424 std::shared_lock<
decltype(insertion_mutex)> lock(insertion_mutex);
426 const auto it = data.find(my_id);
427 if (it != data.end())
441 std::unique_lock<
decltype(insertion_mutex)> lock(insertion_mutex);
443 return internal::construct_element(data, my_id, exemplar);
448 template <
typename T>
457 template <
typename T>
458 inline ThreadLocalStorage<T>::operator T &()
464 template <
typename T>
465 inline ThreadLocalStorage<T> &
473 template <
typename T>
474 inline ThreadLocalStorage<T> &
477 get() = std::forward<T>(t);
482 template <
typename T>
486 std::unique_lock<
decltype(insertion_mutex)> lock(insertion_mutex);
A class that provides a separate storage location on each thread that accesses the object.
ThreadLocalStorage & operator=(const ThreadLocalStorage &t)
ThreadLocalStorage(const ThreadLocalStorage &)
ThreadLocalStorage(const T &t)
ThreadLocalStorage()=default
std::map< std::thread::id, T > data
ThreadLocalStorage< T > & operator=(const T &t)
std::shared_ptr< const T > exemplar
ThreadLocalStorage(T &&t)
ThreadLocalStorage & operator=(ThreadLocalStorage &&t) noexcept
ThreadLocalStorage< T > & operator=(T &&t)
ThreadLocalStorage(ThreadLocalStorage &&t) noexcept
std::shared_mutex insertion_mutex
#define DEAL_II_NAMESPACE_OPEN
#define DEAL_II_NAMESPACE_CLOSE