@@ -3402,6 +3402,36 @@ rb_thread_setname(VALUE thread, VALUE name)
34023402 return name ;
34033403}
34043404
3405+ /*
3406+ * call-seq:
3407+ * thr.native_thread_id -> integer
3408+ *
3409+ * Return the native thread ID which is used by the Ruby thread.
3410+ *
3411+ * The ID depends on the OS. (not POSIX thread ID returned by pthread_self(3))
3412+ * * On Linux it is TID returned by gettid(2).
3413+ * * On macOS it is the system-wide unique integral ID of thread returned
3414+ * by pthread_threadid_np(3).
3415+ * * On FreeBSD it is the unique integral ID of the thread returned by
3416+ * pthread_getthreadid_np(3).
3417+ * * On Windows it is the thread identifier returned by GetThreadId().
3418+ * * On other platforms, it raises NotImplementedError.
3419+ *
3420+ * NOTE:
3421+ * If the thread is not associated yet or already deassociated with a native
3422+ * thread, it returns _nil_.
3423+ * If the Ruby implementation uses M:N thread model, the ID may change
3424+ * depending on the timing.
3425+ */
3426+
3427+ static VALUE
3428+ rb_thread_native_thread_id (VALUE thread )
3429+ {
3430+ rb_thread_t * target_th = rb_thread_ptr (thread );
3431+ if (rb_threadptr_dead (target_th )) return Qnil ;
3432+ return native_thread_native_thread_id (target_th );
3433+ }
3434+
34053435/*
34063436 * call-seq:
34073437 * thr.to_s -> string
@@ -5494,6 +5524,7 @@ Init_Thread(void)
54945524
54955525 rb_define_method (rb_cThread , "name" , rb_thread_getname , 0 );
54965526 rb_define_method (rb_cThread , "name=" , rb_thread_setname , 1 );
5527+ rb_define_method (rb_cThread , "native_thread_id" , rb_thread_native_thread_id , 0 );
54975528 rb_define_method (rb_cThread , "to_s" , rb_thread_to_s , 0 );
54985529 rb_define_alias (rb_cThread , "inspect" , "to_s" );
54995530
0 commit comments