@@ -69,6 +69,17 @@ struct RaceBox(helper_signal::signal);
6969unsafe impl Send for RaceBox { }
7070unsafe impl Sync for RaceBox { }
7171
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+
7283impl < M : Send > Helper < M > {
7384 /// Lazily boots a helper thread, becoming a no-op if the helper has already
7485 /// been spawned.
@@ -85,7 +96,7 @@ impl<M: Send> Helper<M> {
8596 {
8697 unsafe {
8798 let _guard = self . lock . lock ( ) . unwrap ( ) ;
88- if ! * self . initialized . get ( ) {
99+ if * self . chan . get ( ) as uint == 0 {
89100 let ( tx, rx) = channel ( ) ;
90101 * self . chan . get ( ) = mem:: transmute ( box tx) ;
91102 let ( receive, send) = helper_signal:: new ( ) ;
@@ -94,15 +105,17 @@ impl<M: Send> Helper<M> {
94105 let receive = RaceBox ( receive) ;
95106
96107 let t = f ( ) ;
97- Thread :: spawn ( move |: | {
108+ Thread :: spawn ( move || {
98109 helper ( receive. 0 , rx, t) ;
99110 let _g = self . lock . lock ( ) . unwrap ( ) ;
100111 * self . shutdown . get ( ) = true ;
101112 self . cond . notify_one ( )
102113 } ) ;
103114
104- rt:: at_exit ( move | : | { self . shutdown ( ) } ) ;
115+ rt:: at_exit ( move | | { self . shutdown ( ) } ) ;
105116 * self . initialized . get ( ) = true ;
117+ } else if * self . chan . get ( ) as uint == 1 {
118+ panic ! ( "cannot continue usage after shutdown" ) ;
106119 }
107120 }
108121 }
@@ -117,7 +130,9 @@ impl<M: Send> Helper<M> {
117130 // Must send and *then* signal to ensure that the child receives the
118131 // message. Otherwise it could wake up and go to sleep before we
119132 // send the message.
120- 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" ) ;
121136 ( * * self . chan . get ( ) ) . send ( msg) . unwrap ( ) ;
122137 helper_signal:: signal ( * self . signal . get ( ) as helper_signal:: signal ) ;
123138 }
@@ -130,9 +145,13 @@ impl<M: Send> Helper<M> {
130145 // returns.
131146 let mut guard = self . lock . lock ( ) . unwrap ( ) ;
132147
148+ let ptr = * self . chan . get ( ) ;
149+ if ptr as uint == 1 {
150+ panic ! ( "cannot continue usage after shutdown" ) ;
151+ }
133152 // Close the channel by destroying it
134153 let chan: Box < Sender < M > > = mem:: transmute ( * self . chan . get ( ) ) ;
135- * self . chan . get ( ) = 0 as * mut Sender < M > ;
154+ * self . chan . get ( ) = 1 as * mut Sender < M > ;
136155 drop ( chan) ;
137156 helper_signal:: signal ( * self . signal . get ( ) as helper_signal:: signal ) ;
138157
0 commit comments