summaryrefslogtreecommitdiff
path: root/absl/flags/internal/flag.cc
diff options
context:
space:
mode:
Diffstat (limited to 'absl/flags/internal/flag.cc')
-rw-r--r--absl/flags/internal/flag.cc340
1 files changed, 263 insertions, 77 deletions
diff --git a/absl/flags/internal/flag.cc b/absl/flags/internal/flag.cc
index 5a921e28..1502e7f1 100644
--- a/absl/flags/internal/flag.cc
+++ b/absl/flags/internal/flag.cc
@@ -15,21 +15,26 @@
#include "absl/flags/internal/flag.h"
+#include <assert.h>
#include <stddef.h>
#include <stdint.h>
#include <string.h>
+#include <array>
#include <atomic>
#include <memory>
+#include <new>
#include <string>
-#include <vector>
+#include <typeinfo>
-#include "absl/base/attributes.h"
+#include "absl/base/call_once.h"
+#include "absl/base/casts.h"
#include "absl/base/config.h"
-#include "absl/base/const_init.h"
#include "absl/base/optimization.h"
+#include "absl/flags/config.h"
#include "absl/flags/internal/commandlineflag.h"
#include "absl/flags/usage_config.h"
+#include "absl/memory/memory.h"
#include "absl/strings/str_cat.h"
#include "absl/strings/string_view.h"
#include "absl/synchronization/mutex.h"
@@ -47,10 +52,10 @@ const char kStrippedFlagHelp[] = "\001\002\003\004 (unknown) \004\003\002\001";
namespace {
// Currently we only validate flag values for user-defined flag types.
-bool ShouldValidateFlagValue(FlagStaticTypeId flag_type_id) {
-#define DONT_VALIDATE(T) \
- if (flag_type_id == &FlagStaticTypeIdGen<T>) return false;
- ABSL_FLAGS_INTERNAL_BUILTIN_TYPES(DONT_VALIDATE)
+bool ShouldValidateFlagValue(FlagFastTypeId flag_type_id) {
+#define DONT_VALIDATE(T, _) \
+ if (flag_type_id == base_internal::FastTypeId<T>()) return false;
+ ABSL_FLAGS_INTERNAL_SUPPORTED_TYPES(DONT_VALIDATE)
#undef DONT_VALIDATE
return true;
@@ -62,55 +67,142 @@ bool ShouldValidateFlagValue(FlagStaticTypeId flag_type_id) {
// need to acquire these locks themselves.
class MutexRelock {
public:
- explicit MutexRelock(absl::Mutex* mu) : mu_(mu) { mu_->Unlock(); }
- ~MutexRelock() { mu_->Lock(); }
+ explicit MutexRelock(absl::Mutex& mu) : mu_(mu) { mu_.Unlock(); }
+ ~MutexRelock() { mu_.Lock(); }
MutexRelock(const MutexRelock&) = delete;
MutexRelock& operator=(const MutexRelock&) = delete;
private:
- absl::Mutex* mu_;
+ absl::Mutex& mu_;
};
} // namespace
+///////////////////////////////////////////////////////////////////////////////
+// Persistent state of the flag data.
+
+class FlagImpl;
+
+class FlagState : public flags_internal::FlagStateInterface {
+ public:
+ template <typename V>
+ FlagState(FlagImpl& flag_impl, const V& v, bool modified,
+ bool on_command_line, int64_t counter)
+ : flag_impl_(flag_impl),
+ value_(v),
+ modified_(modified),
+ on_command_line_(on_command_line),
+ counter_(counter) {}
+
+ ~FlagState() override {
+ if (flag_impl_.ValueStorageKind() != FlagValueStorageKind::kAlignedBuffer)
+ return;
+ flags_internal::Delete(flag_impl_.op_, value_.heap_allocated);
+ }
+
+ private:
+ friend class FlagImpl;
+
+ // Restores the flag to the saved state.
+ void Restore() const override {
+ if (!flag_impl_.RestoreState(*this)) return;
+
+ ABSL_INTERNAL_LOG(INFO,
+ absl::StrCat("Restore saved value of ", flag_impl_.Name(),
+ " to: ", flag_impl_.CurrentValue()));
+ }
+
+ // Flag and saved flag data.
+ FlagImpl& flag_impl_;
+ union SavedValue {
+ explicit SavedValue(void* v) : heap_allocated(v) {}
+ explicit SavedValue(int64_t v) : one_word(v) {}
+ explicit SavedValue(flags_internal::AlignedTwoWords v) : two_words(v) {}
+
+ void* heap_allocated;
+ int64_t one_word;
+ flags_internal::AlignedTwoWords two_words;
+ } value_;
+ bool modified_;
+ bool on_command_line_;
+ int64_t counter_;
+};
+
+///////////////////////////////////////////////////////////////////////////////
+// Flag implementation, which does not depend on flag value type.
+
+DynValueDeleter::DynValueDeleter(FlagOpFn op_arg) : op(op_arg) {}
+
+void DynValueDeleter::operator()(void* ptr) const {
+ if (op == nullptr) return;
+
+ Delete(op, ptr);
+}
+
void FlagImpl::Init() {
new (&data_guard_) absl::Mutex;
- absl::MutexLock lock(reinterpret_cast<absl::Mutex*>(&data_guard_));
+ auto def_kind = static_cast<FlagDefaultKind>(def_kind_);
- value_.dynamic = MakeInitValue().release();
- StoreAtomic();
+ switch (ValueStorageKind()) {
+ case FlagValueStorageKind::kAlignedBuffer:
+ // For this storage kind the default_value_ always points to gen_func
+ // during initialization.
+ assert(def_kind == FlagDefaultKind::kGenFunc);
+ (*default_value_.gen_func)(AlignedBufferValue());
+ break;
+ case FlagValueStorageKind::kOneWordAtomic: {
+ alignas(int64_t) std::array<char, sizeof(int64_t)> buf{};
+ if (def_kind == FlagDefaultKind::kGenFunc) {
+ (*default_value_.gen_func)(buf.data());
+ } else {
+ assert(def_kind != FlagDefaultKind::kDynamicValue);
+ std::memcpy(buf.data(), &default_value_, Sizeof(op_));
+ }
+ OneWordValue().store(absl::bit_cast<int64_t>(buf),
+ std::memory_order_release);
+ break;
+ }
+ case FlagValueStorageKind::kTwoWordsAtomic: {
+ // For this storage kind the default_value_ always points to gen_func
+ // during initialization.
+ assert(def_kind == FlagDefaultKind::kGenFunc);
+ alignas(AlignedTwoWords) std::array<char, sizeof(AlignedTwoWords)> buf{};
+ (*default_value_.gen_func)(buf.data());
+ auto atomic_value = absl::bit_cast<AlignedTwoWords>(buf);
+ TwoWordsValue().store(atomic_value, std::memory_order_release);
+ break;
+ }
+ }
}
-// Ensures that the lazily initialized data is initialized,
-// and returns pointer to the mutex guarding flags data.
absl::Mutex* FlagImpl::DataGuard() const {
absl::call_once(const_cast<FlagImpl*>(this)->init_control_, &FlagImpl::Init,
const_cast<FlagImpl*>(this));
- // data_guard_ is initialized.
+ // data_guard_ is initialized inside Init.
return reinterpret_cast<absl::Mutex*>(&data_guard_);
}
-void FlagImpl::AssertValidType(FlagStaticTypeId type_id) const {
- FlagStaticTypeId this_type_id = flags_internal::StaticTypeId(op_);
+void FlagImpl::AssertValidType(FlagFastTypeId rhs_type_id,
+ const std::type_info* (*gen_rtti)()) const {
+ FlagFastTypeId lhs_type_id = flags_internal::FastTypeId(op_);
- // `type_id` is the type id corresponding to the declaration visibile at the
- // call site. `this_type_id` is the type id corresponding to the type stored
- // during flag definition. They must match for this operation to be
- // well-defined.
- if (ABSL_PREDICT_TRUE(type_id == this_type_id)) return;
+ // `rhs_type_id` is the fast type id corresponding to the declaration
+ // visibile at the call site. `lhs_type_id` is the fast type id
+ // corresponding to the type specified in flag definition. They must match
+ // for this operation to be well-defined.
+ if (ABSL_PREDICT_TRUE(lhs_type_id == rhs_type_id)) return;
- void* lhs_runtime_type_id = type_id();
- void* rhs_runtime_type_id = this_type_id();
+ const std::type_info* lhs_runtime_type_id =
+ flags_internal::RuntimeTypeId(op_);
+ const std::type_info* rhs_runtime_type_id = (*gen_rtti)();
if (lhs_runtime_type_id == rhs_runtime_type_id) return;
#if defined(ABSL_FLAGS_INTERNAL_HAS_RTTI)
- if (*reinterpret_cast<std::type_info*>(lhs_runtime_type_id) ==
- *reinterpret_cast<std::type_info*>(rhs_runtime_type_id))
- return;
+ if (*lhs_runtime_type_id == *rhs_runtime_type_id) return;
#endif
ABSL_INTERNAL_LOG(
@@ -120,17 +212,40 @@ void FlagImpl::AssertValidType(FlagStaticTypeId type_id) const {
std::unique_ptr<void, DynValueDeleter> FlagImpl::MakeInitValue() const {
void* res = nullptr;
- if (DefaultKind() == FlagDefaultKind::kDynamicValue) {
- res = flags_internal::Clone(op_, default_value_.dynamic_value);
- } else {
- res = (*default_value_.gen_func)();
+ switch (DefaultKind()) {
+ case FlagDefaultKind::kDynamicValue:
+ res = flags_internal::Clone(op_, default_value_.dynamic_value);
+ break;
+ case FlagDefaultKind::kGenFunc:
+ res = flags_internal::Alloc(op_);
+ (*default_value_.gen_func)(res);
+ break;
+ default:
+ res = flags_internal::Clone(op_, &default_value_);
+ break;
}
return {res, DynValueDeleter{op_}};
}
void FlagImpl::StoreValue(const void* src) {
- flags_internal::Copy(op_, src, value_.dynamic);
- StoreAtomic();
+ switch (ValueStorageKind()) {
+ case FlagValueStorageKind::kAlignedBuffer:
+ Copy(op_, src, AlignedBufferValue());
+ break;
+ case FlagValueStorageKind::kOneWordAtomic: {
+ int64_t one_word_val = 0;
+ std::memcpy(&one_word_val, src, Sizeof(op_));
+ OneWordValue().store(one_word_val, std::memory_order_release);
+ break;
+ }
+ case FlagValueStorageKind::kTwoWordsAtomic: {
+ AlignedTwoWords two_words_val{0, 0};
+ std::memcpy(&two_words_val, src, Sizeof(op_));
+ TwoWordsValue().store(two_words_val, std::memory_order_release);
+ break;
+ }
+ }
+
modified_ = true;
++counter_;
InvokeCallback();
@@ -147,9 +262,8 @@ std::string FlagImpl::Help() const {
: help_.gen_func();
}
-bool FlagImpl::IsModified() const {
- absl::MutexLock l(DataGuard());
- return modified_;
+FlagFastTypeId FlagImpl::TypeId() const {
+ return flags_internal::FastTypeId(op_);
}
bool FlagImpl::IsSpecifiedOnCommandLine() const {
@@ -165,9 +279,27 @@ std::string FlagImpl::DefaultValue() const {
}
std::string FlagImpl::CurrentValue() const {
- absl::MutexLock l(DataGuard());
+ auto* guard = DataGuard(); // Make sure flag initialized
+ switch (ValueStorageKind()) {
+ case FlagValueStorageKind::kAlignedBuffer: {
+ absl::MutexLock l(guard);
+ return flags_internal::Unparse(op_, AlignedBufferValue());
+ }
+ case FlagValueStorageKind::kOneWordAtomic: {
+ const auto one_word_val =
+ absl::bit_cast<std::array<char, sizeof(int64_t)>>(
+ OneWordValue().load(std::memory_order_acquire));
+ return flags_internal::Unparse(op_, one_word_val.data());
+ }
+ case FlagValueStorageKind::kTwoWordsAtomic: {
+ const auto two_words_val =
+ absl::bit_cast<std::array<char, sizeof(AlignedTwoWords)>>(
+ TwoWordsValue().load(std::memory_order_acquire));
+ return flags_internal::Unparse(op_, two_words_val.data());
+ }
+ }
- return flags_internal::Unparse(op_, value_.dynamic);
+ return "";
}
void FlagImpl::SetCallback(const FlagCallbackFunc mutation_callback) {
@@ -199,44 +331,98 @@ void FlagImpl::InvokeCallback() const {
// and it also can be different by the time the callback invocation is
// completed. Requires that *primary_lock be held in exclusive mode; it may be
// released and reacquired by the implementation.
- MutexRelock relock(DataGuard());
+ MutexRelock relock(*DataGuard());
absl::MutexLock lock(&callback_->guard);
cb();
}
-bool FlagImpl::RestoreState(const void* value, bool modified,
- bool on_command_line, int64_t counter) {
- {
- absl::MutexLock l(DataGuard());
+std::unique_ptr<FlagStateInterface> FlagImpl::SaveState() {
+ absl::MutexLock l(DataGuard());
- if (counter_ == counter) return false;
+ bool modified = modified_;
+ bool on_command_line = on_command_line_;
+ switch (ValueStorageKind()) {
+ case FlagValueStorageKind::kAlignedBuffer: {
+ return absl::make_unique<FlagState>(
+ *this, flags_internal::Clone(op_, AlignedBufferValue()), modified,
+ on_command_line, counter_);
+ }
+ case FlagValueStorageKind::kOneWordAtomic: {
+ return absl::make_unique<FlagState>(
+ *this, OneWordValue().load(std::memory_order_acquire), modified,
+ on_command_line, counter_);
+ }
+ case FlagValueStorageKind::kTwoWordsAtomic: {
+ return absl::make_unique<FlagState>(
+ *this, TwoWordsValue().load(std::memory_order_acquire), modified,
+ on_command_line, counter_);
+ }
}
+ return nullptr;
+}
- Write(value);
+bool FlagImpl::RestoreState(const FlagState& flag_state) {
+ absl::MutexLock l(DataGuard());
- {
- absl::MutexLock l(DataGuard());
+ if (flag_state.counter_ == counter_) {
+ return false;
+ }
- modified_ = modified;
- on_command_line_ = on_command_line;
+ switch (ValueStorageKind()) {
+ case FlagValueStorageKind::kAlignedBuffer:
+ StoreValue(flag_state.value_.heap_allocated);
+ break;
+ case FlagValueStorageKind::kOneWordAtomic:
+ StoreValue(&flag_state.value_.one_word);
+ break;
+ case FlagValueStorageKind::kTwoWordsAtomic:
+ StoreValue(&flag_state.value_.two_words);
+ break;
}
+ modified_ = flag_state.modified_;
+ on_command_line_ = flag_state.on_command_line_;
+
return true;
}
+template <typename StorageT>
+StorageT* FlagImpl::OffsetValue() const {
+ char* p = reinterpret_cast<char*>(const_cast<FlagImpl*>(this));
+ // The offset is deduced via Flag value type specific op_.
+ size_t offset = flags_internal::ValueOffset(op_);
+
+ return reinterpret_cast<StorageT*>(p + offset);
+}
+
+void* FlagImpl::AlignedBufferValue() const {
+ assert(ValueStorageKind() == FlagValueStorageKind::kAlignedBuffer);
+ return OffsetValue<void>();
+}
+
+std::atomic<int64_t>& FlagImpl::OneWordValue() const {
+ assert(ValueStorageKind() == FlagValueStorageKind::kOneWordAtomic);
+ return OffsetValue<FlagOneWordValue>()->value;
+}
+
+std::atomic<AlignedTwoWords>& FlagImpl::TwoWordsValue() const {
+ assert(ValueStorageKind() == FlagValueStorageKind::kTwoWordsAtomic);
+ return OffsetValue<FlagTwoWordsValue>()->value;
+}
+
// Attempts to parse supplied `value` string using parsing routine in the `flag`
// argument. If parsing successful, this function replaces the dst with newly
// parsed value. In case if any error is encountered in either step, the error
// message is stored in 'err'
std::unique_ptr<void, DynValueDeleter> FlagImpl::TryParse(
- absl::string_view value, std::string* err) const {
+ absl::string_view value, std::string& err) const {
std::unique_ptr<void, DynValueDeleter> tentative_value = MakeInitValue();
std::string parse_err;
if (!flags_internal::Parse(op_, value, tentative_value.get(), &parse_err)) {
absl::string_view err_sep = parse_err.empty() ? "" : "; ";
- *err = absl::StrCat("Illegal value '", value, "' specified for flag '",
- Name(), "'", err_sep, parse_err);
+ err = absl::StrCat("Illegal value '", value, "' specified for flag '",
+ Name(), "'", err_sep, parse_err);
return nullptr;
}
@@ -244,32 +430,32 @@ std::unique_ptr<void, DynValueDeleter> FlagImpl::TryParse(
}
void FlagImpl::Read(void* dst) const {
- absl::ReaderMutexLock l(DataGuard());
-
- flags_internal::CopyConstruct(op_, value_.dynamic, dst);
-}
-
-void FlagImpl::StoreAtomic() {
- size_t data_size = flags_internal::Sizeof(op_);
-
- if (data_size <= sizeof(int64_t)) {
- int64_t t = 0;
- std::memcpy(&t, value_.dynamic, data_size);
- value_.atomics.small_atomic.store(t, std::memory_order_release);
- }
-#if defined(ABSL_FLAGS_INTERNAL_ATOMIC_DOUBLE_WORD)
- else if (data_size <= sizeof(FlagsInternalTwoWordsType)) {
- FlagsInternalTwoWordsType t{0, 0};
- std::memcpy(&t, value_.dynamic, data_size);
- value_.atomics.big_atomic.store(t, std::memory_order_release);
+ auto* guard = DataGuard(); // Make sure flag initialized
+ switch (ValueStorageKind()) {
+ case FlagValueStorageKind::kAlignedBuffer: {
+ absl::MutexLock l(guard);
+ flags_internal::CopyConstruct(op_, AlignedBufferValue(), dst);
+ break;
+ }
+ case FlagValueStorageKind::kOneWordAtomic: {
+ const int64_t one_word_val =
+ OneWordValue().load(std::memory_order_acquire);
+ std::memcpy(dst, &one_word_val, Sizeof(op_));
+ break;
+ }
+ case FlagValueStorageKind::kTwoWordsAtomic: {
+ const AlignedTwoWords two_words_val =
+ TwoWordsValue().load(std::memory_order_acquire);
+ std::memcpy(dst, &two_words_val, Sizeof(op_));
+ break;
+ }
}
-#endif
}
void FlagImpl::Write(const void* src) {
absl::MutexLock l(DataGuard());
- if (ShouldValidateFlagValue(flags_internal::StaticTypeId(op_))) {
+ if (ShouldValidateFlagValue(flags_internal::FastTypeId(op_))) {
std::unique_ptr<void, DynValueDeleter> obj{flags_internal::Clone(op_, src),
DynValueDeleter{op_}};
std::string ignored_error;
@@ -291,8 +477,8 @@ void FlagImpl::Write(const void* src) {
// * Update the flag's default value
// * Update the current flag value if it was never set before
// The mode is selected based on 'set_mode' parameter.
-bool FlagImpl::SetFromString(absl::string_view value, FlagSettingMode set_mode,
- ValueSource source, std::string* err) {
+bool FlagImpl::ParseFrom(absl::string_view value, FlagSettingMode set_mode,
+ ValueSource source, std::string& err) {
absl::MutexLock l(DataGuard());
switch (set_mode) {
@@ -339,7 +525,7 @@ bool FlagImpl::SetFromString(absl::string_view value, FlagSettingMode set_mode,
}
if (!modified_) {
- // Need to set both default value *and* current, in this case
+ // Need to set both default value *and* current, in this case.
StoreValue(default_value_.dynamic_value);
modified_ = false;
}
@@ -361,7 +547,7 @@ void FlagImpl::CheckDefaultValueParsingRoundtrip() const {
ABSL_INTERNAL_LOG(
FATAL,
absl::StrCat("Flag ", Name(), " (from ", Filename(),
- "): std::string form of default value '", v,
+ "): string form of default value '", v,
"' could not be parsed; error=", error));
}