Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion ext/ffi_c/Function.c
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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"));
}
}

Expand Down
11 changes: 11 additions & 0 deletions spec/ffi/async_callback_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,15 @@ module LibTest
expect(called).to be true
expect(v).to eq(0x7fffffff)
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
9 changes: 9 additions & 0 deletions spec/ffi/function_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@ module LibTest
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
Expand Down