diff options
| author | The8472 <git@infinite-source.de> | 2021-03-14 19:10:34 +0100 |
|---|---|---|
| committer | The8472 <git@infinite-source.de> | 2021-03-14 19:10:34 +0100 |
| commit | e22143c075c7aee0a3a5bc90b51adff6cd250b34 (patch) | |
| tree | 0437dc1abc8a47b16a66b475ef3fd90511a053bb /library/std/src/sys/unix/os.rs | |
| parent | 9320b121b5b20d2201c9f5ec40fb3c241fcac6f0 (diff) | |
| download | rust-e22143c075c7aee0a3a5bc90b51adff6cd250b34.tar.gz rust-e22143c075c7aee0a3a5bc90b51adff6cd250b34.zip | |
Revert "Revert "use RWlock when accessing os::env #81850""
This reverts commit acdca316c3d42299d31c1b47eb792006ffdfc29c.
Diffstat (limited to 'library/std/src/sys/unix/os.rs')
| -rw-r--r-- | library/std/src/sys/unix/os.rs | 19 |
1 files changed, 10 insertions, 9 deletions
diff --git a/library/std/src/sys/unix/os.rs b/library/std/src/sys/unix/os.rs index d5e14bec765..1d1118aa694 100644 --- a/library/std/src/sys/unix/os.rs +++ b/library/std/src/sys/unix/os.rs @@ -22,6 +22,7 @@ use crate::str; 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}; @@ -490,20 +491,20 @@ pub unsafe fn environ() -> *mut *const *const c_char { 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() { @@ -540,7 +541,7 @@ pub fn getenv(k: &OsStr) -> io::Result<Option<OsString>> { // 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 @@ -556,7 +557,7 @@ pub fn setenv(k: &OsStr, v: &OsStr) -> io::Result<()> { 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) } } @@ -565,7 +566,7 @@ pub fn unsetenv(n: &OsStr) -> io::Result<()> { 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) } } |
