blob: e52dbd3186e1e375f7cb271f39c0b826a47e7bcf (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
|
use std::hint;
use std::sync::{Mutex, TryLockError, atomic};
fn main() {
test_mutex_stdlib();
test_rwlock_stdlib();
test_spin_loop_hint();
test_thread_yield_now();
}
fn test_mutex_stdlib() {
let m = Mutex::new(0);
{
let _guard = m.lock();
assert!(m.try_lock().unwrap_err().would_block());
}
drop(m.try_lock().unwrap());
drop(m);
}
fn test_rwlock_stdlib() {
use std::sync::RwLock;
let rw = RwLock::new(0);
{
let _read_guard = rw.read().unwrap();
drop(rw.read().unwrap());
drop(rw.try_read().unwrap());
assert!(rw.try_write().unwrap_err().would_block());
}
{
let _write_guard = rw.write().unwrap();
assert!(rw.try_read().unwrap_err().would_block());
assert!(rw.try_write().unwrap_err().would_block());
}
}
trait TryLockErrorExt<T> {
fn would_block(&self) -> bool;
}
impl<T> TryLockErrorExt<T> for TryLockError<T> {
fn would_block(&self) -> bool {
match self {
TryLockError::WouldBlock => true,
TryLockError::Poisoned(_) => false,
}
}
}
fn test_spin_loop_hint() {
#[allow(deprecated)]
atomic::spin_loop_hint();
hint::spin_loop();
}
fn test_thread_yield_now() {
std::thread::yield_now();
}
|