diff options
| author | Mikaël Capelle <capelle.mikael@gmail.com> | 2020-11-18 19:22:13 +0100 |
|---|---|---|
| committer | Mikaël Capelle <capelle.mikael@gmail.com> | 2020-11-18 19:22:13 +0100 |
| commit | e76e7e034d2b42d5819b7b184cd7034ff51ebb26 (patch) | |
| tree | f244f239ca37e7da3321fcf0e7819bb4cb2d2fcc /src | |
| parent | e39de4dd2ab6c19c6b9557f99117f7ffc9ed1cc1 (diff) | |
Move MemoizedLock to uibase.
Diffstat (limited to 'src')
| -rw-r--r-- | src/modinfowithconflictinfo.h | 8 | ||||
| -rw-r--r-- | src/thread_utils.h | 45 |
2 files changed, 4 insertions, 49 deletions
diff --git a/src/modinfowithconflictinfo.h b/src/modinfowithconflictinfo.h index feded99b..e312a89a 100644 --- a/src/modinfowithconflictinfo.h +++ b/src/modinfowithconflictinfo.h @@ -3,7 +3,7 @@ #include <ifiletree.h> -#include "thread_utils.h" +#include "memoizedlock.h" #include "modinfo.h" #include <set> @@ -145,9 +145,9 @@ protected: private: - MOShared::MemoizedLocked<std::shared_ptr<const MOBase::IFileTree>> m_FileTree; - MOShared::MemoizedLocked<bool> m_Valid; - MOShared::MemoizedLocked<std::set<int>> m_Contents; + MOBase::MemoizedLocked<std::shared_ptr<const MOBase::IFileTree>> m_FileTree; + MOBase::MemoizedLocked<bool> m_Valid; + MOBase::MemoizedLocked<std::set<int>> m_Contents; MOShared::DirectoryEntry **m_DirectoryStructure; diff --git a/src/thread_utils.h b/src/thread_utils.h index f64dd601..e816a4e2 100644 --- a/src/thread_utils.h +++ b/src/thread_utils.h @@ -24,51 +24,6 @@ std::thread startSafeThread(F&& f) }); } - -/** - * Class that can be used to perform thread-safe memoization. - * - * Each instance hold a flag indicating if the current value is up-to-date - * or not. This flag can be reset using `invalidate()`. When the value is queried, - * the flag is checked, and if it is not up-to-date, the given callback is used - * to compute the value. - * - * The computation and update of the value is locked to avoid concurrent modifications. - * - * @tparam T Type of value ot memoized. - * @tparam Fn Type of the callback. - */ -template <class T, class Fn = std::function<T()>> -struct MemoizedLocked { - - template <class Callable> - MemoizedLocked(Callable &&callable, T value = {}) : - m_Fn{ std::forward<Callable>(callable) }, m_Value{ std::move(value) } { } - - template <class... Args> - T& value(Args&&... args) const { - if (m_NeedUpdating) { - std::scoped_lock lock(m_Mutex); - if (m_NeedUpdating) { - m_Value = std::invoke(m_Fn, std::forward<Args>(args)... ); - m_NeedUpdating = false; - } - } - return m_Value; - } - - void invalidate() { - m_NeedUpdating = true; - } - -private: - mutable std::mutex m_Mutex; - mutable std::atomic<bool> m_NeedUpdating{ true }; - - Fn m_Fn; - mutable T m_Value; -}; - /** * @brief Apply the given callable to each element between the two given iterators * in a parallel way. |
