Store ractor_id directly on EC

This is easier to access as ec->ractor_id instead of pointer-chasing through
ec->thread->ractor->ractor_id

Co-authored-by: Luke Gruber <luke.gru@gmail.com>
This commit is contained in:
John Hawthorn 2025-12-17 12:12:17 -08:00
parent aace29d485
commit 63b082cf0e
Notes: git 2025-12-18 21:44:20 +00:00
5 changed files with 13 additions and 3 deletions

View File

@ -860,6 +860,7 @@ thread_create_core(VALUE thval, struct thread_create_params *params)
#endif
th->invoke_type = thread_invoke_type_ractor_proc;
th->ractor = params->g;
th->ec->ractor_id = rb_ractor_id(th->ractor);
th->ractor->threads.main = th;
th->invoke_arg.proc.proc = rb_proc_isolate_bang(params->proc, Qnil);
th->invoke_arg.proc.args = INT2FIX(RARRAY_LENINT(params->args));

1
vm.c
View File

@ -3955,6 +3955,7 @@ th_init(rb_thread_t *th, VALUE self, rb_vm_t *vm)
th->ec->local_storage_recursive_hash_for_trace = Qnil;
th->ec->storage = Qnil;
th->ec->ractor_id = rb_ractor_id(th->ractor);
#if OPT_CALL_THREADED_CODE
th->retval = Qundef;

View File

@ -1048,6 +1048,7 @@ struct rb_execution_context_struct {
rb_fiber_t *fiber_ptr;
struct rb_thread_struct *thread_ptr;
rb_serial_t serial;
rb_serial_t ractor_id;
/* storage (ec (fiber) local) */
struct rb_id_table *local_storage;
@ -2070,6 +2071,13 @@ rb_ec_ractor_ptr(const rb_execution_context_t *ec)
}
}
static inline rb_serial_t
rb_ec_ractor_id(const rb_execution_context_t *ec)
{
VM_ASSERT(ec->ractor_id == rb_ractor_id(rb_ec_ractor_ptr(ec)));
return ec->ractor_id;
}
static inline rb_vm_t *
rb_ec_vm_ptr(const rb_execution_context_t *ec)
{

View File

@ -4120,7 +4120,7 @@ vm_call_bmethod_body(rb_execution_context_t *ec, struct rb_calling_info *calling
VALUE procv = cme->def->body.bmethod.proc;
if (!RB_OBJ_SHAREABLE_P(procv) &&
cme->def->body.bmethod.defined_ractor_id != rb_ractor_id(rb_ec_ractor_ptr(ec))) {
cme->def->body.bmethod.defined_ractor_id != rb_ec_ractor_id(ec)) {
rb_raise(rb_eRuntimeError, "defined with an un-shareable Proc in a different Ractor");
}
@ -4143,7 +4143,7 @@ vm_call_iseq_bmethod(rb_execution_context_t *ec, rb_control_frame_t *cfp, struct
VALUE procv = cme->def->body.bmethod.proc;
if (!RB_OBJ_SHAREABLE_P(procv) &&
cme->def->body.bmethod.defined_ractor_id != rb_ractor_id(rb_ec_ractor_ptr(ec))) {
cme->def->body.bmethod.defined_ractor_id != rb_ec_ractor_id(ec)) {
rb_raise(rb_eRuntimeError, "defined with an un-shareable Proc in a different Ractor");
}

View File

@ -1030,7 +1030,7 @@ rb_method_definition_set(const rb_method_entry_t *me, rb_method_definition_t *de
}
case VM_METHOD_TYPE_BMETHOD:
RB_OBJ_WRITE(me, &def->body.bmethod.proc, (VALUE)opts);
def->body.bmethod.defined_ractor_id = rb_ractor_id(rb_ec_ractor_ptr(GET_EC()));
def->body.bmethod.defined_ractor_id = rb_ec_ractor_id(GET_EC());
return;
case VM_METHOD_TYPE_NOTIMPLEMENTED:
setup_method_cfunc_struct(UNALIGNED_MEMBER_PTR(def, body.cfunc), (VALUE(*)(ANYARGS))rb_f_notimplement_internal, -1);