From 6b4951ade670acbe3cb34b2002fbcd4b4e6a7300 Mon Sep 17 00:00:00 2001 From: Mica White Date: Fri, 8 Mar 2024 11:45:15 -0500 Subject: Replace ownership with mutable access --- examples/basic.rs | 12 ++++++------ examples/double_mutex.rs | 14 +++++++------- examples/list.rs | 19 +++++++++---------- 3 files changed, 22 insertions(+), 23 deletions(-) (limited to 'examples') diff --git a/examples/basic.rs b/examples/basic.rs index 535b80a..8dfca84 100644 --- a/examples/basic.rs +++ b/examples/basic.rs @@ -1,22 +1,22 @@ use std::thread; -use happylock::mutex::Mutex; +use happylock::mutex::{Mutex, SpinLock}; use happylock::ThreadKey; const N: usize = 10; -static DATA: Mutex = Mutex::new(0); +static DATA: SpinLock = Mutex::new(0); fn main() { for _ in 0..N { thread::spawn(move || { - let key = ThreadKey::lock().unwrap(); - let mut data = DATA.lock(key); + let mut key = ThreadKey::lock().unwrap(); + let mut data = DATA.lock(&mut key); *data += 1; }); } - let key = ThreadKey::lock().unwrap(); - let data = DATA.lock(key); + let mut key = ThreadKey::lock().unwrap(); + let data = DATA.lock(&mut key); println!("{}", *data); } diff --git a/examples/double_mutex.rs b/examples/double_mutex.rs index 76f3294..033bfed 100644 --- a/examples/double_mutex.rs +++ b/examples/double_mutex.rs @@ -1,27 +1,27 @@ use std::thread; -use happylock::mutex::Mutex; +use happylock::mutex::{Mutex, SpinLock}; use happylock::{LockGuard, ThreadKey}; const N: usize = 10; -static DATA_1: Mutex = Mutex::new(0); -static DATA_2: Mutex = Mutex::new(String::new()); +static DATA_1: SpinLock = Mutex::new(0); +static DATA_2: SpinLock = Mutex::new(String::new()); fn main() { for _ in 0..N { thread::spawn(move || { - let key = ThreadKey::lock().unwrap(); + let mut key = ThreadKey::lock().unwrap(); let data = (&DATA_1, &DATA_2); - let mut guard = LockGuard::lock(&data, key); + let mut guard = LockGuard::lock(&data, &mut key); *guard.1 = (100 - *guard.0).to_string(); *guard.0 += 1; }); } - let key = ThreadKey::lock().unwrap(); + let mut key = ThreadKey::lock().unwrap(); let data = (&DATA_1, &DATA_2); - let data = LockGuard::lock(&data, key); + let data = LockGuard::lock(&data, &mut key); println!("{}", *data.0); println!("{}", *data.1); } diff --git a/examples/list.rs b/examples/list.rs index 904d9f2..c73b54e 100644 --- a/examples/list.rs +++ b/examples/list.rs @@ -1,11 +1,11 @@ use std::thread; -use happylock::mutex::Mutex; +use happylock::mutex::{Mutex, SpinLock}; use happylock::{LockGuard, ThreadKey}; const N: usize = 10; -static DATA: [Mutex; 6] = [ +static DATA: [SpinLock; 6] = [ Mutex::new(0), Mutex::new(1), Mutex::new(2), @@ -14,39 +14,38 @@ static DATA: [Mutex; 6] = [ Mutex::new(5), ]; -static SEED: Mutex = Mutex::new(42); +static SEED: SpinLock = Mutex::new(42); -fn random(key: ThreadKey) -> (ThreadKey, usize) { +fn random(key: &mut ThreadKey) -> usize { let mut seed = SEED.lock(key); let x = *seed; let x = x ^ (x << 13); let x = x ^ (x >> 17); let x = x ^ (x << 5); *seed = x; - (Mutex::unlock(seed), x as usize) + x as usize } fn main() { for _ in 0..N { thread::spawn(move || { let mut key = ThreadKey::lock().unwrap(); - let mut rand; let mut data = Vec::new(); for _ in 0..3 { - (key, rand) = random(key); + let rand = random(&mut key); data.push(&DATA[rand % 6]); } let data = [data[0], data[1], data[2]]; - let mut guard = LockGuard::lock(&data, key); + let mut guard = LockGuard::lock(&data, &mut key); *guard[0] += *guard[1]; *guard[1] += *guard[2]; *guard[2] += *guard[0]; }); } - let key = ThreadKey::lock().unwrap(); - let data = LockGuard::lock(&DATA, key); + let mut key = ThreadKey::lock().unwrap(); + let data = LockGuard::lock(&DATA, &mut key); for val in &*data { println!("{}", **val); } -- cgit v1.2.3