File tree Expand file tree Collapse file tree 2 files changed +43
-4
lines changed
tests/run-pass/concurrency Expand file tree Collapse file tree 2 files changed +43
-4
lines changed Original file line number Diff line number Diff line change 1
1
// ignore-windows: Concurrency on Windows is not supported yet.
2
2
3
- //! Check that destructors of the library thread locals are executed immediately
4
- //! after a thread terminates.
5
-
6
3
use std:: cell:: RefCell ;
7
4
use std:: thread;
8
5
@@ -20,7 +17,9 @@ thread_local! {
20
17
static A : TestCell = TestCell { value: RefCell :: new( 0 ) } ;
21
18
}
22
19
23
- fn main ( ) {
20
+ /// Check that destructors of the library thread locals are executed immediately
21
+ /// after a thread terminates.
22
+ fn check_destructors ( ) {
24
23
thread:: spawn ( || {
25
24
A . with ( |f| {
26
25
assert_eq ! ( * f. value. borrow( ) , 0 ) ;
@@ -31,3 +30,41 @@ fn main() {
31
30
. unwrap ( ) ;
32
31
println ! ( "Continue main." )
33
32
}
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
+ }
Original file line number Diff line number Diff line change 1
1
Dropping: 5
2
2
Continue main.
3
+ Continue main 2.
4
+ Joining: 7
You can’t perform that action at this time.
0 commit comments