Skip to content

Commit 3378c66

Browse files
committed
[GR-27687] Change Primitive.thread_run_blocking_nfi_system_call to take an interop isExecutable object
PullRequest: truffleruby/2341
2 parents 713a452 + d997f0b commit 3378c66

File tree

4 files changed

+27
-43
lines changed

4 files changed

+27
-43
lines changed

lib/truffle/truffle/ffi_backend/function.rb

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -82,14 +82,9 @@ def call(*args, &block)
8282
end
8383

8484
if blocking
85-
result = Primitive.thread_run_blocking_nfi_system_call -> {
86-
r = @function.call(*args)
87-
if Integer === r and r == -1 and Errno.errno == Errno::EINTR::Errno
88-
Truffle::UNDEFINED # retry
89-
else
90-
r
91-
end
92-
}
85+
begin
86+
result = Primitive.thread_run_blocking_nfi_system_call(@function, args)
87+
end while Integer === result and result == -1 and Errno.errno == Errno::EINTR::Errno
9388
else
9489
result = @function.call(*args)
9590
end

src/main/java/org/truffleruby/core/thread/ThreadNodes.java

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
import org.truffleruby.core.InterruptMode;
6161
import org.truffleruby.core.VMPrimitiveNodes.VMRaiseExceptionNode;
6262
import org.truffleruby.core.array.ArrayGuards;
63+
import org.truffleruby.core.array.ArrayToObjectArrayNode;
6364
import org.truffleruby.core.array.RubyArray;
6465
import org.truffleruby.core.array.library.ArrayStoreLibrary;
6566
import org.truffleruby.core.basicobject.RubyBasicObject;
@@ -105,7 +106,6 @@
105106
import com.oracle.truffle.api.nodes.Node;
106107
import com.oracle.truffle.api.object.Shape;
107108
import com.oracle.truffle.api.profiles.BranchProfile;
108-
import com.oracle.truffle.api.profiles.LoopConditionProfile;
109109
import com.oracle.truffle.api.source.SourceSection;
110110

111111
@CoreModule(value = "Thread", isClass = true)
@@ -872,36 +872,30 @@ protected RubyBasicObject getFiberLocals(RubyThread thread) {
872872

873873
/** Similar to {@link ThreadManager#runUntilResult(Node, ThreadManager.BlockingAction)} but purposed for blocking
874874
* native calls. If the {@link SafepointManager} needs to interrupt the thread, it will send a SIGVTALRM to abort
875-
* the blocking syscall and the action will return NotProvided if the syscall fails with errno=EINTR, meaning it was
876-
* interrupted. */
875+
* the blocking syscall and the syscall will return -1 with errno=EINTR, meaning it was interrupted. */
877876
@Primitive(name = "thread_run_blocking_nfi_system_call")
878877
public static abstract class ThreadRunBlockingSystemCallNode extends PrimitiveArrayArgumentsNode {
879878

880879
@Specialization
881-
protected Object runBlockingSystemCall(RubyProc block,
882-
@Cached("createCountingProfile()") LoopConditionProfile loopProfile,
883-
@Cached YieldNode yieldNode) {
880+
protected Object runBlockingSystemCall(Object executable, RubyArray argsArray,
881+
@Cached GetCurrentRubyThreadNode getCurrentRubyThreadNode,
882+
@CachedLibrary(limit = "1") InteropLibrary receivers,
883+
@Cached ArrayToObjectArrayNode arrayToObjectArrayNode,
884+
@Cached TranslateInteropExceptionNode translateInteropExceptionNode) {
885+
final Object[] args = arrayToObjectArrayNode.executeToObjectArray(argsArray);
886+
final RubyThread thread = getCurrentRubyThreadNode.execute();
887+
884888
final ThreadManager threadManager = getContext().getThreadManager();
885889
final UnblockingAction unblockingAction = threadManager.getNativeCallUnblockingAction();
886-
final RubyThread thread = threadManager.getCurrentThread();
887890
final UnblockingActionHolder actionHolder = threadManager.getActionHolder(Thread.currentThread());
888891

889892
final UnblockingAction oldAction = actionHolder.changeTo(unblockingAction);
893+
final ThreadStatus status = thread.status;
894+
thread.status = ThreadStatus.SLEEP;
890895
try {
891-
Object result;
892-
do {
893-
final ThreadStatus status = thread.status;
894-
thread.status = ThreadStatus.SLEEP;
895-
896-
try {
897-
result = yieldNode.executeDispatch(block);
898-
} finally {
899-
thread.status = status;
900-
}
901-
} while (loopProfile.profile(result == NotProvided.INSTANCE));
902-
903-
return result;
896+
return InteropNodes.execute(executable, args, receivers, translateInteropExceptionNode);
904897
} finally {
898+
thread.status = status;
905899
actionHolder.restore(oldAction);
906900
}
907901
}

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

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -139,14 +139,9 @@ def self.attach_function_eagerly(native_name, argument_types, return_type,
139139
end
140140

141141
if blocking
142-
result = Primitive.thread_run_blocking_nfi_system_call -> {
143-
r = bound_func.call(*args)
144-
if Integer === r and r == -1 and Errno.errno == EINTR
145-
undefined # retry
146-
else
147-
r
148-
end
149-
}
142+
begin
143+
result = Primitive.thread_run_blocking_nfi_system_call(bound_func, args)
144+
end while Integer === result and result == -1 and Errno.errno == EINTR
150145
else
151146
result = bound_func.call(*args)
152147
end
@@ -316,6 +311,7 @@ def self.unsetenv(name)
316311
attach_function :dup3, [:int, :int, :int], :int
317312
end
318313

314+
SELECT = method(:truffleposix_select)
319315

320316
def self.with_array_of_ints(ints)
321317
if ints.empty?
@@ -514,5 +510,4 @@ class << self
514510
end
515511
end
516512
end
517-
518513
end

src/main/ruby/truffleruby/core/truffle/io_operations.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -145,12 +145,12 @@ def self.select(readables, readable_ios, writables, writable_ios, errorables, er
145145
to_fds(errorable_ios, errorables_pointer)
146146

147147
begin
148-
primitive_result = Primitive.thread_run_blocking_nfi_system_call -> do
149-
Truffle::POSIX.truffleposix_select(readables.size, readables_pointer,
150-
writables.size, writables_pointer,
151-
errorables.size, errorables_pointer,
152-
remaining_timeout)
153-
end
148+
primitive_result = Primitive.thread_run_blocking_nfi_system_call(Truffle::POSIX::SELECT, [
149+
readables.size, readables_pointer,
150+
writables.size, writables_pointer,
151+
errorables.size, errorables_pointer,
152+
remaining_timeout
153+
])
154154

155155
result =
156156
if primitive_result < 0

0 commit comments

Comments
 (0)