From 80459bbf774f6238936837ace61fa6c1c95051ec Mon Sep 17 00:00:00 2001 From: Vytautas Astrauskas Date: Sun, 19 Apr 2020 14:22:55 -0700 Subject: [PATCH] Improve concurrency tests. --- .../concurrency/dangling_tls_lib.rs | 2 +- ...libc_pthread_rwlock_write_read_deadlock.rs | 32 ------------------- ...ibc_pthread_rwlock_write_write_deadlock.rs | 32 ------------------- .../{ => concurrency}/thread-spawn.rs | 5 +-- .../libc_pthread_mutex_deadlock.rs | 0 .../libc_pthread_mutex_wrong_owner.rs | 0 ...libc_pthread_rwlock_write_read_deadlock.rs | 25 +++++++++++++-- ...wlock_write_read_deadlock_single_thread.rs | 13 ++++++++ ...ibc_pthread_rwlock_write_write_deadlock.rs | 25 +++++++++++++-- ...lock_write_write_deadlock_single_thread.rs | 13 ++++++++ tests/run-pass/concurrency/locks.rs | 4 ++- tests/run-pass/concurrency/locks.stderr | 2 +- tests/run-pass/concurrency/simple.rs | 2 +- tests/run-pass/concurrency/simple.stderr | 2 +- tests/run-pass/concurrency/thread_locals.rs | 7 +++- .../run-pass/concurrency/thread_locals.stderr | 2 +- 16 files changed, 87 insertions(+), 79 deletions(-) delete mode 100644 tests/compile-fail/concurrency/libc_pthread_rwlock_write_read_deadlock.rs delete mode 100644 tests/compile-fail/concurrency/libc_pthread_rwlock_write_write_deadlock.rs rename tests/compile-fail/{ => concurrency}/thread-spawn.rs (52%) rename tests/compile-fail/{concurrency => sync}/libc_pthread_mutex_deadlock.rs (100%) rename tests/compile-fail/{concurrency => sync}/libc_pthread_mutex_wrong_owner.rs (100%) create mode 100644 tests/compile-fail/sync/libc_pthread_rwlock_write_read_deadlock_single_thread.rs create mode 100644 tests/compile-fail/sync/libc_pthread_rwlock_write_write_deadlock_single_thread.rs diff --git a/tests/compile-fail/concurrency/dangling_tls_lib.rs b/tests/compile-fail/concurrency/dangling_tls_lib.rs index ad12c107bffb..684dd0e86f60 100644 --- a/tests/compile-fail/concurrency/dangling_tls_lib.rs +++ b/tests/compile-fail/concurrency/dangling_tls_lib.rs @@ -1,4 +1,4 @@ -// ignore-windows +// ignore-windows: Concurrency on Windows is not supported yet. #![feature(thread_local_internals)] diff --git a/tests/compile-fail/concurrency/libc_pthread_rwlock_write_read_deadlock.rs b/tests/compile-fail/concurrency/libc_pthread_rwlock_write_read_deadlock.rs deleted file mode 100644 index 19dce431c8b1..000000000000 --- a/tests/compile-fail/concurrency/libc_pthread_rwlock_write_read_deadlock.rs +++ /dev/null @@ -1,32 +0,0 @@ -// ignore-windows: No libc on Windows - -#![feature(rustc_private)] - -extern crate libc; - -use std::cell::UnsafeCell; -use std::sync::Arc; -use std::thread; - -struct RwLock(UnsafeCell); - -unsafe impl Send for RwLock {} -unsafe impl Sync for RwLock {} - -fn new_lock() -> Arc { - Arc::new(RwLock(UnsafeCell::new(libc::PTHREAD_RWLOCK_INITIALIZER))) -} - -fn main() { - unsafe { - let lock = new_lock(); - assert_eq!(libc::pthread_rwlock_rdlock(lock.0.get() as *mut _), 0); - - let lock_copy = lock.clone(); - thread::spawn(move || { - assert_eq!(libc::pthread_rwlock_wrlock(lock_copy.0.get() as *mut _), 0); //~ ERROR: deadlock - }) - .join() - .unwrap(); - } -} diff --git a/tests/compile-fail/concurrency/libc_pthread_rwlock_write_write_deadlock.rs b/tests/compile-fail/concurrency/libc_pthread_rwlock_write_write_deadlock.rs deleted file mode 100644 index 098c1c2fe26c..000000000000 --- a/tests/compile-fail/concurrency/libc_pthread_rwlock_write_write_deadlock.rs +++ /dev/null @@ -1,32 +0,0 @@ -// ignore-windows: No libc on Windows - -#![feature(rustc_private)] - -extern crate libc; - -use std::cell::UnsafeCell; -use std::sync::Arc; -use std::thread; - -struct RwLock(UnsafeCell); - -unsafe impl Send for RwLock {} -unsafe impl Sync for RwLock {} - -fn new_lock() -> Arc { - Arc::new(RwLock(UnsafeCell::new(libc::PTHREAD_RWLOCK_INITIALIZER))) -} - -fn main() { - unsafe { - let lock = new_lock(); - assert_eq!(libc::pthread_rwlock_wrlock(lock.0.get() as *mut _), 0); - - let lock_copy = lock.clone(); - thread::spawn(move || { - assert_eq!(libc::pthread_rwlock_wrlock(lock_copy.0.get() as *mut _), 0); //~ ERROR: deadlock - }) - .join() - .unwrap(); - } -} diff --git a/tests/compile-fail/thread-spawn.rs b/tests/compile-fail/concurrency/thread-spawn.rs similarity index 52% rename from tests/compile-fail/thread-spawn.rs rename to tests/compile-fail/concurrency/thread-spawn.rs index 4b9073f3a73e..f0e4ab3817d3 100644 --- a/tests/compile-fail/thread-spawn.rs +++ b/tests/compile-fail/concurrency/thread-spawn.rs @@ -1,5 +1,6 @@ -// ignore-linux -// ignore-macos +// ignore-linux: Only Windows is not supported. +// ignore-macos: Only Windows is not supported. + use std::thread; // error-pattern: Miri does not support threading diff --git a/tests/compile-fail/concurrency/libc_pthread_mutex_deadlock.rs b/tests/compile-fail/sync/libc_pthread_mutex_deadlock.rs similarity index 100% rename from tests/compile-fail/concurrency/libc_pthread_mutex_deadlock.rs rename to tests/compile-fail/sync/libc_pthread_mutex_deadlock.rs diff --git a/tests/compile-fail/concurrency/libc_pthread_mutex_wrong_owner.rs b/tests/compile-fail/sync/libc_pthread_mutex_wrong_owner.rs similarity index 100% rename from tests/compile-fail/concurrency/libc_pthread_mutex_wrong_owner.rs rename to tests/compile-fail/sync/libc_pthread_mutex_wrong_owner.rs diff --git a/tests/compile-fail/sync/libc_pthread_rwlock_write_read_deadlock.rs b/tests/compile-fail/sync/libc_pthread_rwlock_write_read_deadlock.rs index 1b460e7174d2..19dce431c8b1 100644 --- a/tests/compile-fail/sync/libc_pthread_rwlock_write_read_deadlock.rs +++ b/tests/compile-fail/sync/libc_pthread_rwlock_write_read_deadlock.rs @@ -4,10 +4,29 @@ extern crate libc; +use std::cell::UnsafeCell; +use std::sync::Arc; +use std::thread; + +struct RwLock(UnsafeCell); + +unsafe impl Send for RwLock {} +unsafe impl Sync for RwLock {} + +fn new_lock() -> Arc { + Arc::new(RwLock(UnsafeCell::new(libc::PTHREAD_RWLOCK_INITIALIZER))) +} + fn main() { - let rw = std::cell::UnsafeCell::new(libc::PTHREAD_RWLOCK_INITIALIZER); unsafe { - assert_eq!(libc::pthread_rwlock_wrlock(rw.get()), 0); - libc::pthread_rwlock_rdlock(rw.get()); //~ ERROR: deadlock + let lock = new_lock(); + assert_eq!(libc::pthread_rwlock_rdlock(lock.0.get() as *mut _), 0); + + let lock_copy = lock.clone(); + thread::spawn(move || { + assert_eq!(libc::pthread_rwlock_wrlock(lock_copy.0.get() as *mut _), 0); //~ ERROR: deadlock + }) + .join() + .unwrap(); } } diff --git a/tests/compile-fail/sync/libc_pthread_rwlock_write_read_deadlock_single_thread.rs b/tests/compile-fail/sync/libc_pthread_rwlock_write_read_deadlock_single_thread.rs new file mode 100644 index 000000000000..1b460e7174d2 --- /dev/null +++ b/tests/compile-fail/sync/libc_pthread_rwlock_write_read_deadlock_single_thread.rs @@ -0,0 +1,13 @@ +// ignore-windows: No libc on Windows + +#![feature(rustc_private)] + +extern crate libc; + +fn main() { + let rw = std::cell::UnsafeCell::new(libc::PTHREAD_RWLOCK_INITIALIZER); + unsafe { + assert_eq!(libc::pthread_rwlock_wrlock(rw.get()), 0); + libc::pthread_rwlock_rdlock(rw.get()); //~ ERROR: deadlock + } +} diff --git a/tests/compile-fail/sync/libc_pthread_rwlock_write_write_deadlock.rs b/tests/compile-fail/sync/libc_pthread_rwlock_write_write_deadlock.rs index cc327ec46bc2..098c1c2fe26c 100644 --- a/tests/compile-fail/sync/libc_pthread_rwlock_write_write_deadlock.rs +++ b/tests/compile-fail/sync/libc_pthread_rwlock_write_write_deadlock.rs @@ -4,10 +4,29 @@ extern crate libc; +use std::cell::UnsafeCell; +use std::sync::Arc; +use std::thread; + +struct RwLock(UnsafeCell); + +unsafe impl Send for RwLock {} +unsafe impl Sync for RwLock {} + +fn new_lock() -> Arc { + Arc::new(RwLock(UnsafeCell::new(libc::PTHREAD_RWLOCK_INITIALIZER))) +} + fn main() { - let rw = std::cell::UnsafeCell::new(libc::PTHREAD_RWLOCK_INITIALIZER); unsafe { - assert_eq!(libc::pthread_rwlock_wrlock(rw.get()), 0); - libc::pthread_rwlock_wrlock(rw.get()); //~ ERROR: deadlock + let lock = new_lock(); + assert_eq!(libc::pthread_rwlock_wrlock(lock.0.get() as *mut _), 0); + + let lock_copy = lock.clone(); + thread::spawn(move || { + assert_eq!(libc::pthread_rwlock_wrlock(lock_copy.0.get() as *mut _), 0); //~ ERROR: deadlock + }) + .join() + .unwrap(); } } diff --git a/tests/compile-fail/sync/libc_pthread_rwlock_write_write_deadlock_single_thread.rs b/tests/compile-fail/sync/libc_pthread_rwlock_write_write_deadlock_single_thread.rs new file mode 100644 index 000000000000..cc327ec46bc2 --- /dev/null +++ b/tests/compile-fail/sync/libc_pthread_rwlock_write_write_deadlock_single_thread.rs @@ -0,0 +1,13 @@ +// ignore-windows: No libc on Windows + +#![feature(rustc_private)] + +extern crate libc; + +fn main() { + let rw = std::cell::UnsafeCell::new(libc::PTHREAD_RWLOCK_INITIALIZER); + unsafe { + assert_eq!(libc::pthread_rwlock_wrlock(rw.get()), 0); + libc::pthread_rwlock_wrlock(rw.get()); //~ ERROR: deadlock + } +} diff --git a/tests/run-pass/concurrency/locks.rs b/tests/run-pass/concurrency/locks.rs index 90c10b8ffe24..f5469712c5f5 100644 --- a/tests/run-pass/concurrency/locks.rs +++ b/tests/run-pass/concurrency/locks.rs @@ -1,4 +1,4 @@ -// ignore-windows +// ignore-windows: Concurrency on Windows is not supported yet. use std::sync::{Arc, Mutex, RwLock}; use std::thread; @@ -11,6 +11,7 @@ fn check_mutex() { let data = Arc::clone(&data); let thread = thread::spawn(move || { let mut data = data.lock().unwrap(); + thread::yield_now(); *data += 1; }); threads.push(thread); @@ -34,6 +35,7 @@ fn check_rwlock_write() { let data = Arc::clone(&data); let thread = thread::spawn(move || { let mut data = data.write().unwrap(); + thread::yield_now(); *data += 1; }); threads.push(thread); diff --git a/tests/run-pass/concurrency/locks.stderr b/tests/run-pass/concurrency/locks.stderr index 20a2bf3eeb87..2dbfb7721d36 100644 --- a/tests/run-pass/concurrency/locks.stderr +++ b/tests/run-pass/concurrency/locks.stderr @@ -1,2 +1,2 @@ -warning: The thread support is experimental. For example, Miri does not detect data races yet. +warning: thread support is experimental. For example, Miri does not detect data races yet. diff --git a/tests/run-pass/concurrency/simple.rs b/tests/run-pass/concurrency/simple.rs index 5adc521f59c2..ad47bb144b58 100644 --- a/tests/run-pass/concurrency/simple.rs +++ b/tests/run-pass/concurrency/simple.rs @@ -1,4 +1,4 @@ -// ignore-windows +// ignore-windows: Concurrency on Windows is not supported yet. use std::thread; diff --git a/tests/run-pass/concurrency/simple.stderr b/tests/run-pass/concurrency/simple.stderr index 20a2bf3eeb87..2dbfb7721d36 100644 --- a/tests/run-pass/concurrency/simple.stderr +++ b/tests/run-pass/concurrency/simple.stderr @@ -1,2 +1,2 @@ -warning: The thread support is experimental. For example, Miri does not detect data races yet. +warning: thread support is experimental. For example, Miri does not detect data races yet. diff --git a/tests/run-pass/concurrency/thread_locals.rs b/tests/run-pass/concurrency/thread_locals.rs index 1805a1da3d0b..384c2ac9155b 100644 --- a/tests/run-pass/concurrency/thread_locals.rs +++ b/tests/run-pass/concurrency/thread_locals.rs @@ -1,4 +1,9 @@ -// ignore-windows +// ignore-windows: Concurrency on Windows is not supported yet. + +//! The main purpose of this test is to check that if we take a pointer to +//! thread's `t1` thread-local `A` and send it to another thread `t2`, +//! dereferencing the pointer on `t2` resolves to `t1`'s thread-local. In this +//! test, we also check that thread-locals act as per-thread statics. #![feature(thread_local)] diff --git a/tests/run-pass/concurrency/thread_locals.stderr b/tests/run-pass/concurrency/thread_locals.stderr index 20a2bf3eeb87..2dbfb7721d36 100644 --- a/tests/run-pass/concurrency/thread_locals.stderr +++ b/tests/run-pass/concurrency/thread_locals.stderr @@ -1,2 +1,2 @@ -warning: The thread support is experimental. For example, Miri does not detect data races yet. +warning: thread support is experimental. For example, Miri does not detect data races yet.