diff options
| author | The8472 <git@infinite-source.de> | 2021-02-09 18:49:29 +0100 |
|---|---|---|
| committer | The8472 <git@infinite-source.de> | 2021-02-09 19:13:21 +0100 |
| commit | 4fc181dd62f38c7b424e5261756a2f01ded68a5b (patch) | |
| tree | 4e7508d182ea51d434b1ec53fed5b0df6f20d9e7 /library/std/src | |
| parent | 44abad5b12afa58b9f495593f1c8b090e644fd7e (diff) | |
split guard into read and write types
Diffstat (limited to 'library/std/src')
| -rw-r--r-- | library/std/src/sys/unix/os.rs | 4 | ||||
| -rw-r--r-- | library/std/src/sys_common/rwlock.rs | 29 |
2 files changed, 16 insertions, 17 deletions
diff --git a/library/std/src/sys/unix/os.rs b/library/std/src/sys/unix/os.rs index f5a607561c0..1d1118aa694 100644 --- a/library/std/src/sys/unix/os.rs +++ b/library/std/src/sys/unix/os.rs @@ -22,7 +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::{RWLockGuard, StaticRWLock}; +use crate::sys_common::rwlock::{RWLockReadGuard, StaticRWLock}; use crate::vec; use libc::{c_char, c_int, c_void}; @@ -496,7 +496,7 @@ pub unsafe fn environ() -> *mut *const *const c_char { static ENV_LOCK: StaticRWLock = StaticRWLock::new(); -pub fn env_read_lock() -> RWLockGuard { +pub fn env_read_lock() -> RWLockReadGuard { ENV_LOCK.read_with_guard() } diff --git a/library/std/src/sys_common/rwlock.rs b/library/std/src/sys_common/rwlock.rs index cc13771009f..41e8ad77294 100644 --- a/library/std/src/sys_common/rwlock.rs +++ b/library/std/src/sys_common/rwlock.rs @@ -102,13 +102,13 @@ impl StaticRWLock { /// /// The lock is automatically unlocked when the returned guard is dropped. #[inline] - pub fn read_with_guard(&'static self) -> RWLockGuard { + pub fn read_with_guard(&'static self) -> RWLockReadGuard { // Safety: All methods require static references, therefore self // cannot be moved between invocations. unsafe { self.0.read(); } - RWLockGuard(&self.0, GuardType::Read) + RWLockReadGuard(&self.0) } /// Acquires write access to the underlying lock, blocking the current thread @@ -116,33 +116,32 @@ impl StaticRWLock { /// /// The lock is automatically unlocked when the returned guard is dropped. #[inline] - pub fn write_with_guard(&'static self) -> RWLockGuard { + pub fn write_with_guard(&'static self) -> RWLockWriteGuard { // Safety: All methods require static references, therefore self // cannot be moved between invocations. unsafe { self.0.write(); } - RWLockGuard(&self.0, GuardType::Write) + RWLockWriteGuard(&self.0) } } #[cfg(unix)] -enum GuardType { - Read, - Write, +pub struct RWLockReadGuard(&'static RWLock); + +#[cfg(unix)] +impl Drop for RWLockReadGuard { + fn drop(&mut self) { + unsafe { self.0.read_unlock() } + } } #[cfg(unix)] -pub struct RWLockGuard(&'static RWLock, GuardType); +pub struct RWLockWriteGuard(&'static RWLock); #[cfg(unix)] -impl Drop for RWLockGuard { +impl Drop for RWLockWriteGuard { fn drop(&mut self) { - unsafe { - match &self.1 { - GuardType::Read => self.0.read_unlock(), - GuardType::Write => self.0.write_unlock(), - } - } + unsafe { self.0.write_unlock() } } } |
