@@ -85,7 +85,7 @@ impl Thread<()> {
85
85
* mem:: transmute :: < & Box < Option < T > > , * const * mut Option < T > > ( & packet)
86
86
} ;
87
87
let main = proc ( ) unsafe { * packet2 = Some ( main ( ) ) ; } ;
88
- let native = unsafe { imp:: create ( stack, box main) } ;
88
+ let native = unsafe { imp:: create ( stack, box main, false ) . unwrap ( ) } ;
89
89
90
90
Thread {
91
91
native : native,
@@ -108,8 +108,7 @@ impl Thread<()> {
108
108
/// stack size for the new thread.
109
109
pub fn spawn_stack ( stack : uint , main : proc ( ) : Send ) {
110
110
unsafe {
111
- let handle = imp:: create ( stack, box main) ;
112
- imp:: detach ( handle) ;
111
+ imp:: create ( stack, box main, true ) ;
113
112
}
114
113
}
115
114
@@ -159,7 +158,7 @@ mod imp {
159
158
pub type rust_thread = HANDLE ;
160
159
pub type rust_thread_return = DWORD ;
161
160
162
- pub unsafe fn create ( stack : uint , p : Box < proc ( ) : Send > ) -> rust_thread {
161
+ pub unsafe fn create ( stack : uint , p : Box < proc ( ) : Send > , detach : bool ) -> Option < rust_thread > {
163
162
let arg: * mut libc:: c_void = mem:: transmute ( p) ;
164
163
// FIXME On UNIX, we guard against stack sizes that are too small but
165
164
// that's because pthreads enforces that stacks are at least
@@ -179,15 +178,20 @@ mod imp {
179
178
let _p: Box < proc ( ) : Send > = mem:: transmute ( arg) ;
180
179
fail ! ( "failed to spawn native thread: {}" , ret) ;
181
180
}
182
- return ret;
181
+ if detach {
182
+ detach ( ret) ;
183
+ return None ;
184
+ } else {
185
+ return Some ( ret) ;
186
+ }
183
187
}
184
188
185
189
pub unsafe fn join ( native : rust_thread ) {
186
190
use libc:: consts:: os:: extra:: INFINITE ;
187
191
WaitForSingleObject ( native, INFINITE ) ;
188
192
}
189
193
190
- pub unsafe fn detach ( native : rust_thread ) {
194
+ unsafe fn detach ( native : rust_thread ) {
191
195
assert ! ( libc:: CloseHandle ( native) != 0 ) ;
192
196
}
193
197
@@ -219,20 +223,26 @@ mod imp {
219
223
use core:: cmp;
220
224
use core:: mem;
221
225
use core:: ptr;
222
- use libc:: consts:: os:: posix01:: { PTHREAD_CREATE_JOINABLE , PTHREAD_STACK_MIN } ;
226
+ use libc:: consts:: os:: posix01:: { PTHREAD_CREATE_JOINABLE , PTHREAD_CREATE_DETACHED ,
227
+ PTHREAD_STACK_MIN } ;
223
228
use libc;
224
229
225
230
use stack:: RED_ZONE ;
226
231
227
232
pub type rust_thread = libc:: pthread_t ;
228
233
pub type rust_thread_return = * mut u8 ;
229
234
230
- pub unsafe fn create ( stack : uint , p : Box < proc ( ) : Send > ) -> rust_thread {
235
+ pub unsafe fn create ( stack : uint , p : Box < proc ( ) : Send > , create_detached : bool ) -> Option < rust_thread > {
231
236
let mut native: libc:: pthread_t = mem:: zeroed ( ) ;
232
237
let mut attr: libc:: pthread_attr_t = mem:: zeroed ( ) ;
233
238
assert_eq ! ( pthread_attr_init( & mut attr) , 0 ) ;
234
- assert_eq ! ( pthread_attr_setdetachstate( & mut attr,
235
- PTHREAD_CREATE_JOINABLE ) , 0 ) ;
239
+ if !create_detached {
240
+ assert_eq ! ( pthread_attr_setdetachstate( & mut attr,
241
+ PTHREAD_CREATE_JOINABLE ) , 0 ) ;
242
+ } else {
243
+ assert_eq ! ( pthread_attr_setdetachstate( & mut attr,
244
+ PTHREAD_CREATE_DETACHED ) , 0 ) ;
245
+ }
236
246
237
247
// Reserve room for the red zone, the runtime's stack of last resort.
238
248
let stack_size = cmp:: max ( stack, RED_ZONE + min_stack_size ( & attr) as uint ) ;
@@ -264,17 +274,17 @@ mod imp {
264
274
let _p: Box < proc ( ) : Send > = mem:: transmute ( arg) ;
265
275
fail ! ( "failed to spawn native thread: {}" , ret) ;
266
276
}
267
- native
277
+ if create_detached {
278
+ return None ;
279
+ } else {
280
+ return Some ( native) ;
281
+ }
268
282
}
269
283
270
284
pub unsafe fn join ( native : rust_thread ) {
271
285
assert_eq ! ( pthread_join( native, ptr:: mut_null( ) ) , 0 ) ;
272
286
}
273
287
274
- pub unsafe fn detach ( native : rust_thread ) {
275
- assert_eq ! ( pthread_detach( native) , 0 ) ;
276
- }
277
-
278
288
pub unsafe fn yield_now ( ) { assert_eq ! ( sched_yield( ) , 0 ) ; }
279
289
// glibc >= 2.15 has a __pthread_get_minstack() function that returns
280
290
// PTHREAD_STACK_MIN plus however many bytes are needed for thread-local
@@ -320,7 +330,6 @@ mod imp {
320
330
stack_size : libc:: size_t ) -> libc:: c_int ;
321
331
fn pthread_attr_setdetachstate ( attr : * mut libc:: pthread_attr_t ,
322
332
state : libc:: c_int ) -> libc:: c_int ;
323
- fn pthread_detach ( thread : libc:: pthread_t ) -> libc:: c_int ;
324
333
fn sched_yield ( ) -> libc:: c_int ;
325
334
}
326
335
}
0 commit comments