From 325fd7b042ff4ec34f7dd32e602cd81ad0e24b22 Mon Sep 17 00:00:00 2001 From: Abseil Team Date: Fri, 6 Sep 2019 02:25:12 -0700 Subject: Export of internal Abseil changes -- 2e894f3c2fadc789abf9011222526d5da1e0433e by Gennadiy Rozental : Internal change PiperOrigin-RevId: 267557172 -- 535be36d401a556156223ecc1aabd73a271f0f05 by Abseil Team : Internal change. PiperOrigin-RevId: 267456795 GitOrigin-RevId: 2e894f3c2fadc789abf9011222526d5da1e0433e Change-Id: I95d29cbde5cd8342ae71b77728baa61b7cf6d440 --- absl/flags/internal/flag.h | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) (limited to 'absl/flags/internal/flag.h') diff --git a/absl/flags/internal/flag.h b/absl/flags/internal/flag.h index a84f7fa..1633038 100644 --- a/absl/flags/internal/flag.h +++ b/absl/flags/internal/flag.h @@ -16,12 +16,16 @@ #ifndef ABSL_FLAGS_INTERNAL_FLAG_H_ #define ABSL_FLAGS_INTERNAL_FLAG_H_ +#include + #include "absl/flags/internal/commandlineflag.h" #include "absl/flags/internal/registry.h" namespace absl { namespace flags_internal { +constexpr int64_t AtomicInit() { return 0xababababababababll; } + // Signature for the mutation callback used by watched Flags // The callback is noexcept. // TODO(rogeeff): add noexcept after C++17 support is added. @@ -44,6 +48,7 @@ class Flag final : public flags_internal::CommandLineFlag { initial_value_gen, /*def=*/nullptr, /*cur=*/nullptr), + atomic_(flags_internal::AtomicInit()), callback_(nullptr) {} T Get() const { @@ -76,8 +81,8 @@ class Flag final : public flags_internal::CommandLineFlag { bool AtomicGet(T* v) const { const int64_t r = atomic_.load(std::memory_order_acquire); - if (r != flags_internal::CommandLineFlag::kAtomicInit) { - memcpy(v, &r, sizeof(T)); + if (r != flags_internal::AtomicInit()) { + std::memcpy(v, &r, sizeof(T)); return true; } @@ -108,7 +113,18 @@ class Flag final : public flags_internal::CommandLineFlag { delete locks_; } + void StoreAtomic() override { + if (sizeof(T) <= sizeof(int64_t)) { + int64_t t = 0; + std::memcpy(&t, cur_, (std::min)(sizeof(T), sizeof(int64_t))); + atomic_.store(t, std::memory_order_release); + } + } + // Flag's data + // For some types, a copy of the current value is kept in an atomically + // accessible field. + std::atomic atomic_; FlagCallback callback_; // Mutation callback }; -- cgit v1.2.3