Skip to content

Commit 5e421ce

Browse files
committed
ractor: don't inherit the default thread group
[Bug #17506] `Thread.current.group` isn't shareable so it shouldn't be inherited by the main thread of a new Ractor. This cause an extra allocation when spawning a ractor, which could be elided with a bit of extra work, but not sure if it's worth the effort.
1 parent 2263e26 commit 5e421ce

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

test/ruby/test_ractor.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,17 @@ def test_shareability_of_method_proc
6060
assert_unshareable(x, "can not make shareable object for #<Method: String(Kernel)#itself()>", exception: Ractor::Error)
6161
end
6262

63+
def test_default_thread_group
64+
assert_separately([], "#{<<~"begin;"}\n#{<<~'end;'}")
65+
begin;
66+
Warning[:experimental] = false
67+
68+
main_ractor_id = Thread.current.group.object_id
69+
ractor_id = Ractor.new { Thread.current.group.object_id }.take
70+
refute_equal main_ractor_id, ractor_id
71+
end;
72+
end
73+
6374
def assert_make_shareable(obj)
6475
refute Ractor.shareable?(obj), "object was already shareable"
6576
Ractor.make_shareable(obj)

thread.c

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@
106106
#endif
107107

108108
static VALUE rb_cThreadShield;
109+
static VALUE cThGroup;
109110

110111
static VALUE sym_immediate;
111112
static VALUE sym_on_blocking;
@@ -795,6 +796,7 @@ struct thread_create_params {
795796
// for normal proc thread
796797
VALUE args;
797798
VALUE proc;
799+
VALUE group;
798800

799801
// for ractor
800802
rb_ractor_t *g;
@@ -853,7 +855,13 @@ thread_create_core(VALUE thval, struct thread_create_params *params)
853855
}
854856

855857
th->priority = current_th->priority;
856-
th->thgroup = current_th->thgroup;
858+
859+
if (params->group) {
860+
th->thgroup = params->group;
861+
}
862+
else {
863+
th->thgroup = current_th->thgroup;
864+
}
857865

858866
th->pending_interrupt_queue = rb_ary_hidden_new(0);
859867
th->pending_interrupt_queue_checked = 0;
@@ -993,13 +1001,20 @@ rb_thread_create(VALUE (*fn)(void *), void *arg)
9931001
VALUE
9941002
rb_thread_create_ractor(rb_ractor_t *r, VALUE args, VALUE proc)
9951003
{
1004+
VALUE thgroup = r->thgroup_default = rb_obj_alloc(cThGroup);
1005+
#if RACTOR_CHECK_MODE > 0
1006+
rb_ractor_setup_belonging_to(thgroup, r->pub.id);
1007+
#endif
1008+
9961009
struct thread_create_params params = {
9971010
.type = thread_invoke_type_ractor_proc,
9981011
.g = r,
1012+
.group = thgroup,
9991013
.args = args,
10001014
.proc = proc,
10011015
};
1002-
return thread_create_core(rb_thread_alloc(rb_cThread), &params);
1016+
RB_GC_GUARD(thgroup);
1017+
return thread_create_core(rb_thread_alloc(rb_cThread), &params);;
10031018
}
10041019

10051020

@@ -5427,7 +5442,6 @@ Init_Thread_Mutex(void)
54275442
void
54285443
Init_Thread(void)
54295444
{
5430-
VALUE cThGroup;
54315445
rb_thread_t *th = GET_THREAD();
54325446

54335447
sym_never = ID2SYM(rb_intern_const("never"));

0 commit comments

Comments
 (0)