From 61d752caefb93b57eb27051ffb9e68cfdd882c51 Mon Sep 17 00:00:00 2001 From: Mike Blumtritt Date: Thu, 30 May 2024 21:58:29 +0200 Subject: [PATCH] Support Thread#native_thread_id only when available on platform --- lib/im-lost.rb | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/lib/im-lost.rb b/lib/im-lost.rb index d788ed6..f2125f4 100644 --- a/lib/im-lost.rb +++ b/lib/im-lost.rb @@ -392,16 +392,9 @@ def _instance_vars(object) end def _thread_identifier(thread) - if thread.native_thread_id - return( - "#{thread.status} Thread #{thread.native_thread_id} #{ - thread.name - }".rstrip - ) - end - "#{ - { false => 'terminated', nil => 'aborted' }[thread.status] - } Thread #{thread.native_thread_id} #{thread.name}".rstrip + "#{THREAD_STATE[thread.status] || thread.status} Thread #{ + @thread_id[thread] + } #{thread.name}".rstrip end def _print_vars(kind, names) @@ -547,7 +540,11 @@ def initialize(&block) ARG_SIG = { rest: '*', keyrest: '**', block: '&' }.compare_by_identity.freeze NO_NAME = { :* => 1, :** => 1, :& => 1 }.compare_by_identity.freeze - private_constant :ARG_SIG, :NO_NAME + THREAD_STATE = { + false => 'terminated', + nil => 'aborted' + }.compare_by_identity.freeze + private_constant :ARG_SIG, :NO_NAME, :THREAD_STATE @trace = {}.compare_by_identity @caller_locations = true @@ -621,6 +618,12 @@ def initialize(&block) end @fiber_supported = !!(defined?(Fiber.current) && defined?(Fiber.storage)) + @thread_id = + if defined?(Thread.current.native_thread_id) + lambda(&:native_thread_id) + else + lambda(&:__id__) + end self.trace_calls = true end