Convert some lazy_static to OnceCell/OnceLock

These have clearer sync/unsync semantics and now ship with rust itself.
They don't paper over any possible cross-thread issues, and we can specifically
choose which we want for the purpose.
This commit is contained in:
Mahmoud Al-Qudsi 2024-05-16 20:07:44 -05:00
parent 0f18480559
commit 66618d64cb
2 changed files with 13 additions and 13 deletions

@ -24,15 +24,16 @@ use std::ops::{Deref, DerefMut};
use std::sync::{atomic::AtomicU64, atomic::Ordering, Arc, Mutex, MutexGuard};
// Universal variables instance.
lazy_static! {
static ref UVARS: Mutex<EnvUniversal> = Mutex::new(EnvUniversal::new());
}
/// Getter for universal variables.
/// This is typically initialized in env_init(), and is considered empty before then.
pub fn uvars() -> MutexGuard<'static, EnvUniversal> {
UVARS.lock().unwrap()
use std::sync::OnceLock;
/// Universal variables instance.
static UVARS: OnceLock<Mutex<EnvUniversal>> = OnceLock::new();
UVARS
.get_or_init(|| Mutex::new(EnvUniversal::new()))
.lock()
.unwrap()
}
/// Whether we were launched with no_config; in this case setting a uvar instead sets a global.

13
src/env/var.rs vendored

@ -2,7 +2,6 @@ use crate::signal::Signal;
use crate::wchar::{wstr, WString, L};
use crate::wcstringutil::join_strings;
use bitflags::bitflags;
use lazy_static::lazy_static;
use libc::c_int;
use std::collections::HashMap;
use std::path::PathBuf;
@ -97,11 +96,6 @@ bitflags! {
}
}
// A shared, empty list.
lazy_static! {
static ref EMPTY_LIST: Arc<Box<[WString]>> = Arc::new(Box::new([]));
}
/// EnvVar is an immutable value-type data structure representing the value of an environment
/// variable.
#[derive(Clone, Debug, Eq, PartialEq)]
@ -115,8 +109,13 @@ pub struct EnvVar {
impl Default for EnvVar {
fn default() -> Self {
use std::sync::OnceLock;
/// A shared read-only empty list.
static EMPTY_LIST: OnceLock<Arc<Box<[WString]>>> = OnceLock::new();
let empty_list = EMPTY_LIST.get_or_init(|| Arc::new(Box::new([])));
EnvVar {
values: EMPTY_LIST.clone(),
values: Arc::clone(empty_list),
flags: EnvVarFlags::empty(),
}
}