15#ifndef dealii_thread_local_storage_h
16# 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_v<
108 typename internal::unpack_container<T>::type> ||
109 std::is_default_constructible_v<T>,
110 "The stored type must be either copyable, or default constructible");
261 std::map<std::thread::id, T>
data;
289 template <
typename T>
291 : exemplar(t.exemplar)
295 std::shared_lock<
decltype(insertion_mutex)> lock(t.insertion_mutex);
301 template <
typename T>
303 : exemplar(std::move(t.exemplar))
323 std::unique_lock<
decltype(insertion_mutex)> lock(t.insertion_mutex);
324 data = std::move(t.data);
329 template <
typename T>
331 : exemplar(
std::make_shared<const T>(t))
336 template <
typename T>
338 : exemplar(
std::make_shared<T>(
std::forward<T>(t)))
343 template <
typename T>
344 inline ThreadLocalStorage<T> &
349 std::shared_lock<
decltype(insertion_mutex)> reader_lock(t.insertion_mutex);
350 std::unique_lock<
decltype(insertion_mutex)> writer_lock(insertion_mutex);
353 exemplar = t.exemplar;
360 template <
typename T>
361 inline ThreadLocalStorage<T> &
370 std::unique_lock<
decltype(insertion_mutex)> reader_lock(t.insertion_mutex);
371 std::unique_lock<
decltype(insertion_mutex)> writer_lock(insertion_mutex);
373 data = std::move(t.data);
374 exemplar = std::move(t.exemplar);
389 template <
typename T>
391 std::is_copy_constructible_v<typename unpack_container<T>::type>,
393 construct_element(std::map<std::thread::id, T> &data,
394 const std::thread::id &
id,
395 const std::shared_ptr<const T> &exemplar)
399 const auto it = data.emplace(
id, *exemplar).first;
405 template <
typename T>
407 !std::is_copy_constructible_v<typename unpack_container<T>::type>,
409 construct_element(std::map<std::thread::id, T> &data,
410 const std::thread::id &
id,
411 const std::shared_ptr<const T> &)
419 template <
typename T>
423 const std::thread::id my_id = std::this_thread::get_id();
435 std::shared_lock<
decltype(insertion_mutex)> lock(insertion_mutex);
437 const auto it = data.find(my_id);
438 if (it != data.end())
452 std::unique_lock<
decltype(insertion_mutex)> lock(insertion_mutex);
454 return internal::construct_element(data, my_id, exemplar);
459 template <
typename T>
464 std::shared_lock<
decltype(insertion_mutex)> lock(insertion_mutex);
468 const auto it = data.find(
id);
469 if (it != data.end())
476 template <
typename T>
485 template <
typename T>
486 inline ThreadLocalStorage<T>::operator T &()
492 template <
typename T>
493 inline ThreadLocalStorage<T> &
501 template <
typename T>
502 inline ThreadLocalStorage<T> &
505 get() = std::forward<T>(t);
510 template <
typename T>
514 std::unique_lock<
decltype(insertion_mutex)> lock(insertion_mutex);
A class that provides a separate storage location on each thread that accesses the object.
std::optional< T > get_for_thread(const std::thread::id &id) const
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