10000 Revert "reduce lock for encoding" · github/ruby@a76a307 · GitHub
[go: up one dir, main page]

Skip to content

Commit a76a307

Browse files
committed
Revert "reduce lock for encoding"
This reverts commit de17e2d. This patch can introduce race condition because of conflicting read/write access for enc_table::default_list. Maybe we need to freeze default_list at the end of Init_encdb() in enc/encdb.c.
1 parent dac3677 commit a76a307

File tree

1 file changed

+53
-72
lines changed

1 file changed

+53
-72
lines changed

encoding.c

Lines changed: 53 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,9 @@ struct rb_encoding_entry {
6868
};
6969

7070
static struct enc_table {
71-
// default_list + additional_list
72-
struct rb_encoding_entry default_list[DEFAULT_ENCODING_LIST_CAPA];
73-
struct rb_encoding_entry *additional_list;
74-
int additional_list_size;
71+
struct rb_encoding_entry *list;
7572
int count;
73+
int size;
7674
st_table *names;
7775
} global_enc_table;
7876

@@ -350,54 +348,24 @@ rb_find_encoding(VALUE enc)
350348
}
351349

352350
static int
353-
enc_table_expand(struct enc_table *enc_table, const int newsize)
351+
enc_table_expand(struct enc_table *enc_table, int newsize)
354352
{
355-
if (newsize <= DEFAULT_ENCODING_LIST_CAPA) {
356-
// ok
357-
}
358-
else {
359-
int add_size = newsize - DEFAULT_ENCODING_LIST_CAPA;
360-
if (add_size <= enc_table->additional_list_size) {
361-
// ok
362-
}
363-
else {
364-
struct rb_encoding_entry *ent;
365-
add_size = (add_size + 7) / 8 * 8;
366-
367-
if (enc_table->additional_list == NULL) {
368-
ent = enc_table->additional_list = ALLOC_N(struct rb_encoding_entry, add_size);
369-
}
370-
else {
371-
ent = REALLOC_N(enc_table->additional_list, struct rb_encoding_entry, add_size);
372-
}
373-
< 10000 /td>
374-
memset(ent + enc_table->additional_list_size, 0, sizeof(*ent)*(add_size - enc_table->additional_list_size));
375-
enc_table->additional_list = ent;
376-
enc_table->additional_list_size = add_size;
377-
}
378-
}
353+
struct rb_encoding_entry *ent;
354+
int count = newsize;
379355

380-
return newsize;
381-
}
382-
383-
static struct rb_encoding_entry *
384-
enc_entry_at(struct enc_table *enc_table, int index)
385-
{
386-
if (LIKELY(index < DEFAULT_ENCODING_LIST_CAPA)) {
387-
return &enc_table->default_list[index];
388-
}
389-
else {
390-
struct rb_encoding_entry *e;
391-
GLOBAL_ENC_TABLE_EVAL(enc_table,
392-
e = &enc_table->additional_list[index - DEFAULT_ENCODING_LIST_CAPA]);
393-
return e;
394-
}
356+
if (enc_table->size >= newsize) return newsize;
357+
newsize = (newsize + 7) / 8 * 8;
358+
ent = REALLOC_N(enc_table->list, struct rb_encoding_entry, newsize);
359+
memset(ent + enc_table->size, 0, sizeof(*ent)*(newsize - enc_table->size));
360+
enc_table->list = ent;
361+
enc_table->size = newsize;
362+
return count;
395363
}
396364

397365
static int
398366
enc_register_at(struct enc_table *enc_table, int index, const char *name, rb_encoding *base_encoding)
399367
{
400-
struct rb_encoding_entry *ent = enc_entry_at(enc_table, index);
368+
struct rb_encoding_entry *ent = &enc_table->list[index];
401369
rb_raw_encoding *encoding;
402370

403371
if (!valid_encoding_name_p(name)) return -1;
@@ -441,18 +409,19 @@ static int enc_registered(struct enc_table *enc_table, const char *name);
441409
static rb_encoding *
442410
enc_from_index(struct enc_table *enc_table, int index)
443411
{
444-
// do not need a lock
445-
446412
if (UNLIKELY(index < 0 || enc_table->count <= (index &= ENC_INDEX_MASK))) {
447413
return 0;
448414
}
449-
return enc_entry_at(enc_table, index)->enc;
415+
return enc_table->list[index].enc;
450416
}
451417

452418
rb_encoding *
453419
rb_enc_from_index(int index)
454420
{
455-
return enc_from_index(&global_enc_table, index);
421+
rb_encoding *enc;
422+
GLOBAL_ENC_TABLE_EVAL(enc_table,
423+
enc = enc_from_index(enc_table, index));
424+
return enc;
456425
}
457426

458427
int
@@ -491,7 +460,7 @@ enc_registered(struct enc_table *enc_table, const char *name)
491460
st_data_t idx = 0;
492461

493462
if (!name) return -1;
494-
if (!enc_table->names) return -1;
463+
if (!enc_table->list) return -1;
495464
if (st_lookup(enc_table->names, (st_data_t)name, &idx)) {
496465
return (int)idx;
497466
}
@@ -523,9 +492,9 @@ enc_check_duplication(struct enc_table *enc_table, const char *name)
523492
static rb_encoding*
524493
set_base_encoding(struct enc_table *enc_table, int index, rb_encoding *base)
525494
{
526-
struct rb_encoding_entry *entry = enc_entry_at(enc_table, index);
527-
rb_encoding *enc = entry->enc;
528-
entry->base = base;
495+
rb_encoding *enc = enc_table->list[index].enc;
496+
497+
enc_table->list[index].base = base;
529498
if (ENC_DUMMY_P(base)) ENC_SET_DUMMY((rb_raw_encoding *)enc);
530499
return enc;
531500
}
@@ -552,7 +521,11 @@ rb_enc_set_base(const char *name, const char *orig)
552521
int
553522
rb_enc_set_dummy(int index)
554523
{
555-
rb_encoding *enc = rb_enc_from_index(index);
524+
rb_encoding *enc;
525+
526+
GLOBAL_ENC_TABLE_EVAL(enc_table,
527+
enc = enc_table->list[index].enc);
528+
556529
ENC_SET_DUMMY((rb_raw_encoding *)enc);
557530
return index;
558531
}
@@ -642,23 +615,32 @@ rb_define_dummy_encoding(const char *name)
642615
{
643616
int index;
644617

645-
GLOBAL_ENC_TABLE_EVAL(enc_table,
646-
index = enc_replicate(enc_table, name, rb_ascii8bit_encoding()));
647-
rb_encoding *enc = rb_enc_from_index(index);
648-
ENC_SET_DUMMY((rb_raw_encoding *)enc);
618+
GLOBAL_ENC_TABLE_ENTER(enc_table);
619+
{
620+
index = enc_replicate(enc_table, name, rb_ascii8bit_encoding());
621+
rb_encoding *enc = enc_table->list[index].enc;
622+
ENC_SET_DUMMY((rb_raw_encoding *)enc);
623+
}
624+
GLOBAL_ENC_TABLE_LEAVE();
625+
649626
return index;
650627
}
651628

652629
int
653630
rb_encdb_dummy(const char *name)
654631
{
655632
int index;
656-
GLOBAL_ENC_TABLE_EVAL(enc_table,
657-
index = enc_replicate_with_index(enc_table, name,
658-
rb_ascii8bit_encoding(),
659-
enc_registered(enc_table, name)));
660-
rb_encoding *enc = rb_enc_from_index(index);
661-
ENC_SET_DUMMY((rb_raw_encoding *)enc);
633+
634+
GLOBAL_ENC_TABLE_ENTER(enc_table);
635+
{
636+
index = enc_replicate_with_index(enc_table, name,
637+
rb_ascii8bit_encoding(),
638+
enc_registered(enc_table, name));
639+
rb_encoding *enc = enc_table->list[index].enc;
640+
ENC_SET_DUMMY((rb_raw_encoding *)enc);
641+
}
642+
GLOBAL_ENC_TABLE_LEAVE();
643+
662644
return index;
663645
}
664646

@@ -788,10 +770,9 @@ rb_enc_init(struct enc_table *enc_table)
788770
ENC_REGISTER(ASCII);
789771
ENC_REGISTER(UTF_8);
790772
ENC_REGISTER(US_ASCII);
791-
792-
global_enc_ascii = enc_table->default_list[ENCINDEX_ASCII].enc;
793-
global_enc_utf_8 = enc_table->default_list[ENCINDEX_UTF_8].enc;
794-
global_enc_us_ascii = enc_table->default_list[ENCINDEX_US_ASCII].enc;
773+
global_enc_ascii = enc_table->list[ENCINDEX_ASCII].enc;
774+
global_enc_utf_8 = enc_table->list[ENCINDEX_UTF_8].enc;
775+
global_enc_us_ascii = enc_table->list[ENCINDEX_US_ASCII].enc;
795776
#undef ENC_REGISTER
796777
#define ENCDB_REGISTER(name, enc) enc_register_at(enc_table, ENCINDEX_##enc, name, NULL)
797778
ENCDB_REGISTER("UTF-16BE", UTF_16BE);
@@ -847,7 +828,7 @@ load_encoding(const char *name)
847828
else if ((idx = enc_registered(enc_table, name)) < 0) {
848829
idx = -1;
849830
}
850-
else if (enc_autoload_p(enc_from_index(enc_table, idx))) {
831+
else if (enc_autoload_p(enc_table->list[idx].enc)) {
851832
idx = -1;
852833
}
853834
}
@@ -859,13 +840,13 @@ load_encoding(const char *name)
859840
static int
860841
enc_autoload_body(struct enc_table *enc_table, rb_encoding *enc)
861842
{
862-
rb_encoding *base = enc_entry_at(enc_table, ENC_TO_ENCINDEX(enc))->base;
843+
rb_encoding *base = enc_table->list[ENC_TO_ENCINDEX(enc)].base;
863844

864845
if (base) {
865846
int i = 0;
866847
do {
867848
if (i >= enc_table->count) return -1;
868-
} while (enc_from_index(enc_table, i) != base && (++i, 1));
849+
} while (enc_table->list[i].enc != base && (++i, 1));
869850
if (enc_autoload_p(base)) {
870851
if (enc_autoload(base) < 0) return -1;
871852
}
@@ -2207,7 +2188,7 @@ Init_Encoding(void)
22072188
rb_gc_register_mark_object(list);
22082189

22092190
for (i = 0; i < enc_table->count; ++i) {
2210-
rb_ary_push(list, enc_new(enc_from_index(enc_table, i)));
2191+
rb_ary_push(list, enc_new(enc_table->list[i].enc));
22112192
}
22122193

22132194
rb_marshal_define_compat(rb_cEncoding, Qnil, 0, enc_m_loader);

0 commit comments

Comments
 (0)
0