summaryrefslogtreecommitdiff
path: root/src/collection/retry_collection.rs
diff options
context:
space:
mode:
authorBotahamec <botahamec@outlook.com>2024-05-21 13:18:10 -0400
committerBotahamec <botahamec@outlook.com>2024-05-21 13:18:10 -0400
commit6e3aa5182604b30ef75ba5676e9f677cc1d18fe3 (patch)
treed8e23a82c8151c4b49c15f49707d96651764db39 /src/collection/retry_collection.rs
parent875d5c4ad6e0c2a78c15476584fc686121b340d3 (diff)
parentc344021797b7e1f8027bd9d1302908f0767e362b (diff)
Merge remote-tracking branch 'origin/0.2' into 0.2
Diffstat (limited to 'src/collection/retry_collection.rs')
-rw-r--r--src/collection/retry_collection.rs138
1 files changed, 138 insertions, 0 deletions
diff --git a/src/collection/retry_collection.rs b/src/collection/retry_collection.rs
new file mode 100644
index 0000000..73f9e18
--- /dev/null
+++ b/src/collection/retry_collection.rs
@@ -0,0 +1,138 @@
+use std::marker::PhantomData;
+
+use crate::{lockable::Lock, Keyable, Lockable, OwnedLockable};
+
+use super::{LockGuard, RetryingLockCollection};
+
+fn contains_duplicates<L: Lockable>(data: L) -> bool {
+ let mut locks = Vec::new();
+ data.get_ptrs(&mut locks);
+ let mut locks: Vec<_> = locks.into_iter().map(|l| l as *const dyn Lock).collect();
+ locks.sort_unstable();
+ locks.windows(2).any(|w| std::ptr::addr_eq(w[0], w[1]))
+}
+
+impl<L: OwnedLockable> RetryingLockCollection<L> {
+ #[must_use]
+ pub const fn new(data: L) -> Self {
+ Self { data }
+ }
+}
+
+impl<'a, L: OwnedLockable> RetryingLockCollection<&'a L> {
+ #[must_use]
+ pub const fn new_ref(data: &'a L) -> Self {
+ Self { data }
+ }
+}
+
+impl<L: Lockable> RetryingLockCollection<L> {
+ #[must_use]
+ pub const unsafe fn new_unchecked(data: L) -> Self {
+ Self { data }
+ }
+
+ pub fn try_new(data: L) -> Option<Self> {
+ contains_duplicates(&data).then_some(Self { data })
+ }
+
+ pub fn lock<'a, 'key: 'a, Key: Keyable + 'key>(
+ &'a self,
+ key: Key,
+ ) -> LockGuard<'a, 'key, L, Key> {
+ let mut first_index = 0;
+ let mut locks = Vec::new();
+ self.data.get_ptrs(&mut locks);
+
+ if locks.is_empty() {
+ return LockGuard {
+ // safety: there's no data being returned
+ guard: unsafe { self.data.guard() },
+ key,
+ _phantom: PhantomData,
+ };
+ }
+
+ let guard = unsafe {
+ '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;
+ }
+ }
+
+ // safety: we locked all the data
+ break self.data.guard();
+ }
+ };
+
+ LockGuard {
+ guard,
+ key,
+ _phantom: PhantomData,
+ }
+ }
+
+ pub fn try_lock<'a, 'key: 'a, Key: Keyable + 'key>(
+ &'a self,
+ key: Key,
+ ) -> Option<LockGuard<'a, 'key, L, Key>> {
+ let mut locks = Vec::new();
+ self.data.get_ptrs(&mut locks);
+
+ if locks.is_empty() {
+ return Some(LockGuard {
+ // safety: there's no data being returned
+ guard: unsafe { self.data.guard() },
+ key,
+ _phantom: PhantomData,
+ });
+ }
+
+ let guard = 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 None;
+ }
+ }
+
+ // safety: we locked all the data
+ self.data.guard()
+ };
+
+ Some(LockGuard {
+ guard,
+ key,
+ _phantom: PhantomData,
+ })
+ }
+
+ pub fn unlock<'key, Key: Keyable + 'key>(guard: LockGuard<'_, 'key, L, Key>) -> Key {
+ drop(guard.guard);
+ guard.key
+ }
+}