summaryrefslogtreecommitdiff
path: root/thread_pthread.c
diff options
context:
space:
mode:
authorNARUSE, Yui <naruse@airemix.jp>2021-05-22 21:36:27 +0900
committerNARUSE, Yui <naruse@airemix.jp>2021-05-26 15:14:11 +0900
commit46655156dcc37509dcb69fcd0717c110eb1c624a (patch)
treee21a75ebde64d8e7f05627fc990b62c47c8099c6 /thread_pthread.c
parent88e3848fca69915a24657bcc26da1a65b659c6f3 (diff)
downloadruby-46655156dcc37509dcb69fcd0717c110eb1c624a.tar.gz
Add Thread#native_thread_id [Feature #17853]
Diffstat (limited to 'thread_pthread.c')
-rw-r--r--thread_pthread.c37
1 files changed, 37 insertions, 0 deletions
diff --git a/thread_pthread.c b/thread_pthread.c
index 3e0c78b256..87db59e638 100644
--- a/thread_pthread.c
+++ b/thread_pthread.c
@@ -34,6 +34,9 @@
#if defined(__HAIKU__)
#include <kernel/OS.h>
#endif
+#ifdef __linux__
+#include <sys/syscall.h> /* for SYS_gettid */
+#endif
#include <time.h>
#include <signal.h>
@@ -659,11 +662,26 @@ Init_native_thread(rb_thread_t *th)
posix_signal(SIGVTALRM, null_func);
}
+#ifdef RB_THREAD_T_HAS_NATIVE_ID
+static int
+get_native_thread_id(void)
+{
+#ifdef __linux__
+ return (int)syscall(SYS_gettid);
+#elif defined(__FreeBSD__)
+ return pthread_getthreadid_np();
+#endif
+}
+#endif
+
static void
native_thread_init(rb_thread_t *th)
{
native_thread_data_t *nd = &th->native_thread_data;
+#ifdef RB_THREAD_T_HAS_NATIVE_ID
+ th->tid = get_native_thread_id();
+#endif
#ifdef USE_UBF_LIST
list_node_init(&nd->node.ubf);
#endif
@@ -1708,6 +1726,25 @@ native_set_another_thread_name(rb_nativethread_id_t thread_id, VALUE name)
#endif
}
+static VALUE
+native_thread_native_thread_id(rb_thread_t *target_th)
+{
+#if !defined(RB_THREAD_T_HAS_NATIVE_ID) && !defined(__APPLE__)
+ rb_notimplement();
+#endif
+
+#ifdef RB_THREAD_T_HAS_NATIVE_ID
+ int tid = target_th->tid;
+ if (tid == 0) return Qnil;
+ return INT2FIX(tid);
+#elif defined(__APPLE__)
+ uint64_t tid;
+ int e = pthread_threadid_np(target_th->thread_id, &tid);
+ if (e != 0) rb_syserr_fail(e, "pthread_threadid_np");
+ return ULL2NUM((unsigned long long)tid);
+#endif
+}
+
static void
ubf_timer_invalidate(void)
{