1 /*
   2  * Copyright (c) 2018, 2024, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 package java.lang;
  26 
  27 import java.security.AccessController;
  28 import java.security.PrivilegedAction;
  29 import java.util.Locale;
  30 import java.util.Objects;
  31 import java.util.concurrent.Callable;
  32 import java.util.concurrent.CountDownLatch;
  33 import java.util.concurrent.Executor;
  34 import java.util.concurrent.Executors;
  35 import java.util.concurrent.ForkJoinPool;
  36 import java.util.concurrent.ForkJoinPool.ForkJoinWorkerThreadFactory;
  37 import java.util.concurrent.ForkJoinTask;
  38 import java.util.concurrent.ForkJoinWorkerThread;
  39 import java.util.concurrent.Future;
  40 import java.util.concurrent.RejectedExecutionException;
  41 import java.util.concurrent.ScheduledExecutorService;
  42 import java.util.concurrent.ScheduledThreadPoolExecutor;
  43 import jdk.internal.event.VirtualThreadEndEvent;
  44 import jdk.internal.event.VirtualThreadPinnedEvent;
  45 import jdk.internal.event.VirtualThreadStartEvent;
  46 import jdk.internal.event.VirtualThreadSubmitFailedEvent;
  47 import jdk.internal.misc.CarrierThread;
  48 import jdk.internal.misc.InnocuousThread;
  49 import jdk.internal.misc.Unsafe;
  50 import jdk.internal.vm.Continuation;
  51 import jdk.internal.vm.ContinuationScope;
  52 import jdk.internal.vm.StackableScope;
  53 import jdk.internal.vm.ThreadContainer;
  54 import jdk.internal.vm.ThreadContainers;
  55 import jdk.internal.vm.annotation.ChangesCurrentThread;
  56 import jdk.internal.vm.annotation.Hidden;
  57 import jdk.internal.vm.annotation.IntrinsicCandidate;
  58 import jdk.internal.vm.annotation.JvmtiMountTransition;
  59 import jdk.internal.vm.annotation.ReservedStackAccess;
  60 import sun.nio.ch.Interruptible;
  61 import sun.security.action.GetPropertyAction;
  62 import static java.util.concurrent.TimeUnit.*;
  63 
  64 /**
  65  * A thread that is scheduled by the Java virtual machine rather than the operating
  66  * system.
  67  */
  68 final class VirtualThread extends BaseVirtualThread {
  69     private static final Unsafe U = Unsafe.getUnsafe();
  70     private static final ContinuationScope VTHREAD_SCOPE = new ContinuationScope("VirtualThreads");
  71     private static final ForkJoinPool DEFAULT_SCHEDULER = createDefaultScheduler();
  72     private static final ScheduledExecutorService UNPARKER = createDelayedTaskScheduler();
  73     private static final int TRACE_PINNING_MODE = tracePinningMode();
  74 
  75     private static final long STATE = U.objectFieldOffset(VirtualThread.class, "state");
  76     private static final long PARK_PERMIT = U.objectFieldOffset(VirtualThread.class, "parkPermit");
  77     private static final long CARRIER_THREAD = U.objectFieldOffset(VirtualThread.class, "carrierThread");
  78     private static final long TERMINATION = U.objectFieldOffset(VirtualThread.class, "termination");
  79 
  80     // scheduler and continuation
  81     private final Executor scheduler;
  82     private final Continuation cont;
  83     private final Runnable runContinuation;
  84 
  85     // virtual thread state, accessed by VM
  86     private volatile int state;
  87 
  88     /*
  89      * Virtual thread state transitions:
  90      *
  91      *      NEW -> STARTED         // Thread.start, schedule to run
  92      *  STARTED -> TERMINATED      // failed to start
  93      *  STARTED -> RUNNING         // first run
  94      *  RUNNING -> TERMINATED      // done
  95      *
  96      *  RUNNING -> PARKING         // Thread parking with LockSupport.park
  97      *  PARKING -> PARKED          // cont.yield successful, parked indefinitely
  98      *  PARKING -> PINNED          // cont.yield failed, parked indefinitely on carrier
  99      *   PARKED -> UNPARKED        // unparked, may be scheduled to continue
 100      *   PINNED -> RUNNING         // unparked, continue execution on same carrier
 101      * UNPARKED -> RUNNING         // continue execution after park
 102      *
 103      *       RUNNING -> TIMED_PARKING   // Thread parking with LockSupport.parkNanos
 104      * TIMED_PARKING -> TIMED_PARKED    // cont.yield successful, timed-parked
 105      * TIMED_PARKING -> TIMED_PINNED    // cont.yield failed, timed-parked on carrier
 106      *  TIMED_PARKED -> UNPARKED        // unparked, may be scheduled to continue
 107      *  TIMED_PINNED -> RUNNING         // unparked, continue execution on same carrier
 108      *
 109      *  RUNNING -> YIELDING        // Thread.yield
 110      * YIELDING -> YIELDED         // cont.yield successful, may be scheduled to continue
 111      * YIELDING -> RUNNING         // cont.yield failed
 112      *  YIELDED -> RUNNING         // continue execution after Thread.yield
 113      */
 114     private static final int NEW      = 0;
 115     private static final int STARTED  = 1;
 116     private static final int RUNNING  = 2;     // runnable-mounted
 117 
 118     // untimed and timed parking
 119     private static final int PARKING       = 3;
 120     private static final int PARKED        = 4;     // unmounted
 121     private static final int PINNED        = 5;     // mounted
 122     private static final int TIMED_PARKING = 6;
 123     private static final int TIMED_PARKED  = 7;     // unmounted
 124     private static final int TIMED_PINNED  = 8;     // mounted
 125     private static final int UNPARKED      = 9;     // unmounted but runnable
 126 
 127     // Thread.yield
 128     private static final int YIELDING = 10;
 129     private static final int YIELDED  = 11;         // unmounted but runnable
 130 
 131     private static final int TERMINATED = 99;  // final state
 132 
 133     // can be suspended from scheduling when unmounted
 134     private static final int SUSPENDED = 1 << 8;
 135 
 136     // parking permit
 137     private volatile boolean parkPermit;
 138 
 139     // carrier thread when mounted, accessed by VM
 140     private volatile Thread carrierThread;
 141 
 142     // termination object when joining, created lazily if needed
 143     private volatile CountDownLatch termination;
 144 
 145     /**
 146      * Returns the continuation scope used for virtual threads.
 147      */
 148     static ContinuationScope continuationScope() {
 149         return VTHREAD_SCOPE;
 150     }
 151 
 152     /**
 153      * Creates a new {@code VirtualThread} to run the given task with the given
 154      * scheduler. If the given scheduler is {@code null} and the current thread
 155      * is a platform thread then the newly created virtual thread will use the
 156      * default scheduler. If given scheduler is {@code null} and the current
 157      * thread is a virtual thread then the current thread's scheduler is used.
 158      *
 159      * @param scheduler the scheduler or null
 160      * @param name thread name
 161      * @param characteristics characteristics
 162      * @param task the task to execute
 163      */
 164     VirtualThread(Executor scheduler, String name, int characteristics, Runnable task) {
 165         super(name, characteristics, /*bound*/ false);
 166         Objects.requireNonNull(task);
 167 
 168         // choose scheduler if not specified
 169         if (scheduler == null) {
 170             Thread parent = Thread.currentThread();
 171             if (parent instanceof VirtualThread vparent) {
 172                 scheduler = vparent.scheduler;
 173             } else {
 174                 scheduler = DEFAULT_SCHEDULER;
 175             }
 176         }
 177 
 178         this.scheduler = scheduler;
 179         this.cont = new VThreadContinuation(this, task);
 180         this.runContinuation = this::runContinuation;
 181     }
 182 
 183     /**
 184      * The continuation that a virtual thread executes.
 185      */
 186     private static class VThreadContinuation extends Continuation {
 187         VThreadContinuation(VirtualThread vthread, Runnable task) {
 188             super(VTHREAD_SCOPE, wrap(vthread, task));
 189         }
 190         @Override
 191         protected void onPinned(Continuation.Pinned reason) {
 192             if (TRACE_PINNING_MODE > 0) {
 193                 boolean printAll = (TRACE_PINNING_MODE == 1);
 194                 VirtualThread vthread = (VirtualThread) Thread.currentThread();
 195                 int oldState = vthread.state();
 196                 try {
 197                     // avoid printing when in transition states
 198                     vthread.setState(RUNNING);
 199                     PinnedThreadPrinter.printStackTrace(System.out, reason, printAll);
 200                 } finally {
 201                     vthread.setState(oldState);
 202                 }
 203             }
 204         }
 205         private static Runnable wrap(VirtualThread vthread, Runnable task) {
 206             return new Runnable() {
 207                 @Hidden
 208                 public void run() {
 209                     vthread.run(task);
 210                 }
 211             };
 212         }
 213     }
 214 
 215     /**
 216      * Runs or continues execution on the current thread. The virtual thread is mounted
 217      * on the current thread before the task runs or continues. It unmounts when the
 218      * task completes or yields.
 219      */
 220     @ChangesCurrentThread
 221     private void runContinuation() {
 222         // the carrier must be a platform thread
 223         if (Thread.currentThread().isVirtual()) {
 224             throw new WrongThreadException();
 225         }
 226 
 227         // set state to RUNNING
 228         int initialState = state();
 229         if (initialState == STARTED || initialState == UNPARKED || initialState == YIELDED) {
 230             // newly started or continue after parking/blocking/Thread.yield
 231             if (!compareAndSetState(initialState, RUNNING)) {
 232                 return;
 233             }
 234             // consume parking permit when continuing after parking
 235             if (initialState == UNPARKED) {
 236                 setParkPermit(false);
 237             }
 238         } else {
 239             // not runnable
 240             return;
 241         }
 242 
 243         mount();
 244         try {
 245             cont.run();
 246         } finally {
 247             unmount();
 248             if (cont.isDone()) {
 249                 afterDone();
 250             } else {
 251                 afterYield();
 252             }
 253         }
 254     }
 255 
 256     /**
 257      * Submits the runContinuation task to the scheduler. For the default scheduler,
 258      * and calling it on a worker thread, the task will be pushed to the local queue,
 259      * otherwise it will be pushed to an external submission queue.
 260      * @throws RejectedExecutionException
 261      */
 262     private void submitRunContinuation() {
 263         try {
 264             scheduler.execute(runContinuation);
 265         } catch (RejectedExecutionException ree) {
 266             submitFailed(ree);
 267             throw ree;
 268         }
 269     }
 270 
 271     /**
 272      * Submits the runContinuation task to given scheduler with a lazy submit.
 273      * @throws RejectedExecutionException
 274      * @see ForkJoinPool#lazySubmit(ForkJoinTask)
 275      */
 276     private void lazySubmitRunContinuation(ForkJoinPool pool) {
 277         try {
 278             pool.lazySubmit(ForkJoinTask.adapt(runContinuation));
 279         } catch (RejectedExecutionException ree) {
 280             submitFailed(ree);
 281             throw ree;
 282         }
 283     }
 284 
 285     /**
 286      * Submits the runContinuation task to the given scheduler as an external submit.
 287      * @throws RejectedExecutionException
 288      * @see ForkJoinPool#externalSubmit(ForkJoinTask)
 289      */
 290     private void externalSubmitRunContinuation(ForkJoinPool pool) {
 291         try {
 292             pool.externalSubmit(ForkJoinTask.adapt(runContinuation));
 293         } catch (RejectedExecutionException ree) {
 294             submitFailed(ree);
 295             throw ree;
 296         }
 297     }
 298 
 299     /**
 300      * If enabled, emits a JFR VirtualThreadSubmitFailedEvent.
 301      */
 302     private void submitFailed(RejectedExecutionException ree) {
 303         var event = new VirtualThreadSubmitFailedEvent();
 304         if (event.isEnabled()) {
 305             event.javaThreadId = threadId();
 306             event.exceptionMessage = ree.getMessage();
 307             event.commit();
 308         }
 309     }
 310 
 311     /**
 312      * Runs a task in the context of this virtual thread.
 313      */
 314     private void run(Runnable task) {
 315         assert Thread.currentThread() == this && state == RUNNING;
 316 
 317         // notify JVMTI, may post VirtualThreadStart event
 318         notifyJvmtiStart();
 319 
 320         // emit JFR event if enabled
 321         if (VirtualThreadStartEvent.isTurnedOn()) {
 322             var event = new VirtualThreadStartEvent();
 323             event.javaThreadId = threadId();
 324             event.commit();
 325         }
 326 
 327         Object bindings = Thread.scopedValueBindings();
 328         try {
 329             runWith(bindings, task);
 330         } catch (Throwable exc) {
 331             dispatchUncaughtException(exc);
 332         } finally {
 333             try {
 334                 // pop any remaining scopes from the stack, this may block
 335                 StackableScope.popAll();
 336 
 337                 // emit JFR event if enabled
 338                 if (VirtualThreadEndEvent.isTurnedOn()) {
 339                     var event = new VirtualThreadEndEvent();
 340                     event.javaThreadId = threadId();
 341                     event.commit();
 342                 }
 343 
 344             } finally {
 345                 // notify JVMTI, may post VirtualThreadEnd event
 346                 notifyJvmtiEnd();
 347             }
 348         }
 349     }
 350 
 351     /**
 352      * Mounts this virtual thread onto the current platform thread. On
 353      * return, the current thread is the virtual thread.
 354      */
 355     @ChangesCurrentThread
 356     @ReservedStackAccess
 357     private void mount() {
 358         // notify JVMTI before mount
 359         notifyJvmtiMount(/*hide*/true);
 360 
 361         // sets the carrier thread
 362         Thread carrier = Thread.currentCarrierThread();
 363         setCarrierThread(carrier);
 364 
 365         // sync up carrier thread interrupt status if needed
 366         if (interrupted) {
 367             carrier.setInterrupt();
 368         } else if (carrier.isInterrupted()) {
 369             synchronized (interruptLock) {
 370                 // need to recheck interrupt status
 371                 if (!interrupted) {
 372                     carrier.clearInterrupt();
 373                 }
 374             }
 375         }
 376 
 377         // set Thread.currentThread() to return this virtual thread
 378         carrier.setCurrentThread(this);
 379     }
 380 
 381     /**
 382      * Unmounts this virtual thread from the carrier. On return, the
 383      * current thread is the current platform thread.
 384      */
 385     @ChangesCurrentThread
 386     @ReservedStackAccess
 387     private void unmount() {
 388         // set Thread.currentThread() to return the platform thread
 389         Thread carrier = this.carrierThread;
 390         carrier.setCurrentThread(carrier);
 391 
 392         // break connection to carrier thread, synchronized with interrupt
 393         synchronized (interruptLock) {
 394             setCarrierThread(null);
 395         }
 396         carrier.clearInterrupt();
 397 
 398         // notify JVMTI after unmount
 399         notifyJvmtiUnmount(/*hide*/false);
 400     }
 401 
 402     /**
 403      * Sets the current thread to the current carrier thread.
 404      */
 405     @ChangesCurrentThread
 406     @JvmtiMountTransition
 407     private void switchToCarrierThread() {
 408         notifyJvmtiHideFrames(true);
 409         Thread carrier = this.carrierThread;
 410         assert Thread.currentThread() == this
 411                 && carrier == Thread.currentCarrierThread();
 412         carrier.setCurrentThread(carrier);
 413     }
 414 
 415     /**
 416      * Sets the current thread to the given virtual thread.
 417      */
 418     @ChangesCurrentThread
 419     @JvmtiMountTransition
 420     private void switchToVirtualThread(VirtualThread vthread) {
 421         Thread carrier = vthread.carrierThread;
 422         assert carrier == Thread.currentCarrierThread();
 423         carrier.setCurrentThread(vthread);
 424         notifyJvmtiHideFrames(false);
 425     }
 426 
 427     /**
 428      * Executes the given value returning task on the current carrier thread.
 429      */
 430     @ChangesCurrentThread
 431     <V> V executeOnCarrierThread(Callable<V> task) throws Exception {
 432         assert Thread.currentThread() == this;
 433         switchToCarrierThread();
 434         try {
 435             return task.call();
 436         } finally {
 437             switchToVirtualThread(this);
 438         }
 439      }
 440 
 441     /**
 442      * Invokes Continuation.yield, notifying JVMTI (if enabled) to hide frames until
 443      * the continuation continues.
 444      */
 445     @Hidden
 446     private boolean yieldContinuation() {
 447         notifyJvmtiUnmount(/*hide*/true);
 448         try {
 449             return Continuation.yield(VTHREAD_SCOPE);
 450         } finally {
 451             notifyJvmtiMount(/*hide*/false);
 452         }
 453     }
 454 
 455     /**
 456      * Invoked after the continuation yields. If parking then it sets the state
 457      * and also re-submits the task to continue if unparked while parking.
 458      * If yielding due to Thread.yield then it just submits the task to continue.
 459      */
 460     private void afterYield() {
 461         assert carrierThread == null;
 462 
 463         // re-adjust parallelism if the virtual thread yielded when compensating
 464         if (currentThread() instanceof CarrierThread ct) {
 465             ct.endBlocking();
 466         }
 467 
 468         int s = state();
 469 
 470         // LockSupport.park/parkNanos
 471         if (s == PARKING || s == TIMED_PARKING) {
 472             int newState = (s == PARKING) ? PARKED : TIMED_PARKED;
 473             setState(newState);
 474 
 475             // may have been unparked while parking
 476             if (parkPermit && compareAndSetState(newState, UNPARKED)) {
 477                 // lazy submit to continue on the current thread as carrier if possible
 478                 if (currentThread() instanceof CarrierThread ct) {
 479                     lazySubmitRunContinuation(ct.getPool());
 480                 } else {
 481                     submitRunContinuation();
 482                 }
 483 
 484             }
 485             return;
 486         }
 487 
 488         // Thread.yield
 489         if (s == YIELDING) {
 490             setState(YIELDED);
 491 
 492             // external submit if there are no tasks in the local task queue
 493             if (currentThread() instanceof CarrierThread ct && ct.getQueuedTaskCount() == 0) {
 494                 externalSubmitRunContinuation(ct.getPool());
 495             } else {
 496                 submitRunContinuation();
 497             }
 498             return;
 499         }
 500 
 501         assert false;
 502     }
 503 
 504     /**
 505      * Invoked after the continuation completes.
 506      */
 507     private void afterDone() {
 508         afterDone(true);
 509     }
 510 
 511     /**
 512      * Invoked after the continuation completes (or start failed). Sets the thread
 513      * state to TERMINATED and notifies anyone waiting for the thread to terminate.
 514      *
 515      * @param notifyContainer true if its container should be notified
 516      */
 517     private void afterDone(boolean notifyContainer) {
 518         assert carrierThread == null;
 519         setState(TERMINATED);
 520 
 521         // notify anyone waiting for this virtual thread to terminate
 522         CountDownLatch termination = this.termination;
 523         if (termination != null) {
 524             assert termination.getCount() == 1;
 525             termination.countDown();
 526         }
 527 
 528         // notify container
 529         if (notifyContainer) {
 530             threadContainer().onExit(this);
 531         }
 532 
 533         // clear references to thread locals
 534         clearReferences();
 535     }
 536 
 537     /**
 538      * Schedules this {@code VirtualThread} to execute.
 539      *
 540      * @throws IllegalStateException if the container is shutdown or closed
 541      * @throws IllegalThreadStateException if the thread has already been started
 542      * @throws RejectedExecutionException if the scheduler cannot accept a task
 543      */
 544     @Override
 545     void start(ThreadContainer container) {
 546         if (!compareAndSetState(NEW, STARTED)) {
 547             throw new IllegalThreadStateException("Already started");
 548         }
 549 
 550         // bind thread to container
 551         assert threadContainer() == null;
 552         setThreadContainer(container);
 553 
 554         // start thread
 555         boolean addedToContainer = false;
 556         boolean started = false;
 557         try {
 558             container.onStart(this);  // may throw
 559             addedToContainer = true;
 560 
 561             // scoped values may be inherited
 562             inheritScopedValueBindings(container);
 563 
 564             // submit task to run thread
 565             submitRunContinuation();
 566             started = true;
 567         } finally {
 568             if (!started) {
 569                 afterDone(addedToContainer);
 570             }
 571         }
 572     }
 573 
 574     @Override
 575     public void start() {
 576         start(ThreadContainers.root());
 577     }
 578 
 579     @Override
 580     public void run() {
 581         // do nothing
 582     }
 583 
 584     /**
 585      * Parks until unparked or interrupted. If already unparked then the parking
 586      * permit is consumed and this method completes immediately (meaning it doesn't
 587      * yield). It also completes immediately if the interrupt status is set.
 588      */
 589     @Override
 590     void park() {
 591         assert Thread.currentThread() == this;
 592 
 593         // complete immediately if parking permit available or interrupted
 594         if (getAndSetParkPermit(false) || interrupted)
 595             return;
 596 
 597         // park the thread
 598         boolean yielded = false;
 599         setState(PARKING);
 600         try {
 601             yielded = yieldContinuation();  // may throw
 602         } finally {
 603             assert (Thread.currentThread() == this) && (yielded == (state() == RUNNING));
 604             if (!yielded) {
 605                 assert state() == PARKING;
 606                 setState(RUNNING);
 607             }
 608         }
 609 
 610         // park on the carrier thread when pinned
 611         if (!yielded) {
 612             parkOnCarrierThread(false, 0);
 613         }
 614     }
 615 
 616     /**
 617      * Parks up to the given waiting time or until unparked or interrupted.
 618      * If already unparked then the parking permit is consumed and this method
 619      * completes immediately (meaning it doesn't yield). It also completes immediately
 620      * if the interrupt status is set or the waiting time is {@code <= 0}.
 621      *
 622      * @param nanos the maximum number of nanoseconds to wait.
 623      */
 624     @Override
 625     void parkNanos(long nanos) {
 626         assert Thread.currentThread() == this;
 627 
 628         // complete immediately if parking permit available or interrupted
 629         if (getAndSetParkPermit(false) || interrupted)
 630             return;
 631 
 632         // park the thread for the waiting time
 633         if (nanos > 0) {
 634             long startTime = System.nanoTime();
 635 
 636             boolean yielded = false;
 637             Future<?> unparker = scheduleUnpark(nanos);  // may throw OOME
 638             setState(TIMED_PARKING);
 639             try {
 640                 yielded = yieldContinuation();  // may throw
 641             } finally {
 642                 assert (Thread.currentThread() == this) && (yielded == (state() == RUNNING));
 643                 if (!yielded) {
 644                     assert state() == TIMED_PARKING;
 645                     setState(RUNNING);
 646                 }
 647                 cancel(unparker);
 648             }
 649 
 650             // park on carrier thread for remaining time when pinned
 651             if (!yielded) {
 652                 long remainingNanos = nanos - (System.nanoTime() - startTime);
 653                 parkOnCarrierThread(true, remainingNanos);
 654             }
 655         }
 656     }
 657 
 658     /**
 659      * Parks the current carrier thread up to the given waiting time or until
 660      * unparked or interrupted. If the virtual thread is interrupted then the
 661      * interrupt status will be propagated to the carrier thread.
 662      * @param timed true for a timed park, false for untimed
 663      * @param nanos the waiting time in nanoseconds
 664      */
 665     private void parkOnCarrierThread(boolean timed, long nanos) {
 666         assert state() == RUNNING;
 667 
 668         VirtualThreadPinnedEvent event;
 669         try {
 670             event = new VirtualThreadPinnedEvent();
 671             event.begin();
 672         } catch (OutOfMemoryError e) {
 673             event = null;
 674         }
 675 
 676         setState(timed ? TIMED_PINNED : PINNED);
 677         try {
 678             if (!parkPermit) {
 679                 if (!timed) {
 680                     U.park(false, 0);
 681                 } else if (nanos > 0) {
 682                     U.park(false, nanos);
 683                 }
 684             }
 685         } finally {
 686             setState(RUNNING);
 687         }
 688 
 689         // consume parking permit
 690         setParkPermit(false);
 691 
 692         if (event != null) {
 693             try {
 694                 event.commit();
 695             } catch (OutOfMemoryError e) {
 696                 // ignore
 697             }
 698         }
 699     }
 700 
 701     /**
 702      * Schedule this virtual thread to be unparked after a given delay.
 703      */
 704     @ChangesCurrentThread
 705     private Future<?> scheduleUnpark(long nanos) {
 706         assert Thread.currentThread() == this;
 707         // need to switch to current carrier thread to avoid nested parking
 708         switchToCarrierThread();
 709         try {
 710             return UNPARKER.schedule(this::unpark, nanos, NANOSECONDS);
 711         } finally {
 712             switchToVirtualThread(this);
 713         }
 714     }
 715 
 716     /**
 717      * Cancels a task if it has not completed.
 718      */
 719     @ChangesCurrentThread
 720     private void cancel(Future<?> future) {
 721         if (!future.isDone()) {
 722             // need to switch to current carrier thread to avoid nested parking
 723             switchToCarrierThread();
 724             try {
 725                 future.cancel(false);
 726             } finally {
 727                 switchToVirtualThread(this);
 728             }
 729         }
 730     }
 731 
 732     /**
 733      * Re-enables this virtual thread for scheduling. If the virtual thread was
 734      * {@link #park() parked} then it will be unblocked, otherwise its next call
 735      * to {@code park} or {@linkplain #parkNanos(long) parkNanos} is guaranteed
 736      * not to block.
 737      * @throws RejectedExecutionException if the scheduler cannot accept a task
 738      */
 739     @Override
 740     @ChangesCurrentThread
 741     void unpark() {
 742         Thread currentThread = Thread.currentThread();
 743         if (!getAndSetParkPermit(true) && currentThread != this) {
 744             int s = state();
 745             boolean parked = (s == PARKED) || (s == TIMED_PARKED);
 746             if (parked && compareAndSetState(s, UNPARKED)) {
 747                 if (currentThread instanceof VirtualThread vthread) {
 748                     vthread.switchToCarrierThread();
 749                     try {
 750                         submitRunContinuation();
 751                     } finally {
 752                         switchToVirtualThread(vthread);
 753                     }
 754                 } else {
 755                     submitRunContinuation();
 756                 }
 757             } else if ((s == PINNED) || (s == TIMED_PINNED)) {
 758                 // unpark carrier thread when pinned
 759                 notifyJvmtiDisableSuspend(true);
 760                 try {
 761                     synchronized (carrierThreadAccessLock()) {
 762                         Thread carrier = carrierThread;
 763                         if (carrier != null && ((s = state()) == PINNED || s == TIMED_PINNED)) {
 764                             U.unpark(carrier);
 765                         }
 766                     }
 767                 } finally {
 768                     notifyJvmtiDisableSuspend(false);
 769                 }
 770             }
 771         }
 772     }
 773 
 774     /**
 775      * Attempts to yield the current virtual thread (Thread.yield).
 776      */
 777     void tryYield() {
 778         assert Thread.currentThread() == this;
 779         setState(YIELDING);
 780         boolean yielded = false;
 781         try {
 782             yielded = yieldContinuation();  // may throw
 783         } finally {
 784             assert (Thread.currentThread() == this) && (yielded == (state() == RUNNING));
 785             if (!yielded) {
 786                 assert state() == YIELDING;
 787                 setState(RUNNING);
 788             }
 789         }
 790     }
 791 
 792     /**
 793      * Sleep the current thread for the given sleep time (in nanoseconds). If
 794      * nanos is 0 then the thread will attempt to yield.
 795      *
 796      * @implNote This implementation parks the thread for the given sleeping time
 797      * and will therefore be observed in PARKED state during the sleep. Parking
 798      * will consume the parking permit so this method makes available the parking
 799      * permit after the sleep. This may be observed as a spurious, but benign,
 800      * wakeup when the thread subsequently attempts to park.
 801      *
 802      * @param nanos the maximum number of nanoseconds to sleep
 803      * @throws InterruptedException if interrupted while sleeping
 804      */
 805     void sleepNanos(long nanos) throws InterruptedException {
 806         assert Thread.currentThread() == this && nanos >= 0;
 807         if (getAndClearInterrupt())
 808             throw new InterruptedException();
 809         if (nanos == 0) {
 810             tryYield();
 811         } else {
 812             // park for the sleep time
 813             try {
 814                 long remainingNanos = nanos;
 815                 long startNanos = System.nanoTime();
 816                 while (remainingNanos > 0) {
 817                     parkNanos(remainingNanos);
 818                     if (getAndClearInterrupt()) {
 819                         throw new InterruptedException();
 820                     }
 821                     remainingNanos = nanos - (System.nanoTime() - startNanos);
 822                 }
 823             } finally {
 824                 // may have been unparked while sleeping
 825                 setParkPermit(true);
 826             }
 827         }
 828     }
 829 
 830     /**
 831      * Waits up to {@code nanos} nanoseconds for this virtual thread to terminate.
 832      * A timeout of {@code 0} means to wait forever.
 833      *
 834      * @throws InterruptedException if interrupted while waiting
 835      * @return true if the thread has terminated
 836      */
 837     boolean joinNanos(long nanos) throws InterruptedException {
 838         if (state() == TERMINATED)
 839             return true;
 840 
 841         // ensure termination object exists, then re-check state
 842         CountDownLatch termination = getTermination();
 843         if (state() == TERMINATED)
 844             return true;
 845 
 846         // wait for virtual thread to terminate
 847         if (nanos == 0) {
 848             termination.await();
 849         } else {
 850             boolean terminated = termination.await(nanos, NANOSECONDS);
 851             if (!terminated) {
 852                 // waiting time elapsed
 853                 return false;
 854             }
 855         }
 856         assert state() == TERMINATED;
 857         return true;
 858     }
 859 
 860     @Override
 861     void blockedOn(Interruptible b) {
 862         notifyJvmtiDisableSuspend(true);
 863         try {
 864             super.blockedOn(b);
 865         } finally {
 866             notifyJvmtiDisableSuspend(false);
 867         }
 868     }
 869 
 870     @Override
 871     @SuppressWarnings("removal")
 872     public void interrupt() {
 873         if (Thread.currentThread() != this) {
 874             checkAccess();
 875 
 876             // if current thread is a virtual thread then prevent it from being
 877             // suspended when entering or holding interruptLock
 878             Interruptible blocker;
 879             notifyJvmtiDisableSuspend(true);
 880             try {
 881                 synchronized (interruptLock) {
 882                     interrupted = true;
 883                     blocker = nioBlocker();
 884                     if (blocker != null) {
 885                         blocker.interrupt(this);
 886                     }
 887 
 888                     // interrupt carrier thread if mounted
 889                     Thread carrier = carrierThread;
 890                     if (carrier != null) carrier.setInterrupt();
 891                 }
 892             } finally {
 893                 notifyJvmtiDisableSuspend(false);
 894             }
 895 
 896             // notify blocker after releasing interruptLock
 897             if (blocker != null) {
 898                 blocker.postInterrupt();
 899             }
 900         } else {
 901             interrupted = true;
 902             carrierThread.setInterrupt();
 903         }
 904         unpark();
 905     }
 906 
 907     @Override
 908     public boolean isInterrupted() {
 909         return interrupted;
 910     }
 911 
 912     @Override
 913     boolean getAndClearInterrupt() {
 914         assert Thread.currentThread() == this;
 915         boolean oldValue = interrupted;
 916         if (oldValue) {
 917             notifyJvmtiDisableSuspend(true);
 918             try {
 919                 synchronized (interruptLock) {
 920                     interrupted = false;
 921                     carrierThread.clearInterrupt();
 922                 }
 923             } finally {
 924                 notifyJvmtiDisableSuspend(false);
 925             }
 926         }
 927         return oldValue;
 928     }
 929 
 930     @Override
 931     Thread.State threadState() {
 932         int s = state();
 933         switch (s & ~SUSPENDED) {
 934             case NEW:
 935                 return Thread.State.NEW;
 936             case STARTED:
 937                 // return NEW if thread container not yet set
 938                 if (threadContainer() == null) {
 939                     return Thread.State.NEW;
 940                 } else {
 941                     return Thread.State.RUNNABLE;
 942                 }
 943             case UNPARKED:
 944             case YIELDED:
 945                 // runnable, not mounted
 946                 return Thread.State.RUNNABLE;
 947             case RUNNING:
 948                 // if mounted then return state of carrier thread
 949                 notifyJvmtiDisableSuspend(true);
 950                 try {
 951                     synchronized (carrierThreadAccessLock()) {
 952                         Thread carrierThread = this.carrierThread;
 953                         if (carrierThread != null) {
 954                             return carrierThread.threadState();
 955                         }
 956                     }
 957                 } finally {
 958                     notifyJvmtiDisableSuspend(false);
 959                 }
 960                 // runnable, mounted
 961                 return Thread.State.RUNNABLE;
 962             case PARKING:
 963             case TIMED_PARKING:
 964             case YIELDING:
 965                 // runnable, in transition
 966                 return Thread.State.RUNNABLE;
 967             case PARKED:
 968             case PINNED:
 969                 return State.WAITING;
 970             case TIMED_PARKED:
 971             case TIMED_PINNED:
 972                 return State.TIMED_WAITING;
 973             case TERMINATED:
 974                 return Thread.State.TERMINATED;
 975             default:
 976                 throw new InternalError();
 977         }
 978     }
 979 
 980     @Override
 981     boolean alive() {
 982         int s = state;
 983         return (s != NEW && s != TERMINATED);
 984     }
 985 
 986     @Override
 987     boolean isTerminated() {
 988         return (state == TERMINATED);
 989     }
 990 
 991     @Override
 992     StackTraceElement[] asyncGetStackTrace() {
 993         StackTraceElement[] stackTrace;
 994         do {
 995             stackTrace = (carrierThread != null)
 996                     ? super.asyncGetStackTrace()  // mounted
 997                     : tryGetStackTrace();         // unmounted
 998             if (stackTrace == null) {
 999                 Thread.yield();
1000             }
1001         } while (stackTrace == null);
1002         return stackTrace;
1003     }
1004 
1005     /**
1006      * Returns the stack trace for this virtual thread if it is unmounted.
1007      * Returns null if the thread is mounted or in transition.
1008      */
1009     private StackTraceElement[] tryGetStackTrace() {
1010         int initialState = state() & ~SUSPENDED;
1011         switch (initialState) {
1012             case NEW, STARTED, TERMINATED -> {
1013                 return new StackTraceElement[0];  // unmounted, empty stack
1014             }
1015             case RUNNING, PINNED, TIMED_PINNED -> {
1016                 return null;   // mounted
1017             }
1018             case PARKED, TIMED_PARKED -> {
1019                 // unmounted, not runnable
1020             }
1021             case UNPARKED, YIELDED -> {
1022                 // unmounted, runnable
1023             }
1024             case PARKING, TIMED_PARKING, YIELDING -> {
1025                 return null;  // in transition
1026             }
1027             default -> throw new InternalError("" + initialState);
1028         }
1029 
1030         // thread is unmounted, prevent it from continuing
1031         int suspendedState = initialState | SUSPENDED;
1032         if (!compareAndSetState(initialState, suspendedState)) {
1033             return null;
1034         }
1035 
1036         // get stack trace and restore state
1037         StackTraceElement[] stack;
1038         try {
1039             stack = cont.getStackTrace();
1040         } finally {
1041             assert state == suspendedState;
1042             setState(initialState);
1043         }
1044         boolean resubmit = switch (initialState) {
1045             case UNPARKED, YIELDED -> {
1046                 // resubmit as task may have run while suspended
1047                 yield true;
1048             }
1049             case PARKED, TIMED_PARKED -> {
1050                 // resubmit if unparked while suspended
1051                 yield parkPermit && compareAndSetState(initialState, UNPARKED);
1052             }
1053             default -> throw new InternalError();
1054         };
1055         if (resubmit) {
1056             submitRunContinuation();
1057         }
1058         return stack;
1059     }
1060 
1061     @Override
1062     public String toString() {
1063         StringBuilder sb = new StringBuilder("VirtualThread[#");
1064         sb.append(threadId());
1065         String name = getName();
1066         if (!name.isEmpty()) {
1067             sb.append(",");
1068             sb.append(name);
1069         }
1070         sb.append("]/");
1071         Thread carrier = carrierThread;
1072         if (carrier != null) {
1073             // include the carrier thread state and name when mounted
1074             notifyJvmtiDisableSuspend(true);
1075             try {
1076                 synchronized (carrierThreadAccessLock()) {
1077                     carrier = carrierThread;
1078                     if (carrier != null) {
1079                         String stateAsString = carrier.threadState().toString();
1080                         sb.append(stateAsString.toLowerCase(Locale.ROOT));
1081                         sb.append('@');
1082                         sb.append(carrier.getName());
1083                     }
1084                 }
1085             } finally {
1086                 notifyJvmtiDisableSuspend(false);
1087             }
1088         }
1089         // include virtual thread state when not mounted
1090         if (carrier == null) {
1091             String stateAsString = threadState().toString();
1092             sb.append(stateAsString.toLowerCase(Locale.ROOT));
1093         }
1094         return sb.toString();
1095     }
1096 
1097     @Override
1098     public int hashCode() {
1099         return (int) threadId();
1100     }
1101 
1102     @Override
1103     public boolean equals(Object obj) {
1104         return obj == this;
1105     }
1106 
1107     /**
1108      * Returns the termination object, creating it if needed.
1109      */
1110     private CountDownLatch getTermination() {
1111         CountDownLatch termination = this.termination;
1112         if (termination == null) {
1113             termination = new CountDownLatch(1);
1114             if (!U.compareAndSetReference(this, TERMINATION, null, termination)) {
1115                 termination = this.termination;
1116             }
1117         }
1118         return termination;
1119     }
1120 
1121     /**
1122      * Returns the lock object to synchronize on when accessing carrierThread.
1123      * The lock prevents carrierThread from being reset to null during unmount.
1124      */
1125     private Object carrierThreadAccessLock() {
1126         // return interruptLock as unmount has to coordinate with interrupt
1127         return interruptLock;
1128     }
1129 
1130     // -- wrappers for get/set of state, parking permit, and carrier thread --
1131 
1132     private int state() {
1133         return state;  // volatile read
1134     }
1135 
1136     private void setState(int newValue) {
1137         state = newValue;  // volatile write
1138     }
1139 
1140     private boolean compareAndSetState(int expectedValue, int newValue) {
1141         return U.compareAndSetInt(this, STATE, expectedValue, newValue);
1142     }
1143 
1144     private void setParkPermit(boolean newValue) {
1145         if (parkPermit != newValue) {
1146             parkPermit = newValue;
1147         }
1148     }
1149 
1150     private boolean getAndSetParkPermit(boolean newValue) {
1151         if (parkPermit != newValue) {
1152             return U.getAndSetBoolean(this, PARK_PERMIT, newValue);
1153         } else {
1154             return newValue;
1155         }
1156     }
1157 
1158     private void setCarrierThread(Thread carrier) {
1159         // U.putReferenceRelease(this, CARRIER_THREAD, carrier);
1160         this.carrierThread = carrier;
1161     }
1162 
1163     // -- JVM TI support --
1164 
1165     @IntrinsicCandidate
1166     @JvmtiMountTransition
1167     private native void notifyJvmtiStart();
1168 
1169     @IntrinsicCandidate
1170     @JvmtiMountTransition
1171     private native void notifyJvmtiEnd();
1172 
1173     @IntrinsicCandidate
1174     @JvmtiMountTransition
1175     private native void notifyJvmtiMount(boolean hide);
1176 
1177     @IntrinsicCandidate
1178     @JvmtiMountTransition
1179     private native void notifyJvmtiUnmount(boolean hide);
1180 
1181     @IntrinsicCandidate
1182     @JvmtiMountTransition
1183     private static native void notifyJvmtiHideFrames(boolean hide);
1184 
1185     @IntrinsicCandidate
1186     private static native void notifyJvmtiDisableSuspend(boolean enter);
1187 
1188     private static native void registerNatives();
1189     static {
1190         registerNatives();
1191     }
1192 
1193     /**
1194      * Creates the default scheduler.
1195      */
1196     @SuppressWarnings("removal")
1197     private static ForkJoinPool createDefaultScheduler() {
1198         ForkJoinWorkerThreadFactory factory = pool -> {
1199             PrivilegedAction<ForkJoinWorkerThread> pa = () -> new CarrierThread(pool);
1200             return AccessController.doPrivileged(pa);
1201         };
1202         PrivilegedAction<ForkJoinPool> pa = () -> {
1203             int parallelism, maxPoolSize, minRunnable;
1204             String parallelismValue = System.getProperty("jdk.virtualThreadScheduler.parallelism");
1205             String maxPoolSizeValue = System.getProperty("jdk.virtualThreadScheduler.maxPoolSize");
1206             String minRunnableValue = System.getProperty("jdk.virtualThreadScheduler.minRunnable");
1207             if (parallelismValue != null) {
1208                 parallelism = Integer.parseInt(parallelismValue);
1209             } else {
1210                 parallelism = Runtime.getRuntime().availableProcessors();
1211             }
1212             if (maxPoolSizeValue != null) {
1213                 maxPoolSize = Integer.parseInt(maxPoolSizeValue);
1214                 parallelism = Integer.min(parallelism, maxPoolSize);
1215             } else {
1216                 maxPoolSize = Integer.max(parallelism, 256);
1217             }
1218             if (minRunnableValue != null) {
1219                 minRunnable = Integer.parseInt(minRunnableValue);
1220             } else {
1221                 minRunnable = Integer.max(parallelism / 2, 1);
1222             }
1223             Thread.UncaughtExceptionHandler handler = (t, e) -> { };
1224             boolean asyncMode = true; // FIFO
1225             return new ForkJoinPool(parallelism, factory, handler, asyncMode,
1226                          0, maxPoolSize, minRunnable, pool -> true, 30, SECONDS);
1227         };
1228         return AccessController.doPrivileged(pa);
1229     }
1230 
1231     /**
1232      * Creates the ScheduledThreadPoolExecutor used for timed unpark.
1233      */
1234     private static ScheduledExecutorService createDelayedTaskScheduler() {
1235         String propValue = GetPropertyAction.privilegedGetProperty("jdk.unparker.maxPoolSize");
1236         int poolSize;
1237         if (propValue != null) {
1238             poolSize = Integer.parseInt(propValue);
1239         } else {
1240             poolSize = 1;
1241         }
1242         ScheduledThreadPoolExecutor stpe = (ScheduledThreadPoolExecutor)
1243             Executors.newScheduledThreadPool(poolSize, task -> {
1244                 return InnocuousThread.newThread("VirtualThread-unparker", task);
1245             });
1246         stpe.setRemoveOnCancelPolicy(true);
1247         return stpe;
1248     }
1249 
1250     /**
1251      * Reads the value of the jdk.tracePinnedThreads property to determine if stack
1252      * traces should be printed when a carrier thread is pinned when a virtual thread
1253      * attempts to park.
1254      */
1255     private static int tracePinningMode() {
1256         String propValue = GetPropertyAction.privilegedGetProperty("jdk.tracePinnedThreads");
1257         if (propValue != null) {
1258             if (propValue.length() == 0 || "full".equalsIgnoreCase(propValue))
1259                 return 1;
1260             if ("short".equalsIgnoreCase(propValue))
1261                 return 2;
1262         }
1263         return 0;
1264     }
1265 }