Add a test for joining in a destructor.
This commit is contained in:
parent
f204b67b0f
commit
331dbd1469
@ -1,8 +1,5 @@
|
||||
// ignore-windows: Concurrency on Windows is not supported yet.
|
||||
|
||||
//! Check that destructors of the library thread locals are executed immediately
|
||||
//! after a thread terminates.
|
||||
|
||||
use std::cell::RefCell;
|
||||
use std::thread;
|
||||
|
||||
@ -20,7 +17,9 @@ fn drop(&mut self) {
|
||||
static A: TestCell = TestCell { value: RefCell::new(0) };
|
||||
}
|
||||
|
||||
fn main() {
|
||||
/// Check that destructors of the library thread locals are executed immediately
|
||||
/// after a thread terminates.
|
||||
fn check_destructors() {
|
||||
thread::spawn(|| {
|
||||
A.with(|f| {
|
||||
assert_eq!(*f.value.borrow(), 0);
|
||||
@ -31,3 +30,41 @@ fn main() {
|
||||
.unwrap();
|
||||
println!("Continue main.")
|
||||
}
|
||||
|
||||
struct JoinCell {
|
||||
value: RefCell<Option<thread::JoinHandle<u8>>>,
|
||||
}
|
||||
|
||||
impl Drop for JoinCell {
|
||||
fn drop(&mut self) {
|
||||
let join_handle = self.value.borrow_mut().take().unwrap();
|
||||
println!("Joining: {}", join_handle.join().unwrap());
|
||||
}
|
||||
}
|
||||
|
||||
thread_local! {
|
||||
static B: JoinCell = JoinCell { value: RefCell::new(None) };
|
||||
}
|
||||
|
||||
/// Check that the destructor can be blocked joining another thread.
|
||||
fn check_blocking() {
|
||||
thread::spawn(|| {
|
||||
B.with(|f| {
|
||||
assert!(f.value.borrow().is_none());
|
||||
let handle = thread::spawn(|| 7);
|
||||
*f.value.borrow_mut() = Some(handle);
|
||||
});
|
||||
})
|
||||
.join()
|
||||
.unwrap();
|
||||
println!("Continue main 2.");
|
||||
// Preempt the main thread so that the destructor gets executed and can join
|
||||
// the thread.
|
||||
thread::yield_now();
|
||||
thread::yield_now();
|
||||
}
|
||||
|
||||
fn main() {
|
||||
check_destructors();
|
||||
check_blocking();
|
||||
}
|
||||
|
@ -1,2 +1,4 @@
|
||||
Dropping: 5
|
||||
Continue main.
|
||||
Continue main 2.
|
||||
Joining: 7
|
||||
|
Loading…
Reference in New Issue
Block a user