Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve compatibility of the Julia GC integration with multi threaded Julia #5722

Merged
merged 1 commit into from
May 23, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 24 additions & 6 deletions src/julia_gc.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@
JL_DLLEXPORT void * jl_get_ptls_states(void);
#endif

#if JULIA_VERSION_MAJOR == 1 && JULIA_VERSION_MINOR >= 10
#define JULIA_MULTIPLE_GC_THREADS_SUPPORTED
#endif


/****************************************************************************
**
Expand Down Expand Up @@ -200,7 +204,10 @@ static TNumFreeFuncBags TabFreeFuncBags[NUM_TYPES];
// HACK: TabMarkFuncBags is accessed by MarkCopyingSubBags in src/objects.c
TNumMarkFuncBags TabMarkFuncBags[NUM_TYPES];

static TaskInfoTree * task_stacks;
static TaskInfoTree * TaskStacks;
#ifdef JULIA_MULTIPLE_GC_THREADS_SUPPORTED
static pthread_mutex_t TaskStacksMutex;
#endif

//
// global bags
Expand Down Expand Up @@ -491,11 +498,12 @@ static void MarkFromList(jl_ptls_t ptls, PtrArray * arr)
static void
ScanTaskStack(int rescan, jl_task_t * task, void * start, void * end)
{
if (!task_stacks) {
task_stacks = TaskInfoTreeMake();
}
#ifdef JULIA_MULTIPLE_GC_THREADS_SUPPORTED
if (jl_n_gcthreads > 1)
pthread_mutex_lock(&TaskStacksMutex);
#endif
TaskInfo tmp = { task, NULL };
TaskInfo * taskinfo = TaskInfoTreeFind(task_stacks, tmp);
TaskInfo * taskinfo = TaskInfoTreeFind(TaskStacks, tmp);
PtrArray * stack;
if (taskinfo != NULL) {
stack = taskinfo->stack;
Expand All @@ -505,8 +513,12 @@ ScanTaskStack(int rescan, jl_task_t * task, void * start, void * end)
else {
tmp.stack = PtrArrayMake(1024);
stack = tmp.stack;
TaskInfoTreeInsert(task_stacks, tmp);
TaskInfoTreeInsert(TaskStacks, tmp);
}
#ifdef JULIA_MULTIPLE_GC_THREADS_SUPPORTED
if (jl_n_gcthreads > 1)
pthread_mutex_unlock(&TaskStacksMutex);
#endif
if (rescan) {
SafeScanTaskStack(stack, start, end);
// Remove duplicates
Expand Down Expand Up @@ -779,6 +791,12 @@ void GAP_InitJuliaMemoryInterface(jl_module_t * module,
SetupGuardPagesSize();
#endif

#ifdef JULIA_MULTIPLE_GC_THREADS_SUPPORTED
if (jl_n_gcthreads > 1)
pthread_mutex_init(&TaskStacksMutex, NULL);
#endif
TaskStacks = TaskInfoTreeMake();

// These callbacks potentially require access to the Julia
// TLS and thus need to be installed after initialization.
jl_gc_set_cb_root_scanner(GapRootScanner, 1);
Expand Down
Loading