Skip to content

Commit 44abad5

Browse files
committed
introduce StaticRWLock wrapper to make methods safe
1 parent 2200cf1 commit 44abad5

File tree

3 files changed

+72
-60
lines changed

3 files changed

+72
-60
lines changed

library/std/src/sys/unix/os.rs

+9-8
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use crate::str;
2222
use crate::sys::cvt;
2323
use crate::sys::fd;
2424
use crate::sys_common::mutex::{StaticMutex, StaticMutexGuard};
25-
use crate::sys_common::rwlock::{RWLock, RWLockGuard};
25+
use crate::sys_common::rwlock::{RWLockGuard, StaticRWLock};
2626
use crate::vec;
2727

2828
use libc::{c_char, c_int, c_void};
@@ -494,16 +494,17 @@ pub unsafe fn environ() -> *mut *const *const c_char {
494494
ptr::addr_of_mut!(environ)
495495
}
496496

497-
pub unsafe fn env_rwlock(readonly: bool) -> RWLockGuard {
498-
static ENV_LOCK: RWLock = RWLock::new();
499-
if readonly { ENV_LOCK.read_with_guard() } else { ENV_LOCK.write_with_guard() }
497+
static ENV_LOCK: StaticRWLock = StaticRWLock::new();
498+
499+
pub fn env_read_lock() -> RWLockGuard {
500+
ENV_LOCK.read_with_guard()
500501
}
501502

502503
/// Returns a vector of (variable, value) byte-vector pairs for all the
503504
/// environment variables of the current process.
504505
pub fn env() -> Env {
505506
unsafe {
506-
let _guard = env_rwlock(true);
507+
let _guard = env_read_lock();
507508
let mut environ = *environ();
508509
let mut result = Vec::new();
509510
if !environ.is_null() {
@@ -540,7 +541,7 @@ pub fn getenv(k: &OsStr) -> io::Result<Option<OsString>> {
540541
// always None as well
541542
let k = CString::new(k.as_bytes())?;
542543
unsafe {
543-
let _guard = env_rwlock(true);
544+
let _guard = env_read_lock();
544545
let s = libc::getenv(k.as_ptr()) as *const libc::c_char;
545546
let ret = if s.is_null() {
546547
None
@@ -556,7 +557,7 @@ pub fn setenv(k: &OsStr, v: &OsStr) -> io::Result<()> {
556557
let v = CString::new(v.as_bytes())?;
557558

558559
unsafe {
559-
let _guard = env_rwlock(false);
560+
let _guard = ENV_LOCK.write_with_guard();
560561
cvt(libc::setenv(k.as_ptr(), v.as_ptr(), 1)).map(drop)
561562
}
562563
}
@@ -565,7 +566,7 @@ pub fn unsetenv(n: &OsStr) -> io::Result<()> {
565566
let nbuf = CString::new(n.as_bytes())?;
566567

567568
unsafe {
568-
let _guard = env_rwlock(false);
569+
let _guard = ENV_LOCK.write_with_guard();
569570
cvt(libc::unsetenv(nbuf.as_ptr())).map(drop)
570571
}
571572
}

library/std/src/sys/unix/process/process_unix.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ impl Command {
4747
// a lock any more because the parent won't do anything and the child is
4848
// in its own process.
4949
let result = unsafe {
50-
let _env_lock = sys::os::env_rwlock(true);
50+
let _env_lock = sys::os::env_read_lock();
5151
cvt(libc::fork())?
5252
};
5353

@@ -124,7 +124,7 @@ impl Command {
124124
// Similar to when forking, we want to ensure that access to
125125
// the environment is synchronized, so make sure to grab the
126126
// environment lock before we try to exec.
127-
let _lock = sys::os::env_rwlock(true);
127+
let _lock = sys::os::env_read_lock();
128128

129129
let Err(e) = self.do_exec(theirs, envp.as_ref());
130130
e
@@ -404,7 +404,7 @@ impl Command {
404404
cvt_nz(libc::posix_spawnattr_setflags(attrs.0.as_mut_ptr(), flags as _))?;
405405

406406
// Make sure we synchronize access to the global `environ` resource
407-
let _env_lock = sys::os::env_rwlock(true);
407+
let _env_lock = sys::os::env_read_lock();
408408
let envp = envp.map(|c| c.as_ptr()).unwrap_or_else(|| *sys::os::environ() as *const _);
409409
cvt_nz(libc::posix_spawnp(
410410
&mut p.pid,

library/std/src/sys_common/rwlock.rs

+60-49
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,5 @@
11
use crate::sys::rwlock as imp;
22

3-
#[cfg(unix)]
4-
enum GuardType {
5-
Read,
6-
Write,
7-
}
8-
9-
#[cfg(unix)]
10-
pub struct RWLockGuard(&'static RWLock, GuardType);
11-
12-
#[cfg(unix)]
13-
impl Drop for RWLockGuard {
14-
fn drop(&mut self) {
15-
unsafe {
16-
match &self.1 {
17-
GuardType::Read => self.0.read_unlock(),
18-
GuardType::Write => self.0.write_unlock(),
19-
}
20-
}
21-
}
22-
}
23-
243
/// An OS-based reader-writer lock.
254
///
265
/// This structure is entirely unsafe and serves as the lowest layer of a
@@ -47,20 +26,6 @@ impl RWLock {
4726
self.0.read()
4827
}
4928

50-
/// Acquires shared access to the underlying lock, blocking the current
51-
/// thread to do so.
52-
///
53-
/// The lock is automatically unlocked when the returned guard is dropped.
54-
///
55-
/// Behavior is undefined if the rwlock has been moved between this and any
56-
/// previous method call.
57-
#[inline]
58-
#[cfg(unix)]
59-
pub unsafe fn read_with_guard(&'static self) -> RWLockGuard {
60-
self.read();
61-
RWLockGuard(&self, GuardType::Read)
62-
}
63-
6429
/// Attempts to acquire shared access to this lock, returning whether it
6530
/// succeeded or not.
6631
///
@@ -83,20 +48,6 @@ impl RWLock {
8348
self.0.write()
8449
}
8550

86-
/// Acquires write access to the underlying lock, blocking the current thread
87-
/// to do so.
88-
///
89-
/// The lock is automatically unlocked when the returned guard is dropped.
90-
///
91-
/// Behavior is undefined if the rwlock has been moved between this and any
92-
/// previous method call.
93-
#[inline]
94-
#[cfg(unix)]
95-
pub unsafe fn write_with_guard(&'static self) -> RWLockGuard {
96-
self.write();
97-
RWLockGuard(&self, GuardType::Write)
98-
}
99-
10051
/// Attempts to acquire exclusive access to this lock, returning whether it
10152
/// succeeded or not.
10253
///
@@ -135,3 +86,63 @@ impl RWLock {
13586
self.0.destroy()
13687
}
13788
}
89+
90+
// the cfg annotations only exist due to dead code warnings. the code itself is portable
91+
#[cfg(unix)]
92+
pub struct StaticRWLock(RWLock);
93+
94+
#[cfg(unix)]
95+
impl StaticRWLock {
96+
pub const fn new() -> StaticRWLock {
97+
StaticRWLock(RWLock::new())
98+
}
99+
100+
/// Acquires shared access to the underlying lock, blocking the current
101+
/// thread to do so.
102+
///
103+
/// The lock is automatically unlocked when the returned guard is dropped.
104+
#[inline]
105+
pub fn read_with_guard(&'static self) -> RWLockGuard {
106+
// Safety: All methods require static references, therefore self
107+
// cannot be moved between invocations.
108+
unsafe {
109+
self.0.read();
110+
}
111+
RWLockGuard(&self.0, GuardType::Read)
112+
}
113+
114+
/// Acquires write access to the underlying lock, blocking the current thread
115+
/// to do so.
116+
///
117+
/// The lock is automatically unlocked when the returned guard is dropped.
118+
#[inline]
119+
pub fn write_with_guard(&'static self) -> RWLockGuard {
120+
// Safety: All methods require static references, therefore self
121+
// cannot be moved between invocations.
122+
unsafe {
123+
self.0.write();
124+
}
125+
RWLockGuard(&self.0, GuardType::Write)
126+
}
127+
}
128+
129+
#[cfg(unix)]
130+
enum GuardType {
131+
Read,
132+
Write,
133+
}
134+
135+
#[cfg(unix)]
136+
pub struct RWLockGuard(&'static RWLock, GuardType);
137+
138+
#[cfg(unix)]
139+
impl Drop for RWLockGuard {
140+
fn drop(&mut self) {
141+
unsafe {
142+
match &self.1 {
143+
GuardType::Read => self.0.read_unlock(),
144+
GuardType::Write => self.0.write_unlock(),
145+
}
146+
}
147+
}
148+
}

0 commit comments

Comments
 (0)