diff options
| author | The8472 <git@infinite-source.de> | 2021-02-06 22:38:16 +0100 |
|---|---|---|
| committer | The8472 <git@infinite-source.de> | 2021-02-07 09:12:21 +0100 |
| commit | 55ca27faa78434d81d3f59535c96bbb2a08f0d5c (patch) | |
| tree | 6d1c3b5d95255346ad63ef4cd2c8c405eb26a85d /library/std/src/sys_common | |
| parent | cfba499271ba53190a1d3647ff8f7202ec9ed6f5 (diff) | |
use rwlock for accessing ENV
Diffstat (limited to 'library/std/src/sys_common')
| -rw-r--r-- | library/std/src/sys_common/rwlock.rs | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/library/std/src/sys_common/rwlock.rs b/library/std/src/sys_common/rwlock.rs index 3705d641a1b..1f2765ffbcc 100644 --- a/library/std/src/sys_common/rwlock.rs +++ b/library/std/src/sys_common/rwlock.rs @@ -1,5 +1,23 @@ use crate::sys::rwlock as imp; +enum GuardType { + Read, + Write, +} + +pub struct RWLockGuard(&'static RWLock, GuardType); + +impl Drop for RWLockGuard { + fn drop(&mut self) { + unsafe { + match &self.1 { + GuardType::Read => self.0.read_unlock(), + GuardType::Write => self.0.write_unlock(), + } + } + } +} + /// An OS-based reader-writer lock. /// /// This structure is entirely unsafe and serves as the lowest layer of a @@ -26,6 +44,19 @@ impl RWLock { self.0.read() } + /// Acquires shared access to the underlying lock, blocking the current + /// thread to do so. + /// + /// The lock is automatically unlocked when the returned guard is dropped. + /// + /// Behavior is undefined if the rwlock has been moved between this and any + /// previous method call. + #[inline] + pub unsafe fn read_with_guard(&'static self) -> RWLockGuard { + self.read(); + RWLockGuard(&self, GuardType::Read) + } + /// Attempts to acquire shared access to this lock, returning whether it /// succeeded or not. /// @@ -48,6 +79,19 @@ impl RWLock { self.0.write() } + /// Acquires write access to the underlying lock, blocking the current thread + /// to do so. + /// + /// The lock is automatically unlocked when the returned guard is dropped. + /// + /// Behavior is undefined if the rwlock has been moved between this and any + /// previous method call. + #[inline] + pub unsafe fn write_with_guard(&'static self) -> RWLockGuard { + self.write(); + RWLockGuard(&self, GuardType::Write) + } + /// Attempts to acquire exclusive access to this lock, returning whether it /// succeeded or not. /// |
