Skip to content

Commit c0494b0

Browse files
committed
Share thread-status-to-symbol logic
1 parent fa98a28 commit c0494b0

File tree

1 file changed

+26
-7
lines changed

1 file changed

+26
-7
lines changed

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

Lines changed: 26 additions & 7 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,18 +580,19 @@ 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 nameSymbol.getString();
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+
ThreadStatus previous = thread.status;
595596

596597
if (status == coreSymbols().RUN) {
597598
thread.status = ThreadStatus.RUN;
@@ -602,11 +603,10 @@ Object threadSetStatus(RubyThread thread, RubySymbol status) {
602603
} else if (status == coreSymbols().DEAD) {
603604
thread.status = ThreadStatus.DEAD;
604605
} else {
605-
throw CompilerDirectives.shouldNotReachHere("Unknown thread status: " + status);
606+
throw CompilerDirectives.shouldNotReachHere(status.toString());
606607
}
607608

608-
String currentName = StringUtils.toLowerCase(current.name());
609-
return getLanguage().getSymbol(currentName);
609+
return threadStatusToRubySymbol(previous, coreSymbols());
610610
}
611611
}
612612

@@ -1081,4 +1081,23 @@ Object eachCallerLocation(VirtualFrame frame, Nil block) {
10811081
throw new RaiseException(getContext(), coreExceptions().localJumpError("no block given", this));
10821082
}
10831083
}
1084+
1085+
private static RubySymbol threadStatusToRubySymbol(ThreadStatus status, CoreSymbols coreSymbols) {
1086+
final RubySymbol symbol;
1087+
1088+
if (status == ThreadStatus.RUN) {
1089+
symbol = coreSymbols.RUN;
1090+
} else if (status == ThreadStatus.SLEEP) {
1091+
symbol = coreSymbols.SLEEP;
1092+
} else if (status == ThreadStatus.ABORTING) {
1093+
symbol = coreSymbols.ABORTING;
1094+
} else if (status == ThreadStatus.DEAD) {
1095+
symbol = coreSymbols.DEAD;
1096+
} else {
1097+
throw CompilerDirectives.shouldNotReachHere(status.toString());
1098+
}
1099+
1100+
return symbol;
1101+
}
1102+
10841103
}

0 commit comments

Comments
 (0)