@@ -69,6 +69,17 @@ struct RaceBox(helper_signal::signal);
69
69
unsafe impl Send for RaceBox { }
70
70
unsafe impl Sync for RaceBox { }
71
71
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
+
72
83
impl < M : Send > Helper < M > {
73
84
/// Lazily boots a helper thread, becoming a no-op if the helper has already
74
85
/// been spawned.
@@ -85,7 +96,7 @@ impl<M: Send> Helper<M> {
85
96
{
86
97
unsafe {
87
98
let _guard = self . lock . lock ( ) . unwrap ( ) ;
88
- if ! * self . initialized . get ( ) {
99
+ if * self . chan . get ( ) as uint == 0 {
89
100
let ( tx, rx) = channel ( ) ;
90
101
* self . chan . get ( ) = mem:: transmute ( box tx) ;
91
102
let ( receive, send) = helper_signal:: new ( ) ;
@@ -94,15 +105,17 @@ impl<M: Send> Helper<M> {
94
105
let receive = RaceBox ( receive) ;
95
106
96
107
let t = f ( ) ;
97
- Thread :: spawn ( move |: | {
108
+ Thread :: spawn ( move || {
98
109
helper ( receive. 0 , rx, t) ;
99
110
let _g = self . lock . lock ( ) . unwrap ( ) ;
100
111
* self . shutdown . get ( ) = true ;
101
112
self . cond . notify_one ( )
102
113
} ) ;
103
114
104
- rt:: at_exit ( move | : | { self . shutdown ( ) } ) ;
115
+ rt:: at_exit ( move | | { self . shutdown ( ) } ) ;
105
116
* self . initialized . get ( ) = true ;
117
+ } else if * self . chan . get ( ) as uint == 1 {
118
+ panic ! ( "cannot continue usage after shutdown" ) ;
106
119
}
107
120
}
108
121
}
@@ -117,7 +130,9 @@ impl<M: Send> Helper<M> {
117
130
// Must send and *then* signal to ensure that the child receives the
118
131
// message. Otherwise it could wake up and go to sleep before we
119
132
// 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" ) ;
121
136
( * * self . chan . get ( ) ) . send ( msg) . unwrap ( ) ;
122
137
helper_signal:: signal ( * self . signal . get ( ) as helper_signal:: signal ) ;
123
138
}
@@ -130,9 +145,13 @@ impl<M: Send> Helper<M> {
130
145
// returns.
131
146
let mut guard = self . lock . lock ( ) . unwrap ( ) ;
132
147
148
+ let ptr = * self . chan . get ( ) ;
149
+ if ptr as uint == 1 {
150
+ panic ! ( "cannot continue usage after shutdown" ) ;
151
+ }
133
152
// Close the channel by destroying it
134
153
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 > ;
136
155
drop ( chan) ;
137
156
helper_signal:: signal ( * self . signal . get ( ) as helper_signal:: signal ) ;
138
157
0 commit comments