@@ -25,10 +25,31 @@ pub struct Thread {
25
25
}
26
26
27
27
impl Thread {
28
- pub unsafe fn new < ' a > ( stack : usize , p : Box < FnBox ( ) + ' a > )
28
+ pub unsafe fn new < F : FnOnce ( ) > ( stack : usize , p : F )
29
29
-> io:: Result < Thread > {
30
+ extern "system" fn thread_start < F : FnOnce ( ) > ( main : * mut c_void )
31
+ -> c:: DWORD {
32
+ unsafe {
33
+ let main = Box :: from_raw ( main as * mut F ) ;
34
+ start_thread ( main) ;
35
+ }
36
+ 0
37
+ }
38
+
30
39
let p = box p;
31
40
41
+ match Thread :: new_inner ( stack, & * p as * const _ as * const _ , thread_start :: < F > ) {
42
+ Ok ( thread) => {
43
+ mem:: forget ( p) ; // ownership passed to CreateThread
44
+ Ok ( thread)
45
+ }
46
+ Err ( e) => Err ( e) ,
47
+ }
48
+ }
49
+
50
+ unsafe fn new_inner ( stack : usize , p : * const c_void ,
51
+ f : extern "system" fn ( * mut c_void ) -> c:: DWORD )
52
+ -> io:: Result < Thread > {
32
53
// FIXME On UNIX, we guard against stack sizes that are too small but
33
54
// that's because pthreads enforces that stacks are at least
34
55
// PTHREAD_STACK_MIN bytes big. Windows has no such lower limit, it's
@@ -37,21 +58,15 @@ impl Thread {
37
58
// Round up to the next 64 kB because that's what the NT kernel does,
38
59
// might as well make it explicit.
39
60
let stack_size = ( stack + 0xfffe ) & ( !0xfffe ) ;
40
- let ret = c:: CreateThread ( ptr:: null_mut ( ) , stack_size ,
41
- thread_start , & * p as * const _ as * mut _ ,
61
+ let ret = c:: CreateThread ( ptr:: null_mut ( ) , stack ,
62
+ f , p as * mut _ ,
42
63
0 , ptr:: null_mut ( ) ) ;
43
64
44
65
return if ret as usize == 0 {
45
66
Err ( io:: Error :: last_os_error ( ) )
46
67
} else {
47
- mem:: forget ( p) ; // ownership passed to CreateThread
48
68
Ok ( Thread { handle : Handle :: new ( ret) } )
49
69
} ;
50
-
51
- extern "system" fn thread_start ( main : * mut c_void ) -> c:: DWORD {
52
- unsafe { start_thread ( main) ; }
53
- 0
54
- }
55
70
}
56
71
57
72
pub fn set_name ( _name : & str ) {
0 commit comments