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