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/zenutil/include/zenutil/logging/fullformatter.h | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) (limited to 'src/zenutil/include') 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