diff --git a/src/Common/ProfileEvents.cpp b/src/Common/ProfileEvents.cpp index 3a102238fbe..c1763d8283f 100644 --- a/src/Common/ProfileEvents.cpp +++ b/src/Common/ProfileEvents.cpp @@ -950,6 +950,15 @@ Counters::Counters(VariableContext level_, Counters * parent_) counters = counters_holder.get(); } +Counters::Counters(Counters && src) noexcept + : counters(std::exchange(src.counters, nullptr)) + , counters_holder(std::move(src.counters_holder)) + , parent(src.parent.exchange(nullptr)) + , trace_profile_events(src.trace_profile_events) + , level(src.level) +{ +} + void Counters::resetCounters() { if (counters) @@ -961,7 +970,7 @@ void Counters::resetCounters() void Counters::reset() { - parent = nullptr; + setParent(nullptr); resetCounters(); } diff --git a/src/Common/ProfileEvents.h b/src/Common/ProfileEvents.h index 94551c23ac0..012fe4ffdfa 100644 --- a/src/Common/ProfileEvents.h +++ b/src/Common/ProfileEvents.h @@ -60,7 +60,7 @@ namespace ProfileEvents Counter * counters = nullptr; std::unique_ptr counters_holder; /// Used to propagate increments - Counters * parent = nullptr; + std::atomic parent = {}; bool trace_profile_events = false; public: @@ -74,6 +74,8 @@ namespace ProfileEvents explicit Counters(Counter * allocated_counters) noexcept : counters(allocated_counters), parent(nullptr), level(VariableContext::Global) {} + Counters(Counters && src) noexcept; + Counter & operator[] (Event event) { return counters[event]; @@ -114,13 +116,13 @@ namespace ProfileEvents /// Get parent (thread unsafe) Counters * getParent() { - return parent; + return parent.load(std::memory_order_relaxed); } /// Set parent (thread unsafe) void setParent(Counters * parent_) { - parent = parent_; + parent.store(parent_, std::memory_order_relaxed); } void setTraceProfileEvents(bool value)