[前][次][番号順一覧][スレッド一覧]

ruby-changes:57868

From: Lourens <ko1@a...>
Date: Mon, 23 Sep 2019 02:15:04 +0900 (JST)
Subject: [ruby-changes:57868] cadfaacb25 (master): Lazy init thread local storage

https://git.ruby-lang.org/ruby.git/commit/?id=cadfaacb25

From cadfaacb2533d47d52dbb5dbefe724d7bf11112e Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Lourens=20Naud=C3=A9?= <lourens@b...>
Date: Sat, 20 Jul 2019 02:42:10 +0100
Subject: Lazy init thread local storage


diff --git a/defs/id.def b/defs/id.def
index 6bfce29..f0e1db9 100644
--- a/defs/id.def
+++ b/defs/id.def
@@ -46,6 +46,7 @@ firstline, predefined = __LINE__+1, %[\ https://github.com/ruby/ruby/blob/trunk/defs/id.def#L46
   call
   mesg
   exception
+  locals
   not                                                   NOT
   and                                                   AND
   or                                                    OR
diff --git a/thread.c b/thread.c
index 3b6ded9..91f07e8 100644
--- a/thread.c
+++ b/thread.c
@@ -97,13 +97,23 @@ static VALUE rb_cThreadShield; https://github.com/ruby/ruby/blob/trunk/thread.c#L97
 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) https://github.com/ruby/ruby/blob/trunk/thread.c#L3413
 {
     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) https://github.com/ruby/ruby/blob/trunk/thread.c#L3438
         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) https://github.com/ruby/ruby/blob/trunk/thread.c#L3541
     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) https://github.com/ruby/ruby/blob/trunk/thread.c#L3575
 
     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) https://github.com/ruby/ruby/blob/trunk/thread.c#L5156
     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 5cc6074..70f9602 100644
--- a/vm.c
+++ b/vm.c
@@ -2727,7 +2727,6 @@ ruby_thread_init(VALUE self) https://github.com/ruby/ruby/blob/trunk/vm.c#L2727
 
     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) https://github.com/ruby/ruby/blob/trunk/vm.c#L3234
 
 	/* 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;
-- 
cgit v0.10.2


--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]