Moved LazyServiceGuard to binder crate.

Bug: 234019127
Test: atest compos_key_tests MicrodroidHostTestCases MicrodroidTestApp libbinder_rs-internal_test
Change-Id: I3b771e1fa401375c4252c486c9af1607a20f7903
diff --git a/compos/composd/src/instance_starter.rs b/compos/composd/src/instance_starter.rs
index 2923ee0..aaa4695 100644
--- a/compos/composd/src/instance_starter.rs
+++ b/compos/composd/src/instance_starter.rs
@@ -21,8 +21,7 @@
     IVirtualizationService::IVirtualizationService, PartitionType::PartitionType,
 };
 use anyhow::{Context, Result};
-use binder::{ParcelFileDescriptor, Strong};
-use binder_common::lazy_service::LazyServiceGuard;
+use binder::{LazyServiceGuard, ParcelFileDescriptor, Strong};
 use compos_aidl_interface::aidl::com::android::compos::ICompOsService::ICompOsService;
 use compos_common::compos_client::{ComposClient, VmParameters};
 use compos_common::{COMPOS_DATA_ROOT, IDSIG_FILE, IDSIG_MANIFEST_APK_FILE, INSTANCE_IMAGE_FILE};
diff --git a/libs/binder_common/Android.bp b/libs/binder_common/Android.bp
index 209955d..47a2f21 100644
--- a/libs/binder_common/Android.bp
+++ b/libs/binder_common/Android.bp
@@ -10,7 +10,6 @@
     rustlibs: [
         "libbinder_rs",
         "libbinder_rpc_unstable_bindgen",
-        "liblazy_static",
     ],
     apex_available: [
         "com.android.compos",
diff --git a/libs/binder_common/lazy_service.rs b/libs/binder_common/lazy_service.rs
deleted file mode 100644
index 9d605b6..0000000
--- a/libs/binder_common/lazy_service.rs
+++ /dev/null
@@ -1,77 +0,0 @@
-/*
- * Copyright (C) 2021 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-//! Rust API for lazy (aka dynamic) AIDL services.
-//! See https://source.android.com/devices/architecture/aidl/dynamic-aidl.
-
-use binder::force_lazy_services_persist;
-use lazy_static::lazy_static;
-use std::sync::Mutex;
-
-// TODO(b/200924402): Move this class to libbinder_rs once the infrastructure needed exists.
-
-/// An RAII object to ensure a server of lazy services is not killed. During the lifetime of any of
-/// these objects the service manager will not not kill the current process even if none of its
-/// lazy services are in use.
-#[must_use]
-#[derive(Debug)]
-pub struct LazyServiceGuard {
-    // Prevent construction outside this module.
-    _private: (),
-}
-
-lazy_static! {
-    // Count of how many LazyServiceGuard objects are in existence.
-    static ref GUARD_COUNT: Mutex<u64> = Mutex::new(0);
-}
-
-impl LazyServiceGuard {
-    /// Create a new LazyServiceGuard to prevent the service manager prematurely killing this
-    /// process.
-    pub fn new() -> Self {
-        let mut count = GUARD_COUNT.lock().unwrap();
-        *count += 1;
-        if *count == 1 {
-            // It's important that we make this call with the mutex held, to make sure
-            // that multiple calls (e.g. if the count goes 1 -> 0 -> 1) are correctly
-            // sequenced. (That also means we can't just use an AtomicU64.)
-            force_lazy_services_persist(true);
-        }
-        Self { _private: () }
-    }
-}
-
-impl Drop for LazyServiceGuard {
-    fn drop(&mut self) {
-        let mut count = GUARD_COUNT.lock().unwrap();
-        *count -= 1;
-        if *count == 0 {
-            force_lazy_services_persist(false);
-        }
-    }
-}
-
-impl Clone for LazyServiceGuard {
-    fn clone(&self) -> Self {
-        Self::new()
-    }
-}
-
-impl Default for LazyServiceGuard {
-    fn default() -> Self {
-        Self::new()
-    }
-}
diff --git a/libs/binder_common/lib.rs b/libs/binder_common/lib.rs
index 16812b4..14dd9f2 100644
--- a/libs/binder_common/lib.rs
+++ b/libs/binder_common/lib.rs
@@ -16,6 +16,5 @@
 
 //! Common items useful for binder clients and/or servers.
 
-pub mod lazy_service;
 pub mod rpc_client;
 pub mod rpc_server;
diff --git a/virtualizationservice/src/aidl.rs b/virtualizationservice/src/aidl.rs
index acd0863..bba75ac 100644
--- a/virtualizationservice/src/aidl.rs
+++ b/virtualizationservice/src/aidl.rs
@@ -35,8 +35,8 @@
     VirtualMachineState::VirtualMachineState,
 };
 use binder::{
-    self, BinderFeatures, ExceptionCode, Interface, ParcelFileDescriptor, SpIBinder, Status,
-    StatusCode, Strong, ThreadState,
+    self, BinderFeatures, ExceptionCode, Interface, LazyServiceGuard, ParcelFileDescriptor,
+    SpIBinder, Status, StatusCode, Strong, ThreadState,
 };
 use android_system_virtualmachineservice::aidl::android::system::virtualmachineservice::{
     IVirtualMachineService::{
@@ -45,7 +45,7 @@
     },
 };
 use anyhow::{anyhow, bail, Context, Result};
-use binder_common::{lazy_service::LazyServiceGuard, rpc_server::run_rpc_server_with_factory};
+use binder_common::rpc_server::run_rpc_server_with_factory;
 use disk::QcowFile;
 use idsig::{HashAlgorithm, V4Signature};
 use log::{debug, error, info, warn, trace};