diff --git a/lib/base/dictionary.cpp b/lib/base/dictionary.cpp index 435df3159..e939cadeb 100644 --- a/lib/base/dictionary.cpp +++ b/lib/base/dictionary.cpp @@ -37,7 +37,7 @@ Dictionary::Dictionary(std::initializer_list init) */ Value Dictionary::Get(const String& key) const { - ObjectLock olock(this); + std::shared_lock lock (m_DataMutex); auto it = m_Data.find(key); @@ -56,7 +56,7 @@ Value Dictionary::Get(const String& key) const */ bool Dictionary::Get(const String& key, Value *result) const { - ObjectLock olock(this); + std::shared_lock lock (m_DataMutex); auto it = m_Data.find(key); @@ -77,6 +77,7 @@ bool Dictionary::Get(const String& key, Value *result) const void Dictionary::Set(const String& key, Value value, bool overrideFrozen) { ObjectLock olock(this); + std::unique_lock lock (m_DataMutex); if (m_Frozen && !overrideFrozen) BOOST_THROW_EXCEPTION(std::invalid_argument("Value in dictionary must not be modified.")); @@ -91,7 +92,7 @@ void Dictionary::Set(const String& key, Value value, bool overrideFrozen) */ size_t Dictionary::GetLength() const { - ObjectLock olock(this); + std::shared_lock lock (m_DataMutex); return m_Data.size(); } @@ -104,7 +105,7 @@ size_t Dictionary::GetLength() const */ bool Dictionary::Contains(const String& key) const { - ObjectLock olock(this); + std::shared_lock lock (m_DataMutex); return (m_Data.find(key) != m_Data.end()); } @@ -146,6 +147,7 @@ Dictionary::Iterator Dictionary::End() void Dictionary::Remove(Dictionary::Iterator it, bool overrideFrozen) { ASSERT(OwnsLock()); + std::unique_lock lock (m_DataMutex); if (m_Frozen && !overrideFrozen) BOOST_THROW_EXCEPTION(std::invalid_argument("Dictionary must not be modified.")); @@ -162,6 +164,7 @@ void Dictionary::Remove(Dictionary::Iterator it, bool overrideFrozen) void Dictionary::Remove(const String& key, bool overrideFrozen) { ObjectLock olock(this); + std::unique_lock lock (m_DataMutex); if (m_Frozen && !overrideFrozen) BOOST_THROW_EXCEPTION(std::invalid_argument("Dictionary must not be modified.")); @@ -183,6 +186,7 @@ void Dictionary::Remove(const String& key, bool overrideFrozen) void Dictionary::Clear(bool overrideFrozen) { ObjectLock olock(this); + std::unique_lock lock (m_DataMutex); if (m_Frozen && !overrideFrozen) BOOST_THROW_EXCEPTION(std::invalid_argument("Dictionary must not be modified.")); @@ -192,7 +196,7 @@ void Dictionary::Clear(bool overrideFrozen) void Dictionary::CopyTo(const Dictionary::Ptr& dest) const { - ObjectLock olock(this); + std::shared_lock lock (m_DataMutex); for (const Dictionary::Pair& kv : m_Data) { dest->Set(kv.first, kv.second); @@ -222,7 +226,7 @@ Object::Ptr Dictionary::Clone() const DictionaryData dict; { - ObjectLock olock(this); + std::shared_lock lock (m_DataMutex); dict.reserve(GetLength()); @@ -242,7 +246,7 @@ Object::Ptr Dictionary::Clone() const */ std::vector Dictionary::GetKeys() const { - ObjectLock olock(this); + std::shared_lock lock (m_DataMutex); std::vector keys; diff --git a/lib/base/dictionary.hpp b/lib/base/dictionary.hpp index 227868751..e1abf826c 100644 --- a/lib/base/dictionary.hpp +++ b/lib/base/dictionary.hpp @@ -8,6 +8,7 @@ #include "base/value.hpp" #include #include +#include #include namespace icinga @@ -75,6 +76,7 @@ public: private: std::map m_Data; /**< The data for the dictionary. */ + mutable std::shared_timed_mutex m_DataMutex; bool m_Frozen{false}; };