Delimit the scopes using encoding/symbol tables

This commit is contained in:
Nobuyoshi Nakada 2021-10-27 11:45:59 +09:00
parent f2ca66fefc
commit fc518fe1ff
No known key found for this signature in database
GPG Key ID: 3582D74E1FEE4465
Notes: git 2025-05-26 02:44:06 +00:00
2 changed files with 35 additions and 78 deletions

View File

@ -93,15 +93,11 @@ static rb_encoding *global_enc_ascii,
*global_enc_utf_8, *global_enc_utf_8,
*global_enc_us_ascii; *global_enc_us_ascii;
#define GLOBAL_ENC_TABLE_ENTER(enc_table) struct enc_table *enc_table = &global_enc_table; RB_VM_LOCK_ENTER() #define GLOBAL_ENC_TABLE_LOCKING(tbl) \
#define GLOBAL_ENC_TABLE_LEAVE() RB_VM_LOCK_LEAVE() for (struct enc_table *tbl = &global_enc_table, **locking = &tbl; \
#define GLOBAL_ENC_TABLE_EVAL(enc_table, expr) do { \ locking; \
GLOBAL_ENC_TABLE_ENTER(enc_table); \ locking = NULL) \
{ \ RB_VM_LOCKING()
expr; \
} \
GLOBAL_ENC_TABLE_LEAVE(); \
} while (0)
#define ENC_DUMMY_FLAG (1<<24) #define ENC_DUMMY_FLAG (1<<24)
@ -409,8 +405,7 @@ rb_enc_register(const char *name, rb_encoding *encoding)
{ {
int index; int index;
GLOBAL_ENC_TABLE_ENTER(enc_table); GLOBAL_ENC_TABLE_LOCKING(enc_table) {
{
index = enc_registered(enc_table, name); index = enc_registered(enc_table, name);
if (index >= 0) { if (index >= 0) {
@ -430,7 +425,6 @@ rb_enc_register(const char *name, rb_encoding *encoding)
set_encoding_const(name, rb_enc_from_index(index)); set_encoding_const(name, rb_enc_from_index(index));
} }
} }
GLOBAL_ENC_TABLE_LEAVE();
return index; return index;
} }
@ -450,15 +444,13 @@ enc_registered(struct enc_table *enc_table, const char *name)
void void
rb_encdb_declare(const char *name) rb_encdb_declare(const char *name)
{ {
GLOBAL_ENC_TABLE_ENTER(enc_table); GLOBAL_ENC_TABLE_LOCKING(enc_table) {
{
int idx = enc_registered(enc_table, name); int idx = enc_registered(enc_table, name);
if (idx < 0) { if (idx < 0) {
idx = enc_register(enc_table, name, 0); idx = enc_register(enc_table, name, 0);
} }
set_encoding_const(name, rb_enc_from_index(idx)); set_encoding_const(name, rb_enc_from_index(idx));
} }
GLOBAL_ENC_TABLE_LEAVE();
} }
static void static void
@ -490,13 +482,11 @@ set_base_encoding(struct enc_table *enc_table, int index, rb_encoding *base)
void void
rb_enc_set_base(const char *name, const char *orig) rb_enc_set_base(const char *name, const char *orig)
{ {
GLOBAL_ENC_TABLE_ENTER(enc_table); GLOBAL_ENC_TABLE_LOCKING(enc_table) {
{
int idx = enc_registered(enc_table, name); int idx = enc_registered(enc_table, name);
int origidx = enc_registered(enc_table, orig); int origidx = enc_registered(enc_table, orig);
set_base_encoding(enc_table, idx, rb_enc_from_index(origidx)); set_base_encoding(enc_table, idx, rb_enc_from_index(origidx));
} }
GLOBAL_ENC_TABLE_LEAVE();
} }
/* for encdb.h /* for encdb.h
@ -547,8 +537,7 @@ rb_encdb_replicate(const char *name, const char *orig)
{ {
int r; int r;
GLOBAL_ENC_TABLE_ENTER(enc_table); GLOBAL_ENC_TABLE_LOCKING(enc_table) {
{
int origidx = enc_registered(enc_table, orig); int origidx = enc_registered(enc_table, orig);
int idx = enc_registered(enc_table, name); int idx = enc_registered(enc_table, name);
@ -557,7 +546,6 @@ rb_encdb_replicate(const char *name, const char *orig)
} }
r = enc_replicate_with_index(enc_table, name, rb_enc_from_index(origidx), idx); r = enc_replicate_with_index(enc_table, name, rb_enc_from_index(origidx), idx);
} }
GLOBAL_ENC_TABLE_LEAVE();
return r; return r;
} }
@ -567,13 +555,11 @@ rb_define_dummy_encoding(const char *name)
{ {
int index; int index;
GLOBAL_ENC_TABLE_ENTER(enc_table); GLOBAL_ENC_TABLE_LOCKING(enc_table) {
{
index = enc_replicate(enc_table, name, rb_ascii8bit_encoding()); index = enc_replicate(enc_table, name, rb_ascii8bit_encoding());
rb_encoding *enc = enc_table->list[index].enc; rb_encoding *enc = enc_table->list[index].enc;
ENC_SET_DUMMY((rb_raw_encoding *)enc); ENC_SET_DUMMY((rb_raw_encoding *)enc);
} }
GLOBAL_ENC_TABLE_LEAVE();
return index; return index;
} }
@ -583,15 +569,13 @@ rb_encdb_dummy(const char *name)
{ {
int index; int index;
GLOBAL_ENC_TABLE_ENTER(enc_table); GLOBAL_ENC_TABLE_LOCKING(enc_table) {
{
index = enc_replicate_with_index(enc_table, name, index = enc_replicate_with_index(enc_table, name,
rb_ascii8bit_encoding(), rb_ascii8bit_encoding(),
enc_registered(enc_table, name)); enc_registered(enc_table, name));
rb_encoding *enc = enc_table->list[index].enc; rb_encoding *enc = enc_table->list[index].enc;
ENC_SET_DUMMY((rb_raw_encoding *)enc); ENC_SET_DUMMY((rb_raw_encoding *)enc);
} }
GLOBAL_ENC_TABLE_LEAVE();
return index; return index;
} }
@ -671,8 +655,7 @@ rb_enc_alias(const char *alias, const char *orig)
{ {
int idx, r; int idx, r;
GLOBAL_ENC_TABLE_ENTER(enc_table); GLOBAL_ENC_TABLE_LOCKING(enc_table) {
{
enc_check_addable(enc_table, alias); enc_check_addable(enc_table, alias);
if ((idx = rb_enc_find_index(orig)) < 0) { if ((idx = rb_enc_find_index(orig)) < 0) {
r = -1; r = -1;
@ -681,7 +664,6 @@ rb_enc_alias(const char *alias, const char *orig)
r = enc_alias(enc_table, alias, idx); r = enc_alias(enc_table, alias, idx);
} }
} }
GLOBAL_ENC_TABLE_LEAVE();
return r; return r;
} }
@ -691,8 +673,7 @@ rb_encdb_alias(const char *alias, const char *orig)
{ {
int r; int r;
GLOBAL_ENC_TABLE_ENTER(enc_table); GLOBAL_ENC_TABLE_LOCKING(enc_table) {
{
int idx = enc_registered(enc_table, orig); int idx = enc_registered(enc_table, orig);
if (idx < 0) { if (idx < 0) {
@ -700,7 +681,6 @@ rb_encdb_alias(const char *alias, const char *orig)
} }
r = enc_alias(enc_table, alias, idx); r = enc_alias(enc_table, alias, idx);
} }
GLOBAL_ENC_TABLE_LEAVE();
return r; return r;
} }
@ -767,8 +747,7 @@ load_encoding(const char *name)
ruby_debug = debug; ruby_debug = debug;
rb_set_errinfo(errinfo); rb_set_errinfo(errinfo);
GLOBAL_ENC_TABLE_ENTER(enc_table); GLOBAL_ENC_TABLE_LOCKING(enc_table) {
{
if (loaded < 0 || 1 < loaded) { if (loaded < 0 || 1 < loaded) {
idx = -1; idx = -1;
} }
@ -779,7 +758,6 @@ load_encoding(const char *name)
idx = -1; idx = -1;
} }
} }
GLOBAL_ENC_TABLE_LEAVE();
return idx; return idx;
} }
@ -812,7 +790,9 @@ int
rb_enc_autoload(rb_encoding *enc) rb_enc_autoload(rb_encoding *enc)
{ {
int i; int i;
GLOBAL_ENC_TABLE_EVAL(enc_table, i = enc_autoload_body(enc_table, enc)); GLOBAL_ENC_TABLE_LOCKING(enc_table) {
i = enc_autoload_body(enc_table, enc);
}
if (i == -2) { if (i == -2) {
i = load_encoding(rb_enc_name(enc)); i = load_encoding(rb_enc_name(enc));
} }
@ -1509,11 +1489,9 @@ rb_locale_encindex(void)
void Init_w32_codepage(void); void Init_w32_codepage(void);
Init_w32_codepage(); Init_w32_codepage();
# endif # endif
GLOBAL_ENC_TABLE_ENTER(enc_table); GLOBAL_ENC_TABLE_LOCKING(enc_table) {
{
enc_alias_internal(enc_table, "locale", idx); enc_alias_internal(enc_table, "locale", idx);
} }
GLOBAL_ENC_TABLE_LEAVE();
} }
return idx; return idx;
@ -1555,8 +1533,7 @@ enc_set_default_encoding(struct default_encoding *def, VALUE encoding, const cha
/* Already set */ /* Already set */
overridden = TRUE; overridden = TRUE;
GLOBAL_ENC_TABLE_ENTER(enc_table); GLOBAL_ENC_TABLE_LOCKING(enc_table) {
{
if (NIL_P(encoding)) { if (NIL_P(encoding)) {
def->index = -1; def->index = -1;
def->enc = 0; def->enc = 0;
@ -1580,7 +1557,6 @@ enc_set_default_encoding(struct default_encoding *def, VALUE encoding, const cha
enc_alias_internal(enc_table, "filesystem", Init_enc_set_filesystem_encoding()); enc_alias_internal(enc_table, "filesystem", Init_enc_set_filesystem_encoding());
} }
} }
GLOBAL_ENC_TABLE_LEAVE();
return overridden; return overridden;
} }

View File

@ -131,8 +131,11 @@ WARN_UNUSED_RESULT(static VALUE lookup_str_sym(const VALUE str));
WARN_UNUSED_RESULT(static VALUE lookup_id_str(ID id)); WARN_UNUSED_RESULT(static VALUE lookup_id_str(ID id));
WARN_UNUSED_RESULT(static ID intern_str(VALUE str, int mutable)); WARN_UNUSED_RESULT(static ID intern_str(VALUE str, int mutable));
#define GLOBAL_SYMBOLS_ENTER(symbols) rb_symbols_t *symbols = &ruby_global_symbols; RB_VM_LOCK_ENTER() #define GLOBAL_SYMBOLS_LOCKING(symbols) \
#define GLOBAL_SYMBOLS_LEAVE() RB_VM_LOCK_LEAVE() for (rb_symbols_t *symbols = &ruby_global_symbols, **locking = &symbols; \
locking; \
locking = NULL) \
RB_VM_LOCKING()
ID ID
rb_id_attrset(ID id) rb_id_attrset(ID id)
@ -467,8 +470,7 @@ get_id_serial_entry(rb_id_serial_t num, ID id, const enum id_entry_type t)
{ {
VALUE result = 0; VALUE result = 0;
GLOBAL_SYMBOLS_ENTER(symbols); GLOBAL_SYMBOLS_LOCKING(symbols) {
{
if (num && num <= symbols->last_id) { if (num && num <= symbols->last_id) {
size_t idx = num / ID_ENTRY_UNIT; size_t idx = num / ID_ENTRY_UNIT;
VALUE ids = symbols->ids; VALUE ids = symbols->ids;
@ -496,7 +498,6 @@ get_id_serial_entry(rb_id_serial_t num, ID id, const enum id_entry_type t)
} }
} }
} }
GLOBAL_SYMBOLS_LEAVE();
if (result) { if (result) {
switch (t) { switch (t) {
@ -567,11 +568,9 @@ register_sym(rb_symbols_t *symbols, VALUE str, VALUE sym)
void void
rb_free_static_symid_str(void) rb_free_static_symid_str(void)
{ {
GLOBAL_SYMBOLS_ENTER(symbols) GLOBAL_SYMBOLS_LOCKING(symbols) {
{
st_free_table(symbols->str_sym); st_free_table(symbols->str_sym);
} }
GLOBAL_SYMBOLS_LEAVE();
} }
static void static void
@ -603,12 +602,10 @@ register_static_symid_str(ID id, VALUE str)
RUBY_DTRACE_CREATE_HOOK(SYMBOL, RSTRING_PTR(str)); RUBY_DTRACE_CREATE_HOOK(SYMBOL, RSTRING_PTR(str));
GLOBAL_SYMBOLS_ENTER(symbols) GLOBAL_SYMBOLS_LOCKING(symbols) {
{
register_sym(symbols, str, sym); register_sym(symbols, str, sym);
set_id_entry(symbols, num, str, sym); set_id_entry(symbols, num, str, sym);
} }
GLOBAL_SYMBOLS_LEAVE();
return id; return id;
} }
@ -705,11 +702,9 @@ lookup_str_id(VALUE str)
st_data_t sym_data; st_data_t sym_data;
int found; int found;
GLOBAL_SYMBOLS_ENTER(symbols); GLOBAL_SYMBOLS_LOCKING(symbols) {
{
found = st_lookup(symbols->str_sym, (st_data_t)str, &sym_data); found = st_lookup(symbols->str_sym, (st_data_t)str, &sym_data);
} }
GLOBAL_SYMBOLS_LEAVE();
if (found) { if (found) {
const VALUE sym = (VALUE)sym_data; const VALUE sym = (VALUE)sym_data;
@ -750,11 +745,9 @@ lookup_str_sym(const VALUE str)
{ {
VALUE sym; VALUE sym;
GLOBAL_SYMBOLS_ENTER(symbols); GLOBAL_SYMBOLS_LOCKING(symbols) {
{
sym = lookup_str_sym_with_lock(symbols, str); sym = lookup_str_sym_with_lock(symbols, str);
} }
GLOBAL_SYMBOLS_LEAVE();
return sym; return sym;
} }
@ -799,11 +792,9 @@ static ID
next_id_base(void) next_id_base(void)
{ {
ID id; ID id;
GLOBAL_SYMBOLS_ENTER(symbols); GLOBAL_SYMBOLS_LOCKING(symbols) {
{
id = next_id_base_with_lock(symbols); id = next_id_base_with_lock(symbols);
} }
GLOBAL_SYMBOLS_LEAVE();
return id; return id;
} }
@ -862,12 +853,10 @@ rb_gc_free_dsymbol(VALUE sym)
if (str) { if (str) {
RSYMBOL(sym)->fstr = 0; RSYMBOL(sym)->fstr = 0;
GLOBAL_SYMBOLS_ENTER(symbols); GLOBAL_SYMBOLS_LOCKING(symbols) {
{
unregister_sym(symbols, str, sym); unregister_sym(symbols, str, sym);
rb_hash_delete_entry(symbols->dsymbol_fstr_hash, str); rb_hash_delete_entry(symbols->dsymbol_fstr_hash, str);
} }
GLOBAL_SYMBOLS_LEAVE();
} }
} }
@ -896,8 +885,7 @@ rb_str_intern(VALUE str)
{ {
VALUE sym; VALUE sym;
GLOBAL_SYMBOLS_ENTER(symbols); GLOBAL_SYMBOLS_LOCKING(symbols) {
{
sym = lookup_str_sym_with_lock(symbols, str); sym = lookup_str_sym_with_lock(symbols, str);
if (sym) { if (sym) {
@ -926,7 +914,6 @@ rb_str_intern(VALUE str)
sym = ID2SYM(id); sym = ID2SYM(id);
} }
} }
GLOBAL_SYMBOLS_LEAVE();
return sym; return sym;
} }
@ -938,8 +925,7 @@ rb_sym2id(VALUE sym)
id = STATIC_SYM2ID(sym); id = STATIC_SYM2ID(sym);
} }
else if (DYNAMIC_SYM_P(sym)) { else if (DYNAMIC_SYM_P(sym)) {
GLOBAL_SYMBOLS_ENTER(symbols); GLOBAL_SYMBOLS_LOCKING(symbols) {
{
sym = dsymbol_check(symbols, sym); sym = dsymbol_check(symbols, sym);
id = RSYMBOL(sym)->id; id = RSYMBOL(sym)->id;
@ -954,7 +940,6 @@ rb_sym2id(VALUE sym)
rb_hash_delete_entry(symbols->dsymbol_fstr_hash, fstr); rb_hash_delete_entry(symbols->dsymbol_fstr_hash, fstr);
} }
} }
GLOBAL_SYMBOLS_LEAVE();
} }
else { else {
rb_raise(rb_eTypeError, "wrong argument type %s (expected Symbol)", rb_raise(rb_eTypeError, "wrong argument type %s (expected Symbol)",
@ -1060,12 +1045,10 @@ rb_sym_all_symbols(void)
{ {
VALUE ary; VALUE ary;
GLOBAL_SYMBOLS_ENTER(symbols); GLOBAL_SYMBOLS_LOCKING(symbols) {
{
ary = rb_ary_new2(symbols->str_sym->num_entries); ary = rb_ary_new2(symbols->str_sym->num_entries);
st_foreach(symbols->str_sym, symbols_i, ary); st_foreach(symbols->str_sym, symbols_i, ary);
} }
GLOBAL_SYMBOLS_LEAVE();
return ary; return ary;
} }
@ -1199,11 +1182,9 @@ rb_check_symbol(volatile VALUE *namep)
} }
else if (DYNAMIC_SYM_P(name)) { else if (DYNAMIC_SYM_P(name)) {
if (!SYMBOL_PINNED_P(name)) { if (!SYMBOL_PINNED_P(name)) {
GLOBAL_SYMBOLS_ENTER(symbols); GLOBAL_SYMBOLS_LOCKING(symbols) {
{
name = dsymbol_check(symbols, name); name = dsymbol_check(symbols, name);
} }
GLOBAL_SYMBOLS_LEAVE();
*namep = name; *namep = name;
} }