Logging: reduce locking frequency by fixing TODO.
Bug: webrtc:11567
Change-Id: I0cd5062c3a088e3781d009242db32069193fbe82
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/176902
Commit-Queue: Markus Handell <handellm@webrtc.org>
Reviewed-by: Karl Wiberg <kwiberg@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#31657}
diff --git a/rtc_base/logging.cc b/rtc_base/logging.cc
index bd2afcc..94ddcba 100644
--- a/rtc_base/logging.cc
+++ b/rtc_base/logging.cc
@@ -90,6 +90,7 @@
// cleanup by setting to null, or let it leak (safe at program exit).
ABSL_CONST_INIT LogSink* LogMessage::streams_ RTC_GUARDED_BY(g_log_mutex_) =
nullptr;
+ABSL_CONST_INIT std::atomic<bool> LogMessage::streams_empty_ = {true};
// Boolean options default to false (0)
bool LogMessage::thread_, LogMessage::timestamp_;
@@ -269,6 +270,7 @@
stream->min_severity_ = min_sev;
stream->next_ = streams_;
streams_ = stream;
+ streams_empty_.store(false, std::memory_order_relaxed);
UpdateMinLogSeverity();
}
@@ -281,6 +283,7 @@
break;
}
}
+ streams_empty_.store(streams_ == nullptr, std::memory_order_relaxed);
UpdateMinLogSeverity();
}
@@ -438,12 +441,7 @@
bool LogMessage::IsNoop(LoggingSeverity severity) {
if (severity >= g_dbg_sev || severity >= g_min_sev)
return false;
-
- // TODO(tommi): We're grabbing this lock for every LogMessage instance that
- // is going to be logged. This introduces unnecessary synchronization for
- // a feature that's mostly used for testing.
- webrtc::MutexLock lock(&g_log_mutex_);
- return streams_ == nullptr;
+ return streams_empty_.load(std::memory_order_relaxed);
}
void LogMessage::FinishPrintStream() {
diff --git a/rtc_base/logging.h b/rtc_base/logging.h
index 0aa1e67..1e5f725 100644
--- a/rtc_base/logging.h
+++ b/rtc_base/logging.h
@@ -46,6 +46,7 @@
#include <errno.h>
+#include <atomic>
#include <sstream> // no-presubmit-check TODO(webrtc:8982)
#include <string>
#include <utility>
@@ -557,6 +558,12 @@
// The output streams and their associated severities
static LogSink* streams_;
+ // Holds true with high probability if |streams_| is empty, false with high
+ // probability otherwise. Operated on with std::memory_order_relaxed because
+ // it's ok to loose or log some additional statements near the instant streams
+ // are added/removed.
+ static std::atomic<bool> streams_empty_;
+
// Flags for formatting options
static bool thread_, timestamp_;