@@ -90,6 +90,23 @@ JL_DLLEXPORT uint32_t jl_rand_ptls(uint32_t max, uint32_t unbias)
9090 return cong (max , - (uint64_t )- unbias , & ptls -> rngseed );
9191}
9292
93+ jl_ptls_t jl_threadfun_preamble (void * arg , uint8_t state )
94+ {
95+ jl_threadarg_t * targ = (jl_threadarg_t * )arg ;
96+ // initialize this thread (set tid and create heap)
97+ jl_ptls_t ptls = jl_init_threadtls (targ -> tid );
98+ void * stack_lo , * stack_hi ;
99+ jl_init_stack_limits (0 , & stack_lo , & stack_hi );
100+ // warning: this changes `jl_current_task`, so be careful not to call that from this function
101+ jl_task_t * ct = jl_init_root_task (ptls , stack_lo , stack_hi );
102+ JL_GC_PROMISE_ROOTED (ct );
103+ // wait for all threads
104+ jl_gc_state_set (ptls , state , 0 );
105+ uv_barrier_wait (targ -> barrier );
106+ free (targ );
107+ return ptls ;
108+ }
109+
93110// initialize the threading infrastructure
94111// (called only by the main thread)
95112void jl_init_threadinginfra (void )
@@ -123,19 +140,7 @@ void jl_parallel_gc_threadfun(void *arg)
123140{
124141 jl_threadarg_t * targ = (jl_threadarg_t * )arg ;
125142
126- // initialize this thread (set tid and create heap)
127- jl_ptls_t ptls = jl_init_threadtls (targ -> tid );
128- void * stack_lo , * stack_hi ;
129- jl_init_stack_limits (0 , & stack_lo , & stack_hi );
130- // warning: this changes `jl_current_task`, so be careful not to call that from this function
131- jl_task_t * ct = jl_init_root_task (ptls , stack_lo , stack_hi );
132- JL_GC_PROMISE_ROOTED (ct );
133- // wait for all threads
134- jl_gc_state_set (ptls , JL_GC_PARALLEL_COLLECTOR_THREAD , 0 );
135- uv_barrier_wait (targ -> barrier );
136-
137- // free the thread argument here
138- free (targ );
143+ jl_ptls_t ptls = jl_threadfun_preamble (targ , JL_GC_PARALLEL_COLLECTOR_THREAD );
139144
140145 while (1 ) {
141146 uv_mutex_lock (& gc_threads_lock );
@@ -158,19 +163,8 @@ void jl_concurrent_gc_threadfun(void *arg)
158163{
159164 jl_threadarg_t * targ = (jl_threadarg_t * )arg ;
160165
161- // initialize this thread (set tid and create heap)
162- jl_ptls_t ptls = jl_init_threadtls (targ -> tid );
163- void * stack_lo , * stack_hi ;
164- jl_init_stack_limits (0 , & stack_lo , & stack_hi );
165- // warning: this changes `jl_current_task`, so be careful not to call that from this function
166- jl_task_t * ct = jl_init_root_task (ptls , stack_lo , stack_hi );
167- JL_GC_PROMISE_ROOTED (ct );
168- // wait for all threads
169- jl_gc_state_set (ptls , JL_GC_CONCURRENT_COLLECTOR_THREAD , 0 );
170- uv_barrier_wait (targ -> barrier );
171-
172- // free the thread argument here
173- free (targ );
166+ jl_ptls_t ptls = jl_threadfun_preamble (targ , JL_GC_CONCURRENT_COLLECTOR_THREAD );
167+ (void )ptls ;
174168
175169 while (1 ) {
176170 assert (jl_atomic_load_relaxed (& ptls -> gc_state ) == JL_GC_CONCURRENT_COLLECTOR_THREAD );
@@ -184,20 +178,8 @@ void jl_threadfun(void *arg)
184178{
185179 jl_threadarg_t * targ = (jl_threadarg_t * )arg ;
186180
187- // initialize this thread (set tid, create heap, set up root task)
188- jl_ptls_t ptls = jl_init_threadtls (targ -> tid );
189- void * stack_lo , * stack_hi ;
190- jl_init_stack_limits (0 , & stack_lo , & stack_hi );
191- // warning: this changes `jl_current_task`, so be careful not to call that from this function
192- jl_task_t * ct = jl_init_root_task (ptls , stack_lo , stack_hi );
193- JL_GC_PROMISE_ROOTED (ct );
194-
195- // wait for all threads
196- jl_gc_state_set (ptls , JL_GC_STATE_SAFE , 0 );
197- uv_barrier_wait (targ -> barrier );
198-
199- // free the thread argument here
200- free (targ );
181+ jl_ptls_t ptls = jl_threadfun_preamble (targ , JL_GC_STATE_SAFE );
182+ jl_task_t * ct = jl_current_task ;
201183
202184 (void )jl_gc_unsafe_enter (ptls );
203185 jl_finish_task (ct ); // noreturn
0 commit comments