diff --git a/profiler/src/ProfilerEngine/Datadog.Profiler.Native.Linux/ProfilerSignalManager.cpp b/profiler/src/ProfilerEngine/Datadog.Profiler.Native.Linux/ProfilerSignalManager.cpp index 3f4fd1feb982..cb9e61fb24e8 100644 --- a/profiler/src/ProfilerEngine/Datadog.Profiler.Native.Linux/ProfilerSignalManager.cpp +++ b/profiler/src/ProfilerEngine/Datadog.Profiler.Native.Linux/ProfilerSignalManager.cpp @@ -28,7 +28,7 @@ ProfilerSignalManager::~ProfilerSignalManager() noexcept _isHandlerInPlace = false; sigaction(_signalToSend, &_previousAction, nullptr); } - _handler = nullptr; + _handler.store(nullptr, std::memory_order_relaxed); } ProfilerSignalManager* ProfilerSignalManager::Get(int signal) @@ -49,7 +49,7 @@ ProfilerSignalManager* ProfilerSignalManager::Get(int signal) bool ProfilerSignalManager::RegisterHandler(HandlerFn_t handler) { - HandlerFn_t current = _handler; + HandlerFn_t current = _handler.load(std::memory_order_acquire); if (current != nullptr) { @@ -58,6 +58,7 @@ bool ProfilerSignalManager::RegisterHandler(HandlerFn_t handler) } std::unique_lock lock(_handlerRegisterMutex); + current = _handler.load(std::memory_order_acquire); if (current != nullptr) { assert(current == handler); @@ -68,7 +69,7 @@ bool ProfilerSignalManager::RegisterHandler(HandlerFn_t handler) if (_isHandlerInPlace) { - _handler = handler; + _handler.store(handler, std::memory_order_release); } return _isHandlerInPlace; @@ -204,7 +205,7 @@ void ProfilerSignalManager::SignalHandler(int signal, siginfo_t* info, void* con bool ProfilerSignalManager::CallCustomHandler(int32_t signal, siginfo_t* info, void* context) { - HandlerFn_t handler = _handler; + HandlerFn_t handler = _handler.load(std::memory_order_acquire); return handler != nullptr && handler(signal, info, context); } diff --git a/profiler/src/ProfilerEngine/Datadog.Profiler.Native.Linux/ProfilerSignalManager.h b/profiler/src/ProfilerEngine/Datadog.Profiler.Native.Linux/ProfilerSignalManager.h index a4f4dcc50b62..8593e616ca90 100644 --- a/profiler/src/ProfilerEngine/Datadog.Profiler.Native.Linux/ProfilerSignalManager.h +++ b/profiler/src/ProfilerEngine/Datadog.Profiler.Native.Linux/ProfilerSignalManager.h @@ -27,7 +27,7 @@ class ProfilerSignalManager #ifdef DD_TEST void Reset() { - _handler = nullptr; + _handler.store(nullptr, std::memory_order_relaxed); sigaction(_signalToSend, &_previousAction, nullptr); _isHandlerInPlace = false; _canReplaceSignalHandler = true; @@ -56,7 +56,7 @@ class ProfilerSignalManager private: bool _canReplaceSignalHandler; int32_t _signalToSend; - HandlerFn_t _handler; + std::atomic _handler; pid_t _processId; bool _isHandlerInPlace; struct sigaction _previousAction;