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