Skip to content

Commit

Permalink
create phantom task for GC threads (JuliaLang#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.
  • Loading branch information
d-netto committed Mar 28, 2024
1 parent 4859e9c commit 031dc83
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
5 changes: 4 additions & 1 deletion src/jl_uv.c
Original file line number Diff line number Diff line change
Expand Up @@ -744,7 +744,10 @@ JL_DLLEXPORT void jl_safe_printf(const char *fmt, ...)
va_end(args);

buf[999] = '\0';
if (jl_inside_signal_handler() && jl_sig_fd != 0) {
// order is important here: we want to ensure that the threading infra
// has been initialized before we start trying to print to the
// safe crash log file
if (jl_sig_fd != 0 && jl_inside_signal_handler()) {
print_error_msg_as_json(buf);
}
if (write(STDERR_FILENO, buf, strlen(buf)) < 0) {
Expand Down
12 changes: 10 additions & 2 deletions src/partr.c
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,11 @@ 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);
// wait for all threads
jl_gc_state_set(ptls, JL_GC_STATE_WAITING, 0);
uv_barrier_wait(targ->barrier);
Expand Down Expand Up @@ -156,7 +160,11 @@ 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);
// wait for all threads
jl_gc_state_set(ptls, JL_GC_STATE_WAITING, 0);
uv_barrier_wait(targ->barrier);
Expand Down

0 comments on commit 031dc83

Please sign in to comment.