]> git.lizzy.rs Git - rust.git/blobdiff - library/std/src/sys/unix/rwlock.rs
Auto merge of #83386 - mark-i-m:stabilize-pat2015, r=nikomatsakis
[rust.git] / library / std / src / sys / unix / rwlock.rs
index 2b5067a34f648fa41baa290706ad3a53a347cce3..d97d9d712fc93a383450b3f270fb5bdfb5a662e7 100644 (file)
@@ -139,3 +139,55 @@ pub unsafe fn destroy(&self) {
         }
     }
 }
+
+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() }
+    }
+}