summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNobuyoshi Nakada <[email protected]>2021-10-27 11:45:59 +0900
committerNobuyoshi Nakada <[email protected]>2025-05-25 15:22:43 +0900
commitfc518fe1ff0410f836b01577b8c4f3940404a24b (patch)
tree4ea552af058a6b9e06350979da72a79670c4d97e
parentf2ca66fefc809f3a6f92864d3e7d3f1769f7bfbd (diff)
Delimit the scopes using encoding/symbol tables
Notes
Notes: Merged: https://github.com/ruby/ruby/pull/13439
-rw-r--r--encoding.c62
-rw-r--r--symbol.c51
2 files changed, 35 insertions, 78 deletions
diff --git a/encoding.c b/encoding.c
index e2aaadb5b9..60d92690a7 100644
--- a/encoding.c
+++ b/encoding.c
@@ -93,15 +93,11 @@ static rb_encoding *global_enc_ascii,
*global_enc_utf_8,
*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_LEAVE() RB_VM_LOCK_LEAVE()
-#define GLOBAL_ENC_TABLE_EVAL(enc_table, expr) do { \
- GLOBAL_ENC_TABLE_ENTER(enc_table); \
- { \
- expr; \
- } \
- GLOBAL_ENC_TABLE_LEAVE(); \
-} while (0)
+#define GLOBAL_ENC_TABLE_LOCKING(tbl) \
+ for (struct enc_table *tbl = &global_enc_table, **locking = &tbl; \
+ locking; \
+ locking = NULL) \
+ RB_VM_LOCKING()
#define ENC_DUMMY_FLAG (1<<24)
@@ -409,8 +405,7 @@ rb_enc_register(const char *name, rb_encoding *encoding)
{
int index;
- GLOBAL_ENC_TABLE_ENTER(enc_table);
- {
+ GLOBAL_ENC_TABLE_LOCKING(enc_table) {
index = enc_registered(enc_table, name);
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));
}
}
- GLOBAL_ENC_TABLE_LEAVE();
return index;
}
@@ -450,15 +444,13 @@ enc_registered(struct enc_table *enc_table, const char *name)
void
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);
if (idx < 0) {
idx = enc_register(enc_table, name, 0);
}
set_encoding_const(name, rb_enc_from_index(idx));
}
- GLOBAL_ENC_TABLE_LEAVE();
}
static void
@@ -490,13 +482,11 @@ set_base_encoding(struct enc_table *enc_table, int index, rb_encoding *base)
void
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 origidx = enc_registered(enc_table, orig);
set_base_encoding(enc_table, idx, rb_enc_from_index(origidx));
}
- GLOBAL_ENC_TABLE_LEAVE();
}
/* for encdb.h
@@ -547,8 +537,7 @@ rb_encdb_replicate(const char *name, const char *orig)
{
int r;
- GLOBAL_ENC_TABLE_ENTER(enc_table);
- {
+ GLOBAL_ENC_TABLE_LOCKING(enc_table) {
int origidx = enc_registered(enc_table, orig);
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);
}
- GLOBAL_ENC_TABLE_LEAVE();
return r;
}
@@ -567,13 +555,11 @@ rb_define_dummy_encoding(const char *name)
{
int index;
- GLOBAL_ENC_TABLE_ENTER(enc_table);
- {
+ GLOBAL_ENC_TABLE_LOCKING(enc_table) {
index = enc_replicate(enc_table, name, rb_ascii8bit_encoding());
rb_encoding *enc = enc_table->list[index].enc;
ENC_SET_DUMMY((rb_raw_encoding *)enc);
}
- GLOBAL_ENC_TABLE_LEAVE();
return index;
}
@@ -583,15 +569,13 @@ rb_encdb_dummy(const char *name)
{
int index;
- GLOBAL_ENC_TABLE_ENTER(enc_table);
- {
+ GLOBAL_ENC_TABLE_LOCKING(enc_table) {
index = enc_replicate_with_index(enc_table, name,
rb_ascii8bit_encoding(),
enc_registered(enc_table, name));
rb_encoding *enc = enc_table->list[index].enc;
ENC_SET_DUMMY((rb_raw_encoding *)enc);
}
- GLOBAL_ENC_TABLE_LEAVE();
return index;
}
@@ -671,8 +655,7 @@ rb_enc_alias(const char *alias, const char *orig)
{
int idx, r;
- GLOBAL_ENC_TABLE_ENTER(enc_table);
- {
+ GLOBAL_ENC_TABLE_LOCKING(enc_table) {
enc_check_addable(enc_table, alias);
if ((idx = rb_enc_find_index(orig)) < 0) {
r = -1;
@@ -681,7 +664,6 @@ rb_enc_alias(const char *alias, const char *orig)
r = enc_alias(enc_table, alias, idx);
}
}
- GLOBAL_ENC_TABLE_LEAVE();
return r;
}
@@ -691,8 +673,7 @@ rb_encdb_alias(const char *alias, const char *orig)
{
int r;
- GLOBAL_ENC_TABLE_ENTER(enc_table);
- {
+ GLOBAL_ENC_TABLE_LOCKING(enc_table) {
int idx = enc_registered(enc_table, orig);
if (idx < 0) {
@@ -700,7 +681,6 @@ rb_encdb_alias(const char *alias, const char *orig)
}
r = enc_alias(enc_table, alias, idx);
}
- GLOBAL_ENC_TABLE_LEAVE();
return r;
}
@@ -767,8 +747,7 @@ load_encoding(const char *name)
ruby_debug = debug;
rb_set_errinfo(errinfo);
- GLOBAL_ENC_TABLE_ENTER(enc_table);
- {
+ GLOBAL_ENC_TABLE_LOCKING(enc_table) {
if (loaded < 0 || 1 < loaded) {
idx = -1;
}
@@ -779,7 +758,6 @@ load_encoding(const char *name)
idx = -1;
}
}
- GLOBAL_ENC_TABLE_LEAVE();
return idx;
}
@@ -812,7 +790,9 @@ int
rb_enc_autoload(rb_encoding *enc)
{
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) {
i = load_encoding(rb_enc_name(enc));
}
@@ -1509,11 +1489,9 @@ rb_locale_encindex(void)
void Init_w32_codepage(void);
Init_w32_codepage();
# endif
- GLOBAL_ENC_TABLE_ENTER(enc_table);
- {
+ GLOBAL_ENC_TABLE_LOCKING(enc_table) {
enc_alias_internal(enc_table, "locale", idx);
}
- GLOBAL_ENC_TABLE_LEAVE();
}
return idx;
@@ -1555,8 +1533,7 @@ enc_set_default_encoding(struct default_encoding *def, VALUE encoding, const cha
/* Already set */
overridden = TRUE;
- GLOBAL_ENC_TABLE_ENTER(enc_table);
- {
+ GLOBAL_ENC_TABLE_LOCKING(enc_table) {
if (NIL_P(encoding)) {
def->index = -1;
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());
}
}
- GLOBAL_ENC_TABLE_LEAVE();
return overridden;
}
diff --git a/symbol.c b/symbol.c
index 7925db451d..4e590eb8ec 100644
--- a/symbol.c
+++ b/symbol.c
@@ -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 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_LEAVE() RB_VM_LOCK_LEAVE()
+#define GLOBAL_SYMBOLS_LOCKING(symbols) \
+ for (rb_symbols_t *symbols = &ruby_global_symbols, **locking = &symbols; \
+ locking; \
+ locking = NULL) \
+ RB_VM_LOCKING()
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;
- GLOBAL_SYMBOLS_ENTER(symbols);
- {
+ GLOBAL_SYMBOLS_LOCKING(symbols) {
if (num && num <= symbols->last_id) {
size_t idx = num / ID_ENTRY_UNIT;
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) {
switch (t) {
@@ -567,11 +568,9 @@ register_sym(rb_symbols_t *symbols, VALUE str, VALUE sym)
void
rb_free_static_symid_str(void)
{
- GLOBAL_SYMBOLS_ENTER(symbols)
- {
+ GLOBAL_SYMBOLS_LOCKING(symbols) {
st_free_table(symbols->str_sym);
}
- GLOBAL_SYMBOLS_LEAVE();
}
static void
@@ -603,12 +602,10 @@ register_static_symid_str(ID id, VALUE str)
RUBY_DTRACE_CREATE_HOOK(SYMBOL, RSTRING_PTR(str));
- GLOBAL_SYMBOLS_ENTER(symbols)
- {
+ GLOBAL_SYMBOLS_LOCKING(symbols) {
register_sym(symbols, str, sym);
set_id_entry(symbols, num, str, sym);
}
- GLOBAL_SYMBOLS_LEAVE();
return id;
}
@@ -705,11 +702,9 @@ lookup_str_id(VALUE str)
st_data_t sym_data;
int found;
- GLOBAL_SYMBOLS_ENTER(symbols);
- {
+ GLOBAL_SYMBOLS_LOCKING(symbols) {
found = st_lookup(symbols->str_sym, (st_data_t)str, &sym_data);
}
- GLOBAL_SYMBOLS_LEAVE();
if (found) {
const VALUE sym = (VALUE)sym_data;
@@ -750,11 +745,9 @@ lookup_str_sym(const VALUE str)
{
VALUE sym;
- GLOBAL_SYMBOLS_ENTER(symbols);
- {
+ GLOBAL_SYMBOLS_LOCKING(symbols) {
sym = lookup_str_sym_with_lock(symbols, str);
}
- GLOBAL_SYMBOLS_LEAVE();
return sym;
}
@@ -799,11 +792,9 @@ static ID
next_id_base(void)
{
ID id;
- GLOBAL_SYMBOLS_ENTER(symbols);
- {
+ GLOBAL_SYMBOLS_LOCKING(symbols) {
id = next_id_base_with_lock(symbols);
}
- GLOBAL_SYMBOLS_LEAVE();
return id;
}
@@ -862,12 +853,10 @@ rb_gc_free_dsymbol(VALUE sym)
if (str) {
RSYMBOL(sym)->fstr = 0;
- GLOBAL_SYMBOLS_ENTER(symbols);
- {
+ GLOBAL_SYMBOLS_LOCKING(symbols) {
unregister_sym(symbols, str, sym);
rb_hash_delete_entry(symbols->dsymbol_fstr_hash, str);
}
- GLOBAL_SYMBOLS_LEAVE();
}
}
@@ -896,8 +885,7 @@ rb_str_intern(VALUE str)
{
VALUE sym;
- GLOBAL_SYMBOLS_ENTER(symbols);
- {
+ GLOBAL_SYMBOLS_LOCKING(symbols) {
sym = lookup_str_sym_with_lock(symbols, str);
if (sym) {
@@ -926,7 +914,6 @@ rb_str_intern(VALUE str)
sym = ID2SYM(id);
}
}
- GLOBAL_SYMBOLS_LEAVE();
return sym;
}
@@ -938,8 +925,7 @@ rb_sym2id(VALUE sym)
id = STATIC_SYM2ID(sym);
}
else if (DYNAMIC_SYM_P(sym)) {
- GLOBAL_SYMBOLS_ENTER(symbols);
- {
+ GLOBAL_SYMBOLS_LOCKING(symbols) {
sym = dsymbol_check(symbols, sym);
id = RSYMBOL(sym)->id;
@@ -954,7 +940,6 @@ rb_sym2id(VALUE sym)
rb_hash_delete_entry(symbols->dsymbol_fstr_hash, fstr);
}
}
- GLOBAL_SYMBOLS_LEAVE();
}
else {
rb_raise(rb_eTypeError, "wrong argument type %s (expected Symbol)",
@@ -1060,12 +1045,10 @@ rb_sym_all_symbols(void)
{
VALUE ary;
- GLOBAL_SYMBOLS_ENTER(symbols);
- {
+ GLOBAL_SYMBOLS_LOCKING(symbols) {
ary = rb_ary_new2(symbols->str_sym->num_entries);
st_foreach(symbols->str_sym, symbols_i, ary);
}
- GLOBAL_SYMBOLS_LEAVE();
return ary;
}
@@ -1199,11 +1182,9 @@ rb_check_symbol(volatile VALUE *namep)
}
else if (DYNAMIC_SYM_P(name)) {
if (!SYMBOL_PINNED_P(name)) {
- GLOBAL_SYMBOLS_ENTER(symbols);
- {
+ GLOBAL_SYMBOLS_LOCKING(symbols) {
name = dsymbol_check(symbols, name);
}
- GLOBAL_SYMBOLS_LEAVE();
*namep = name;
}