2020-01-27 21:49:06 -06:00
|
|
|
#![feature(rustc_private)]
|
|
|
|
|
2020-03-28 10:16:08 -05:00
|
|
|
use std::sync::{Mutex, TryLockError};
|
2020-04-16 23:24:57 -05:00
|
|
|
use std::sync::atomic;
|
2018-08-23 12:28:48 -05:00
|
|
|
|
|
|
|
fn main() {
|
2020-03-27 20:26:21 -05:00
|
|
|
test_mutex_stdlib();
|
2020-02-17 21:30:24 -06:00
|
|
|
#[cfg(not(target_os = "windows"))] // TODO: implement RwLock on Windows
|
|
|
|
{
|
|
|
|
test_rwlock_stdlib();
|
2020-03-28 09:35:51 -05:00
|
|
|
}
|
2020-04-16 23:24:57 -05:00
|
|
|
test_spin_loop_hint();
|
2020-02-17 21:30:24 -06:00
|
|
|
}
|
|
|
|
|
2020-03-27 20:26:21 -05:00
|
|
|
fn test_mutex_stdlib() {
|
2020-01-28 21:07:09 -06:00
|
|
|
let m = Mutex::new(0);
|
2020-01-27 21:49:06 -06:00
|
|
|
{
|
|
|
|
let _guard = m.lock();
|
2020-01-28 21:07:09 -06:00
|
|
|
assert!(m.try_lock().unwrap_err().would_block());
|
2020-01-27 21:49:06 -06:00
|
|
|
}
|
|
|
|
drop(m.try_lock().unwrap());
|
2018-08-23 12:28:48 -05:00
|
|
|
drop(m);
|
2020-02-17 21:30:24 -06:00
|
|
|
}
|
2018-08-23 12:28:48 -05:00
|
|
|
|
2020-02-17 21:30:24 -06:00
|
|
|
#[cfg(not(target_os = "windows"))]
|
|
|
|
fn test_rwlock_stdlib() {
|
2020-03-28 10:16:08 -05:00
|
|
|
use std::sync::RwLock;
|
2020-02-17 21:30:24 -06:00
|
|
|
let rw = RwLock::new(0);
|
2018-08-30 02:20:08 -05:00
|
|
|
{
|
2020-02-17 21:30:24 -06:00
|
|
|
let _read_guard = rw.read().unwrap();
|
|
|
|
drop(rw.read().unwrap());
|
|
|
|
drop(rw.try_read().unwrap());
|
|
|
|
assert!(rw.try_write().unwrap_err().would_block());
|
|
|
|
}
|
2020-01-27 21:49:06 -06:00
|
|
|
|
2020-02-17 21:30:24 -06:00
|
|
|
{
|
|
|
|
let _write_guard = rw.write().unwrap();
|
|
|
|
assert!(rw.try_read().unwrap_err().would_block());
|
|
|
|
assert!(rw.try_write().unwrap_err().would_block());
|
|
|
|
}
|
|
|
|
}
|
2020-01-27 21:49:06 -06:00
|
|
|
|
2020-01-28 21:07:09 -06:00
|
|
|
trait TryLockErrorExt<T> {
|
|
|
|
fn would_block(&self) -> bool;
|
|
|
|
}
|
2020-01-27 21:49:06 -06:00
|
|
|
|
2020-01-28 21:07:09 -06:00
|
|
|
impl<T> TryLockErrorExt<T> for TryLockError<T> {
|
|
|
|
fn would_block(&self) -> bool {
|
|
|
|
match self {
|
|
|
|
TryLockError::WouldBlock => true,
|
|
|
|
TryLockError::Poisoned(_) => false,
|
2020-01-27 21:49:06 -06:00
|
|
|
}
|
2018-08-30 02:20:08 -05:00
|
|
|
}
|
2018-08-23 12:28:48 -05:00
|
|
|
}
|
2020-04-16 23:24:57 -05:00
|
|
|
|
|
|
|
fn test_spin_loop_hint() {
|
|
|
|
atomic::spin_loop_hint();
|
|
|
|
}
|