summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--defs/id.def1
-rw-r--r--thread.c30
-rw-r--r--vm.c2
3 files changed, 25 insertions, 8 deletions
diff --git a/defs/id.def b/defs/id.def
index 6bfce29a9d..f0e1db90dc 100644
--- a/defs/id.def
+++ b/defs/id.def
@@ -46,6 +46,7 @@ firstline, predefined = __LINE__+1, %[\
call
mesg
exception
+ locals
not NOT
and AND
or OR
diff --git a/thread.c b/thread.c
index 3b6ded9609..91f07e8a84 100644
--- a/thread.c
+++ b/thread.c
@@ -97,13 +97,23 @@ static VALUE rb_cThreadShield;
static VALUE sym_immediate;
static VALUE sym_on_blocking;
static VALUE sym_never;
-static ID id_locals;
enum SLEEP_FLAGS {
SLEEP_DEADLOCKABLE = 0x1,
SLEEP_SPURIOUS_CHECK = 0x2
};
+#define THREAD_LOCAL_STORAGE_INITIALISED FL_USER13
+#define THREAD_LOCAL_STORAGE_INITIALISED_P(th) RB_FL_TEST_RAW((th), THREAD_LOCAL_STORAGE_INITIALISED)
+
+static VALUE inline rb_thread_local_storage(VALUE thread) {
+ if (LIKELY(!THREAD_LOCAL_STORAGE_INITIALISED_P(thread))) {
+ rb_ivar_set(thread, idLocals, rb_hash_new());
+ RB_FL_SET_RAW(thread, THREAD_LOCAL_STORAGE_INITIALISED);
+ }
+ return rb_ivar_get(thread, idLocals);
+}
+
static void sleep_hrtime(rb_thread_t *, rb_hrtime_t, unsigned int fl);
static void sleep_forever(rb_thread_t *th, unsigned int fl);
static void rb_thread_sleep_deadly_allow_spurious_wakeup(void);
@@ -3403,7 +3413,10 @@ rb_thread_variable_get(VALUE thread, VALUE key)
{
VALUE locals;
- locals = rb_ivar_get(thread, id_locals);
+ if (LIKELY(!THREAD_LOCAL_STORAGE_INITIALISED_P(thread))) {
+ return Qnil;
+ }
+ locals = rb_thread_local_storage(thread);
return rb_hash_aref(locals, rb_to_symbol(key));
}
@@ -3425,7 +3438,7 @@ rb_thread_variable_set(VALUE thread, VALUE id, VALUE val)
rb_frozen_error_raise(thread, "can't modify frozen thread locals");
}
- locals = rb_ivar_get(thread, id_locals);
+ locals = rb_thread_local_storage(thread);
return rb_hash_aset(locals, rb_to_symbol(id), val);
}
@@ -3528,8 +3541,11 @@ rb_thread_variables(VALUE thread)
VALUE locals;
VALUE ary;
- locals = rb_ivar_get(thread, id_locals);
ary = rb_ary_new();
+ if (LIKELY(!THREAD_LOCAL_STORAGE_INITIALISED_P(thread))) {
+ return ary;
+ }
+ locals = rb_thread_local_storage(thread);
rb_hash_foreach(locals, keys_i, ary);
return ary;
@@ -3559,7 +3575,10 @@ rb_thread_variable_p(VALUE thread, VALUE key)
if (!id) return Qfalse;
- locals = rb_ivar_get(thread, id_locals);
+ if (LIKELY(!THREAD_LOCAL_STORAGE_INITIALISED_P(thread))) {
+ return Qfalse;
+ }
+ locals = rb_thread_local_storage(thread);
if (rb_hash_lookup(locals, ID2SYM(id)) != Qnil) {
return Qtrue;
@@ -5137,7 +5156,6 @@ Init_Thread(void)
sym_never = ID2SYM(rb_intern("never"));
sym_immediate = ID2SYM(rb_intern("immediate"));
sym_on_blocking = ID2SYM(rb_intern("on_blocking"));
- id_locals = rb_intern("locals");
rb_define_singleton_method(rb_cThread, "new", thread_s_new, -1);
rb_define_singleton_method(rb_cThread, "start", thread_start, -2);
diff --git a/vm.c b/vm.c
index 5cc60748e0..70f96025ba 100644
--- a/vm.c
+++ b/vm.c
@@ -2727,7 +2727,6 @@ ruby_thread_init(VALUE self)
th->vm = vm;
th_init(th, self);
- rb_ivar_set(self, rb_intern("locals"), rb_hash_new());
th->top_wrapper = 0;
th->top_self = rb_vm_top_self();
@@ -3235,7 +3234,6 @@ Init_VM(void)
/* create main thread */
th_self = th->self = TypedData_Wrap_Struct(rb_cThread, &thread_data_type, th);
- rb_iv_set(th_self, "locals", rb_hash_new());
vm->main_thread = th;
vm->running_thread = th;
th->vm = vm;