aboutsummaryrefslogtreecommitdiffstats
path: root/thread_pthread.c
diff options
context:
space:
mode:
authorKoichi Sasada <ko1@atdot.net>2020-10-19 16:47:32 +0900
committerKoichi Sasada <ko1@atdot.net>2020-10-20 01:05:06 +0900
commit319afed20fba8f9b44611d16e4930260f7b56b86 (patch)
tree39d24da0464a39a2bfbc93f4ab5849945bc6f652 /thread_pthread.c
parent3f97940252a37db6e601b4bb1aa1e87204f769df (diff)
downloadruby-319afed20fba8f9b44611d16e4930260f7b56b86.tar.gz
Use language TLS specifier if it is possible.
To access TLS, it is faster to use language TLS specifier instead of using pthread_get/setspecific functions. Original proposal is: Use native thread locals. #3665
Diffstat (limited to 'thread_pthread.c')
-rw-r--r--thread_pthread.c16
1 files changed, 16 insertions, 0 deletions
diff --git a/thread_pthread.c b/thread_pthread.c
index 427897cfd8..71667aec69 100644
--- a/thread_pthread.c
+++ b/thread_pthread.c
@@ -550,7 +550,11 @@ native_cond_timeout(rb_nativethread_cond_t *cond, const rb_hrtime_t rel)
#define native_cleanup_push pthread_cleanup_push
#define native_cleanup_pop pthread_cleanup_pop
+#ifdef RB_THREAD_LOCAL_SPECIFIER
+static RB_THREAD_LOCAL_SPECIFIER rb_thread_t *ruby_native_thread;
+#else
static pthread_key_t ruby_native_thread_key;
+#endif
static void
null_func(int i)
@@ -561,7 +565,11 @@ null_func(int i)
static rb_thread_t *
ruby_thread_from_native(void)
{
+#ifdef RB_THREAD_LOCAL_SPECIFIER
+ return ruby_native_thread;
+#else
return pthread_getspecific(ruby_native_thread_key);
+#endif
}
static int
@@ -570,7 +578,12 @@ ruby_thread_set_native(rb_thread_t *th)
if (th && th->ec) {
rb_ractor_set_current_ec(th->ractor, th->ec);
}
+#ifdef RB_THREAD_LOCAL_SPECIFIER
+ ruby_native_thread = th;
+ return 1;
+#else
return pthread_setspecific(ruby_native_thread_key, th) == 0;
+#endif
}
static void native_thread_init(rb_thread_t *th);
@@ -587,12 +600,15 @@ Init_native_thread(rb_thread_t *th)
if (r) condattr_monotonic = NULL;
}
#endif
+
+#ifndef RB_THREAD_LOCAL_SPECIFIER
if (pthread_key_create(&ruby_native_thread_key, 0) == EAGAIN) {
rb_bug("pthread_key_create failed (ruby_native_thread_key)");
}
if (pthread_key_create(&ruby_current_ec_key, 0) == EAGAIN) {
rb_bug("pthread_key_create failed (ruby_current_ec_key)");
}
+#endif
th->thread_id = pthread_self();
ruby_thread_set_native(th);
fill_thread_id_str(th);