2626
2727import static com .oracle .svm .core .SubstrateOptions .MultiThreaded ;
2828import static com .oracle .svm .core .snippets .KnownIntrinsics .readCallerStackPointer ;
29- import static java .util .concurrent .TimeUnit .MILLISECONDS ;
3029
3130import java .lang .Thread .UncaughtExceptionHandler ;
3231import java .util .ArrayList ;
5857import com .oracle .svm .core .SubstrateDiagnostics ;
5958import com .oracle .svm .core .SubstrateOptions ;
6059import com .oracle .svm .core .SubstrateUtil ;
60+ import com .oracle .svm .core .annotate .AlwaysInline ;
6161import com .oracle .svm .core .annotate .NeverInline ;
6262import com .oracle .svm .core .annotate .Uninterruptible ;
6363import com .oracle .svm .core .heap .Heap ;
@@ -192,8 +192,8 @@ static boolean isInterrupted(Thread thread) {
192192 }
193193
194194 static boolean getAndClearInterrupt (Thread thread ) {
195- if (JavaContinuations . isSupported () && thread instanceof VirtualThread ) {
196- return (( VirtualThread ) thread ) .getAndClearInterrupt ();
195+ if (isVirtual ( thread ) ) {
196+ return VirtualThreads . get () .getAndClearInterrupt (thread );
197197 }
198198
199199 /*
@@ -207,7 +207,7 @@ static boolean getAndClearInterrupt(Thread thread) {
207207 }
208208
209209 static boolean platformGetAndUpdateInterrupt (Thread thread , boolean newValue ) {
210- assert !JavaContinuations . isSupported () || !( thread instanceof VirtualThread );
210+ assert !isVirtual ( thread );
211211 Target_java_lang_Thread tjlt = toTarget (thread );
212212 boolean oldValue ;
213213 if (JavaVersionUtil .JAVA_SPEC <= 11 ) {
@@ -262,11 +262,18 @@ public static Thread fromVMThread(IsolateThread thread) {
262262 return platformThread .get (thread );
263263 }
264264
265- public static boolean isVirtual (Thread thread ) {
266- if (!LoomSupport .isEnabled ()) {
265+ @ AlwaysInline ("Inline checks." )
266+ private static boolean isVirtual (Thread thread ) {
267+ return VirtualThreads .get ().isVirtual (thread );
268+ }
269+
270+ @ AlwaysInline ("Inline checks." )
271+ private static boolean isVirtualDisallowLoom (Thread thread ) {
272+ if (LoomSupport .isEnabled ()) {
273+ assert !isVirtual (thread ) : "should not see Loom virtual thread objects here" ;
267274 return false ;
268275 }
269- return toTarget ( thread ). isVirtual ();
276+ return isVirtual (thread );
270277 }
271278
272279 /**
@@ -306,11 +313,8 @@ static void join(Thread thread, long millis) throws InterruptedException {
306313 if (millis < 0 ) {
307314 throw new IllegalArgumentException ("timeout value is negative" );
308315 }
309- if (JavaContinuations .isSupported () && thread instanceof VirtualThread ) {
310- if (thread .isAlive ()) {
311- long nanos = MILLISECONDS .toNanos (millis );
312- ((VirtualThread ) thread ).joinNanos (nanos );
313- }
316+ if (isVirtual (thread )) {
317+ VirtualThreads .get ().join (thread , millis );
314318 return ;
315319 }
316320
@@ -696,8 +700,8 @@ protected void beforeThreadRun(@SuppressWarnings("unused") Thread thread) {
696700 protected abstract void platformYield ();
697701
698702 void yield () {
699- if (JavaContinuations . isSupported () && Thread .currentThread () instanceof VirtualThread ) {
700- (( VirtualThread ) Thread . currentThread ()). tryYield ();
703+ if (isVirtualDisallowLoom ( Thread .currentThread ()) ) {
704+ VirtualThreads . get (). yield ();
701705 } else {
702706 platformYield ();
703707 }
@@ -837,7 +841,7 @@ static void initializeNewThread(
837841 static void platformPark () {
838842 VMOperationControl .guaranteeOkayToBlock ("[JavaThreads.park(): Should not park when it is not okay to block.]" );
839843 final Thread thread = Thread .currentThread ();
840- assert !JavaContinuations . isSupported () || !( thread instanceof VirtualThread );
844+ assert !isVirtual ( thread );
841845 if (isInterrupted (thread )) { // avoid state changes and synchronization
842846 return ;
843847 }
@@ -861,7 +865,7 @@ static void platformPark() {
861865 static void platformPark (long delayNanos ) {
862866 VMOperationControl .guaranteeOkayToBlock ("[JavaThreads.park(long): Should not park when it is not okay to block.]" );
863867 final Thread thread = Thread .currentThread ();
864- assert !JavaContinuations . isSupported () || !( thread instanceof VirtualThread );
868+ assert !isVirtual ( thread );
865869 if (isInterrupted (thread )) { // avoid state changes and synchronization
866870 return ;
867871 }
@@ -887,7 +891,7 @@ static void platformPark(long delayNanos) {
887891 * @see #platformPark(long)
888892 */
889893 static void platformUnpark (Thread thread ) {
890- assert !JavaContinuations . isSupported () || !( thread instanceof VirtualThread );
894+ assert !isVirtual ( thread );
891895 ensureUnsafeParkEvent (thread ).unpark ();
892896 }
893897
@@ -897,9 +901,8 @@ private static ParkEvent ensureUnsafeParkEvent(Thread thread) {
897901 }
898902
899903 static void sleep (long millis ) throws InterruptedException {
900- if (JavaContinuations .isSupported () && Thread .currentThread () instanceof VirtualThread ) {
901- long nanos = TimeUnit .NANOSECONDS .convert (millis , TimeUnit .MILLISECONDS );
902- ((VirtualThread ) Thread .currentThread ()).sleepNanos (nanos );
904+ if (isVirtualDisallowLoom (Thread .currentThread ())) {
905+ VirtualThreads .get ().sleepMillis (millis );
903906 } else {
904907 platformSleep (millis );
905908 }
@@ -947,23 +950,22 @@ private static void platformSleep0(long delayNanos) {
947950 * @see #platformSleep(long)
948951 */
949952 static void platformInterrupt (Thread thread ) {
950- assert !JavaContinuations . isSupported () || !( thread instanceof VirtualThread );
953+ assert !isVirtual ( Thread . currentThread () );
951954 final ParkEvent sleepEvent = JavaThreads .getSleepParkEvent (thread ).get ();
952955 if (sleepEvent != null ) {
953956 sleepEvent .unpark ();
954957 }
955958 }
956959
957960 static boolean isAlive (Thread thread ) {
958- if (JavaContinuations .isSupported () && thread instanceof VirtualThread ) {
959- Thread .State state = thread .getState ();
960- return !(state == Thread .State .NEW || state == Thread .State .TERMINATED );
961+ if (isVirtualDisallowLoom (thread )) {
962+ return VirtualThreads .get ().isAlive (thread );
961963 }
962964 return platformIsAlive (thread );
963965 }
964966
965967 static boolean platformIsAlive (Thread thread ) {
966- assert !JavaContinuations . isSupported () || !( thread instanceof VirtualThread );
968+ assert !isVirtual ( Thread . currentThread () );
967969 int threadStatus = LoomSupport .CompatibilityUtil .getThreadStatus (toTarget (thread ));
968970 return !(threadStatus == ThreadStatus .NEW || threadStatus == ThreadStatus .TERMINATED );
969971 }
0 commit comments