From 4fc181dd62f38c7b424e5261756a2f01ded68a5b Mon Sep 17 00:00:00 2001 From: The8472 Date: Tue, 9 Feb 2021 18:49:29 +0100 Subject: [PATCH] split guard into read and write types --- library/std/src/sys/unix/os.rs | 4 ++-- library/std/src/sys_common/rwlock.rs | 35 ++++++++++++++-------------- 2 files changed, 19 insertions(+), 20 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)] -pub struct RWLockGuard(&'static RWLock, GuardType); - -#[cfg(unix)] -impl Drop for RWLockGuard { +impl Drop for RWLockReadGuard { fn drop(&mut self) { - unsafe { - match &self.1 { - GuardType::Read => self.0.read_unlock(), - GuardType::Write => self.0.write_unlock(), - } - } + unsafe { self.0.read_unlock() } + } +} + +#[cfg(unix)] +pub struct RWLockWriteGuard(&'static RWLock); + +#[cfg(unix)] +impl Drop for RWLockWriteGuard { + fn drop(&mut self) { + unsafe { self.0.write_unlock() } } }