about summary refs log tree commit diff
path: root/library/std/src/sys/unix
diff options
context:
space:
mode:
authorDylan DPC <dylan.dpc@gmail.com>2021-04-22 18:14:41 +0200
committerGitHub <noreply@github.com>2021-04-22 18:14:41 +0200
commitaac5125da4c8f06de57ab89985a5fc204c122483 (patch)
tree46237dc2672e9161fd9f2c96e8cdcf5f44d45f6e /library/std/src/sys/unix
parent54af84b7ef245791dbe8dc00be807284f9431071 (diff)
parenteb9b0f6ab7886bf89b1cc00dbc5248bfd62ea21d (diff)
downloadrust-aac5125da4c8f06de57ab89985a5fc204c122483.tar.gz
rust-aac5125da4c8f06de57ab89985a5fc204c122483.zip
Rollup merge of #84402 - CDirkx:rwlock, r=dtolnay
Move `sys_common::rwlock::StaticRWLock` etc. to `sys::unix::rwlock`

This moves `sys_common::rwlock::StaticRwLock`, `RWLockReadGuard` and `RWLockWriteGuard` to `sys::unix::rwlock`. They are already `#[cfg(unix)]` and don't need to be in `sys_common`.
Diffstat (limited to 'library/std/src/sys/unix')
-rw-r--r--library/std/src/sys/unix/os.rs2
-rw-r--r--library/std/src/sys/unix/rwlock.rs52
2 files changed, 53 insertions, 1 deletions
diff --git a/library/std/src/sys/unix/os.rs b/library/std/src/sys/unix/os.rs
index bf649f6d76f..503ba410097 100644
--- a/library/std/src/sys/unix/os.rs
+++ b/library/std/src/sys/unix/os.rs
@@ -21,8 +21,8 @@ use crate::slice;
 use crate::str;
 use crate::sys::cvt;
 use crate::sys::fd;
+use crate::sys::rwlock::{RWLockReadGuard, StaticRWLock};
 use crate::sys_common::mutex::{StaticMutex, StaticMutexGuard};
-use crate::sys_common::rwlock::{RWLockReadGuard, StaticRWLock};
 use crate::vec;
 
 use libc::{c_char, c_int, c_void};
diff --git a/library/std/src/sys/unix/rwlock.rs b/library/std/src/sys/unix/rwlock.rs
index 2b5067a34f6..d97d9d712fc 100644
--- a/library/std/src/sys/unix/rwlock.rs
+++ b/library/std/src/sys/unix/rwlock.rs
@@ -139,3 +139,55 @@ impl RWLock {
         }
     }
 }
+
+pub struct StaticRWLock(RWLock);
+
+impl StaticRWLock {
+    pub const fn new() -> StaticRWLock {
+        StaticRWLock(RWLock::new())
+    }
+
+    /// 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.
+    #[inline]
+    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();
+        }
+        RWLockReadGuard(&self.0)
+    }
+
+    /// 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.
+    #[inline]
+    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();
+        }
+        RWLockWriteGuard(&self.0)
+    }
+}
+
+pub struct RWLockReadGuard(&'static RWLock);
+
+impl Drop for RWLockReadGuard {
+    fn drop(&mut self) {
+        unsafe { self.0.read_unlock() }
+    }
+}
+
+pub struct RWLockWriteGuard(&'static RWLock);
+
+impl Drop for RWLockWriteGuard {
+    fn drop(&mut self) {
+        unsafe { self.0.write_unlock() }
+    }
+}