Skip to content

Commit 88590e1

Browse files
d-nettoKristofferC
authored andcommitted
create phantom task for GC threads (#53815)
A common idiom used throughout the codebase is to get a pointer to thread-local-state through `jl_current_task->ptls`. Create a phantom task for GC threads so that we can make use of this idiom when running in the GC threads as well. Idea originally suggested by @vchuravy, bugs are mine. (cherry picked from commit 9636ef7)
1 parent 3120989 commit 88590e1

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

src/partr.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,11 @@ void jl_gc_mark_threadfun(void *arg)
121121

122122
// initialize this thread (set tid and create heap)
123123
jl_ptls_t ptls = jl_init_threadtls(targ->tid);
124-
124+
void *stack_lo, *stack_hi;
125+
jl_init_stack_limits(0, &stack_lo, &stack_hi);
126+
// warning: this changes `jl_current_task`, so be careful not to call that from this function
127+
jl_task_t *ct = jl_init_root_task(ptls, stack_lo, stack_hi);
128+
JL_GC_PROMISE_ROOTED(ct);
125129
// wait for all threads
126130
jl_gc_state_set(ptls, JL_GC_STATE_WAITING, 0);
127131
uv_barrier_wait(targ->barrier);
@@ -146,7 +150,11 @@ void jl_gc_sweep_threadfun(void *arg)
146150

147151
// initialize this thread (set tid and create heap)
148152
jl_ptls_t ptls = jl_init_threadtls(targ->tid);
149-
153+
void *stack_lo, *stack_hi;
154+
jl_init_stack_limits(0, &stack_lo, &stack_hi);
155+
// warning: this changes `jl_current_task`, so be careful not to call that from this function
156+
jl_task_t *ct = jl_init_root_task(ptls, stack_lo, stack_hi);
157+
JL_GC_PROMISE_ROOTED(ct);
150158
// wait for all threads
151159
jl_gc_state_set(ptls, JL_GC_STATE_WAITING, 0);
152160
uv_barrier_wait(targ->barrier);

src/task.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1685,6 +1685,7 @@ jl_task_t *jl_init_root_task(jl_ptls_t ptls, void *stack_lo, void *stack_hi)
16851685
JL_GC_PROMISE_ROOTED(ct);
16861686
jl_set_pgcstack(&ct->gcstack);
16871687
assert(jl_current_task == ct);
1688+
assert(jl_current_task->ptls == ptls);
16881689

16891690
#ifdef _COMPILER_TSAN_ENABLED_
16901691
ct->ctx.tsan_state = __tsan_get_current_fiber();

0 commit comments

Comments
 (0)