Skip to content
This repository has been archived by the owner on Dec 9, 2018. It is now read-only.

Commit

Permalink
Implement sync::Mutex
Browse files Browse the repository at this point in the history
  • Loading branch information
anatol committed May 10, 2017
1 parent 527c543 commit 4b34b90
Show file tree
Hide file tree
Showing 7 changed files with 1,016 additions and 3 deletions.
89 changes: 89 additions & 0 deletions examples/mutex.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
#[cfg(not(any(target_arch = "aarch64",
target_arch = "arm",
target_arch = "powerpc",
target_arch = "x86",
target_arch = "x86_64")))]
fn main() {}

fn test_mutex() {
use std::{time, thread};
use std::sync::{Arc, Mutex};

let mut threads = vec![];
const NTHREADS: i32 = 100;
const ITERATIONS: i32 = 100;

let data = Arc::new(Mutex::new(5));
for _ in 0..NTHREADS {
let data = data.clone();
let t = thread::spawn(move ||
for _ in 0..ITERATIONS {
// sleep for 1 ms
thread::sleep(time::Duration::from_millis(1));

let mut data = data.lock().unwrap();
*data += 1;

match data.try_lock() {
Ok(_) => panic!("cannot re-lock non-reentrant mutex"),
Err(_) => {},
}
});

threads.push(t);
}

for t in threads {
t.join().unwrap();
}

assert_eq!(*data.lock().unwrap(), 5 + NTHREADS * ITERATIONS);
}

fn test_reentrant_mutex() {
use std::{time, thread};
use std::sync::{Arc, Mutex};

let mut threads = vec![];
const NTHREADS: i32 = 100;
const ITERATIONS: i32 = 100;

let data = Arc::new(Mutex::new(5));
for _ in 0..NTHREADS {
let data = data.clone();
let t = thread::spawn(move ||
for _ in 0..ITERATIONS {
// sleep for 1 ms
thread::sleep(time::Duration::from_millis(1));

let mut data = data.lock().unwrap();
*data += 1;

let mut data2 = data.lock().unwrap();
*data2 += 1;

match data.try_lock() {
Ok(_) => {},
Err(_) => panic!("failed to lock reentrant mutex"),
}
});

threads.push(t);
}

for t in threads {
t.join().unwrap();
}

assert_eq!(*data.lock().unwrap(), 5 + 2 * NTHREADS * ITERATIONS);
}

#[cfg(any(target_arch = "aarch64",
target_arch = "arm",
target_arch = "powerpc",
target_arch = "x86",
target_arch = "x86_64"))]
fn main() {
test_mutex();
test_reentrant_mutex();
}
6 changes: 3 additions & 3 deletions src/sync/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ pub use core::sync::atomic;
// pub use self::barrier::{Barrier, BarrierWaitResult};
// #[stable(feature = "rust1", since = "1.0.0")]
// pub use self::condvar::{Condvar, WaitTimeoutResult};
// #[stable(feature = "rust1", since = "1.0.0")]
// pub use self::mutex::{Mutex, MutexGuard};
#[stable(feature = "rust1", since = "1.0.0")]
pub use self::mutex::{Mutex, MutexGuard};
// #[stable(feature = "rust1", since = "1.0.0")]
// pub use self::once::{Once, OnceState, ONCE_INIT};
// #[stable(feature = "rust1", since = "1.0.0")]
Expand All @@ -39,6 +39,6 @@ pub use self::rwlock::{RwLock, RwLockReadGuard, RwLockWriteGuard};

// mod barrier;
// mod condvar;
// mod mutex;
mod mutex;
// mod once;
mod rwlock;
Loading

0 comments on commit 4b34b90

Please sign in to comment.