Skip to content

Commit

Permalink
create phantom task for GC threads (#53815)
Browse files Browse the repository at this point in the history
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)
  • Loading branch information
d-netto authored and KristofferC committed May 23, 2024
1 parent 431dfdc commit babf0f8
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/scheduler.c
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,12 @@ void jl_parallel_gc_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);
(void)jl_atomic_fetch_add_relaxed(&nrunning, -1);
// wait for all threads
jl_gc_state_set(ptls, JL_GC_STATE_WAITING, JL_GC_STATE_UNSAFE);
uv_barrier_wait(targ->barrier);
Expand Down Expand Up @@ -158,7 +163,12 @@ void jl_concurrent_gc_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);
(void)jl_atomic_fetch_add_relaxed(&nrunning, -1);
// wait for all threads
jl_gc_state_set(ptls, JL_GC_STATE_WAITING, JL_GC_STATE_UNSAFE);
uv_barrier_wait(targ->barrier);
Expand Down
1 change: 1 addition & 0 deletions src/task.c
Original file line number Diff line number Diff line change
Expand Up @@ -1649,6 +1649,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();
Expand Down

0 comments on commit babf0f8

Please sign in to comment.