Skip to content

Commit

Permalink
Run the GC when a Ractor terminates
Browse files Browse the repository at this point in the history
  • Loading branch information
rm155 committed Sep 7, 2022
1 parent 8828883 commit 1b171f6
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 6 deletions.
11 changes: 11 additions & 0 deletions gc.c
Expand Up @@ -11166,6 +11166,17 @@ rb_gc_start(void)
return Qnil;
}

VALUE
rb_gc_ractor_teardown_cleanup()
{
rb_ractor_t *cr = GET_RACTOR();
cr->during_teardown_cleanup = true;
rb_gc();
cr->during_teardown_cleanup = false;
gc_finalize_deferred(cr->local_objspace);
return Qnil;
}

void
rb_gc(void)
{
Expand Down
2 changes: 2 additions & 0 deletions gc.h
Expand Up @@ -114,6 +114,8 @@ int ruby_get_stack_grow_direction(volatile VALUE *addr);
const char *rb_obj_info(VALUE obj);
const char *rb_raw_obj_info(char *const buff, const size_t buff_size, VALUE obj);

VALUE rb_gc_ractor_teardown_cleanup();

VALUE rb_gc_disable_no_rest(void);

struct rb_thread_struct;
Expand Down
13 changes: 7 additions & 6 deletions ractor.c
Expand Up @@ -203,7 +203,9 @@ rb_ractor_related_objects_mark(rb_ractor_t *r)
ccan_list_for_each(&r->threads.set, th, lt_node) {
VM_ASSERT(th != NULL);
rb_gc_mark(th->self);
rb_thread_fiber_mark(th);
if (!r->during_teardown_cleanup) {
rb_thread_fiber_mark(th);
}
}
}

Expand Down Expand Up @@ -931,11 +933,6 @@ ractor_basket_setup(rb_execution_context_t *ec, struct rb_ractor_basket *basket,
basket->sender = rb_ec_ractor_ptr(ec)->pub.self;
basket->exception = exc;

void rb_add_to_exemption_tbl(VALUE obj);
if (!RTEST(move)) {
rb_add_to_exemption_tbl(obj);
}

if (is_will) {
basket->type = basket_type_will;
basket->v = obj;
Expand All @@ -958,6 +955,8 @@ ractor_basket_setup(rb_execution_context_t *ec, struct rb_ractor_basket *basket,
basket->v = ractor_move(obj);
}
}
void rb_add_to_exemption_tbl(VALUE obj);
rb_add_to_exemption_tbl(basket->v);
}

static VALUE
Expand Down Expand Up @@ -1591,6 +1590,7 @@ ractor_init(rb_ractor_t *r, VALUE name, VALUE loc)
}
r->name = name;
r->loc = loc;
r->during_teardown_cleanup = false;
}

void
Expand Down Expand Up @@ -1681,6 +1681,7 @@ rb_ractor_teardown(rb_execution_context_t *ec)
cr->threads.main = NULL;
}
RB_VM_LOCK_LEAVE();
rb_gc_ractor_teardown_cleanup();
}

void
Expand Down
1 change: 1 addition & 0 deletions ractor_core.h
Expand Up @@ -110,6 +110,7 @@ struct rb_ractor_struct {
struct rb_ractor_sync sync;
VALUE receiving_mutex;
bool yield_atexit;
bool during_teardown_cleanup;

// vm wide barrier synchronization
rb_nativethread_cond_t barrier_wait_cond;
Expand Down

0 comments on commit 1b171f6

Please sign in to comment.