about summary refs log tree commit diff
path: root/library/std/src/sys_common
diff options
context:
space:
mode:
authorThe8472 <git@infinite-source.de>2021-02-08 23:34:23 +0100
committerThe8472 <git@infinite-source.de>2021-02-08 23:35:02 +0100
commit44abad5b12afa58b9f495593f1c8b090e644fd7e (patch)
tree3e06f6dfa335c60bcc53d35fd6c402ac717b17af /library/std/src/sys_common
parent2200cf10d8051f535f726f2800b935a527696de8 (diff)
downloadrust-44abad5b12afa58b9f495593f1c8b090e644fd7e.tar.gz
rust-44abad5b12afa58b9f495593f1c8b090e644fd7e.zip
introduce StaticRWLock wrapper to make methods safe
Diffstat (limited to 'library/std/src/sys_common')
-rw-r--r--library/std/src/sys_common/rwlock.rs109
1 files changed, 60 insertions, 49 deletions
diff --git a/library/std/src/sys_common/rwlock.rs b/library/std/src/sys_common/rwlock.rs
index 7fd902ee0fe..cc13771009f 100644
--- a/library/std/src/sys_common/rwlock.rs
+++ b/library/std/src/sys_common/rwlock.rs
@@ -1,26 +1,5 @@
 use crate::sys::rwlock as imp;
 
-#[cfg(unix)]
-enum GuardType {
-    Read,
-    Write,
-}
-
-#[cfg(unix)]
-pub struct RWLockGuard(&'static RWLock, GuardType);
-
-#[cfg(unix)]
-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
@@ -47,20 +26,6 @@ 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]
-    #[cfg(unix)]
-    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.
     ///
@@ -83,20 +48,6 @@ 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]
-    #[cfg(unix)]
-    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.
     ///
@@ -135,3 +86,63 @@ impl RWLock {
         self.0.destroy()
     }
 }
+
+// the cfg annotations only exist due to dead code warnings. the code itself is portable
+#[cfg(unix)]
+pub struct StaticRWLock(RWLock);
+
+#[cfg(unix)]
+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) -> RWLockGuard {
+        // Safety: All methods require static references, therefore self
+        // cannot be moved between invocations.
+        unsafe {
+            self.0.read();
+        }
+        RWLockGuard(&self.0, GuardType::Read)
+    }
+
+    /// 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) -> RWLockGuard {
+        // Safety: All methods require static references, therefore self
+        // cannot be moved between invocations.
+        unsafe {
+            self.0.write();
+        }
+        RWLockGuard(&self.0, GuardType::Write)
+    }
+}
+
+#[cfg(unix)]
+enum GuardType {
+    Read,
+    Write,
+}
+
+#[cfg(unix)]
+pub struct RWLockGuard(&'static RWLock, GuardType);
+
+#[cfg(unix)]
+impl Drop for RWLockGuard {
+    fn drop(&mut self) {
+        unsafe {
+            match &self.1 {
+                GuardType::Read => self.0.read_unlock(),
+                GuardType::Write => self.0.write_unlock(),
+            }
+        }
+    }
+}