Merge "aconfig: Use LazyLock rather than lazy_static" into main am: 36a352cd02 am: 364dca6ca5

Original change: https://android-review.googlesource.com/c/platform/build/+/3252494

Change-Id: Idd38abe84a61b4ab55b13d625df6c1d8c0433abb
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
This commit is contained in:
Treehugger Robot
2024-09-03 17:12:35 +00:00
committed by Automerger Merge Worker
2 changed files with 325 additions and 349 deletions

View File

@@ -113,6 +113,7 @@ mod tests {
use aconfig_storage_read_api::{Mmap, AconfigStorageError, StorageFileType, PackageReadContext, get_mapped_storage_file, get_boolean_flag_value, get_package_read_context};
use std::path::Path;
use std::io::Write;
use std::sync::LazyLock;
use log::{log, LevelFilter, Level};
static STORAGE_MIGRATION_MARKER_FILE: &str =
@@ -123,36 +124,28 @@ static MIGRATION_LOG_TAG: &str = "AconfigTestMission1";
pub struct FlagProvider;
/// flag value cache for disabled_rw
lazy_static::lazy_static! {
static ref CACHED_disabled_rw: bool = flags_rust::GetServerConfigurableFlag(
static CACHED_disabled_rw: LazyLock<bool> = LazyLock::new(|| flags_rust::GetServerConfigurableFlag(
"aconfig_flags.aconfig_test",
"com.android.aconfig.test.disabled_rw",
"false") == "true";
}
"false") == "true");
/// flag value cache for disabled_rw_exported
lazy_static::lazy_static! {
static ref CACHED_disabled_rw_exported: bool = flags_rust::GetServerConfigurableFlag(
static CACHED_disabled_rw_exported: LazyLock<bool> = LazyLock::new(|| flags_rust::GetServerConfigurableFlag(
"aconfig_flags.aconfig_test",
"com.android.aconfig.test.disabled_rw_exported",
"false") == "true";
}
"false") == "true");
/// flag value cache for disabled_rw_in_other_namespace
lazy_static::lazy_static! {
static ref CACHED_disabled_rw_in_other_namespace: bool = flags_rust::GetServerConfigurableFlag(
static CACHED_disabled_rw_in_other_namespace: LazyLock<bool> = LazyLock::new(|| flags_rust::GetServerConfigurableFlag(
"aconfig_flags.other_namespace",
"com.android.aconfig.test.disabled_rw_in_other_namespace",
"false") == "true";
}
"false") == "true");
/// flag value cache for enabled_rw
lazy_static::lazy_static! {
static ref CACHED_enabled_rw: bool = flags_rust::GetServerConfigurableFlag(
static CACHED_enabled_rw: LazyLock<bool> = LazyLock::new(|| flags_rust::GetServerConfigurableFlag(
"aconfig_flags.aconfig_test",
"com.android.aconfig.test.enabled_rw",
"true") == "true";
}
"true") == "true");
impl FlagProvider {
/// query flag disabled_ro
@@ -264,6 +257,7 @@ pub fn enabled_rw() -> bool {
use aconfig_storage_read_api::{Mmap, AconfigStorageError, StorageFileType, PackageReadContext, get_mapped_storage_file, get_boolean_flag_value, get_package_read_context};
use std::path::Path;
use std::io::Write;
use std::sync::LazyLock;
use log::{log, LevelFilter, Level};
static STORAGE_MIGRATION_MARKER_FILE: &str =
@@ -273,21 +267,18 @@ static MIGRATION_LOG_TAG: &str = "AconfigTestMission1";
/// flag provider
pub struct FlagProvider;
lazy_static::lazy_static! {
static ref PACKAGE_OFFSET: Result<Option<u32>, AconfigStorageError> = unsafe {
static PACKAGE_OFFSET: LazyLock<Result<Option<u32>, AconfigStorageError>> = LazyLock::new(|| unsafe {
get_mapped_storage_file("system", StorageFileType::PackageMap)
.and_then(|package_map| get_package_read_context(&package_map, "com.android.aconfig.test"))
.map(|context| context.map(|c| c.boolean_start_index))
};
});
static ref FLAG_VAL_MAP: Result<Mmap, AconfigStorageError> = unsafe {
static FLAG_VAL_MAP: LazyLock<Result<Mmap, AconfigStorageError>> = LazyLock::new(|| unsafe {
get_mapped_storage_file("system", StorageFileType::FlagVal)
};
}
});
/// flag value cache for disabled_rw
lazy_static::lazy_static! {
static ref CACHED_disabled_rw: bool = {
static CACHED_disabled_rw: LazyLock<bool> = LazyLock::new(|| {
let result = flags_rust::GetServerConfigurableFlag(
"aconfig_flags.aconfig_test",
"com.android.aconfig.test.disabled_rw",
@@ -349,12 +340,10 @@ lazy_static::lazy_static! {
}
result
};
}
});
/// flag value cache for disabled_rw_exported
lazy_static::lazy_static! {
static ref CACHED_disabled_rw_exported: bool = {
static CACHED_disabled_rw_exported: LazyLock<bool> = LazyLock::new(|| {
let result = flags_rust::GetServerConfigurableFlag(
"aconfig_flags.aconfig_test",
"com.android.aconfig.test.disabled_rw_exported",
@@ -416,12 +405,10 @@ lazy_static::lazy_static! {
}
result
};
}
});
/// flag value cache for disabled_rw_in_other_namespace
lazy_static::lazy_static! {
static ref CACHED_disabled_rw_in_other_namespace: bool = {
static CACHED_disabled_rw_in_other_namespace: LazyLock<bool> = LazyLock::new(|| {
let result = flags_rust::GetServerConfigurableFlag(
"aconfig_flags.other_namespace",
"com.android.aconfig.test.disabled_rw_in_other_namespace",
@@ -483,12 +470,11 @@ lazy_static::lazy_static! {
}
result
};
}
});
/// flag value cache for enabled_rw
lazy_static::lazy_static! {
static ref CACHED_enabled_rw: bool = {
static CACHED_enabled_rw: LazyLock<bool> = LazyLock::new(|| {
let result = flags_rust::GetServerConfigurableFlag(
"aconfig_flags.aconfig_test",
"com.android.aconfig.test.enabled_rw",
@@ -550,8 +536,7 @@ lazy_static::lazy_static! {
}
result
};
}
});
impl FlagProvider {
@@ -1215,6 +1200,7 @@ pub fn reset_flags() {
use aconfig_storage_read_api::{Mmap, AconfigStorageError, StorageFileType, PackageReadContext, get_mapped_storage_file, get_boolean_flag_value, get_package_read_context};
use std::path::Path;
use std::io::Write;
use std::sync::LazyLock;
use log::{log, LevelFilter, Level};
static STORAGE_MIGRATION_MARKER_FILE: &str =
@@ -1225,28 +1211,22 @@ static MIGRATION_LOG_TAG: &str = "AconfigTestMission1";
pub struct FlagProvider;
/// flag value cache for disabled_rw_exported
lazy_static::lazy_static! {
static ref CACHED_disabled_rw_exported: bool = flags_rust::GetServerConfigurableFlag(
static CACHED_disabled_rw_exported: LazyLock<bool> = LazyLock::new(|| flags_rust::GetServerConfigurableFlag(
"aconfig_flags.aconfig_test",
"com.android.aconfig.test.disabled_rw_exported",
"false") == "true";
}
"false") == "true");
/// flag value cache for enabled_fixed_ro_exported
lazy_static::lazy_static! {
static ref CACHED_enabled_fixed_ro_exported: bool = flags_rust::GetServerConfigurableFlag(
static CACHED_enabled_fixed_ro_exported: LazyLock<bool> = LazyLock::new(|| flags_rust::GetServerConfigurableFlag(
"aconfig_flags.aconfig_test",
"com.android.aconfig.test.enabled_fixed_ro_exported",
"false") == "true";
}
"false") == "true");
/// flag value cache for enabled_ro_exported
lazy_static::lazy_static! {
static ref CACHED_enabled_ro_exported: bool = flags_rust::GetServerConfigurableFlag(
static CACHED_enabled_ro_exported: LazyLock<bool> = LazyLock::new(|| flags_rust::GetServerConfigurableFlag(
"aconfig_flags.aconfig_test",
"com.android.aconfig.test.enabled_ro_exported",
"false") == "true";
}
"false") == "true");
impl FlagProvider {
/// query flag disabled_rw_exported
@@ -1292,6 +1272,7 @@ pub fn enabled_ro_exported() -> bool {
use aconfig_storage_read_api::{Mmap, AconfigStorageError, StorageFileType, PackageReadContext, get_mapped_storage_file, get_boolean_flag_value, get_package_read_context};
use std::path::Path;
use std::io::Write;
use std::sync::LazyLock;
use log::{log, LevelFilter, Level};
static STORAGE_MIGRATION_MARKER_FILE: &str =

View File

@@ -2,6 +2,7 @@
use aconfig_storage_read_api::\{Mmap, AconfigStorageError, StorageFileType, PackageReadContext, get_mapped_storage_file, get_boolean_flag_value, get_package_read_context};
use std::path::Path;
use std::io::Write;
use std::sync::LazyLock;
use log::\{log, LevelFilter, Level};
static STORAGE_MIGRATION_MARKER_FILE: &str =
@@ -13,25 +14,22 @@ pub struct FlagProvider;
{{ if has_readwrite- }}
{{ if allow_instrumentation }}
lazy_static::lazy_static! \{
static ref PACKAGE_OFFSET: Result<Option<u32>, AconfigStorageError> = unsafe \{
static PACKAGE_OFFSET: LazyLock<Result<Option<u32>, AconfigStorageError>> = LazyLock::new(|| unsafe \{
get_mapped_storage_file("{container}", StorageFileType::PackageMap)
.and_then(|package_map| get_package_read_context(&package_map, "{package}"))
.map(|context| context.map(|c| c.boolean_start_index))
};
});
static ref FLAG_VAL_MAP: Result<Mmap, AconfigStorageError> = unsafe \{
static FLAG_VAL_MAP: LazyLock<Result<Mmap, AconfigStorageError>> = LazyLock::new(|| unsafe \{
get_mapped_storage_file("{container}", StorageFileType::FlagVal)
};
}
});
{{ -endif }}
{{ -for flag in template_flags }}
{{ -if flag.readwrite }}
/// flag value cache for {flag.name}
{{ if allow_instrumentation }}
lazy_static::lazy_static! \{
static ref CACHED_{flag.name}: bool = \{
static CACHED_{flag.name}: LazyLock<bool> = LazyLock::new(|| \{
let result = flags_rust::GetServerConfigurableFlag(
"aconfig_flags.{flag.device_config_namespace}",
"{flag.device_config_flag}",
@@ -93,15 +91,12 @@ lazy_static::lazy_static! \{
}
result
};
}
});
{{ else }}
lazy_static::lazy_static! \{
static ref CACHED_{flag.name}: bool = flags_rust::GetServerConfigurableFlag(
static CACHED_{flag.name}: LazyLock<bool> = LazyLock::new(|| flags_rust::GetServerConfigurableFlag(
"aconfig_flags.{flag.device_config_namespace}",
"{flag.device_config_flag}",
"{flag.default_value}") == "true";
}
"{flag.default_value}") == "true");
{{ endif }}
{{ -endif }}
{{ -endfor }}