Use LazyLock rather than lazy_static.
Bug: 364211748
Test: m virtmgr virtualizationservice libcompos_common libservice_vm_manager libvm_payload_rs
Change-Id: I3e92489a925709c3ab91968d1a38c9b52a54f3c5
diff --git a/libs/libcompos_common/Android.bp b/libs/libcompos_common/Android.bp
index 72cb5e1..01836ae 100644
--- a/libs/libcompos_common/Android.bp
+++ b/libs/libcompos_common/Android.bp
@@ -14,7 +14,6 @@
"libanyhow",
"libbinder_rs",
"libglob",
- "liblazy_static",
"liblog_rust",
"libnested_virt",
"libnum_traits",
diff --git a/libs/libcompos_common/timeouts.rs b/libs/libcompos_common/timeouts.rs
index 7bd7679..d22f7f7 100644
--- a/libs/libcompos_common/timeouts.rs
+++ b/libs/libcompos_common/timeouts.rs
@@ -17,7 +17,7 @@
//! Timeouts for common situations, with support for longer timeouts when using nested
//! virtualization.
-use lazy_static::lazy_static;
+use std::sync::LazyLock;
use std::time::Duration;
/// Holder for the various timeouts we use.
@@ -31,15 +31,15 @@
pub vm_max_time_to_exit: Duration,
}
-lazy_static! {
/// The timeouts that are appropriate on the current platform.
-pub static ref TIMEOUTS: Timeouts = if nested_virt::is_nested_virtualization().unwrap() {
- // Nested virtualization is slow.
- EXTENDED_TIMEOUTS
-} else {
- NORMAL_TIMEOUTS
-};
-}
+pub static TIMEOUTS: LazyLock<Timeouts> = LazyLock::new(|| {
+ if nested_virt::is_nested_virtualization().unwrap() {
+ // Nested virtualization is slow.
+ EXTENDED_TIMEOUTS
+ } else {
+ NORMAL_TIMEOUTS
+ }
+});
/// The timeouts that we use normally.
const NORMAL_TIMEOUTS: Timeouts = Timeouts {
diff --git a/libs/libservice_vm_manager/Android.bp b/libs/libservice_vm_manager/Android.bp
index 6469212..b3618a6 100644
--- a/libs/libservice_vm_manager/Android.bp
+++ b/libs/libservice_vm_manager/Android.bp
@@ -12,7 +12,6 @@
"android.system.virtualizationservice-rust",
"libanyhow",
"libciborium",
- "liblazy_static",
"liblog_rust",
"libnix",
"libservice_vm_comm",
diff --git a/libs/libservice_vm_manager/src/lib.rs b/libs/libservice_vm_manager/src/lib.rs
index d3d86e9..d7b4dd6 100644
--- a/libs/libservice_vm_manager/src/lib.rs
+++ b/libs/libservice_vm_manager/src/lib.rs
@@ -25,7 +25,6 @@
binder::ParcelFileDescriptor,
};
use anyhow::{anyhow, ensure, Context, Result};
-use lazy_static::lazy_static;
use log::{info, warn};
use service_vm_comm::{Request, Response, ServiceVmRequest, VmType};
use std::fs::{self, File, OpenOptions};
@@ -48,11 +47,10 @@
const WRITE_BUFFER_CAPACITY: usize = 512;
const READ_TIMEOUT: Duration = Duration::from_secs(10);
const WRITE_TIMEOUT: Duration = Duration::from_secs(10);
-lazy_static! {
- static ref PENDING_REQUESTS: AtomicCounter = AtomicCounter::default();
- static ref SERVICE_VM: Mutex<Option<ServiceVm>> = Mutex::new(None);
- static ref SERVICE_VM_SHUTDOWN: Condvar = Condvar::new();
-}
+
+static PENDING_REQUESTS: AtomicCounter = AtomicCounter::new();
+static SERVICE_VM: Mutex<Option<ServiceVm>> = Mutex::new(None);
+static SERVICE_VM_SHUTDOWN: Condvar = Condvar::new();
/// Atomic counter with a condition variable that is used to wait for the counter
/// to become positive within a timeout.
@@ -63,6 +61,10 @@
}
impl AtomicCounter {
+ const fn new() -> Self {
+ Self { num: Mutex::new(0), num_increased: Condvar::new() }
+ }
+
/// Checks if the counter becomes positive within the given timeout.
fn is_positive_within_timeout(&self, timeout: Duration) -> bool {
let (guard, _wait_result) = self
diff --git a/libs/libvm_payload/Android.bp b/libs/libvm_payload/Android.bp
index cf2a002..bb91737 100644
--- a/libs/libvm_payload/Android.bp
+++ b/libs/libvm_payload/Android.bp
@@ -16,7 +16,6 @@
"libandroid_logger",
"libanyhow",
"libbinder_rs",
- "liblazy_static",
"liblibc",
"liblog_rust",
"libopenssl",
diff --git a/libs/libvm_payload/src/lib.rs b/libs/libvm_payload/src/lib.rs
index 13c6e76..40f7b79 100644
--- a/libs/libvm_payload/src/lib.rs
+++ b/libs/libvm_payload/src/lib.rs
@@ -23,7 +23,6 @@
unstable_api::{new_spibinder, AIBinder},
Strong, ExceptionCode,
};
-use lazy_static::lazy_static;
use log::{error, info, LevelFilter};
use rpcbinder::{RpcServer, RpcSession};
use openssl::{ec::EcKey, sha::sha256, ecdsa::EcdsaSig};
@@ -35,6 +34,7 @@
use std::ptr::{self, NonNull};
use std::sync::{
atomic::{AtomicBool, Ordering},
+ LazyLock,
Mutex,
};
use vm_payload_status_bindgen::AVmAttestationStatus;
@@ -42,13 +42,11 @@
/// Maximum size of an ECDSA signature for EC P-256 key is 72 bytes.
const MAX_ECDSA_P256_SIGNATURE_SIZE: usize = 72;
-lazy_static! {
- static ref VM_APK_CONTENTS_PATH_C: CString =
- CString::new(VM_APK_CONTENTS_PATH).expect("CString::new failed");
- static ref PAYLOAD_CONNECTION: Mutex<Option<Strong<dyn IVmPayloadService>>> = Mutex::default();
- static ref VM_ENCRYPTED_STORAGE_PATH_C: CString =
- CString::new(ENCRYPTEDSTORE_MOUNTPOINT).expect("CString::new failed");
-}
+static VM_APK_CONTENTS_PATH_C: LazyLock<CString> =
+ LazyLock::new(|| CString::new(VM_APK_CONTENTS_PATH).expect("CString::new failed"));
+static PAYLOAD_CONNECTION: Mutex<Option<Strong<dyn IVmPayloadService>>> = Mutex::new(None);
+static VM_ENCRYPTED_STORAGE_PATH_C: LazyLock<CString> =
+ LazyLock::new(|| CString::new(ENCRYPTEDSTORE_MOUNTPOINT).expect("CString::new failed"));
static ALREADY_NOTIFIED: AtomicBool = AtomicBool::new(false);