Skip to content

Commit

Permalink
gcext: add ptls argument to task scanner
Browse files Browse the repository at this point in the history
Access to ptls is generally necessary when marking objects so it
makes sense to pass it to the task scanner.

We pass it as the last argument to the callback so that existing
consumers of this API hopefully keep working, they will just
ignore this new argument.
  • Loading branch information
fingolfin committed May 31, 2024
1 parent 4ecdc37 commit 340f13f
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/gc.c
Original file line number Diff line number Diff line change
Expand Up @@ -2726,7 +2726,7 @@ FORCE_INLINE void gc_mark_outrefs(jl_ptls_t ptls, jl_gc_markqueue_t *mq, void *_
if (gc_cblist_task_scanner) {
int16_t tid = jl_atomic_load_relaxed(&ta->tid);
gc_invoke_callbacks(jl_gc_cb_task_scanner_t, gc_cblist_task_scanner,
(ta, tid != -1 && ta == gc_all_tls_states[tid]->root_task));
(ta, tid != -1 && ta == gc_all_tls_states[tid]->root_task, ptls));
}
#ifdef COPY_STACKS
void *stkbuf = ta->stkbuf;
Expand Down
2 changes: 1 addition & 1 deletion src/julia_gcext.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ extern "C" {
// along with custom mark functions must not alter the GC state except
// through calling jl_gc_mark_queue_obj() and jl_gc_mark_queue_objarray().
typedef void (*jl_gc_cb_root_scanner_t)(int full) JL_NOTSAFEPOINT;
typedef void (*jl_gc_cb_task_scanner_t)(jl_task_t *task, int full) JL_NOTSAFEPOINT;
typedef void (*jl_gc_cb_task_scanner_t)(jl_task_t *task, int is_root_task, jl_ptls_t ptls) JL_NOTSAFEPOINT;

// Callbacks that are invoked before and after a collection.
typedef void (*jl_gc_cb_pre_gc_t)(int full) JL_NOTSAFEPOINT;
Expand Down
15 changes: 8 additions & 7 deletions test/gcext/gcext.c
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,6 @@ typedef struct {
static jl_datatype_t *datatype_stack_internal;
static jl_datatype_t *datatype_stack_external;
static jl_datatype_t *datatype_stack;
static jl_ptls_t ptls;

static size_t gc_alloc_size(jl_value_t *val)
{
Expand Down Expand Up @@ -333,7 +332,7 @@ int internal_obj_scan(jl_value_t *val)
}
}

dynstack_t *allocate_stack_mem(size_t capacity)
dynstack_t *allocate_stack_mem(jl_ptls_t ptls, size_t capacity)
{
size_t size = offsetof(dynstack_t, data) + capacity * sizeof(jl_value_t *);
jl_datatype_t *type = datatype_stack_internal;
Expand Down Expand Up @@ -377,13 +376,14 @@ jl_value_t *stk_type()

// Create a new stack object

jl_value_t *stk_make()
jl_value_t *stk_make(void)
{
jl_ptls_t ptls = jl_get_ptls_states();
jl_value_t *hdr =
jl_gc_alloc_typed(ptls, sizeof(jl_value_t *), datatype_stack);
JL_GC_PUSH1(hdr);
*(dynstack_t **)hdr = NULL;
dynstack_t *stk = allocate_stack_mem(8);
dynstack_t *stk = allocate_stack_mem(ptls, 8);
*(dynstack_t **)hdr = stk;
jl_gc_schedule_foreign_sweepfunc(ptls, (jl_value_t *)(stk));
JL_GC_POP();
Expand All @@ -408,7 +408,8 @@ void stk_push(jl_value_t *s, jl_value_t *v)
jl_gc_wb((jl_value_t *)stk, v);
}
else {
dynstack_t *newstk = allocate_stack_mem(stk->capacity * 3 / 2 + 1);
jl_ptls_t ptls = jl_get_ptls_states();
dynstack_t *newstk = allocate_stack_mem(ptls, stk->capacity * 3 / 2 + 1);
newstk->size = stk->size;
memcpy(newstk->data, stk->data, sizeof(jl_value_t *) * stk->size);
*(dynstack_t **)s = newstk;
Expand Down Expand Up @@ -453,6 +454,7 @@ static jl_module_t *module;

void root_scanner(int full)
{
jl_ptls_t ptls = jl_get_ptls_states();
for (int i = 0; i < NAUXROOTS; i++) {
if (aux_roots[i])
jl_gc_mark_queue_obj(ptls, aux_roots[i]);
Expand All @@ -477,7 +479,7 @@ static int stack_grows_down(void) {
return is_lower_stack_frame_ptr(buf);
}

void task_scanner(jl_task_t *task, int root_task)
void task_scanner(jl_task_t *task, int root_task, jl_ptls_t ptls)
{
int var_on_frame;

Expand Down Expand Up @@ -603,7 +605,6 @@ int main()
jl_init();
if (jl_gc_enable_conservative_gc_support() < 0)
abort();
ptls = jl_get_ptls_states();
jl_gc_set_cb_root_scanner(root_scanner, 1);
jl_gc_set_cb_task_scanner(task_scanner, 1);
jl_gc_set_cb_pre_gc(pre_gc_func, 1);
Expand Down

0 comments on commit 340f13f

Please sign in to comment.