From 88590e1cd28e5a9e500c78329dcc0ad3836c8bad Mon Sep 17 00:00:00 2001 From: Diogo Netto <61364108+d-netto@users.noreply.github.com> Date: Mon, 25 Mar 2024 10:23:54 -0300 Subject: [PATCH] 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 9636ef72eebe0155710471fb79eb5c75cc26949b) --- src/partr.c | 12 ++++++++++-- src/task.c | 1 + 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/src/partr.c b/src/partr.c index 0f3b581f5122f..cb82dc2c3d5fe 100644 --- a/src/partr.c +++ b/src/partr.c @@ -121,7 +121,11 @@ void jl_gc_mark_threadfun(void *arg) // initialize this thread (set tid and create heap) jl_ptls_t ptls = jl_init_threadtls(targ->tid); - + void *stack_lo, *stack_hi; + jl_init_stack_limits(0, &stack_lo, &stack_hi); + // warning: this changes `jl_current_task`, so be careful not to call that from this function + jl_task_t *ct = jl_init_root_task(ptls, stack_lo, stack_hi); + JL_GC_PROMISE_ROOTED(ct); // wait for all threads jl_gc_state_set(ptls, JL_GC_STATE_WAITING, 0); uv_barrier_wait(targ->barrier); @@ -146,7 +150,11 @@ void jl_gc_sweep_threadfun(void *arg) // initialize this thread (set tid and create heap) jl_ptls_t ptls = jl_init_threadtls(targ->tid); - + void *stack_lo, *stack_hi; + jl_init_stack_limits(0, &stack_lo, &stack_hi); + // warning: this changes `jl_current_task`, so be careful not to call that from this function + jl_task_t *ct = jl_init_root_task(ptls, stack_lo, stack_hi); + JL_GC_PROMISE_ROOTED(ct); // wait for all threads jl_gc_state_set(ptls, JL_GC_STATE_WAITING, 0); uv_barrier_wait(targ->barrier); diff --git a/src/task.c b/src/task.c index 1dab8688cb079..86033a81ddf41 100644 --- a/src/task.c +++ b/src/task.c @@ -1685,6 +1685,7 @@ jl_task_t *jl_init_root_task(jl_ptls_t ptls, void *stack_lo, void *stack_hi) JL_GC_PROMISE_ROOTED(ct); jl_set_pgcstack(&ct->gcstack); assert(jl_current_task == ct); + assert(jl_current_task->ptls == ptls); #ifdef _COMPILER_TSAN_ENABLED_ ct->ctx.tsan_state = __tsan_get_current_fiber();