chiark / gitweb /
locks: Make config an Option
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Fri, 9 Apr 2021 16:27:10 +0000 (17:27 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Fri, 9 Apr 2021 16:27:10 +0000 (17:27 +0100)
This makes it plausible to implement ConstDefault for it.

Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
src/config.rs
src/global.rs
src/prelude.rs

index cc6d129210fecd5a2895f4e7ad1da7efa0a31f89..38b7dab9e2469fe1b1909bb37d482e7ddc7245a1 100644 (file)
@@ -191,15 +191,29 @@ impl TryFrom<ServerConfigSpec> 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<ServerConfig> {
-  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 {
index 1e33ace813a21151fd5d857032545c4cacfaf033..44b258c1ef554ecf48064266ec8d6c61dcfb3915 100644 (file)
@@ -206,7 +206,7 @@ pub struct Global {
   // <- InstanceContainer ->
   // inner locks which the game needs:
   dirty: Mutex<VecDeque<InstanceRef>>,
-  pub config: RwLock<WholeServerConfig>,
+  pub config: RwLock<Option<WholeServerConfig>>,
 
   // fast global lookups
   players: RwLock<TokenTable<PlayerId>>,
index 7911b6b7a6fc8aa043d0f406904ac965a922710a..964323358f2ef317fe45ba1a9fa6de4ce0b59e1b 100644 (file)
@@ -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;