2929#include "ruby/util.h"
3030#include "ruby_assert.h"
3131#include "vm_sync.h"
32+ #include "ruby_atomic.h"
3233
3334#ifndef ENC_DEBUG
3435#define ENC_DEBUG 0
@@ -144,10 +145,14 @@ enc_list_update(int index, rb_raw_encoding *encoding)
144145{
145146 RUBY_ASSERT (index < ENCODING_LIST_CAPA );
146147
147- VALUE list = rb_encoding_list ;
148+ VALUE list = RUBY_ATOMIC_VALUE_LOAD (rb_encoding_list );
149+
148150 if (list && NIL_P (rb_ary_entry (list , index ))) {
151+ VALUE new_list = rb_ary_dup (list );
152+ RBASIC_CLEAR_CLASS (new_list );
149153 /* initialize encoding data */
150- rb_ary_store (list , index , enc_new (encoding ));
154+ rb_ary_store (new_list , index , enc_new (encoding ));
155+ RUBY_ATOMIC_VALUE_SET (rb_encoding_list , new_list );
151156 }
152157}
153158
@@ -157,7 +162,7 @@ enc_list_lookup(int idx)
157162 VALUE list , enc = Qnil ;
158163
159164 if (idx < ENCODING_LIST_CAPA ) {
160- list = rb_encoding_list ;
165+ list = RUBY_ATOMIC_VALUE_LOAD ( rb_encoding_list ) ;
161166 RUBY_ASSERT (list );
162167 enc = rb_ary_entry (list , idx );
163168 }
@@ -1387,7 +1392,8 @@ static VALUE
13871392enc_list (VALUE klass )
13881393{
13891394 VALUE ary = rb_ary_new2 (0 );
1390- rb_ary_replace (ary , rb_encoding_list );
1395+ VALUE list = RUBY_ATOMIC_VALUE_LOAD (rb_encoding_list );
1396+ rb_ary_replace (ary , list );
13911397 return ary ;
13921398}
13931399
@@ -1982,9 +1988,9 @@ Init_Encoding(void)
19821988
19831989 struct enc_table * enc_table = & global_enc_table ;
19841990
1991+ rb_gc_register_address (& rb_encoding_list );
19851992 list = rb_encoding_list = rb_ary_new2 (ENCODING_LIST_CAPA );
19861993 RBASIC_CLEAR_CLASS (list );
1987- rb_vm_register_global_object (list );
19881994
19891995 for (i = 0 ; i < enc_table -> count ; ++ i ) {
19901996 rb_ary_push (list , enc_new (enc_table -> list [i ].enc ));
0 commit comments