summaryrefslogtreecommitdiff
path: root/spec/ffi/function_spec.rb
diff options
context:
space:
mode:
authorLars Kanis <lars@greiz-reinsdorf.de>2023-04-16 20:40:17 +0200
committerLars Kanis <lars@greiz-reinsdorf.de>2023-04-18 11:27:14 +0200
commit5247d3e736f77ce19bbb3e69cf5186fa5a7084f4 (patch)
tree9675fed8212b9ca4ffbe0929112be10bdd1c5919 /spec/ffi/function_spec.rb
parent250c31a25d81339cfe928a433ada3c0f17eae580 (diff)
downloadffi-5247d3e736f77ce19bbb3e69cf5186fa5a7084f4.tar.gz
Add support for using FFI in Ractor
All objects are shareable now when frozen. All objects can be created in a non-main Ractor. Typedefs are a global mutable state and are not accessable from Ractor other than the main Ractor. So all Function, Struct, etc. must be defined in the main Ractor and can then be used in other Ractors.
Diffstat (limited to 'spec/ffi/function_spec.rb')
-rw-r--r--spec/ffi/function_spec.rb24
1 files changed, 24 insertions, 0 deletions
diff --git a/spec/ffi/function_spec.rb b/spec/ffi/function_spec.rb
index 7f90ea3..0d89a7d 100644
--- a/spec/ffi/function_spec.rb
+++ b/spec/ffi/function_spec.rb
@@ -48,6 +48,30 @@ describe FFI::Function do
expect(LibTest.testFunctionAdd(10, 10, function_add)).to eq(20)
end
+ def adder(a, b)
+ a + b
+ end
+
+ it "should be shareable for Ractor", :ractor do
+ add = FFI::Function.new(:int, [:int, :int], &method(:adder))
+ Ractor.make_shareable(add)
+
+ res = Ractor.new(add) do |add2|
+ LibTest.testFunctionAdd(10, 10, add2)
+ end.take
+
+ expect( res ).to eq(20)
+ end
+
+ it "should be usable with Ractor", :ractor do
+ res = Ractor.new(@conninfo) do |conninfo|
+ function_add = FFI::Function.new(:int, [:int, :int]) { |a, b| a + b }
+ LibTest.testFunctionAdd(10, 10, function_add)
+ end.take
+
+ expect( res ).to eq(20)
+ end
+
it 'can be used to wrap an existing function pointer' do
expect(FFI::Function.new(:int, [:int, :int], @libtest.find_function('testAdd')).call(10, 10)).to eq(20)
end