From eedb2cfbbe928b99a8283fea16f0d22b41cf80d0 Mon Sep 17 00:00:00 2001 From: Stefan Boberg Date: Fri, 26 Apr 2024 14:43:40 +0200 Subject: miscellaneous minor bugfixes (#66) this change addresses some TSAN warnings for improved robustness and less TSAN noise - Added dedicated timer for EnqueueStateExitFlagTimer - Made log formatter `fullformatter` output consistent time stamps across threads - Made Linux/Mac event implementation TSAN clean --- src/zencore/thread.cpp | 7 +++---- src/zenserver/zenserver.cpp | 4 ++-- src/zenserver/zenserver.h | 1 + src/zenutil/include/zenutil/logging/fullformatter.h | 15 ++++++++++----- 4 files changed, 16 insertions(+), 11 deletions(-) (limited to 'src') diff --git a/src/zencore/thread.cpp b/src/zencore/thread.cpp index 06093d6b0..329e17eea 100644 --- a/src/zencore/thread.cpp +++ b/src/zencore/thread.cpp @@ -153,10 +153,10 @@ Event::Event() m_EventHandle = CreateEvent(nullptr, bManualReset, bInitialState, nullptr); #else ZEN_UNUSED(bManualReset); - auto* Inner = new EventInner(); + auto* Inner = new EventInner(); + std::unique_lock Lock(Inner->Mutex); Inner->bSet = bInitialState; m_EventHandle = Inner; - std::atomic_thread_fence(std::memory_order_release); #endif } @@ -208,9 +208,8 @@ Event::Close() { std::unique_lock Lock(Inner->Mutex); Inner->bSet.store(true); + m_EventHandle = nullptr; } - m_EventHandle = nullptr; - std::atomic_thread_fence(std::memory_order_release); delete Inner; #endif } diff --git a/src/zenserver/zenserver.cpp b/src/zenserver/zenserver.cpp index 86dbc9617..3fd5d53c7 100644 --- a/src/zenserver/zenserver.cpp +++ b/src/zenserver/zenserver.cpp @@ -809,8 +809,8 @@ ZenServer::EnqueueSigIntTimer() void ZenServer::EnqueueStateExitFlagTimer() { - m_SigIntTimer.expires_after(std::chrono::milliseconds(500)); - m_SigIntTimer.async_wait([this](const asio::error_code&) { CheckStateExitFlag(); }); + m_StateExitFlagTimer.expires_after(std::chrono::milliseconds(500)); + m_StateExitFlagTimer.async_wait([this](const asio::error_code&) { CheckStateExitFlag(); }); EnsureIoRunner(); } diff --git a/src/zenserver/zenserver.h b/src/zenserver/zenserver.h index 550047a5d..0bab4e0a7 100644 --- a/src/zenserver/zenserver.h +++ b/src/zenserver/zenserver.h @@ -98,6 +98,7 @@ private: asio::io_context m_IoContext; asio::steady_timer m_PidCheckTimer{m_IoContext}; asio::steady_timer m_StateMakerTimer{m_IoContext}; + asio::steady_timer m_StateExitFlagTimer{m_IoContext}; asio::steady_timer m_SigIntTimer{m_IoContext}; asio::steady_timer m_StatsReportingTimer{m_IoContext}; ProcessMonitor m_ProcessMonitor; diff --git a/src/zenutil/include/zenutil/logging/fullformatter.h b/src/zenutil/include/zenutil/logging/fullformatter.h index 146fea7a0..d4a04d3e5 100644 --- a/src/zenutil/include/zenutil/logging/fullformatter.h +++ b/src/zenutil/include/zenutil/logging/fullformatter.h @@ -42,6 +42,7 @@ public: TimestampSeconds = std::chrono::duration_cast(msg.time.time_since_epoch()); if (TimestampSeconds != m_LastLogSecs) { + RwLock::ExclusiveLockScope _(m_TimestampLock); m_LastLogSecs = TimestampSeconds; m_CachedLocalTm = spdlog::details::os::localtime(spdlog::log_clock::to_time_t(msg.time)); @@ -66,10 +67,10 @@ public: auto ElapsedTime = msg.time - m_Epoch; TimestampSeconds = std::chrono::duration_cast(ElapsedTime); - // cache the date/time part for the next second. - - if (m_CacheTimestamp != TimestampSeconds || m_CachedDatetime.size() == 0) + if (m_CacheTimestamp.load() != TimestampSeconds) { + RwLock::ExclusiveLockScope _(m_TimestampLock); + m_CacheTimestamp = TimestampSeconds; int Count = int(TimestampSeconds.count()); @@ -90,7 +91,10 @@ public: } } - OutBuffer.append(m_CachedDatetime.begin(), m_CachedDatetime.end()); + { + RwLock::SharedLockScope _(m_TimestampLock); + OutBuffer.append(m_CachedDatetime.begin(), m_CachedDatetime.end()); + } auto millis = spdlog::details::fmt_helper::time_fraction(msg.time); spdlog::details::fmt_helper::pad3(static_cast(millis.count()), OutBuffer); @@ -180,11 +184,12 @@ private: std::chrono::time_point m_Epoch; std::tm m_CachedLocalTm; std::chrono::seconds m_LastLogSecs; - std::chrono::seconds m_CacheTimestamp{0}; + std::atomic m_CacheTimestamp; spdlog::memory_buf_t m_CachedDatetime; std::string m_LogId; std::string m_LinePrefix; bool m_UseFullDate = true; + RwLock m_TimestampLock; }; } // namespace zen::logging -- cgit v1.2.3