use absl::Mutex for NearbyFlags

PiperOrigin-RevId: 661424236
This commit is contained in:
Guogang Li
2024-08-09 15:30:44 -07:00
committed by Copybara-Service
parent 9378752513
commit c148174cb0
3 changed files with 21 additions and 17 deletions
-2
View File
@@ -48,7 +48,6 @@ cc_library(
],
deps = [
":flag_reader",
"//internal/platform:types",
"@com_google_absl//absl/base:core_headers",
"@com_google_absl//absl/container:flat_hash_map",
"@com_google_absl//absl/strings",
@@ -62,7 +61,6 @@ cc_test(
deps = [
":flag_reader",
":nearby_flags",
"//internal/platform/implementation/g3",
"@com_github_protobuf_matchers//protobuf-matchers",
"@com_google_absl//absl/strings",
"@com_google_googletest//:gtest",
+15 -12
View File
@@ -14,10 +14,13 @@
#include "internal/flags/nearby_flags.h"
#include <cstdint>
#include <string>
#include "internal/platform/mutex.h"
#include "internal/platform/mutex_lock.h"
#include "absl/strings/string_view.h"
#include "absl/synchronization/mutex.h"
#include "internal/flags/flag.h"
#include "internal/flags/flag_reader.h"
namespace nearby {
@@ -27,7 +30,7 @@ NearbyFlags& NearbyFlags::GetInstance() {
}
bool NearbyFlags::GetBoolFlag(const flags::Flag<bool>& flag) {
MutexLock lock(&mutex_);
absl::MutexLock lock(&mutex_);
const auto& it = overrided_bool_flag_values_.find(flag.name());
if (it != overrided_bool_flag_values_.end()) {
@@ -41,7 +44,7 @@ bool NearbyFlags::GetBoolFlag(const flags::Flag<bool>& flag) {
}
int64_t NearbyFlags::GetInt64Flag(const flags::Flag<int64_t>& flag) {
MutexLock lock(&mutex_);
absl::MutexLock lock(&mutex_);
const auto& it = overrided_int64_flag_values_.find(flag.name());
if (it != overrided_int64_flag_values_.end()) {
@@ -55,7 +58,7 @@ int64_t NearbyFlags::GetInt64Flag(const flags::Flag<int64_t>& flag) {
}
double NearbyFlags::GetDoubleFlag(const flags::Flag<double>& flag) {
MutexLock lock(&mutex_);
absl::MutexLock lock(&mutex_);
const auto& it = overrided_double_flag_values_.find(flag.name());
if (it != overrided_double_flag_values_.end()) {
@@ -70,7 +73,7 @@ double NearbyFlags::GetDoubleFlag(const flags::Flag<double>& flag) {
std::string NearbyFlags::GetStringFlag(
const flags::Flag<absl::string_view>& flag) {
MutexLock lock(&mutex_);
absl::MutexLock lock(&mutex_);
const auto& it = overrided_string_flag_values_.find(flag.name());
if (it != overrided_string_flag_values_.end()) {
@@ -84,36 +87,36 @@ std::string NearbyFlags::GetStringFlag(
}
void NearbyFlags::SetFlagReader(flags::FlagReader& flag_reader) {
MutexLock lock(&mutex_);
absl::MutexLock lock(&mutex_);
flag_reader_ = &flag_reader;
}
void NearbyFlags::OverrideBoolFlagValue(const flags::Flag<bool>& flag,
bool new_value) {
MutexLock lock(&mutex_);
absl::MutexLock lock(&mutex_);
overrided_bool_flag_values_[flag.name()] = new_value;
}
void NearbyFlags::OverrideInt64FlagValue(const flags::Flag<int64_t>& flag,
int64_t new_value) {
MutexLock lock(&mutex_);
absl::MutexLock lock(&mutex_);
overrided_int64_flag_values_[flag.name()] = new_value;
}
void NearbyFlags::OverrideDoubleFlagValue(const flags::Flag<double>& flag,
double new_value) {
MutexLock lock(&mutex_);
absl::MutexLock lock(&mutex_);
overrided_double_flag_values_[flag.name()] = new_value;
}
void NearbyFlags::OverrideStringFlagValue(
const flags::Flag<absl::string_view>& flag, absl::string_view new_value) {
MutexLock lock(&mutex_);
absl::MutexLock lock(&mutex_);
overrided_string_flag_values_[flag.name()] = std::string(new_value);
}
void NearbyFlags::ResetOverridedValues() {
MutexLock lock(&mutex_);
absl::MutexLock lock(&mutex_);
overrided_bool_flag_values_.clear();
overrided_int64_flag_values_.clear();
overrided_double_flag_values_.clear();
+6 -3
View File
@@ -15,13 +15,16 @@
#ifndef THIRD_PARTY_NEARBY_INTERNAL_FLAGS_NEARBY_FLAGS_H_
#define THIRD_PARTY_NEARBY_INTERNAL_FLAGS_NEARBY_FLAGS_H_
#include <cstdint>
#include <string>
#include "absl/base/thread_annotations.h"
#include "absl/container/flat_hash_map.h"
#include "absl/strings/string_view.h"
#include "absl/synchronization/mutex.h"
#include "internal/flags/default_flag_reader.h"
#include "internal/flags/flag.h"
#include "internal/flags/flag_reader.h"
#include "internal/platform/mutex.h"
namespace nearby {
@@ -65,7 +68,7 @@ class NearbyFlags final : public nearby::flags::FlagReader {
absl::string_view new_value)
ABSL_LOCKS_EXCLUDED(mutex_);
// Reset all overrided values.
// Reset all overridden values.
void ResetOverridedValues() ABSL_LOCKS_EXCLUDED(mutex_);
private:
@@ -74,7 +77,7 @@ class NearbyFlags final : public nearby::flags::FlagReader {
flags::FlagReader* flag_reader_ = nullptr;
flags::DefaultFlagReader default_flag_reader_;
mutable Mutex mutex_;
mutable absl::Mutex mutex_;
absl::flat_hash_map<std::string, bool> overrided_bool_flag_values_
ABSL_GUARDED_BY(mutex_);