Commit b4a4a6b0 by Derek Mauro Committed by Copybara-Service

Replace std::atomic_flag with std::atomic<bool> to avoid the C++20

deprecation of ATOMIC_FLAG_INIT.

Another option would have been to use macros to only initialize
std::atomic_flag before C++20, but I decided to use one compilation
path instead.

The major difference between std::atomic_flag and std::atomic<bool> is
that the former is guaranteed to be lock-free, but we already assume
std::atomic<bool> is lock-free in many places.

https://en.cppreference.com/w/cpp/atomic/atomic_flag

PiperOrigin-RevId: 487397075
Change-Id: I3f1c539ec8b2ca58547282e69ed73e93243e8efe
parent db8cd478
...@@ -29,6 +29,7 @@ ...@@ -29,6 +29,7 @@
#include <cstdint> #include <cstdint>
#include <limits> #include <limits>
#include "absl/base/attributes.h"
#include "absl/base/macros.h" #include "absl/base/macros.h"
#include "absl/base/port.h" #include "absl/base/port.h"
#include "absl/debugging/internal/address_is_readable.h" #include "absl/debugging/internal/address_is_readable.h"
......
...@@ -372,12 +372,16 @@ void LogMessage::Flush() { ...@@ -372,12 +372,16 @@ void LogMessage::Flush() {
} }
// Have we already seen a fatal message? // Have we already seen a fatal message?
ABSL_CONST_INIT static std::atomic_flag seen_fatal = ATOMIC_FLAG_INIT; ABSL_CONST_INIT static std::atomic<bool> seen_fatal(false);
if (data_->entry.log_severity() == absl::LogSeverity::kFatal && if (data_->entry.log_severity() == absl::LogSeverity::kFatal &&
absl::log_internal::ExitOnDFatal()) { absl::log_internal::ExitOnDFatal()) {
// Exactly one LOG(FATAL) message is responsible for aborting the process, // Exactly one LOG(FATAL) message is responsible for aborting the process,
// even if multiple threads LOG(FATAL) concurrently. // even if multiple threads LOG(FATAL) concurrently.
data_->first_fatal = !seen_fatal.test_and_set(std::memory_order_relaxed); bool expected_seen_fatal = false;
if (seen_fatal.compare_exchange_strong(expected_seen_fatal, true,
std::memory_order_relaxed)) {
data_->first_fatal = true;
}
} }
data_->entry.text_message_with_prefix_and_newline_and_nul_ = data_->entry.text_message_with_prefix_and_newline_and_nul_ =
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment