Skip to content

Commit 1bd2f6a

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 1bd2f6a

File tree

3 files changed

+4
-1
lines changed

3 files changed

+4
-1
lines changed

src/gc.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3068,7 +3068,7 @@ static void sweep_finalizer_list(arraylist_t *list)
30683068
}
30693069

30703070
// collector entry point and control
3071-
static _Atomic(uint32_t) jl_gc_disable_counter = 1;
3071+
_Atomic(uint32_t) jl_gc_disable_counter = 1;
30723072

30733073
JL_DLLEXPORT int jl_gc_enable(int on)
30743074
{

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)