9595import com .oracle .svm .core .thread .VMThreads .StatusSupport ;
9696import com .oracle .svm .core .threadlocal .FastThreadLocal ;
9797import com .oracle .svm .core .threadlocal .FastThreadLocalFactory ;
98+ import com .oracle .svm .core .threadlocal .FastThreadLocalLong ;
9899import com .oracle .svm .core .threadlocal .FastThreadLocalObject ;
99100import com .oracle .svm .core .util .TimeUtils ;
100101import 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" )
0 commit comments