rust/tests/run-pass/sync.rs

82 lines
3.3 KiB
Rust
Raw Normal View History

// Just instantiate some data structures to make sure we got all their foreign items covered.
// Requires full MIR on Windows.
2020-01-27 21:49:06 -06:00
#![feature(rustc_private)]
use std::sync;
2020-01-27 21:49:06 -06:00
extern crate libc;
fn main() {
let m = sync::Mutex::new(0);
2020-01-27 21:49:06 -06:00
{
let _guard = m.lock();
let try_lock_error = m.try_lock().unwrap_err();
if let sync::TryLockError::Poisoned(e) = try_lock_error {
panic!("{}", e);
}
}
drop(m.try_lock().unwrap());
drop(m);
2019-06-30 09:45:41 -05:00
#[cfg(not(target_os = "windows"))] // TODO: implement RwLock on Windows
2018-08-30 02:20:08 -05:00
{
let rw = sync::RwLock::new(0);
2020-01-27 21:49:06 -06:00
{
let _read_guard = rw.read().unwrap();
drop(rw.read().unwrap());
drop(rw.try_read().unwrap());
let try_lock_error = rw.try_write().unwrap_err();
if let sync::TryLockError::Poisoned(e) = try_lock_error {
panic!("{}", e);
}
}
{
let _write_guard = rw.write().unwrap();
let try_lock_error = rw.try_read().unwrap_err();
if let sync::TryLockError::Poisoned(e) = try_lock_error {
panic!("{}", e);
}
let try_lock_error = rw.try_write().unwrap_err();
if let sync::TryLockError::Poisoned(e) = try_lock_error {
panic!("{}", e);
}
}
// 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
unsafe {
let mut mutex: libc::pthread_mutex_t = std::mem::zeroed();
assert_eq!(libc::pthread_mutex_init(&mut mutex as *mut _, std::ptr::null_mut()), 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);
}
unsafe {
let mut rw: libc::pthread_rwlock_t = std::mem::zeroed();
assert_eq!(libc::pthread_rwlock_init(&mut rw as *mut _, std::ptr::null_mut()), 0);
assert_eq!(libc::pthread_rwlock_rdlock(&mut rw as *mut _), 0);
assert_eq!(libc::pthread_rwlock_rdlock(&mut rw as *mut _), 0);
assert_eq!(libc::pthread_rwlock_unlock(&mut rw as *mut _), 0);
assert_eq!(libc::pthread_rwlock_tryrdlock(&mut rw as *mut _), 0);
assert_eq!(libc::pthread_rwlock_unlock(&mut rw as *mut _), 0);
assert_eq!(libc::pthread_rwlock_trywrlock(&mut rw as *mut _), libc::EBUSY);
assert_eq!(libc::pthread_rwlock_unlock(&mut rw as *mut _), 0);
assert_eq!(libc::pthread_rwlock_wrlock(&mut rw as *mut _), 0);
assert_eq!(libc::pthread_rwlock_tryrdlock(&mut rw as *mut _), libc::EBUSY);
assert_eq!(libc::pthread_rwlock_trywrlock(&mut rw as *mut _), libc::EBUSY);
assert_eq!(libc::pthread_rwlock_unlock(&mut rw as *mut _), 0);
assert_eq!(libc::pthread_rwlock_destroy(&mut rw as *mut _), 0);
}
2018-08-30 02:20:08 -05:00
}
}