This repository has been archived by the owner on Dec 9, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
1,237 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
#[cfg(not(any(target_arch = "aarch64", | ||
target_arch = "arm", | ||
target_arch = "powerpc", | ||
target_arch = "x86", | ||
target_arch = "x86_64")))] | ||
fn main() {} | ||
|
||
#[cfg(any(target_arch = "aarch64", | ||
target_arch = "arm", | ||
target_arch = "powerpc", | ||
target_arch = "x86", | ||
target_arch = "x86_64"))] | ||
fn main() { | ||
use std::{time, thread}; | ||
use std::sync::{Arc, RwLock}; | ||
|
||
let mut threads = vec![]; | ||
const NTHREADS: i32 = 100; | ||
const ITERATIONS: i32 = 100; | ||
|
||
let lock = Arc::new(RwLock::new(5)); | ||
for _ in 0..NTHREADS { | ||
let lock = lock.clone(); | ||
let t = thread::spawn(move || | ||
for _ in 0..ITERATIONS { | ||
// sleep for 1 ms | ||
thread::sleep(time::Duration::from_millis(1)); | ||
{ | ||
let r1 = lock.read().unwrap(); | ||
assert!(*r1 >= 5); | ||
|
||
match lock.try_read() { | ||
Ok(r2) => assert!(*r2 >= 5), | ||
Err(_) => {}, | ||
} | ||
} | ||
|
||
{ | ||
//let mut w = lock.try_write().unwrap(); | ||
//*w += 1; | ||
} | ||
|
||
{ | ||
let mut w = lock.write().unwrap(); | ||
*w += 1; | ||
} | ||
}); | ||
|
||
threads.push(t); | ||
} | ||
|
||
for t in threads { | ||
t.join().unwrap(); | ||
} | ||
|
||
assert_eq!(*lock.read().unwrap(), 5 + NTHREADS * ITERATIONS); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT | ||
// file at the top-level directory of this distribution and at | ||
// http://rust-lang.org/COPYRIGHT. | ||
// | ||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | ||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | ||
// option. This file may not be copied, modified, or distributed | ||
// except according to those terms. | ||
|
||
//! Useful synchronization primitives. | ||
//! | ||
//! This module contains useful safe and unsafe synchronization primitives. | ||
//! Most of the primitives in this module do not provide any sort of locking | ||
//! and/or blocking at all, but rather provide the necessary tools to build | ||
//! other types of concurrent primitives. | ||
|
||
#![stable(feature = "rust1", since = "1.0.0")] | ||
|
||
#[stable(feature = "rust1", since = "1.0.0")] | ||
pub use alloc::arc::{Arc, Weak}; | ||
#[stable(feature = "rust1", since = "1.0.0")] | ||
pub use core::sync::atomic; | ||
|
||
// #[stable(feature = "rust1", since = "1.0.0")] | ||
// 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::once::{Once, OnceState, ONCE_INIT}; | ||
// #[stable(feature = "rust1", since = "1.0.0")] | ||
// pub use sys_common::poison::{PoisonError, TryLockError, TryLockResult, LockResult}; | ||
#[stable(feature = "rust1", since = "1.0.0")] | ||
pub use self::rwlock::{RwLock, RwLockReadGuard, RwLockWriteGuard}; | ||
|
||
// pub mod mpsc; | ||
|
||
// mod barrier; | ||
// mod condvar; | ||
// mod mutex; | ||
// mod once; | ||
mod rwlock; |
Oops, something went wrong.