use crate::sys::cvt;
use crate::sys::fd;
use crate::sys_common::mutex::{StaticMutex, StaticMutexGuard};
+use crate::sys_common::rwlock::{RWLockReadGuard, StaticRWLock};
use crate::vec;
use libc::{c_char, c_int, c_void};
extern "C" {
static mut environ: *const *const c_char;
}
- &mut environ
+ ptr::addr_of_mut!(environ)
}
-pub unsafe fn env_lock() -> StaticMutexGuard {
- // It is UB to attempt to acquire this mutex reentrantly!
- static ENV_LOCK: StaticMutex = StaticMutex::new();
- ENV_LOCK.lock()
+static ENV_LOCK: StaticRWLock = StaticRWLock::new();
+
+pub fn env_read_lock() -> RWLockReadGuard {
+ ENV_LOCK.read_with_guard()
}
/// 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_lock();
+ let _guard = env_read_lock();
let mut environ = *environ();
let mut result = Vec::new();
if !environ.is_null() {
// always None as well
let k = CString::new(k.as_bytes())?;
unsafe {
- let _guard = env_lock();
+ let _guard = env_read_lock();
let s = libc::getenv(k.as_ptr()) as *const libc::c_char;
let ret = if s.is_null() {
None
let v = CString::new(v.as_bytes())?;
unsafe {
- let _guard = env_lock();
+ let _guard = ENV_LOCK.write_with_guard();
cvt(libc::setenv(k.as_ptr(), v.as_ptr(), 1)).map(drop)
}
}
let nbuf = CString::new(n.as_bytes())?;
unsafe {
- let _guard = env_lock();
+ let _guard = ENV_LOCK.write_with_guard();
cvt(libc::unsetenv(nbuf.as_ptr())).map(drop)
}
}