From 32f972a26a0066291873445088718deec3ed4233 Mon Sep 17 00:00:00 2001 From: Mica White Date: Tue, 16 Jul 2024 17:18:33 -0400 Subject: Impl Lock for LockCollection --- src/collection/retry.rs | 134 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 134 insertions(+) (limited to 'src/collection/retry.rs') diff --git a/src/collection/retry.rs b/src/collection/retry.rs index 2b9b0a0..b73788a 100644 --- a/src/collection/retry.rs +++ b/src/collection/retry.rs @@ -22,6 +22,140 @@ fn contains_duplicates(data: L) -> bool { false } +unsafe impl RawLock for RetryingLockCollection { + unsafe fn lock(&self) { + let mut first_index = 0; + let mut locks = Vec::new(); + self.data.get_ptrs(&mut locks); + + 'outer: loop { + // safety: we have the thread key + locks[first_index].lock(); + for (i, lock) in locks.iter().enumerate() { + if i == first_index { + continue; + } + + // safety: we have the thread key + if !lock.try_lock() { + for lock in locks.iter().take(i) { + // safety: we already locked all of these + lock.unlock(); + } + + if first_index >= i { + // safety: this is already locked and can't be unlocked + // by the previous loop + locks[first_index].unlock(); + } + + first_index = i; + continue 'outer; + } + } + } + } + + unsafe fn try_lock(&self) -> bool { + let mut locks = Vec::new(); + self.data.get_ptrs(&mut locks); + + if locks.is_empty() { + return true; + } + + unsafe { + for (i, lock) in locks.iter().enumerate() { + // safety: we have the thread key + if !lock.try_lock() { + for lock in locks.iter().take(i) { + // safety: we already locked all of these + lock.unlock(); + } + return false; + } + } + } + + true + } + + unsafe fn unlock(&self) { + let mut locks = Vec::new(); + self.get_ptrs(&mut locks); + + for lock in locks { + lock.unlock(); + } + } + + unsafe fn read(&self) { + let mut first_index = 0; + let mut locks = Vec::new(); + self.data.get_ptrs(&mut locks); + + 'outer: loop { + // safety: we have the thread key + locks[first_index].read(); + for (i, lock) in locks.iter().enumerate() { + if i == first_index { + continue; + } + + // safety: we have the thread key + if !lock.try_read() { + for lock in locks.iter().take(i) { + // safety: we already locked all of these + lock.unlock_read(); + } + + if first_index >= i { + // safety: this is already locked and can't be unlocked + // by the previous loop + locks[first_index].unlock_read(); + } + + first_index = i; + continue 'outer; + } + } + } + } + + unsafe fn try_read(&self) -> bool { + let mut locks = Vec::new(); + self.data.get_ptrs(&mut locks); + + if locks.is_empty() { + return true; + } + + unsafe { + for (i, lock) in locks.iter().enumerate() { + // safety: we have the thread key + if !lock.try_read() { + for lock in locks.iter().take(i) { + // safety: we already locked all of these + lock.unlock_read(); + } + return false; + } + } + } + + true + } + + unsafe fn unlock_read(&self) { + let mut locks = Vec::new(); + self.get_ptrs(&mut locks); + + for lock in locks { + lock.unlock_read(); + } + } +} + unsafe impl Lockable for RetryingLockCollection { type Guard<'g> = L::Guard<'g> where Self: 'g; -- cgit v1.2.3