Skip to content

Commit

Permalink
Merge pull request #4020 from RalfJung/thread-sope
Browse files Browse the repository at this point in the history
pthread-sync test: use thread::scope for more reliable thread scoping
  • Loading branch information
RalfJung authored Nov 9, 2024
2 parents 9160aa0 + 29e1641 commit b7839b6
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions tests/pass-dep/libc/pthread-sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,22 @@ fn main() {
check_condattr();
}

// We want to only use pthread APIs here for easier testing.
// So we can't use `thread::scope`. That means panics can lead
// to a failure to join threads which can lead to further issues,
// so let's turn such unwinding into aborts.
struct AbortOnDrop;
impl AbortOnDrop {
fn defuse(self) {
mem::forget(self);
}
}
impl Drop for AbortOnDrop {
fn drop(&mut self) {
std::process::abort();
}
}

fn test_mutex_libc_init_recursive() {
unsafe {
let mut attr: libc::pthread_mutexattr_t = mem::zeroed();
Expand Down Expand Up @@ -122,6 +138,7 @@ impl<T> Clone for SendPtr<T> {
}

fn check_mutex() {
let bomb = AbortOnDrop;
// Specifically *not* using `Arc` to make sure there is no synchronization apart from the mutex.
unsafe {
let data = SyncUnsafeCell::new((libc::PTHREAD_MUTEX_INITIALIZER, 0));
Expand All @@ -148,9 +165,11 @@ fn check_mutex() {
assert_eq!(libc::pthread_mutex_trylock(mutexptr), 0);
assert_eq!((*ptr.ptr).1, 3);
}
bomb.defuse();
}

fn check_rwlock_write() {
let bomb = AbortOnDrop;
unsafe {
let data = SyncUnsafeCell::new((libc::PTHREAD_RWLOCK_INITIALIZER, 0));
let ptr = SendPtr { ptr: data.get() };
Expand Down Expand Up @@ -187,9 +206,11 @@ fn check_rwlock_write() {
assert_eq!(libc::pthread_rwlock_tryrdlock(rwlockptr), 0);
assert_eq!((*ptr.ptr).1, 3);
}
bomb.defuse();
}

fn check_rwlock_read_no_deadlock() {
let bomb = AbortOnDrop;
unsafe {
let l1 = SyncUnsafeCell::new(libc::PTHREAD_RWLOCK_INITIALIZER);
let l1 = SendPtr { ptr: l1.get() };
Expand All @@ -213,9 +234,11 @@ fn check_rwlock_read_no_deadlock() {
assert_eq!(libc::pthread_rwlock_rdlock(l2.ptr), 0);
handle.join().unwrap();
}
bomb.defuse();
}

fn check_cond() {
let bomb = AbortOnDrop;
unsafe {
let mut cond: MaybeUninit<libc::pthread_cond_t> = MaybeUninit::uninit();
assert_eq!(libc::pthread_cond_init(cond.as_mut_ptr(), ptr::null()), 0);
Expand Down Expand Up @@ -260,6 +283,7 @@ fn check_cond() {

t.join().unwrap();
}
bomb.defuse();
}

fn check_condattr() {
Expand Down

0 comments on commit b7839b6

Please sign in to comment.