Skip to content

Commit 9636ef7

Browse files
authored
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.
1 parent 4a2c593 commit 9636ef7

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

src/scheduler.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,11 @@ void jl_parallel_gc_threadfun(void *arg)
129129

130130
// initialize this thread (set tid and create heap)
131131
jl_ptls_t ptls = jl_init_threadtls(targ->tid);
132+
void *stack_lo, *stack_hi;
133+
jl_init_stack_limits(0, &stack_lo, &stack_hi);
134+
// warning: this changes `jl_current_task`, so be careful not to call that from this function
135+
jl_task_t *ct = jl_init_root_task(ptls, stack_lo, stack_hi);
136+
JL_GC_PROMISE_ROOTED(ct);
132137
(void)jl_atomic_fetch_add_relaxed(&nrunning, -1);
133138
// wait for all threads
134139
jl_gc_state_set(ptls, JL_GC_STATE_WAITING, JL_GC_STATE_UNSAFE);
@@ -158,6 +163,11 @@ void jl_concurrent_gc_threadfun(void *arg)
158163

159164
// initialize this thread (set tid and create heap)
160165
jl_ptls_t ptls = jl_init_threadtls(targ->tid);
166+
void *stack_lo, *stack_hi;
167+
jl_init_stack_limits(0, &stack_lo, &stack_hi);
168+
// warning: this changes `jl_current_task`, so be careful not to call that from this function
169+
jl_task_t *ct = jl_init_root_task(ptls, stack_lo, stack_hi);
170+
JL_GC_PROMISE_ROOTED(ct);
161171
(void)jl_atomic_fetch_add_relaxed(&nrunning, -1);
162172
// wait for all threads
163173
jl_gc_state_set(ptls, JL_GC_STATE_WAITING, JL_GC_STATE_UNSAFE);

src/task.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1649,6 +1649,7 @@ jl_task_t *jl_init_root_task(jl_ptls_t ptls, void *stack_lo, void *stack_hi)
16491649
JL_GC_PROMISE_ROOTED(ct);
16501650
jl_set_pgcstack(&ct->gcstack);
16511651
assert(jl_current_task == ct);
1652+
assert(jl_current_task->ptls == ptls);
16521653

16531654
#ifdef _COMPILER_TSAN_ENABLED_
16541655
ct->ctx.tsan_state = __tsan_get_current_fiber();

0 commit comments

Comments
 (0)