Skip to content

Commit daa9e6e

Browse files
committed
Always use Thread.tid for JavaMonitor owners.
1 parent 198589e commit daa9e6e

File tree

4 files changed

+33
-15
lines changed

4 files changed

+33
-15
lines changed

substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/monitor/JavaMonitor.java

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
import com.oracle.svm.core.jfr.SubstrateJVM;
3535
import com.oracle.svm.core.jfr.events.JavaMonitorEnterEvent;
3636
import com.oracle.svm.core.thread.JavaThreads;
37-
import com.oracle.svm.core.thread.VirtualThreads;
3837
import com.oracle.svm.core.util.VMError;
3938

4039
import jdk.internal.misc.Unsafe;
@@ -214,19 +213,10 @@ boolean isLocked() {
214213
*/
215214
@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
216215
protected static long getCurrentThreadIdentity() {
217-
if (VirtualThreads.isSupported()) {
218-
return JavaThreads.getThreadId(Thread.currentThread());
219-
} else {
220-
return CurrentIsolate.getCurrentThread().rawValue();
221-
}
216+
return JavaThreads.getCurrentThreadId();
222217
}
223218

224-
protected static long getThreadIdentity(IsolateThread isolateThread, Thread thread) {
225-
if (VirtualThreads.isSupported()) {
226-
return JavaThreads.getThreadId(thread);
227-
} else {
228-
VMError.guarantee(isolateThread.isNonNull());
229-
return isolateThread.rawValue();
230-
}
219+
protected static long getThreadIdentity(Thread thread) {
220+
return JavaThreads.getThreadId(thread);
231221
}
232222
}

substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/thread/JavaThreads.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,10 @@
3030
import java.util.concurrent.atomic.AtomicInteger;
3131
import java.util.concurrent.atomic.AtomicLong;
3232

33+
import org.graalvm.compiler.api.directives.GraalDirectives;
3334
import org.graalvm.compiler.api.replacements.Fold;
3435
import org.graalvm.compiler.core.common.SuppressFBWarnings;
36+
import org.graalvm.compiler.replacements.ReplacementsUtil;
3537
import org.graalvm.compiler.serviceprovider.JavaVersionUtil;
3638
import org.graalvm.nativeimage.IsolateThread;
3739
import org.graalvm.word.Pointer;
@@ -383,4 +385,19 @@ public static void blockedOn(Target_sun_nio_ch_Interruptible b) {
383385
PlatformThreads.blockedOn(b);
384386
}
385387
}
388+
389+
/**
390+
* Returns the result of calling {@link #getThreadId} on {@link Thread#currentThread}, but from
391+
* a thread-local cache with potentially fewer accesses.
392+
*/
393+
@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
394+
public static long getCurrentThreadId() {
395+
long id = PlatformThreads.currentVThreadId.get();
396+
if (GraalDirectives.inIntrinsic()) {
397+
ReplacementsUtil.dynamicAssert(id == getThreadId(Thread.currentThread()), "ids must match");
398+
} else {
399+
assert id == getThreadId(Thread.currentThread());
400+
}
401+
return id;
402+
}
386403
}

substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/thread/PlatformThreads.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@
9595
import com.oracle.svm.core.thread.VMThreads.StatusSupport;
9696
import com.oracle.svm.core.threadlocal.FastThreadLocal;
9797
import com.oracle.svm.core.threadlocal.FastThreadLocalFactory;
98+
import com.oracle.svm.core.threadlocal.FastThreadLocalLong;
9899
import com.oracle.svm.core.threadlocal.FastThreadLocalObject;
99100
import com.oracle.svm.core.util.TimeUtils;
100101
import com.oracle.svm.core.util.VMError;
@@ -124,6 +125,13 @@ public static PlatformThreads singleton() {
124125
/** The platform {@link java.lang.Thread} for the {@link IsolateThread}. */
125126
static final FastThreadLocalObject<Thread> currentThread = FastThreadLocalFactory.createObject(Thread.class, "PlatformThreads.currentThread").setMaxOffset(FastThreadLocal.BYTE_OFFSET);
126127

128+
/**
129+
* The {@linkplain JavaThreads#getThreadId thread id} of the {@link Thread#currentThread()},
130+
* which can be a {@linkplain Target_java_lang_Thread#vthread virtual thread} or the
131+
* {@linkplain #currentThread platform thread itself}.
132+
*/
133+
static final FastThreadLocalLong currentVThreadId = FastThreadLocalFactory.createLong("PlatformThreads.currentVThreadId").setMaxOffset(FastThreadLocal.BYTE_OFFSET);
134+
127135
/**
128136
* A thread-local helper object for locking. Use only if each {@link Thread} corresponds to an
129137
* {@link IsolateThread}, otherwise use {@link Target_java_lang_Thread#lockHelper}.
@@ -453,7 +461,7 @@ public static boolean ensureCurrentAssigned(String name, ThreadGroup group, bool
453461
* {@link #ensureCurrentAssigned(String, ThreadGroup, boolean)}. It is false when the thread is
454462
* started via {@link #doStartThread} and {@link #threadStartRoutine}.
455463
*/
456-
public static void assignCurrent(Thread thread, boolean manuallyStarted) {
464+
static void assignCurrent(Thread thread, boolean manuallyStarted) {
457465
/*
458466
* First of all, ensure we are in RUNNABLE state. If !manuallyStarted, we race with the
459467
* thread that launched us to set the status and we could still be in status NEW.
@@ -479,17 +487,20 @@ public static void assignCurrent(Thread thread, boolean manuallyStarted) {
479487
@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
480488
private static void assignCurrent0(Thread thread) {
481489
VMError.guarantee(currentThread.get() == null, "overwriting existing java.lang.Thread");
490+
currentVThreadId.set(JavaThreads.getThreadId(thread));
482491
currentThread.set(thread);
483492

484493
assert toTarget(thread).isolateThread.isNull();
485494
toTarget(thread).isolateThread = CurrentIsolate.getCurrentThread();
486495
ThreadListenerSupport.get().beforeThreadStart(CurrentIsolate.getCurrentThread(), thread);
487496
}
488497

498+
@Uninterruptible(reason = "Ensure consistency of vthread and cached vthread id.")
489499
static void setCurrentThread(Thread carrier, Thread thread) {
490500
assert carrier == currentThread.get();
491501
assert thread == carrier || (VirtualThreads.isSupported() && VirtualThreads.singleton().isVirtual(thread));
492502
toTarget(carrier).vthread = (thread != carrier) ? thread : null;
503+
currentVThreadId.set(JavaThreads.getThreadId(thread));
493504
}
494505

495506
@Uninterruptible(reason = "Called during isolate initialization")

substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/thread/Target_java_lang_Thread.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ static Thread currentCarrierThread() {
301301
return thread;
302302
}
303303

304-
/** On HotSpot, a field of C++ class {@code JavaThread}. Loads and stores are unordered. */
304+
/** On HotSpot, a field in C++ class {@code JavaThread}. Loads and stores are unordered. */
305305
@Inject @TargetElement(onlyWith = ContinuationsSupported.class)//
306306
@RecomputeFieldValue(kind = RecomputeFieldValue.Kind.Reset)//
307307
Thread vthread = null;

0 commit comments

Comments
 (0)