Skip to content

Commit 1e805ee

Browse files
committed
Support blocking FFI calls
1 parent ebe660f commit 1e805ee

File tree

3 files changed

+16
-3
lines changed

3 files changed

+16
-3
lines changed

lib/truffle/truffle/ffi_backend/function.rb

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ def call(*args, &block)
6565
param_types = @function_info.param_types
6666
return_type = @function_info.return_type
6767
enums = @function_info.enums
68+
blocking = @function_info.blocking
6869

6970
converted_args = args.dup
7071
if block
@@ -80,7 +81,18 @@ def call(*args, &block)
8081
converted_args[i] = convert_ruby_to_native(param_types[i], arg, enums)
8182
end
8283

83-
result = @function.call(*converted_args)
84+
if blocking
85+
result = Truffle.invoke_primitive :thread_run_blocking_nfi_system_call, -> {
86+
r = @function.call(*converted_args)
87+
if Integer === r and r == -1 and Errno.errno == Errno::EINTR::Errno
88+
undefined # retry
89+
else
90+
r
91+
end
92+
}
93+
else
94+
result = @function.call(*converted_args)
95+
end
8496

8597
convert_native_to_ruby(return_type, result)
8698
end

lib/truffle/truffle/ffi_backend/type.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ def get_at(pointer, offset)
195195
FFI::Type::Struct = StructByValue
196196

197197
class FunctionType < Type
198-
attr_reader :return_type, :param_types, :enums
198+
attr_reader :return_type, :param_types, :enums, :blocking
199199

200200
def initialize(return_type, param_types, options = {})
201201
super(FFI::Type::POINTER.size, FFI::Type::POINTER.alignment, FFI::Type::POINTER.nfi_type)
@@ -211,6 +211,7 @@ def initialize(return_type, param_types, options = {})
211211
end
212212
@param_types = param_types
213213
@enums = options[:enums]
214+
@blocking = options[:blocking]
214215

215216
if varargs
216217
@signature = nfi_varags_signature(@return_type, varargs, @param_types)

src/main/ruby/truffleruby/core/posix.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ def self.attach_function_eagerly(native_name, argument_types, return_type,
134134
result = Truffle.invoke_primitive :thread_run_blocking_nfi_system_call, -> {
135135
r = bound_func.call(*args)
136136
if Integer === r and r == -1 and Errno.errno == EINTR
137-
undefined
137+
undefined # retry
138138
else
139139
r
140140
end

0 commit comments

Comments
 (0)