Skip to content

Commit 331dbd1

Browse files
author
Vytautas Astrauskas
committed
Add a test for joining in a destructor.
1 parent f204b67 commit 331dbd1

File tree

2 files changed

+43
-4
lines changed

2 files changed

+43
-4
lines changed

tests/run-pass/concurrency/tls_lib_drop.rs

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
// ignore-windows: Concurrency on Windows is not supported yet.
22

3-
//! Check that destructors of the library thread locals are executed immediately
4-
//! after a thread terminates.
5-
63
use std::cell::RefCell;
74
use std::thread;
85

@@ -20,7 +17,9 @@ thread_local! {
2017
static A: TestCell = TestCell { value: RefCell::new(0) };
2118
}
2219

23-
fn main() {
20+
/// Check that destructors of the library thread locals are executed immediately
21+
/// after a thread terminates.
22+
fn check_destructors() {
2423
thread::spawn(|| {
2524
A.with(|f| {
2625
assert_eq!(*f.value.borrow(), 0);
@@ -31,3 +30,41 @@ fn main() {
3130
.unwrap();
3231
println!("Continue main.")
3332
}
33+
34+
struct JoinCell {
35+
value: RefCell<Option<thread::JoinHandle<u8>>>,
36+
}
37+
38+
impl Drop for JoinCell {
39+
fn drop(&mut self) {
40+
let join_handle = self.value.borrow_mut().take().unwrap();
41+
println!("Joining: {}", join_handle.join().unwrap());
42+
}
43+
}
44+
45+
thread_local! {
46+
static B: JoinCell = JoinCell { value: RefCell::new(None) };
47+
}
48+
49+
/// Check that the destructor can be blocked joining another thread.
50+
fn check_blocking() {
51+
thread::spawn(|| {
52+
B.with(|f| {
53+
assert!(f.value.borrow().is_none());
54+
let handle = thread::spawn(|| 7);
55+
*f.value.borrow_mut() = Some(handle);
56+
});
57+
})
58+
.join()
59+
.unwrap();
60+
println!("Continue main 2.");
61+
// Preempt the main thread so that the destructor gets executed and can join
62+
// the thread.
63+
thread::yield_now();
64+
thread::yield_now();
65+
}
66+
67+
fn main() {
68+
check_destructors();
69+
check_blocking();
70+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
Dropping: 5
22
Continue main.
3+
Continue main 2.
4+
Joining: 7

0 commit comments

Comments
 (0)