Skip to content

Commit c7cb0b0

Browse files
committed
Disable GC during thread adoption.
Otherwise GC might run after the foreign thread exits the spin loop, but before it is initialized and ready to handle GC safepoints.
1 parent 23537f7 commit c7cb0b0

File tree

2 files changed

+3
-0
lines changed

2 files changed

+3
-0
lines changed

src/julia_internal.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -883,6 +883,7 @@ STATIC_INLINE int jl_addr_is_safepoint(uintptr_t addr)
883883
return addr >= safepoint_addr && addr < safepoint_addr + jl_page_size * 3;
884884
}
885885
extern _Atomic(uint32_t) jl_gc_running;
886+
extern _Atomic(uint32_t) jl_gc_disable_counter;
886887
// All the functions are safe to be called from within a signal handler
887888
// provided that the thread will not be interrupted by another asynchronous
888889
// signal.

src/threading.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,7 @@ JL_DLLEXPORT jl_gcframe_t **jl_adopt_thread(void) JL_NOTSAFEPOINT_LEAVE
414414
while (jl_atomic_load_relaxed(&jl_gc_running) || jl_atomic_load_acquire(&jl_gc_running)) {
415415
jl_cpu_pause();
416416
}
417+
jl_atomic_fetch_add(&jl_gc_disable_counter, 1);
417418

418419
// initialize this thread (assign tid, create heap, set up root task)
419420
jl_ptls_t ptls = jl_init_threadtls(-1);
@@ -424,6 +425,7 @@ JL_DLLEXPORT jl_gcframe_t **jl_adopt_thread(void) JL_NOTSAFEPOINT_LEAVE
424425
jl_task_t *ct = jl_init_root_task(ptls, stack_lo, stack_hi);
425426
JL_GC_PROMISE_ROOTED(ct);
426427
uv_random(NULL, NULL, &ct->rngState, sizeof(ct->rngState), 0, NULL);
428+
jl_atomic_fetch_add(&jl_gc_disable_counter, -1);
427429
return &ct->gcstack;
428430
}
429431

0 commit comments

Comments
 (0)