Skip to content

Commit 2606b40

Browse files
committed
std: use OnceLock for SGX environment variable storage
1 parent 2574810 commit 2606b40

1 file changed

Lines changed: 11 additions & 30 deletions

File tree

library/std/src/sys/env/sgx.rs

Lines changed: 11 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,35 @@
1-
#![allow(implicit_provenance_casts)] // FIXME: this module systematically confuses pointers and integers
2-
31
pub use super::common::Env;
42
use crate::collections::HashMap;
53
use crate::ffi::{OsStr, OsString};
64
use crate::io;
7-
use crate::sync::atomic::{Atomic, AtomicUsize, Ordering};
8-
use crate::sync::{Mutex, Once};
5+
use crate::sync::{Mutex, OnceLock};
6+
7+
type EnvStore = Mutex<HashMap<OsString, OsString>>;
98

109
// Specifying linkage/symbol name is solely to ensure a single instance between this crate and its unit tests
1110
#[cfg_attr(test, linkage = "available_externally")]
1211
#[unsafe(export_name = "_ZN16__rust_internals3std3sys3pal3sgx2os3ENVE")]
13-
static ENV: Atomic<usize> = AtomicUsize::new(0);
14-
// Specifying linkage/symbol name is solely to ensure a single instance between this crate and its unit tests
15-
#[cfg_attr(test, linkage = "available_externally")]
16-
#[unsafe(export_name = "_ZN16__rust_internals3std3sys3pal3sgx2os8ENV_INITE")]
17-
static ENV_INIT: Once = Once::new();
18-
type EnvStore = Mutex<HashMap<OsString, OsString>>;
19-
20-
fn get_env_store() -> Option<&'static EnvStore> {
21-
unsafe { (ENV.load(Ordering::Relaxed) as *const EnvStore).as_ref() }
22-
}
23-
24-
fn create_env_store() -> &'static EnvStore {
25-
ENV_INIT.call_once(|| {
26-
ENV.store(Box::into_raw(Box::new(EnvStore::default())) as _, Ordering::Relaxed)
27-
});
28-
unsafe { &*(ENV.load(Ordering::Relaxed) as *const EnvStore) }
29-
}
12+
static ENV: OnceLock<EnvStore> = OnceLock::new();
3013

3114
pub fn env() -> Env {
32-
let clone_to_vec = |map: &HashMap<OsString, OsString>| -> Vec<_> {
33-
map.iter().map(|(k, v)| (k.clone(), v.clone())).collect()
34-
};
35-
36-
let env = get_env_store().map(|env| clone_to_vec(&env.lock().unwrap())).unwrap_or_default();
15+
let env = ENV
16+
.get()
17+
.map(|env| env.lock().unwrap().iter().map(|(k, v)| (k.clone(), v.clone())).collect())
18+
.unwrap_or_default();
3719
Env::new(env)
3820
}
3921

4022
pub fn getenv(k: &OsStr) -> Option<OsString> {
41-
get_env_store().and_then(|s| s.lock().unwrap().get(k).cloned())
23+
ENV.get().and_then(|s| s.lock().unwrap().get(k).cloned())
4224
}
4325

4426
pub unsafe fn setenv(k: &OsStr, v: &OsStr) -> io::Result<()> {
45-
let (k, v) = (k.to_owned(), v.to_owned());
46-
create_env_store().lock().unwrap().insert(k, v);
27+
ENV.get_or_init(|| EnvStore::default()).lock().unwrap().insert(k.to_owned(), v.to_owned());
4728
Ok(())
4829
}
4930

5031
pub unsafe fn unsetenv(k: &OsStr) -> io::Result<()> {
51-
if let Some(env) = get_env_store() {
32+
if let Some(env) = ENV.get() {
5233
env.lock().unwrap().remove(k);
5334
}
5435
Ok(())

0 commit comments

Comments
 (0)