@@ -24,7 +24,6 @@ use prelude::v1::*;
2424
2525use boxed;
2626use cell:: UnsafeCell ;
27- use ptr;
2827use rt;
2928use sync:: { StaticMutex , StaticCondvar } ;
3029use sync:: mpsc:: { channel, Sender , Receiver } ;
@@ -70,6 +69,17 @@ struct RaceBox(helper_signal::signal);
7069unsafe impl Send for RaceBox { }
7170unsafe impl Sync for RaceBox { }
7271
72+ macro_rules! helper_init { ( static $name: ident: Helper <$m: ty>) => (
73+ static $name: Helper <$m> = Helper {
74+ lock: :: sync:: MUTEX_INIT ,
75+ cond: :: sync:: CONDVAR_INIT ,
76+ chan: :: cell:: UnsafeCell { value: 0 as * mut Sender <$m> } ,
77+ signal: :: cell:: UnsafeCell { value: 0 } ,
78+ initialized: :: cell:: UnsafeCell { value: false } ,
79+ shutdown: :: cell:: UnsafeCell { value: false } ,
80+ } ;
81+ ) }
82+
7383impl < M : Send > Helper < M > {
7484 /// Lazily boots a helper thread, becoming a no-op if the helper has already
7585 /// been spawned.
@@ -86,7 +96,7 @@ impl<M: Send> Helper<M> {
8696 {
8797 unsafe {
8898 let _guard = self . lock . lock ( ) . unwrap ( ) ;
89- if ! * self . initialized . get ( ) {
99+ if * self . chan . get ( ) as uint == 0 {
90100 let ( tx, rx) = channel ( ) ;
91101 * self . chan . get ( ) = boxed:: into_raw ( box tx) ;
92102 let ( receive, send) = helper_signal:: new ( ) ;
@@ -102,8 +112,10 @@ impl<M: Send> Helper<M> {
102112 self . cond . notify_one ( )
103113 } ) ;
104114
105- rt:: at_exit ( move || { self . shutdown ( ) } ) ;
115+ rt:: at_exit ( move || { self . shutdown ( ) } ) ;
106116 * self . initialized . get ( ) = true ;
117+ } else if * self . chan . get ( ) as uint == 1 {
118+ panic ! ( "cannot continue usage after shutdown" ) ;
107119 }
108120 }
109121 }
@@ -118,7 +130,9 @@ impl<M: Send> Helper<M> {
118130 // Must send and *then* signal to ensure that the child receives the
119131 // message. Otherwise it could wake up and go to sleep before we
120132 // send the message.
121- assert ! ( !self . chan. get( ) . is_null( ) ) ;
133+ assert ! ( * self . chan. get( ) as uint != 0 ) ;
134+ assert ! ( * self . chan. get( ) as uint != 1 ,
135+ "cannot continue usage after shutdown" ) ;
122136 ( * * self . chan . get ( ) ) . send ( msg) . unwrap ( ) ;
123137 helper_signal:: signal ( * self . signal . get ( ) as helper_signal:: signal ) ;
124138 }
@@ -131,9 +145,13 @@ impl<M: Send> Helper<M> {
131145 // returns.
132146 let mut guard = self . lock . lock ( ) . unwrap ( ) ;
133147
148+ let ptr = * self . chan . get ( ) ;
149+ if ptr as uint == 1 {
150+ panic ! ( "cannot continue usage after shutdown" ) ;
151+ }
134152 // Close the channel by destroying it
135- let chan: Box < Sender < M > > = Box :: from_raw ( * self . chan . get ( ) ) ;
136- * self . chan . get ( ) = ptr :: null_mut ( ) ;
153+ let chan = Box :: from_raw ( * self . chan . get ( ) ) ;
154+ * self . chan . get ( ) = 1 as * mut Sender < M > ;
137155 drop ( chan) ;
138156 helper_signal:: signal ( * self . signal . get ( ) as helper_signal:: signal ) ;
139157
0 commit comments