Skip to content

Commit 7cf3aee

Browse files
committed
Deal with Thread#getThreadId() warnings on JDK19
1 parent 263eae5 commit 7cf3aee

File tree

4 files changed

+15
-8
lines changed

4 files changed

+15
-8
lines changed

src/main/java/org/truffleruby/RubyLanguage.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -575,7 +575,7 @@ protected boolean isThreadAccessAllowed(Thread thread, boolean singleThreaded) {
575575

576576
@Override
577577
public void initializeThread(RubyContext context, Thread thread) {
578-
LOGGER.fine(() -> "initializeThread(#" + thread.getId() + " " + thread + ")");
578+
LOGGER.fine(() -> "initializeThread(#" + getThreadId(thread) + " " + thread + ")");
579579

580580
if (thread == context.getThreadManager().getOrInitializeRootJavaThread()) {
581581
// Already initialized when creating the context
@@ -602,7 +602,7 @@ public void initializeThread(RubyContext context, Thread thread) {
602602

603603
@Override
604604
public void disposeThread(RubyContext context, Thread thread) {
605-
LOGGER.fine(() -> "disposeThread(#" + thread.getId() + " " + thread + " on " + getCurrentThread() + ")");
605+
LOGGER.fine(() -> "disposeThread(#" + getThreadId(thread) + " " + thread + " on " + getCurrentThread() + ")");
606606

607607
if (thread == context.getThreadManager().getRootJavaThread()) {
608608
if (context.getEnv().isPreInitialization()) {
@@ -865,4 +865,9 @@ private static String buildCoreLoadPath(String coreLoadPath) {
865865
throw CompilerDirectives.shouldNotReachHere(e);
866866
}
867867
}
868+
869+
@SuppressWarnings("deprecation") // deprecated on JDK19 by Thread#threadId, but that's added in JDK19
870+
public static long getThreadId(Thread thread) {
871+
return thread.getId();
872+
}
868873
}

src/main/java/org/truffleruby/core/fiber/FiberManager.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,7 @@ public String getFiberDebugInfo(RubyThread rubyThread) {
394394
if (thread == null) {
395395
builder.append(" (no Java thread)");
396396
} else {
397-
builder.append(" #").append(thread.getId()).append(' ').append(thread);
397+
builder.append(" #").append(RubyLanguage.getThreadId(thread)).append(' ').append(thread);
398398
}
399399

400400
if (fiber.isRootFiber()) {

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,8 @@ private Thread createFiberJavaThread(RubyFiber fiber, SourceSection sourceSectio
142142

143143
language.rubyThreadInitMap.put(thread, fiber.rubyThread);
144144
language.rubyFiberInitMap.put(thread, fiber);
145-
thread.setName(FiberManager.NAME_PREFIX + " id=" + thread.getId() + " from " + context.fileLine(sourceSection));
145+
thread.setName(FiberManager.NAME_PREFIX + " id=" + RubyLanguage.getThreadId(thread) + " from " +
146+
context.fileLine(sourceSection));
146147
thread.setDaemon(true); // GR-33255
147148
rubyManagedThreads.add(thread); // need to be set before initializeThread()
148149
thread.setUncaughtExceptionHandler(uncaughtExceptionHandler(fiber));
@@ -164,7 +165,7 @@ private Thread createJavaThread(Runnable runnable, RubyThread rubyThread, String
164165

165166
language.rubyThreadInitMap.put(thread, rubyThread);
166167
language.rubyFiberInitMap.put(thread, rubyThread.getRootFiber());
167-
thread.setName(NAME_PREFIX + " id=" + thread.getId() + " from " + info);
168+
thread.setName(NAME_PREFIX + " id=" + RubyLanguage.getThreadId(thread) + " from " + info);
168169
rubyManagedThreads.add(thread); // need to be set before initializeThread()
169170
thread.setUncaughtExceptionHandler(uncaughtExceptionHandler(rubyThread.getRootFiber()));
170171
return thread;

src/main/java/org/truffleruby/language/threadlocal/ThreadAndFrameLocalStorage.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
package org.truffleruby.language.threadlocal;
1111

1212
import org.truffleruby.RubyContext;
13+
import org.truffleruby.RubyLanguage;
1314
import org.truffleruby.language.Nil;
1415

1516
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
@@ -26,12 +27,12 @@ public class ThreadAndFrameLocalStorage {
2627

2728
public ThreadAndFrameLocalStorage(RubyContext context) {
2829
// Cannot store a Thread id while pre-initializing
29-
originalThreadId = context.isPreInitializing() ? 0 : Thread.currentThread().getId();
30+
originalThreadId = context.isPreInitializing() ? 0 : RubyLanguage.getThreadId(Thread.currentThread());
3031
originalThreadValue = Nil.INSTANCE;
3132
}
3233

3334
public Object get(ConditionProfile sameThreadProfile) {
34-
if (sameThreadProfile.profile(Thread.currentThread().getId() == originalThreadId)) {
35+
if (sameThreadProfile.profile(RubyLanguage.getThreadId(Thread.currentThread()) == originalThreadId)) {
3536
return originalThreadValue;
3637
} else {
3738
return fallbackGet();
@@ -60,7 +61,7 @@ private Object fallbackGet() {
6061
}
6162

6263
public void set(Object value, ConditionProfile sameThreadProfile) {
63-
if (sameThreadProfile.profile(Thread.currentThread().getId() == originalThreadId)) {
64+
if (sameThreadProfile.profile(RubyLanguage.getThreadId(Thread.currentThread()) == originalThreadId)) {
6465
originalThreadValue = value;
6566
} else {
6667
fallbackSet(value);

0 commit comments

Comments
 (0)