about summary refs log tree commit diff
diff options
context:
space:
mode:
authorPavel Samolysov <samolisov@gmail.com>2020-04-14T21·25+0300
committerGitHub <noreply@github.com>2020-04-14T21·25-0400
commit2946ac0dea49c894215a998aebb19d2f1d942909 (patch)
treec0fbefaa60b39e767a1fa634334c05f248e25f32
parent567bee2f7393fca6ca737ecf061bd9f97302e346 (diff)
Use base_internal::AtomicHook instead of std::atomic (#661)
* Use base_internal::AtomicHook instead of std::atomic

std::atomic has a broken implementation on the Windows platform and it
is not conform to the ABSL_CONST_INIT macro when clang-cl is used as a
compiler: the macro is expanded to the [[clang::require_constant_initialization]]
attribute and the attribute cannot be applied to the broken std::atomic.

Therefore, std::atomic has been replaced with absl::base_internal::AtomicHook
to fix the compilation error (thank Derek Mauro for the suggestion).

Issue: #659

Signed-off-by: Pavel Samolysov <samolisov@gmail.com>

* Update build files for pull request

Co-authored-by: Derek Mauro <dmauro@google.com>
-rw-r--r--absl/status/BUILD.bazel1
-rw-r--r--absl/status/CMakeLists.txt1
-rw-r--r--absl/status/status_payload_printer.cc15
3 files changed, 7 insertions, 10 deletions
diff --git a/absl/status/BUILD.bazel b/absl/status/BUILD.bazel
index 2b83077def79..d164252da9e2 100644
--- a/absl/status/BUILD.bazel
+++ b/absl/status/BUILD.bazel
@@ -40,6 +40,7 @@ cc_library(
     ],
     copts = ABSL_DEFAULT_COPTS,
     deps = [
+        "//absl/base:atomic_hook",
         "//absl/base:config",
         "//absl/base:core_headers",
         "//absl/base:raw_logging_internal",
diff --git a/absl/status/CMakeLists.txt b/absl/status/CMakeLists.txt
index f05cee5e954d..c041d69517ef 100644
--- a/absl/status/CMakeLists.txt
+++ b/absl/status/CMakeLists.txt
@@ -25,6 +25,7 @@ absl_cc_library(
   COPTS
     ${ABSL_DEFAULT_COPTS}
   DEPS
+    absl::atomic_hook
     absl::config
     absl::core_headers
     absl::raw_logging_internal
diff --git a/absl/status/status_payload_printer.cc b/absl/status/status_payload_printer.cc
index ad96d76acf49..a47aea11c2d3 100644
--- a/absl/status/status_payload_printer.cc
+++ b/absl/status/status_payload_printer.cc
@@ -16,26 +16,21 @@
 #include <atomic>
 
 #include "absl/base/attributes.h"
+#include "absl/base/internal/atomic_hook.h"
 
 namespace absl {
 ABSL_NAMESPACE_BEGIN
 namespace status_internal {
 
-namespace {
-// Tried constant initialized global variable but it doesn't work with Lexan
-// (MSVC's `std::atomic` has trouble constant initializing).
-std::atomic<StatusPayloadPrinter>& GetStatusPayloadPrinterStorage() {
-  ABSL_CONST_INIT static std::atomic<StatusPayloadPrinter> instance{nullptr};
-  return instance;
-}
-}  // namespace
+ABSL_INTERNAL_ATOMIC_HOOK_ATTRIBUTES
+static absl::base_internal::AtomicHook<StatusPayloadPrinter> storage;
 
 void SetStatusPayloadPrinter(StatusPayloadPrinter printer) {
-  GetStatusPayloadPrinterStorage().store(printer, std::memory_order_relaxed);
+  storage.Store(printer);
 }
 
 StatusPayloadPrinter GetStatusPayloadPrinter() {
-  return GetStatusPayloadPrinterStorage().load(std::memory_order_relaxed);
+  return storage.Load();
 }
 
 }  // namespace status_internal