Skip to content

Commit a089f07

Browse files
committed
[GR-45490] Follow-up for "Fix IO#wait specs transient failure"
PullRequest: truffleruby/4212
2 parents 1d1556d + 8b51827 commit a089f07

File tree

4 files changed

+48
-15
lines changed

4 files changed

+48
-15
lines changed

src/main/java/org/truffleruby/core/symbol/CoreSymbols.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ public final class CoreSymbols {
4747
public final RubySymbol DECONSTRUCT = createRubySymbol("deconstruct");
4848
public final RubySymbol DECONSTRUCT_KEYS = createRubySymbol("deconstruct_keys");
4949

50+
public final RubySymbol RUN = createRubySymbol("run");
51+
public final RubySymbol SLEEP = createRubySymbol("sleep");
52+
public final RubySymbol ABORTING = createRubySymbol("aborting");
53+
public final RubySymbol DEAD = createRubySymbol("dead");
54+
5055
// Added to workaround liquid's no symbols leaked test (SecurityTest#test_does_not_permanently_add_filters_to_symbol_table)
5156
public final RubySymbol IMMEDIATE_SWEEP = createRubySymbol("immediate_sweep");
5257
public final RubySymbol IMMEDIATE_MARK = createRubySymbol("immediate_mark");

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

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,8 @@
8282
import org.truffleruby.core.proc.ProcOperations;
8383
import org.truffleruby.core.proc.RubyProc;
8484
import org.truffleruby.core.string.RubyString;
85-
import org.truffleruby.core.string.StringUtils;
8685
import org.truffleruby.core.support.RubyPRNGRandomizer;
86+
import org.truffleruby.core.symbol.CoreSymbols;
8787
import org.truffleruby.core.symbol.RubySymbol;
8888
import org.truffleruby.core.thread.ThreadManager.BlockingCallInterruptible;
8989
import org.truffleruby.interop.ForeignToRubyNode;
@@ -580,35 +580,34 @@ Object status(RubyThread self) {
580580
return false;
581581
}
582582
}
583-
return createString(fromJavaStringNode, StringUtils.toLowerCase(status.name()), Encodings.US_ASCII); // CR_7BIT
583+
584+
RubySymbol nameSymbol = threadStatusToRubySymbol(status, coreSymbols());
585+
return createString(nameSymbol.tstring, nameSymbol.encoding);
584586
}
585587

586588
}
587589

588590
@Primitive(name = "thread_set_status")
589591
public abstract static class ThreadSetStatusPrimitiveNode extends PrimitiveArrayArgumentsNode {
590592

591-
@TruffleBoundary
592593
@Specialization
593594
Object threadSetStatus(RubyThread thread, RubySymbol status) {
594-
ThreadStatus current = thread.status;
595-
String newName = status.getString();
595+
ThreadStatus previous = thread.status;
596596

597-
if (newName.equals("run")) {
597+
if (status == coreSymbols().RUN) {
598598
thread.status = ThreadStatus.RUN;
599-
} else if (newName.equals("sleep")) {
599+
} else if (status == coreSymbols().SLEEP) {
600600
thread.status = ThreadStatus.SLEEP;
601-
} else if (newName.equals("aborting")) {
601+
} else if (status == coreSymbols().ABORTING) {
602602
thread.status = ThreadStatus.ABORTING;
603-
} else if (newName.equals("dead")) {
603+
} else if (status == coreSymbols().DEAD) {
604604
thread.status = ThreadStatus.DEAD;
605605
} else {
606-
throw new RaiseException(getContext(),
607-
coreExceptions().argumentError("Unknown thread status: " + newName, this));
606+
CompilerDirectives.transferToInterpreterAndInvalidate();
607+
throw CompilerDirectives.shouldNotReachHere("Unknown thread status: " + status.getString());
608608
}
609609

610-
String currentName = StringUtils.toLowerCase(current.name());
611-
return getLanguage().getSymbol(currentName);
610+
return threadStatusToRubySymbol(previous, coreSymbols());
612611
}
613612
}
614613

@@ -1083,4 +1082,24 @@ Object eachCallerLocation(VirtualFrame frame, Nil block) {
10831082
throw new RaiseException(getContext(), coreExceptions().localJumpError("no block given", this));
10841083
}
10851084
}
1085+
1086+
private static RubySymbol threadStatusToRubySymbol(ThreadStatus status, CoreSymbols coreSymbols) {
1087+
final RubySymbol symbol;
1088+
1089+
if (status == ThreadStatus.RUN) {
1090+
symbol = coreSymbols.RUN;
1091+
} else if (status == ThreadStatus.SLEEP) {
1092+
symbol = coreSymbols.SLEEP;
1093+
} else if (status == ThreadStatus.ABORTING) {
1094+
symbol = coreSymbols.ABORTING;
1095+
} else if (status == ThreadStatus.DEAD) {
1096+
symbol = coreSymbols.DEAD;
1097+
} else {
1098+
CompilerDirectives.transferToInterpreterAndInvalidate();
1099+
throw CompilerDirectives.shouldNotReachHere("Unknown thread status: " + status);
1100+
}
1101+
1102+
return symbol;
1103+
}
1104+
10861105
}

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -263,8 +263,12 @@ def self.poll(io, event_mask, timeout)
263263
# Don't mark Truffle::POSIX.truffleposix_poll_single_fd as blocking because
264264
# then it would automatically retry on EINTR without considering/adjusting the timeout.
265265
status = Primitive.thread_set_status(Thread.current, :sleep)
266-
returned_events = Truffle::POSIX.truffleposix_poll_single_fd(Primitive.io_fd(io), event_mask, remaining_timeout)
267-
Primitive.thread_set_status(Thread.current, status)
266+
267+
begin
268+
returned_events = Truffle::POSIX.truffleposix_poll_single_fd(Primitive.io_fd(io), event_mask, remaining_timeout)
269+
ensure
270+
Primitive.thread_set_status(Thread.current, status)
271+
end
268272

269273
result =
270274
if returned_events < 0

tool/generate-core-symbols.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,11 @@
6666
public final RubySymbol DECONSTRUCT = createRubySymbol("deconstruct");
6767
public final RubySymbol DECONSTRUCT_KEYS = createRubySymbol("deconstruct_keys");
6868
69+
public final RubySymbol RUN = createRubySymbol("run");
70+
public final RubySymbol SLEEP = createRubySymbol("sleep");
71+
public final RubySymbol ABORTING = createRubySymbol("aborting");
72+
public final RubySymbol DEAD = createRubySymbol("dead");
73+
6974
// Added to workaround liquid's no symbols leaked test (SecurityTest#test_does_not_permanently_add_filters_to_symbol_table)
7075
public final RubySymbol IMMEDIATE_SWEEP = createRubySymbol("immediate_sweep");
7176
public final RubySymbol IMMEDIATE_MARK = createRubySymbol("immediate_mark");

0 commit comments

Comments
 (0)