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 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-
63use std:: cell:: RefCell ;
74use 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+ }
Original file line number Diff line number Diff line change 11Dropping: 5
22Continue main.
3+ Continue main 2.
4+ Joining: 7
You can’t perform that action at this time.
0 commit comments