diff options
author | Lars Kanis <lars@greiz-reinsdorf.de> | 2021-02-28 19:33:12 +0100 |
---|---|---|
committer | Lars Kanis <lars@greiz-reinsdorf.de> | 2021-02-28 19:33:12 +0100 |
commit | 1cce06db0b774ee46ef98ea9ca6ceec5d21694a4 (patch) | |
tree | 2d659f4dccf889247df366c4e4502f4275aee3c4 | |
parent | c48dfe79c772074d6afc9dfa85fbf8f58a27b860 (diff) | |
parent | 7099d9594667297baa8fd3a23a5f610649b4a7fc (diff) | |
download | ffi-1cce06db0b774ee46ef98ea9ca6ceec5d21694a4.tar.gz |
Merge branch 'name-dispatcher-thread' of https://github.com/DataDog/ffi into DataDog-name-dispatcher-thread
-rw-r--r-- | ext/ffi_c/Function.c | 6 | ||||
-rw-r--r-- | spec/ffi/async_callback_spec.rb | 11 | ||||
-rw-r--r-- | spec/ffi/function_spec.rb | 9 |
3 files changed, 25 insertions, 1 deletions
diff --git a/ext/ffi_c/Function.c b/ext/ffi_c/Function.c index 671caee..30478d9 100644 --- a/ext/ffi_c/Function.c +++ b/ext/ffi_c/Function.c @@ -310,6 +310,8 @@ function_init(VALUE self, VALUE rbFunctionInfo, VALUE rbProc) #if defined(DEFER_ASYNC_CALLBACK) if (async_cb_thread == Qnil) { async_cb_thread = rb_thread_create(async_cb_event, NULL); + /* Name thread, for better debugging */ + rb_funcall(async_cb_thread, rb_intern("name="), 1, rb_str_new2("FFI::Function Callback Dispatcher")); } #endif @@ -525,7 +527,9 @@ async_cb_event(void* unused) rb_thread_call_without_gvl(async_cb_wait, &w, async_cb_stop, &w); if (w.cb != NULL) { /* Start up a new ruby thread to run the ruby callback */ - rb_thread_create(async_cb_call, w.cb); + VALUE new_thread = rb_thread_create(async_cb_call, w.cb); + /* Name thread, for better debugging */ + rb_funcall(new_thread, rb_intern("name="), 1, rb_str_new2("FFI::Function Callback Runner")); } } diff --git a/spec/ffi/async_callback_spec.rb b/spec/ffi/async_callback_spec.rb index 7783a8a..d770355 100644 --- a/spec/ffi/async_callback_spec.rb +++ b/spec/ffi/async_callback_spec.rb @@ -39,4 +39,15 @@ describe "async callback" do expect(th1).to_not eq(th2) expect(v).to eq(6) end + + it "sets the name of the thread that runs the callback" do + skip "not yet supported on TruffleRuby" if RUBY_ENGINE == "truffleruby" + skip "not yet supported on JRuby" if RUBY_ENGINE == "jruby" + + callback_runner_thread = nil + + LibTest.testAsyncCallback(proc { callback_runner_thread = Thread.current }, 0) + + expect(callback_runner_thread.name).to eq("FFI::Function Callback Runner") + end end diff --git a/spec/ffi/function_spec.rb b/spec/ffi/function_spec.rb index dd4d2ea..a7d6dc7 100644 --- a/spec/ffi/function_spec.rb +++ b/spec/ffi/function_spec.rb @@ -21,6 +21,15 @@ describe FFI::Function do expect(fn.call).to eql 5 end + context 'when called with a block' do + it 'creates a thread for dispatching callbacks and sets its name' do + skip 'this is MRI-specific' if RUBY_ENGINE == 'truffleruby' || RUBY_ENGINE == 'jruby' + FFI::Function.new(:int, []) { 5 } # Trigger initialization + + expect(Thread.list.map(&:name)).to include('FFI::Function Callback Dispatcher') + end + end + it 'raises an error when passing a wrong signature' do expect { FFI::Function.new([], :int).new { } }.to raise_error TypeError end |