From: Matthias Maier Date: Tue, 26 May 2020 19:06:56 +0000 (-0500) Subject: Add && variants for constuctor and operator=, optimize code X-Git-Tag: v9.3.0-rc1~1535^2~10 X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=e20b92f68258423b77978c6209fec62d7e373337;p=dealii.git Add && variants for constuctor and operator=, optimize code - avoid unnecessary copies --- diff --git a/include/deal.II/base/thread_local_storage.h b/include/deal.II/base/thread_local_storage.h index 5483f957ae..3c0e6335c6 100644 --- a/include/deal.II/base/thread_local_storage.h +++ b/include/deal.II/base/thread_local_storage.h @@ -19,6 +19,9 @@ # include +# include + +# include # include # include @@ -62,8 +65,8 @@ namespace Threads class ThreadLocalStorage { static_assert( - std::is_trivially_copyable::value || - std::is_copy_constructible::value, + std::is_copy_constructible::value || + std::is_default_constructible::value, "The stored type must be either copyable, or default constructible"); public: @@ -79,12 +82,22 @@ namespace Threads */ explicit ThreadLocalStorage(const T &t); + /** + * FIXME + */ + explicit ThreadLocalStorage(T &&t); + /** * Copy constructor. Initialize each thread local object with the * corresponding object of the given object. */ ThreadLocalStorage(const ThreadLocalStorage &t) = default; + /** + * Move constructor. FIXME + */ + ThreadLocalStorage(ThreadLocalStorage &&t) = default; + /** * Return a reference to the data stored by this object for the current * thread this function is called on. @@ -131,6 +144,13 @@ namespace Threads ThreadLocalStorage & operator=(const T &t); + + /** + * FIXME + */ + ThreadLocalStorage & + operator=(T &&t); + /** * Remove the thread-local objects stored for all threads that have * created one with this object (i.e., that have called get() at least @@ -195,6 +215,13 @@ namespace Threads } + template + inline ThreadLocalStorage::ThreadLocalStorage(T &&t) + { + exemplar = std::make_shared(std::forward(t)); + } + + template inline T & ThreadLocalStorage::get(bool &exists) @@ -212,7 +239,8 @@ namespace Threads // Take a shared ("reader") lock for lookup and record the fact // whether we could find an entry in the boolean exists. std::shared_lock lock(insertion_mutex); - const auto it = data.find(my_id); + + const auto it = data.find(my_id); if (it != data.end()) { exists = true; @@ -230,19 +258,19 @@ namespace Threads // std::unique_lock lock(insertion_mutex); - if constexpr (std::is_trivially_copyable::value) + if constexpr (std::is_copy_constructible::value) if (exemplar) { - const auto it = - data.emplace(std::make_pair(my_id, *exemplar)).first; + const auto it = data.emplace(my_id, *exemplar).first; return it->second; } if constexpr (std::is_default_constructible::value) { - const auto it = data.emplace(std::make_pair(my_id, T())).first; - return it->second; + return data[my_id]; // default construct } + + Assert(false, ExcInternalError()); } } @@ -272,6 +300,15 @@ namespace Threads } + template + inline ThreadLocalStorage & + ThreadLocalStorage::operator=(T &&t) + { + get() = std::forward(t); + return *this; + } + + template inline void ThreadLocalStorage::clear()