From 79bf71d23437a33eaf4ce27f2d325d21508bb45e Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Fri, 9 Apr 2021 17:27:10 +0100 Subject: [PATCH] locks: Make config an Option This makes it plausible to implement ConstDefault for it. Signed-off-by: Ian Jackson --- src/config.rs | 20 +++++++++++++++++--- src/global.rs | 2 +- src/prelude.rs | 1 + 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/src/config.rs b/src/config.rs index cc6d1292..38b7dab9 100644 --- a/src/config.rs +++ b/src/config.rs @@ -191,15 +191,29 @@ impl TryFrom for WholeServerConfig { } } +fn config_read() -> MappedRwLockReadGuard<'static, WholeServerConfig> { + { + let g = GLOBAL.config.read(); + let g = RwLockReadGuard::try_map(g, |g| g.as_ref()); + if let Ok(g) = g { return g } + } + { + let mut g = GLOBAL.config.write(); + g.get_or_insert_with(default); + let g = RwLockWriteGuard::downgrade(g); + RwLockReadGuard::try_map(g, |g| g.as_ref()).unwrap() + } +} + pub fn config() -> Arc { - GLOBAL.config.read().server.clone() + config_read().server.clone() } pub fn log_config() -> LogSpecification { - GLOBAL.config.read().log.clone() + config_read().log.clone() } fn set_config(whole: WholeServerConfig) { - *GLOBAL.config.write() = whole; + *GLOBAL.config.write() = Some(whole); } impl ServerConfig { diff --git a/src/global.rs b/src/global.rs index 1e33ace8..44b258c1 100644 --- a/src/global.rs +++ b/src/global.rs @@ -206,7 +206,7 @@ pub struct Global { // <- InstanceContainer -> // inner locks which the game needs: dirty: Mutex>, - pub config: RwLock, + pub config: RwLock>, // fast global lookups players: RwLock>, diff --git a/src/prelude.rs b/src/prelude.rs index 7911b6b7..96432335 100644 --- a/src/prelude.rs +++ b/src/prelude.rs @@ -77,6 +77,7 @@ pub use num_traits::{Bounded, FromPrimitive, ToPrimitive}; pub use ordered_float::OrderedFloat; pub use parking_lot::{Condvar, Mutex, MutexGuard}; pub use parking_lot::{RwLock, RwLockReadGuard, RwLockWriteGuard}; +pub use parking_lot::{MappedRwLockReadGuard}; pub use percent_encoding::percent_decode_str; pub use percent_encoding::utf8_percent_encode; pub use percent_encoding::NON_ALPHANUMERIC; -- 2.30.2