@@ -8,6 +8,7 @@ use crate::sync::atomic::{Atomic, AtomicUsize, Ordering};
88use crate :: sys:: pal:: itron:: error:: { ItronError , expect_success, expect_success_aborting} ;
99use crate :: sys:: pal:: itron:: time:: dur2reltims;
1010use crate :: sys:: pal:: itron:: { abi, task} ;
11+ use crate :: thread:: ThreadInit ;
1112use crate :: time:: Duration ;
1213use crate :: { hint, io} ;
1314
@@ -27,9 +28,9 @@ unsafe impl Sync for Thread {}
2728/// State data shared between a parent thread and child thread. It's dropped on
2829/// a transition to one of the final states.
2930struct ThreadInner {
30- /// This field is used on thread creation to pass a closure from
31+ /// This field is used on thread creation to pass initialization data from
3132 /// `Thread::new` to the created task.
32- start : UnsafeCell < ManuallyDrop < Box < dyn FnOnce ( ) > > > ,
33+ init : UnsafeCell < ManuallyDrop < Box < ThreadInit > > > ,
3334
3435 /// A state machine. Each transition is annotated with `[...]` in the
3536 /// source code.
@@ -65,7 +66,7 @@ struct ThreadInner {
6566 lifecycle : Atomic < usize > ,
6667}
6768
68- // Safety: The only `!Sync` field, `ThreadInner::start `, is only touched by
69+ // Safety: The only `!Sync` field, `ThreadInner::init `, is only touched by
6970// the task represented by `ThreadInner`.
7071unsafe impl Sync for ThreadInner { }
7172
@@ -84,13 +85,9 @@ impl Thread {
8485 /// # Safety
8586 ///
8687 /// See `thread::Builder::spawn_unchecked` for safety requirements.
87- pub unsafe fn new (
88- stack : usize ,
89- _name : Option < & str > ,
90- p : Box < dyn FnOnce ( ) > ,
91- ) -> io:: Result < Thread > {
88+ pub unsafe fn new ( stack : usize , init : Box < ThreadInit > ) -> io:: Result < Thread > {
9289 let inner = Box :: new ( ThreadInner {
93- start : UnsafeCell :: new ( ManuallyDrop :: new ( p ) ) ,
90+ init : UnsafeCell :: new ( ManuallyDrop :: new ( init ) ) ,
9491 lifecycle : AtomicUsize :: new ( LIFECYCLE_INIT ) ,
9592 } ) ;
9693
@@ -100,10 +97,11 @@ impl Thread {
10097 let inner = unsafe { & * p_inner } ;
10198
10299 // Safety: Since `trampoline` is called only once for each
103- // `ThreadInner` and only `trampoline` touches `start`,
104- // `start` contains contents and is safe to mutably borrow.
105- let p = unsafe { ManuallyDrop :: take ( & mut * inner. start . get ( ) ) } ;
106- p ( ) ;
100+ // `ThreadInner` and only `trampoline` touches `init`,
101+ // `init` contains contents and is safe to mutably borrow.
102+ let init = unsafe { ManuallyDrop :: take ( & mut * inner. init . get ( ) ) } ;
103+ let rust_start = init. init ( ) ;
104+ rust_start ( ) ;
107105
108106 // Fix the current thread's state just in case, so that the
109107 // destructors won't abort
0 commit comments