From 9ca41e999159096cb0872b4babbb94bf5d1af4ce Mon Sep 17 00:00:00 2001 From: Jean Boussier Date: Thu, 9 Nov 2023 13:56:29 +0100 Subject: GVL Instrumentation: pass thread->self as part of event data Context: https://github.com/ivoanjo/gvl-tracing/pull/4 Some hooks may want to collect data on a per thread basis. Right now the only way to identify the concerned thread is to use `rb_nativethread_self()` or similar, but even then because of the thread cache or MaNy, two distinct Ruby threads may report the same native thread id. By passing `thread->self`, hooks can use it as a key to store the metadata. NB: Most hooks are executed outside the GVL, so such data collection need to use a thread-safe data-structure, and shouldn't use the reference in other ways from inside the hook. They must also either pin that value or handle compaction. --- test/-ext-/thread/test_instrumentation_api.rb | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'test/-ext-/thread') diff --git a/test/-ext-/thread/test_instrumentation_api.rb b/test/-ext-/thread/test_instrumentation_api.rb index dd620e7380..208d11de85 100644 --- a/test/-ext-/thread/test_instrumentation_api.rb +++ b/test/-ext-/thread/test_instrumentation_api.rb @@ -22,6 +22,7 @@ class TestThreadInstrumentation < Test::Unit::TestCase def teardown return if /mswin|mingw|bccwin/ =~ RUBY_PLATFORM Bug::ThreadInstrumentation::unregister_callback + Bug::ThreadInstrumentation.last_spawned_thread = nil end THREADS_COUNT = 3 @@ -68,6 +69,12 @@ class TestThreadInstrumentation < Test::Unit::TestCase assert Bug::ThreadInstrumentation::register_and_unregister_callbacks end + def test_thread_instrumentation_event_data + assert_nil Bug::ThreadInstrumentation.last_spawned_thread + thr = Thread.new{ }.join + assert_same thr, Bug::ThreadInstrumentation.last_spawned_thread + end + private def fib(n = 20) -- cgit v1.2.3