Skip to content

Commit b5d3e64

Browse files
authored
create phantom task for GC threads (JuliaLang#53815) (#141)
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 4859e9c commit b5d3e64

File tree

3 files changed

+15
-3
lines changed

3 files changed

+15
-3
lines changed

src/jl_uv.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -744,7 +744,10 @@ JL_DLLEXPORT void jl_safe_printf(const char *fmt, ...)
744744
va_end(args);
745745

746746
buf[999] = '\0';
747-
if (jl_inside_signal_handler() && jl_sig_fd != 0) {
747+
// order is important here: we want to ensure that the threading infra
748+
// has been initialized before we start trying to print to the
749+
// safe crash log file
750+
if (jl_sig_fd != 0 && jl_inside_signal_handler()) {
748751
print_error_msg_as_json(buf);
749752
}
750753
if (write(STDERR_FILENO, buf, strlen(buf)) < 0) {

src/partr.c

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

126126
// initialize this thread (set tid and create heap)
127127
jl_ptls_t ptls = jl_init_threadtls(targ->tid);
128-
128+
void *stack_lo, *stack_hi;
129+
jl_init_stack_limits(0, &stack_lo, &stack_hi);
130+
// warning: this changes `jl_current_task`, so be careful not to call that from this function
131+
jl_task_t *ct = jl_init_root_task(ptls, stack_lo, stack_hi);
132+
JL_GC_PROMISE_ROOTED(ct);
129133
// wait for all threads
130134
jl_gc_state_set(ptls, JL_GC_STATE_WAITING, 0);
131135
uv_barrier_wait(targ->barrier);
@@ -156,7 +160,11 @@ void jl_concurrent_gc_threadfun(void *arg)
156160

157161
// initialize this thread (set tid and create heap)
158162
jl_ptls_t ptls = jl_init_threadtls(targ->tid);
159-
163+
void *stack_lo, *stack_hi;
164+
jl_init_stack_limits(0, &stack_lo, &stack_hi);
165+
// warning: this changes `jl_current_task`, so be careful not to call that from this function
166+
jl_task_t *ct = jl_init_root_task(ptls, stack_lo, stack_hi);
167+
JL_GC_PROMISE_ROOTED(ct);
160168
// wait for all threads
161169
jl_gc_state_set(ptls, JL_GC_STATE_WAITING, 0);
162170
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)