2020//! can be created in the future and there must be no active timers at that 
2121//! time. 
2222
23+ #![ macro_escape]  
24+ 
2325use  prelude:: * ; 
2426
2527use  cell:: UnsafeCell ; 
@@ -68,6 +70,17 @@ struct RaceBox(helper_signal::signal);
6870unsafe  impl  Send  for  RaceBox  { } 
6971unsafe  impl  Sync  for  RaceBox  { } 
7072
73+ macro_rules!  helper_init {  ( static  $name: ident:  Helper <$m: ty>)  => ( 
74+     static  $name:  Helper <$m> = Helper  { 
75+         lock:  :: sync:: MUTEX_INIT , 
76+         cond:  :: sync:: CONDVAR_INIT , 
77+         chan:  :: cell:: UnsafeCell  {  value:  0  as  * mut  Sender <$m> } , 
78+         signal:  :: cell:: UnsafeCell  {  value:  0  } , 
79+         initialized:  :: cell:: UnsafeCell  {  value:  false  } , 
80+         shutdown:  :: cell:: UnsafeCell  {  value:  false  } , 
81+     } ; 
82+ )  } 
83+ 
7184impl < M :  Send >  Helper < M >  { 
7285    /// Lazily boots a helper thread, becoming a no-op if the helper has already 
7386/// been spawned. 
@@ -84,7 +97,7 @@ impl<M: Send> Helper<M> {
8497    { 
8598        unsafe  { 
8699            let  _guard = self . lock . lock ( ) . unwrap ( ) ; 
87-             if  ! * self . initialized . get ( )  { 
100+             if  * self . chan . get ( )   as   uint  ==  0  { 
88101                let  ( tx,  rx)  = channel ( ) ; 
89102                * self . chan . get ( )  = mem:: transmute ( box tx) ; 
90103                let  ( receive,  send)  = helper_signal:: new ( ) ; 
@@ -93,15 +106,17 @@ impl<M: Send> Helper<M> {
93106                let  receive = RaceBox ( receive) ; 
94107
95108                let  t = f ( ) ; 
96-                 Thread :: spawn ( move  |: | { 
109+                 Thread :: spawn ( move  || { 
97110                    helper ( receive. 0 ,  rx,  t) ; 
98111                    let  _g = self . lock . lock ( ) . unwrap ( ) ; 
99112                    * self . shutdown . get ( )  = true ; 
100113                    self . cond . notify_one ( ) 
101114                } ) . detach ( ) ; 
102115
103-                 rt:: at_exit ( move | : | {  self . shutdown ( )  } ) ; 
116+                 rt:: at_exit ( move  | | {  self . shutdown ( )  } ) ; 
104117                * self . initialized . get ( )  = true ; 
118+             }  else  if  * self . chan . get ( )  as  uint  == 1  { 
119+                 panic ! ( "cannot continue usage after shutdown" ) ; 
105120            } 
106121        } 
107122    } 
@@ -116,7 +131,9 @@ impl<M: Send> Helper<M> {
116131            // Must send and *then* signal to ensure that the child receives the 
117132            // message. Otherwise it could wake up and go to sleep before we 
118133            // send the message. 
119-             assert ! ( !self . chan. get( ) . is_null( ) ) ; 
134+             assert ! ( * self . chan. get( )  as  uint != 0 ) ; 
135+             assert ! ( * self . chan. get( )  as  uint != 1 , 
136+                     "cannot continue usage after shutdown" ) ; 
120137            ( * * self . chan . get ( ) ) . send ( msg) ; 
121138            helper_signal:: signal ( * self . signal . get ( )  as  helper_signal:: signal ) ; 
122139        } 
@@ -129,9 +146,13 @@ impl<M: Send> Helper<M> {
129146            // returns. 
130147            let  mut  guard = self . lock . lock ( ) . unwrap ( ) ; 
131148
149+             let  ptr = * self . chan . get ( ) ; 
150+             if  ptr as  uint  == 1  { 
151+                 panic ! ( "cannot continue usage after shutdown" ) ; 
152+             } 
132153            // Close the channel by destroying it 
133154            let  chan:  Box < Sender < M > >  = mem:: transmute ( * self . chan . get ( ) ) ; 
134-             * self . chan . get ( )  = 0  as  * mut  Sender < M > ; 
155+             * self . chan . get ( )  = 1  as  * mut  Sender < M > ; 
135156            drop ( chan) ; 
136157            helper_signal:: signal ( * self . signal . get ( )  as  helper_signal:: signal ) ; 
137158
0 commit comments