Use a shared_mutex for read Namespace operations

This allows multiple parallel read operations resulting in a overall speedup on
systems with many cores.
This commit is contained in:
Julian Brost 2022-12-07 13:37:17 +01:00
parent 1c066fc02e
commit cc0e2ec181
2 changed files with 36 additions and 6 deletions

View File

@ -25,8 +25,6 @@ Namespace::Namespace(bool constValues)
Value Namespace::Get(const String& field) const
{
ObjectLock olock(this);
Value value;
if (!Get(field, &value))
BOOST_THROW_EXCEPTION(ScriptError("Namespace does not contain field '" + field + "'"));
@ -35,7 +33,7 @@ Value Namespace::Get(const String& field) const
bool Namespace::Get(const String& field, Value *value) const
{
ObjectLock olock(this);
std::shared_lock<decltype(m_DataMutex)> lock(m_DataMutex);
auto nsVal = m_Data.find(field);
@ -50,6 +48,7 @@ bool Namespace::Get(const String& field, Value *value) const
void Namespace::Set(const String& field, const Value& value, bool isConst, bool overrideFrozen, const DebugInfo& debugInfo)
{
ObjectLock olock(this);
std::unique_lock<decltype(m_DataMutex)> dlock (m_DataMutex);
auto nsVal = m_Data.find(field);
@ -75,14 +74,14 @@ void Namespace::Set(const String& field, const Value& value, bool isConst, bool
*/
size_t Namespace::GetLength() const
{
ObjectLock olock(this);
std::shared_lock<decltype(m_DataMutex)> lock(m_DataMutex);
return m_Data.size();
}
bool Namespace::Contains(const String& field) const
{
ObjectLock olock(this);
std::shared_lock<decltype(m_DataMutex)> lock(m_DataMutex);
return m_Data.find(field) != m_Data.end();
}
@ -95,6 +94,8 @@ void Namespace::Remove(const String& field, bool overrideFrozen)
BOOST_THROW_EXCEPTION(ScriptError("Namespace is read-only and must not be modified."));
}
std::unique_lock<decltype(m_DataMutex)> dlock (m_DataMutex);
if (!overrideFrozen) {
auto attr = m_Data.find(field);
@ -125,7 +126,7 @@ void Namespace::Freeze() {
Value Namespace::GetFieldByName(const String& field, bool, const DebugInfo& debugInfo) const
{
ObjectLock olock(this);
std::shared_lock<decltype(m_DataMutex)> lock(m_DataMutex);
auto nsVal = m_Data.find(field);

View File

@ -11,6 +11,7 @@
#include <map>
#include <vector>
#include <memory>
#include <shared_mutex>
namespace icinga
{
@ -25,6 +26,33 @@ struct NamespaceValue
/**
* A namespace.
*
* ## External Locking
*
* Synchronization is handled internally, so almost all functions are safe for concurrent use without external locking.
* The only exception to this are functions returning an iterator. To use these, the caller has to acquire an ObjectLock
* on the namespace. The iterators only remain valid for as long as that ObjectLock is held. Note that this also
* includes range-based for loops.
*
* If consistency across multiple operations is required, an ObjectLock must also be acquired to prevent concurrent
* modifications.
*
* ## Internal Locking
*
* Two mutex objects are involved in locking a namespace: the recursive mutex inherited from the Object class that is
* acquired and released using the ObjectLock class and the m_DataMutex shared mutex contained directly in the
* Namespace class. The ObjectLock is used to synchronize multiple write operations against each other. The shared mutex
* is only used to ensure the consistency of the m_Data data structure.
*
* Read operations must acquire a shared lock on m_DataMutex. This prevents concurrent writes to that data structure
* but still allows concurrent reads.
*
* Write operations must first obtain an ObjectLock and then a shared lock on m_DataMutex. This order is important for
* preventing deadlocks. The ObjectLock prevents concurrent write operations while the shared lock prevents concurrent
* read operations.
*
* External read access to iterators is synchronized by the caller holding an ObjectLock. This ensures no concurrent
* write operations as these require the ObjectLock but still allows concurrent reads as m_DataMutex is not locked.
*
* @ingroup base
*/
class Namespace final : public Object
@ -59,6 +87,7 @@ public:
private:
std::map<String, NamespaceValue> m_Data;
mutable std::shared_timed_mutex m_DataMutex;
bool m_ConstValues;
bool m_Frozen;
};