Skip to content

Commit

Permalink
refactor(pal/hermit): make ENV a non-mutable static
Browse files Browse the repository at this point in the history
Signed-off-by: Martin Kröning <martin.kroening@eonerc.rwth-aachen.de>
  • Loading branch information
mkroening committed Aug 1, 2024
1 parent 8b7f4ee commit 7bd6b11
Showing 1 changed file with 15 additions and 19 deletions.
34 changes: 15 additions & 19 deletions std/src/sys/pal/hermit/os.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,21 +68,21 @@ pub fn current_exe() -> io::Result<PathBuf> {
unsupported()
}

static mut ENV: Option<Mutex<HashMap<OsString, OsString>>> = None;
static ENV: Mutex<Option<HashMap<OsString, OsString>>> = Mutex::new(None);

pub fn init_environment(env: *const *const i8) {
unsafe {
ENV = Some(Mutex::new(HashMap::new()));
let mut guard = ENV.lock().unwrap();
let map = guard.insert(HashMap::new());

if env.is_null() {
return;
}
if env.is_null() {
return;
}

let mut guard = ENV.as_ref().unwrap().lock().unwrap();
unsafe {
let mut environ = env;
while !(*environ).is_null() {
if let Some((key, value)) = parse(CStr::from_ptr(*environ).to_bytes()) {
guard.insert(key, value);
map.insert(key, value);
}
environ = environ.add(1);
}
Expand Down Expand Up @@ -154,30 +154,26 @@ impl Iterator for Env {
/// Returns a vector of (variable, value) byte-vector pairs for all the
/// environment variables of the current process.
pub fn env() -> Env {
unsafe {
let guard = ENV.as_ref().unwrap().lock().unwrap();
let mut result = Vec::new();
let guard = ENV.lock().unwrap();
let env = guard.as_ref().unwrap();

for (key, value) in guard.iter() {
result.push((key.clone(), value.clone()));
}
let result = env.iter().map(|(key, value)| (key.clone(), value.clone())).collect::<Vec<_>>();

return Env { iter: result.into_iter() };
}
Env { iter: result.into_iter() }
}

pub fn getenv(k: &OsStr) -> Option<OsString> {
unsafe { ENV.as_ref().unwrap().lock().unwrap().get_mut(k).cloned() }
ENV.lock().unwrap().as_ref().unwrap().get(k).cloned()
}

pub unsafe fn setenv(k: &OsStr, v: &OsStr) -> io::Result<()> {
let (k, v) = (k.to_owned(), v.to_owned());
ENV.as_ref().unwrap().lock().unwrap().insert(k, v);
ENV.lock().unwrap().as_mut().unwrap().insert(k, v);
Ok(())
}

pub unsafe fn unsetenv(k: &OsStr) -> io::Result<()> {
ENV.as_ref().unwrap().lock().unwrap().remove(k);
ENV.lock().unwrap().as_mut().unwrap().remove(k);
Ok(())
}

Expand Down

0 comments on commit 7bd6b11

Please sign in to comment.