]> git.proxmox.com Git - cargo.git/blobdiff - vendor/crossbeam-utils/src/atomic/seq_lock.rs
New upstream version 0.52.0
[cargo.git] / vendor / crossbeam-utils / src / atomic / seq_lock.rs
index 533a036b5db7e9b18459f7eaa8f1a3fc7dc27181..ff8defd26dab85c744fa1f3987f9d4c67dd568f1 100644 (file)
@@ -1,9 +1,10 @@
+use core::mem;
 use core::sync::atomic::{self, AtomicUsize, Ordering};
 
-use Backoff;
+use crate::Backoff;
 
 /// A simple stamped lock.
-pub struct SeqLock {
+pub(crate) struct SeqLock {
     /// The current state of the lock.
     ///
     /// All bits except the least significant one hold the current stamp. When locked, the state
@@ -12,15 +13,17 @@ pub struct SeqLock {
 }
 
 impl SeqLock {
-    pub const INIT: Self = Self {
-        state: AtomicUsize::new(0),
-    };
+    pub(crate) const fn new() -> Self {
+        Self {
+            state: AtomicUsize::new(0),
+        }
+    }
 
     /// If not locked, returns the current stamp.
     ///
     /// This method should be called before optimistic reads.
     #[inline]
-    pub fn optimistic_read(&self) -> Option<usize> {
+    pub(crate) fn optimistic_read(&self) -> Option<usize> {
         let state = self.state.load(Ordering::Acquire);
         if state == 1 {
             None
@@ -34,14 +37,14 @@ impl SeqLock {
     /// This method should be called after optimistic reads to check whether they are valid. The
     /// argument `stamp` should correspond to the one returned by method `optimistic_read`.
     #[inline]
-    pub fn validate_read(&self, stamp: usize) -> bool {
+    pub(crate) fn validate_read(&self, stamp: usize) -> bool {
         atomic::fence(Ordering::Acquire);
         self.state.load(Ordering::Relaxed) == stamp
     }
 
     /// Grabs the lock for writing.
     #[inline]
-    pub fn write(&'static self) -> SeqLockWriteGuard {
+    pub(crate) fn write(&'static self) -> SeqLockWriteGuard {
         let backoff = Backoff::new();
         loop {
             let previous = self.state.swap(1, Ordering::Acquire);
@@ -61,7 +64,7 @@ impl SeqLock {
 }
 
 /// An RAII guard that releases the lock and increments the stamp when dropped.
-pub struct SeqLockWriteGuard {
+pub(crate) struct SeqLockWriteGuard {
     /// The parent lock.
     lock: &'static SeqLock,
 
@@ -72,8 +75,12 @@ pub struct SeqLockWriteGuard {
 impl SeqLockWriteGuard {
     /// Releases the lock without incrementing the stamp.
     #[inline]
-    pub fn abort(self) {
+    pub(crate) fn abort(self) {
         self.lock.state.store(self.state, Ordering::Release);
+
+        // We specifically don't want to call drop(), since that's
+        // what increments the stamp.
+        mem::forget(self);
     }
 }
 
@@ -86,3 +93,20 @@ impl Drop for SeqLockWriteGuard {
             .store(self.state.wrapping_add(2), Ordering::Release);
     }
 }
+
+#[cfg(test)]
+mod tests {
+    use super::SeqLock;
+
+    #[test]
+    fn test_abort() {
+        static LK: SeqLock = SeqLock::new();
+        let before = LK.optimistic_read().unwrap();
+        {
+            let guard = LK.write();
+            guard.abort();
+        }
+        let after = LK.optimistic_read().unwrap();
+        assert_eq!(before, after, "aborted write does not update the stamp");
+    }
+}