rust/tests/run-pass/sync.rs

140 lines
5.5 KiB
Rust
Raw Normal View History

2020-01-27 21:49:06 -06:00
#![feature(rustc_private)]
2020-01-28 21:07:09 -06:00
use std::sync::{Mutex, RwLock, TryLockError};
2020-01-27 21:49:06 -06:00
extern crate libc;
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
{
2020-03-27 20:26:21 -05:00
test_mutex_libc_init_recursive();
test_mutex_libc_init_normal();
2020-02-17 21:30:24 -06:00
test_rwlock_stdlib();
test_rwlock_libc_static_initializer();
}
#[cfg(target_os = "linux")]
{
test_mutex_libc_static_initializer_recursive();
}
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());
drop(m);
2020-02-17 21:30:24 -06:00
}
#[cfg(not(target_os = "windows"))]
2020-03-27 20:26:21 -05:00
fn test_mutex_libc_init_recursive() {
unsafe {
let mut attr: libc::pthread_mutexattr_t = std::mem::zeroed();
assert_eq!(libc::pthread_mutexattr_init(&mut attr as *mut _), 0);
assert_eq!(libc::pthread_mutexattr_settype(&mut attr as *mut _, libc::PTHREAD_MUTEX_RECURSIVE), 0);
let mut mutex: libc::pthread_mutex_t = std::mem::zeroed();
assert_eq!(libc::pthread_mutex_init(&mut mutex as *mut _, &mut attr as *mut _), 0);
assert_eq!(libc::pthread_mutex_lock(&mut mutex as *mut _), 0);
assert_eq!(libc::pthread_mutex_trylock(&mut mutex as *mut _), 0);
assert_eq!(libc::pthread_mutex_unlock(&mut mutex as *mut _), 0);
assert_eq!(libc::pthread_mutex_unlock(&mut mutex as *mut _), 0);
assert_eq!(libc::pthread_mutex_trylock(&mut mutex as *mut _), 0);
assert_eq!(libc::pthread_mutex_lock(&mut mutex as *mut _), 0);
assert_eq!(libc::pthread_mutex_unlock(&mut mutex as *mut _), 0);
assert_eq!(libc::pthread_mutex_unlock(&mut mutex as *mut _), 0);
assert_eq!(libc::pthread_mutex_unlock(&mut mutex as *mut _), libc::EPERM);
assert_eq!(libc::pthread_mutex_destroy(&mut mutex as *mut _), 0);
assert_eq!(libc::pthread_mutexattr_destroy(&mut attr as *mut _), 0);
}
}
2020-03-27 20:26:21 -05:00
#[cfg(not(target_os = "windows"))]
fn test_mutex_libc_init_normal() {
unsafe {
let mut mutexattr: libc::pthread_mutexattr_t = std::mem::zeroed();
assert_eq!(libc::pthread_mutexattr_settype(&mut mutexattr as *mut _, libc::PTHREAD_MUTEX_NORMAL), 0);
let mut mutex: libc::pthread_mutex_t = std::mem::zeroed();
assert_eq!(libc::pthread_mutex_init(&mut mutex as *mut _, &mutexattr as *const _), 0);
assert_eq!(libc::pthread_mutex_lock(&mut mutex as *mut _), 0);
assert_eq!(libc::pthread_mutex_trylock(&mut mutex as *mut _), libc::EBUSY);
assert_eq!(libc::pthread_mutex_unlock(&mut mutex as *mut _), 0);
assert_eq!(libc::pthread_mutex_trylock(&mut mutex as *mut _), 0);
assert_eq!(libc::pthread_mutex_unlock(&mut mutex as *mut _), 0);
assert_eq!(libc::pthread_mutex_destroy(&mut mutex as *mut _), 0);
}
}
#[cfg(target_os = "linux")]
2020-03-27 20:26:21 -05:00
fn test_mutex_libc_static_initializer_recursive() {
let mutex = std::cell::UnsafeCell::new(libc::PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP);
unsafe {
assert_eq!(libc::pthread_mutex_lock(mutex.get()), 0);
assert_eq!(libc::pthread_mutex_trylock(mutex.get()), 0);
assert_eq!(libc::pthread_mutex_unlock(mutex.get()), 0);
assert_eq!(libc::pthread_mutex_unlock(mutex.get()), 0);
assert_eq!(libc::pthread_mutex_trylock(mutex.get()), 0);
assert_eq!(libc::pthread_mutex_lock(mutex.get()), 0);
assert_eq!(libc::pthread_mutex_unlock(mutex.get()), 0);
assert_eq!(libc::pthread_mutex_unlock(mutex.get()), 0);
assert_eq!(libc::pthread_mutex_unlock(mutex.get()), libc::EPERM);
assert_eq!(libc::pthread_mutex_destroy(mutex.get()), 0);
}
}
2020-02-17 21:30:24 -06:00
#[cfg(not(target_os = "windows"))]
fn test_rwlock_stdlib() {
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-02-17 21:30:24 -06:00
// need to go a layer deeper and test the behavior of libc functions, because
// std::sys::unix::rwlock::RWLock keeps track of write_locked and num_readers
2020-01-27 21:49:06 -06:00
2020-02-17 21:30:24 -06:00
#[cfg(not(target_os = "windows"))]
fn test_rwlock_libc_static_initializer() {
let rw = std::cell::UnsafeCell::new(libc::PTHREAD_RWLOCK_INITIALIZER);
unsafe {
assert_eq!(libc::pthread_rwlock_rdlock(rw.get()), 0);
assert_eq!(libc::pthread_rwlock_rdlock(rw.get()), 0);
assert_eq!(libc::pthread_rwlock_unlock(rw.get()), 0);
assert_eq!(libc::pthread_rwlock_tryrdlock(rw.get()), 0);
assert_eq!(libc::pthread_rwlock_unlock(rw.get()), 0);
assert_eq!(libc::pthread_rwlock_trywrlock(rw.get()), libc::EBUSY);
assert_eq!(libc::pthread_rwlock_unlock(rw.get()), 0);
2020-01-28 21:07:09 -06:00
2020-02-17 21:30:24 -06:00
assert_eq!(libc::pthread_rwlock_wrlock(rw.get()), 0);
assert_eq!(libc::pthread_rwlock_tryrdlock(rw.get()), libc::EBUSY);
assert_eq!(libc::pthread_rwlock_trywrlock(rw.get()), libc::EBUSY);
assert_eq!(libc::pthread_rwlock_unlock(rw.get()), 0);
2020-01-27 21:49:06 -06:00
2020-02-17 21:30:24 -06:00
assert_eq!(libc::pthread_rwlock_destroy(rw.get()), 0);
2020-01-28 21:07:09 -06:00
}
}
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
}
}