ApplyRule: Make m_HasMatches atomic

This prevents the `m_HasMatches` property from being altered simultaneously.
This might seem harmless (since this property can only be set to true by any calling thread),
however, from a technical (C++) point of view, this constitutes a data race.
This commit is contained in:
Yonas Habteab 2022-09-07 09:16:22 +02:00
parent ae32b3cbbd
commit 834709543a
2 changed files with 4 additions and 3 deletions

View File

@ -126,12 +126,12 @@ const std::vector<String>& ApplyRule::GetTargetTypes(const String& sourceType)
void ApplyRule::AddMatch() void ApplyRule::AddMatch()
{ {
m_HasMatches = true; m_HasMatches.store(true, std::memory_order_relaxed);
} }
bool ApplyRule::HasMatches() const bool ApplyRule::HasMatches() const
{ {
return m_HasMatches; return m_HasMatches.load(std::memory_order_relaxed);
} }
const std::vector<ApplyRule::Ptr>& ApplyRule::GetRules(const Type::Ptr& sourceType, const Type::Ptr& targetType) const std::vector<ApplyRule::Ptr>& ApplyRule::GetRules(const Type::Ptr& sourceType, const Type::Ptr& targetType)

View File

@ -9,6 +9,7 @@
#include "base/shared-object.hpp" #include "base/shared-object.hpp"
#include "base/type.hpp" #include "base/type.hpp"
#include <unordered_map> #include <unordered_map>
#include <atomic>
namespace icinga namespace icinga
{ {
@ -101,7 +102,7 @@ private:
bool m_IgnoreOnError; bool m_IgnoreOnError;
DebugInfo m_DebugInfo; DebugInfo m_DebugInfo;
Dictionary::Ptr m_Scope; Dictionary::Ptr m_Scope;
bool m_HasMatches; std::atomic<bool> m_HasMatches;
static TypeMap m_Types; static TypeMap m_Types;
static RuleMap m_Rules; static RuleMap m_Rules;