From 331dbd1469abb9ee7959684305732b0613f0bf15 Mon Sep 17 00:00:00 2001 From: Vytautas Astrauskas Date: Sun, 26 Apr 2020 22:05:09 -0700 Subject: [PATCH] Add a test for joining in a destructor. --- tests/run-pass/concurrency/tls_lib_drop.rs | 45 +++++++++++++++++-- .../run-pass/concurrency/tls_lib_drop.stdout | 2 + 2 files changed, 43 insertions(+), 4 deletions(-) diff --git a/tests/run-pass/concurrency/tls_lib_drop.rs b/tests/run-pass/concurrency/tls_lib_drop.rs index 0d1808cbe0f..de2566de85c 100644 --- a/tests/run-pass/concurrency/tls_lib_drop.rs +++ b/tests/run-pass/concurrency/tls_lib_drop.rs @@ -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>>, +} + +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(); +} diff --git a/tests/run-pass/concurrency/tls_lib_drop.stdout b/tests/run-pass/concurrency/tls_lib_drop.stdout index d2bbb866b77..d622c0ccce8 100644 --- a/tests/run-pass/concurrency/tls_lib_drop.stdout +++ b/tests/run-pass/concurrency/tls_lib_drop.stdout @@ -1,2 +1,4 @@ Dropping: 5 Continue main. +Continue main 2. +Joining: 7