From ad37eae8694af76485e3d598dfa92f60341e4280 Mon Sep 17 00:00:00 2001 From: Charles Hall Date: Tue, 24 Sep 2024 20:05:18 -0700 Subject: [PATCH] use OnceLock instead of RwLock for SERVICES It actually has the semantics we need. Until we get rid of SERVICES. --- src/service.rs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/service.rs b/src/service.rs index 4bf7c5e2..c2dcb0f8 100644 --- a/src/service.rs +++ b/src/service.rs @@ -1,6 +1,6 @@ use std::{ collections::{BTreeMap, HashMap}, - sync::{Arc, Mutex as StdMutex, RwLock as StdRwLock}, + sync::{Arc, Mutex as StdMutex, OnceLock}, }; use lru_cache::LruCache; @@ -22,14 +22,11 @@ pub(crate) mod transaction_ids; pub(crate) mod uiaa; pub(crate) mod users; -static SERVICES: StdRwLock> = StdRwLock::new(None); +static SERVICES: OnceLock<&'static Services> = OnceLock::new(); /// Convenient access to the global [`Services`] instance pub(crate) fn services() -> &'static Services { - SERVICES - .read() - .unwrap() - .expect("SERVICES should be initialized when this is called") + SERVICES.get().expect("`Services::install` should have been called first") } pub(crate) struct Services { @@ -166,7 +163,10 @@ impl Services { /// Installs `self` to be globally accessed via [`services`] pub(crate) fn install(self) { - *SERVICES.write().unwrap() = Some(Box::leak(Box::new(self))); + assert!( + SERVICES.set(Box::leak(Box::new(self))).is_ok(), + "Services::install was called more than once" + ); } async fn memory_usage(&self) -> String {