Skip to content

Commit ac91c11

Browse files
committed
create separate function to spawn GC threads
1 parent 7b0a189 commit ac91c11

File tree

4 files changed

+33
-13
lines changed

4 files changed

+33
-13
lines changed

src/gc.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3957,6 +3957,29 @@ void jl_free_thread_gc_state(jl_ptls_t ptls)
39573957
arraylist_free(&mq->reclaim_set);
39583958
}
39593959

3960+
void jl_spawn_gc_threads(void)
3961+
{
3962+
int nthreads = jl_atomic_load_relaxed(&jl_n_threads);
3963+
int ngcthreads = jl_n_gcthreads;
3964+
int nmutator_threads = nthreads - ngcthreads;
3965+
uv_thread_t uvtid;
3966+
for (int i = 1; i < nthreads; ++i) {
3967+
if (i < nmutator_threads) {
3968+
continue;
3969+
}
3970+
jl_threadarg_t *t = (jl_threadarg_t *)malloc_s(sizeof(jl_threadarg_t)); // ownership will be passed to the thread
3971+
t->tid = i;
3972+
t->barrier = &thread_init_done;
3973+
if (i == nthreads - 1 && jl_n_sweepthreads == 1) {
3974+
uv_thread_create(&uvtid, jl_concurrent_gc_threadfun, t);
3975+
}
3976+
else {
3977+
uv_thread_create(&uvtid, jl_parallel_gc_threadfun, t);
3978+
}
3979+
uv_thread_detach(&uvtid);
3980+
}
3981+
}
3982+
39603983
// System-wide initializations
39613984
void jl_gc_init(void)
39623985
{

src/gc.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -572,6 +572,7 @@ extern uv_cond_t gc_threads_cond;
572572
extern uv_sem_t gc_sweep_assists_needed;
573573
extern _Atomic(int) gc_n_threads_marking;
574574
extern _Atomic(int) gc_n_threads_sweeping;
575+
extern uv_barrier_t thread_init_done;
575576
void gc_mark_queue_all_roots(jl_ptls_t ptls, jl_gc_markqueue_t *mq);
576577
void gc_mark_finlist_(jl_gc_markqueue_t *mq, jl_value_t *fl_parent, jl_value_t **fl_begin, jl_value_t **fl_end) JL_NOTSAFEPOINT;
577578
void gc_mark_finlist(jl_gc_markqueue_t *mq, arraylist_t *list, size_t start) JL_NOTSAFEPOINT;

src/julia_internal.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -960,9 +960,10 @@ extern JL_DLLEXPORT ssize_t jl_tls_offset;
960960
extern JL_DLLEXPORT const int jl_tls_elf_support;
961961
void jl_init_threading(void);
962962
void jl_start_threads(void);
963-
extern uv_mutex_t safepoint_lock;
963+
void jl_spawn_gc_threads(void);
964964

965965
// Whether the GC is running
966+
extern uv_mutex_t safepoint_lock;
966967
extern char *jl_safepoint_pages;
967968
STATIC_INLINE int jl_addr_is_safepoint(uintptr_t addr)
968969
{

src/threading.c

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -761,7 +761,7 @@ void jl_init_threading(void)
761761
gc_first_tid = nthreads + nthreadsi;
762762
}
763763

764-
static uv_barrier_t thread_init_done;
764+
uv_barrier_t thread_init_done;
765765

766766
void jl_start_threads(void)
767767
{
@@ -800,28 +800,23 @@ void jl_start_threads(void)
800800
uv_barrier_init(&thread_init_done, nthreads);
801801

802802
// GC/System threads need to be after the worker threads.
803-
int nworker_threads = nthreads - ngcthreads;
803+
int nmutator_threads = nthreads - ngcthreads;
804804

805805
for (i = 1; i < nthreads; ++i) {
806-
jl_threadarg_t *t = (jl_threadarg_t *)malloc_s(sizeof(jl_threadarg_t)); // ownership will be passed to the thread
807-
t->tid = i;
808-
t->barrier = &thread_init_done;
809-
if (i < nworker_threads) {
806+
if (i < nmutator_threads) {
807+
jl_threadarg_t *t = (jl_threadarg_t *)malloc_s(sizeof(jl_threadarg_t)); // ownership will be passed to the thread
808+
t->tid = i;
809+
t->barrier = &thread_init_done;
810810
uv_thread_create(&uvtid, jl_threadfun, t);
811811
if (exclusive) {
812812
mask[i] = 1;
813813
uv_thread_setaffinity(&uvtid, mask, NULL, cpumasksize);
814814
mask[i] = 0;
815815
}
816816
}
817-
else if (i == nthreads - 1 && jl_n_sweepthreads == 1) {
818-
uv_thread_create(&uvtid, jl_concurrent_gc_threadfun, t);
819-
}
820-
else {
821-
uv_thread_create(&uvtid, jl_parallel_gc_threadfun, t);
822-
}
823817
uv_thread_detach(&uvtid);
824818
}
819+
jl_spawn_gc_threads();
825820

826821
uv_barrier_wait(&thread_init_done);
827822
}

0 commit comments

Comments
 (0)