rust/tests/fail/sync/libc_pthread_mutex_wrong_owner.rs

31 lines
789 B
Rust
Raw Normal View History

//@ignore-target-windows: No libc on Windows
2020-04-18 14:25:11 -05:00
#![feature(rustc_private)]
use std::cell::UnsafeCell;
use std::sync::Arc;
use std::thread;
struct Mutex(UnsafeCell<libc::pthread_mutex_t>);
unsafe impl Send for Mutex {}
unsafe impl Sync for Mutex {}
fn new_lock() -> Arc<Mutex> {
Arc::new(Mutex(UnsafeCell::new(libc::PTHREAD_MUTEX_INITIALIZER)))
}
fn main() {
unsafe {
let lock = new_lock();
assert_eq!(libc::pthread_mutex_lock(lock.0.get() as *mut _), 0);
let lock_copy = lock.clone();
thread::spawn(move || {
2020-05-24 13:29:56 -05:00
assert_eq!(libc::pthread_mutex_unlock(lock_copy.0.get() as *mut _), 0); //~ ERROR: Undefined Behavior: unlocked a default mutex that was not locked by the current thread
2020-04-18 14:25:11 -05:00
})
.join()
.unwrap();
}
}