From 7bd236853ef5ae705328c8fdc492cf60fc6887c1 Mon Sep 17 00:00:00 2001 From: Mica White Date: Wed, 13 Mar 2024 22:44:46 -0400 Subject: Lockable overhaul --- examples/dining_philosophers.rs | 6 +++--- examples/double_mutex.rs | 6 +++--- examples/list.rs | 6 +++--- 3 files changed, 9 insertions(+), 9 deletions(-) (limited to 'examples') diff --git a/examples/dining_philosophers.rs b/examples/dining_philosophers.rs index 35aa330..34efb0e 100644 --- a/examples/dining_philosophers.rs +++ b/examples/dining_philosophers.rs @@ -1,6 +1,6 @@ use std::{thread, time::Duration}; -use happylock::{LockCollection, Mutex, ThreadKey}; +use happylock::{Mutex, RefLockCollection, ThreadKey}; static PHILOSOPHERS: [Philosopher; 5] = [ Philosopher { @@ -50,8 +50,8 @@ impl Philosopher { thread::sleep(Duration::from_secs(1)); // safety: no philosopher asks for the same fork twice - let forks = - unsafe { LockCollection::new_unchecked([&FORKS[self.left], &FORKS[self.right]]) }; + let forks = [&FORKS[self.left], &FORKS[self.right]]; + let forks = unsafe { RefLockCollection::new_unchecked(&forks) }; let forks = forks.lock(key); println!("{} is eating...", self.name); thread::sleep(Duration::from_secs(1)); diff --git a/examples/double_mutex.rs b/examples/double_mutex.rs index 621f81e..cd627c4 100644 --- a/examples/double_mutex.rs +++ b/examples/double_mutex.rs @@ -1,6 +1,6 @@ use std::thread; -use happylock::{LockCollection, Mutex, ThreadKey}; +use happylock::{Mutex, RefLockCollection, ThreadKey}; const N: usize = 10; @@ -11,7 +11,7 @@ fn main() { for _ in 0..N { let th = thread::spawn(move || { let key = ThreadKey::get().unwrap(); - let lock = LockCollection::new_ref(&DATA); + let lock = RefLockCollection::new(&DATA); let mut guard = lock.lock(key); *guard.1 = (100 - *guard.0).to_string(); *guard.0 += 1; @@ -24,7 +24,7 @@ fn main() { } let key = ThreadKey::get().unwrap(); - let data = LockCollection::new_ref(&DATA); + let data = RefLockCollection::new(&DATA); let data = data.lock(key); println!("{}", *data.0); println!("{}", *data.1); diff --git a/examples/list.rs b/examples/list.rs index 3b03f2d..cf344e7 100644 --- a/examples/list.rs +++ b/examples/list.rs @@ -1,6 +1,6 @@ use std::thread; -use happylock::{LockCollection, Mutex, ThreadKey}; +use happylock::{Mutex, RefLockCollection, ThreadKey}; const N: usize = 10; @@ -37,7 +37,7 @@ fn main() { data.push(&DATA[rand % 6]); } - let Some(lock) = LockCollection::try_new(data) else { + let Some(lock) = RefLockCollection::try_new(&data) else { continue; }; let mut guard = lock.lock(&mut key); @@ -56,7 +56,7 @@ fn main() { } let key = ThreadKey::get().unwrap(); - let data = LockCollection::new_ref(&DATA); + let data = RefLockCollection::new(&DATA); let data = data.lock(key); for val in &*data { println!("{}", **val); -- cgit v1.2.3