sync generic_ivtbl

generic_ivtbl is a process global table to maintain instance variables
for non T_OBJECT/T_CLASS/... objects. So we need to protect them
for multi-Ractor exection.

Hint: we can make them Ractor local for unshareable objects, but
      now it is premature optimization.
This commit is contained in:
Koichi Sasada 2020-10-14 10:43:13 +09:00
parent ae693fff74
commit fad97f1f96
Notes: git 2020-10-14 16:37:21 +09:00
6 changed files with 54 additions and 18 deletions

View File

@ -780,6 +780,7 @@ assert_equal "#{N}#{N}", %Q{
}.map{|r| r.take}.join
}
# enc_table
assert_equal "#{N/10}", %Q{
Ractor.new do
loop do
@ -794,4 +795,20 @@ assert_equal "#{N/10}", %Q{
}
}
# Generic ivtbl
n = N/2
assert_equal "#{n}#{n}", %Q{
2.times.map{
Ractor.new do
#{n}.times do
obj = ''
obj.instance_variable_set("@a", 1)
obj.instance_variable_set("@b", 1)
obj.instance_variable_set("@c", 1)
obj.instance_variable_defined?("@a")
end
end
}.map{|r| r.take}.join
}
end # if !ENV['GITHUB_WORKFLOW']

View File

@ -15082,6 +15082,7 @@ variable.$(OBJEXT): {$(VPATH)}variable.h
variable.$(OBJEXT): {$(VPATH)}vm_core.h
variable.$(OBJEXT): {$(VPATH)}vm_debug.h
variable.$(OBJEXT): {$(VPATH)}vm_opts.h
variable.$(OBJEXT): {$(VPATH)}vm_sync.h
version.$(OBJEXT): $(CCAN_DIR)/check_type/check_type.h
version.$(OBJEXT): $(CCAN_DIR)/container_of/container_of.h
version.$(OBJEXT): $(CCAN_DIR)/list/list.h

View File

@ -82,7 +82,7 @@
% # JIT: cache hit path of vm_getivar, or cancel JIT (recompile it without any ivar optimization)
fprintf(f, " struct gen_ivtbl *ivtbl;\n");
fprintf(f, " VALUE val;\n");
fprintf(f, " if (LIKELY(FL_TEST_RAW(obj, FL_EXIVAR) && ic_serial == RCLASS_SERIAL(RBASIC(obj)->klass) && st_lookup(rb_ivar_generic_ivtbl(obj), (st_data_t)obj, (st_data_t *)&ivtbl) && index < ivtbl->numiv && (val = ivtbl->ivptr[index]) != Qundef)) {\n");
fprintf(f, " if (LIKELY(FL_TEST_RAW(obj, FL_EXIVAR) && ic_serial == RCLASS_SERIAL(RBASIC(obj)->klass) && rb_ivar_generic_ivtbl_lookup(obj, &ivtbl) && index < ivtbl->numiv && (val = ivtbl->ivptr[index]) != Qundef)) {\n");
fprintf(f, " stack[%d] = val;\n", b->stack_size);
fprintf(f, " }\n");
fprintf(f, " else {\n");

View File

@ -37,6 +37,7 @@
#include "variable.h"
#include "vm_core.h"
#include "ractor_pub.h"
#include "vm_sync.h"
typedef void rb_gvar_compact_t(void *var);
@ -896,6 +897,8 @@ IVAR_ACCESSOR_SHOULD_BE_MAIN_RACTOR(ID id)
static inline struct st_table *
generic_ivtbl(VALUE obj, ID id, bool force_check_ractor)
{
ASSERT_vm_locking();
if ((force_check_ractor || rb_is_instance_id(id)) && // not internal ID
UNLIKELY(rb_ractor_shareable_p(obj) && !rb_ractor_main_p())) {
rb_raise(rb_eRuntimeError, "can not access instance variables of shareable objects from non-main Ractors");
@ -909,22 +912,28 @@ generic_ivtbl_no_ractor_check(VALUE obj)
return generic_ivtbl(obj, 0, false);
}
MJIT_FUNC_EXPORTED struct st_table *
rb_ivar_generic_ivtbl(VALUE obj)
{
return generic_ivtbl(obj, 0, true);
}
static int
gen_ivtbl_get(VALUE obj, ID id, struct gen_ivtbl **ivtbl)
{
st_data_t data;
int r = 0;
if (st_lookup(generic_ivtbl(obj, id, false), (st_data_t)obj, &data)) {
*ivtbl = (struct gen_ivtbl *)data;
return 1;
RB_VM_LOCK_ENTER();
{
if (st_lookup(generic_ivtbl(obj, id, false), (st_data_t)obj, &data)) {
*ivtbl = (struct gen_ivtbl *)data;
r = 1;
}
}
return 0;
RB_VM_LOCK_LEAVE();
return r;
}
MJIT_FUNC_EXPORTED int
rb_ivar_generic_ivtbl_lookup(VALUE obj, struct gen_ivtbl **ivtbl)
{
return gen_ivtbl_get(obj, 0, ivtbl);
}
static VALUE
@ -1275,8 +1284,13 @@ generic_ivar_set(VALUE obj, ID id, VALUE val)
ivup.iv_extended = 0;
ivup.u.iv_index_tbl = iv_index_tbl_make(obj);
iv_index_tbl_extend(&ivup, id);
st_update(generic_ivtbl(obj, id, false), (st_data_t)obj, generic_ivar_update,
(st_data_t)&ivup);
RB_VM_LOCK_ENTER();
{
st_update(generic_ivtbl(obj, id, false), (st_data_t)obj, generic_ivar_update,
(st_data_t)&ivup);
}
RB_VM_LOCK_LEAVE();
ivup.u.ivtbl->ivptr[ivup.index] = val;
@ -1590,8 +1604,12 @@ rb_copy_generic_ivar(VALUE clone, VALUE obj)
* c.ivtbl may change in gen_ivar_copy due to realloc,
* no need to free
*/
generic_ivtbl_no_ractor_check(clone);
st_insert(generic_ivtbl_no_ractor_check(obj), (st_data_t)clone, (st_data_t)c.ivtbl);
RB_VM_LOCK_ENTER();
{
generic_ivtbl_no_ractor_check(clone);
st_insert(generic_ivtbl_no_ractor_check(obj), (st_data_t)clone, (st_data_t)c.ivtbl);
}
RB_VM_LOCK_LEAVE();
}
return;

View File

@ -16,6 +16,6 @@ struct gen_ivtbl {
VALUE ivptr[FLEX_ARY_LEN];
};
struct st_table *rb_ivar_generic_ivtbl(VALUE obj);
int rb_ivar_generic_ivtbl_lookup(VALUE obj, struct gen_ivtbl **);
#endif /* RUBY_TOPLEVEL_VARIABLE_H */

View File

@ -1101,7 +1101,7 @@ vm_getivar(VALUE obj, ID id, IVC ic, const struct rb_callcache *cc, int is_attr)
else if (FL_TEST_RAW(obj, FL_EXIVAR)) {
struct gen_ivtbl *ivtbl;
if (LIKELY(st_lookup(rb_ivar_generic_ivtbl(obj), (st_data_t)obj, (st_data_t *)&ivtbl)) &&
if (LIKELY(rb_ivar_generic_ivtbl_lookup(obj, &ivtbl)) &&
LIKELY(index < ivtbl->numiv)) {
val = ivtbl->ivptr[index];
}
@ -1123,7 +1123,7 @@ vm_getivar(VALUE obj, ID id, IVC ic, const struct rb_callcache *cc, int is_attr)
}
else if (FL_TEST_RAW(obj, FL_EXIVAR)) {
struct gen_ivtbl *ivtbl;
if (LIKELY(st_lookup(rb_ivar_generic_ivtbl(obj), (st_data_t)obj, (st_data_t *)&ivtbl))) {
if (LIKELY(rb_ivar_generic_ivtbl_lookup(obj, &ivtbl))) {
numiv = ivtbl->numiv;
ivptr = ivtbl->ivptr;
iv_index_tbl = RCLASS_IV_INDEX_TBL(rb_obj_class(obj));