Skip to content

Commit 4fc181d

Browse files
committed
split guard into read and write types
1 parent 44abad5 commit 4fc181d

File tree

2 files changed

+16
-17
lines changed

2 files changed

+16
-17
lines changed

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

+2-2
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::{RWLockGuard, StaticRWLock};
25+
use crate::sys_common::rwlock::{RWLockReadGuard, StaticRWLock};
2626
use crate::vec;
2727

2828
use libc::{c_char, c_int, c_void};
@@ -496,7 +496,7 @@ pub unsafe fn environ() -> *mut *const *const c_char {
496496

497497
static ENV_LOCK: StaticRWLock = StaticRWLock::new();
498498

499-
pub fn env_read_lock() -> RWLockGuard {
499+
pub fn env_read_lock() -> RWLockReadGuard {
500500
ENV_LOCK.read_with_guard()
501501
}
502502

library/std/src/sys_common/rwlock.rs

+14-15
Original file line numberDiff line numberDiff line change
@@ -102,47 +102,46 @@ impl StaticRWLock {
102102
///
103103
/// The lock is automatically unlocked when the returned guard is dropped.
104104
#[inline]
105-
pub fn read_with_guard(&'static self) -> RWLockGuard {
105+
pub fn read_with_guard(&'static self) -> RWLockReadGuard {
106106
// Safety: All methods require static references, therefore self
107107
// cannot be moved between invocations.
108108
unsafe {
109109
self.0.read();
110110
}
111-
RWLockGuard(&self.0, GuardType::Read)
111+
RWLockReadGuard(&self.0)
112112
}
113113

114114
/// Acquires write access to the underlying lock, blocking the current thread
115115
/// to do so.
116116
///
117117
/// The lock is automatically unlocked when the returned guard is dropped.
118118
#[inline]
119-
pub fn write_with_guard(&'static self) -> RWLockGuard {
119+
pub fn write_with_guard(&'static self) -> RWLockWriteGuard {
120120
// Safety: All methods require static references, therefore self
121121
// cannot be moved between invocations.
122122
unsafe {
123123
self.0.write();
124124
}
125-
RWLockGuard(&self.0, GuardType::Write)
125+
RWLockWriteGuard(&self.0)
126126
}
127127
}
128128

129129
#[cfg(unix)]
130-
enum GuardType {
131-
Read,
132-
Write,
130+
pub struct RWLockReadGuard(&'static RWLock);
131+
132+
#[cfg(unix)]
133+
impl Drop for RWLockReadGuard {
134+
fn drop(&mut self) {
135+
unsafe { self.0.read_unlock() }
136+
}
133137
}
134138

135139
#[cfg(unix)]
136-
pub struct RWLockGuard(&'static RWLock, GuardType);
140+
pub struct RWLockWriteGuard(&'static RWLock);
137141

138142
#[cfg(unix)]
139-
impl Drop for RWLockGuard {
143+
impl Drop for RWLockWriteGuard {
140144
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-
}
145+
unsafe { self.0.write_unlock() }
147146
}
148147
}

0 commit comments

Comments
 (0)