< prev index next >

src/java.base/share/classes/java/lang/VirtualThread.java

Print this page

   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.util.Locale;
  28 import java.util.Objects;
  29 import java.util.concurrent.CountDownLatch;
  30 import java.util.concurrent.Executor;
  31 import java.util.concurrent.Executors;
  32 import java.util.concurrent.ForkJoinPool;
  33 import java.util.concurrent.ForkJoinPool.ForkJoinWorkerThreadFactory;
  34 import java.util.concurrent.ForkJoinTask;
  35 import java.util.concurrent.Future;
  36 import java.util.concurrent.RejectedExecutionException;
  37 import java.util.concurrent.ScheduledExecutorService;
  38 import java.util.concurrent.ScheduledThreadPoolExecutor;
  39 import java.util.concurrent.TimeUnit;
  40 import jdk.internal.event.VirtualThreadEndEvent;
  41 import jdk.internal.event.VirtualThreadStartEvent;
  42 import jdk.internal.event.VirtualThreadSubmitFailedEvent;
  43 import jdk.internal.misc.CarrierThread;
  44 import jdk.internal.misc.InnocuousThread;
  45 import jdk.internal.misc.Unsafe;
  46 import jdk.internal.vm.Continuation;
  47 import jdk.internal.vm.ContinuationScope;
  48 import jdk.internal.vm.StackableScope;
  49 import jdk.internal.vm.ThreadContainer;
  50 import jdk.internal.vm.ThreadContainers;
  51 import jdk.internal.vm.annotation.ChangesCurrentThread;
  52 import jdk.internal.vm.annotation.Hidden;
  53 import jdk.internal.vm.annotation.IntrinsicCandidate;
  54 import jdk.internal.vm.annotation.JvmtiHideEvents;
  55 import jdk.internal.vm.annotation.JvmtiMountTransition;
  56 import jdk.internal.vm.annotation.ReservedStackAccess;
  57 import sun.nio.ch.Interruptible;
  58 import static java.util.concurrent.TimeUnit.*;
  59 
  60 /**
  61  * A thread that is scheduled by the Java virtual machine rather than the operating system.
  62  */
  63 final class VirtualThread extends BaseVirtualThread {
  64     private static final Unsafe U = Unsafe.getUnsafe();
  65     private static final ContinuationScope VTHREAD_SCOPE = new ContinuationScope("VirtualThreads");
  66     private static final ForkJoinPool DEFAULT_SCHEDULER = createDefaultScheduler();



















  67 
  68     private static final long STATE = U.objectFieldOffset(VirtualThread.class, "state");
  69     private static final long PARK_PERMIT = U.objectFieldOffset(VirtualThread.class, "parkPermit");
  70     private static final long CARRIER_THREAD = U.objectFieldOffset(VirtualThread.class, "carrierThread");
  71     private static final long TERMINATION = U.objectFieldOffset(VirtualThread.class, "termination");
  72     private static final long ON_WAITING_LIST = U.objectFieldOffset(VirtualThread.class, "onWaitingList");
  73 
  74     // scheduler and continuation
  75     private final Executor scheduler;
  76     private final Continuation cont;
  77     private final Runnable runContinuation;
  78 
  79     // virtual thread state, accessed by VM
  80     private volatile int state;
  81 
  82     /*
  83      * Virtual thread state transitions:
  84      *
  85      *      NEW -> STARTED         // Thread.start, schedule to run
  86      *  STARTED -> TERMINATED      // failed to start
  87      *  STARTED -> RUNNING         // first run
  88      *  RUNNING -> TERMINATED      // done
  89      *
  90      *  RUNNING -> PARKING         // Thread parking with LockSupport.park
  91      *  PARKING -> PARKED          // cont.yield successful, parked indefinitely
  92      *  PARKING -> PINNED          // cont.yield failed, parked indefinitely on carrier
  93      *   PARKED -> UNPARKED        // unparked, may be scheduled to continue
  94      *   PINNED -> RUNNING         // unparked, continue execution on same carrier
  95      * UNPARKED -> RUNNING         // continue execution after park

 151     private static final int TERMINATED = 99;  // final state
 152 
 153     // can be suspended from scheduling when unmounted
 154     private static final int SUSPENDED = 1 << 8;
 155 
 156     // parking permit made available by LockSupport.unpark
 157     private volatile boolean parkPermit;
 158 
 159     // blocking permit made available by unblocker thread when another thread exits monitor
 160     private volatile boolean blockPermit;
 161 
 162     // true when on the list of virtual threads waiting to be unblocked
 163     private volatile boolean onWaitingList;
 164 
 165     // next virtual thread on the list of virtual threads waiting to be unblocked
 166     private volatile VirtualThread next;
 167 
 168     // notified by Object.notify/notifyAll while waiting in Object.wait
 169     private volatile boolean notified;
 170 



 171     // timed-wait support
 172     private byte timedWaitSeqNo;
 173 
 174     // timeout for timed-park and timed-wait, only accessed on current/carrier thread
 175     private long timeout;
 176 
 177     // timer task for timed-park and timed-wait, only accessed on current/carrier thread
 178     private Future<?> timeoutTask;
 179 
 180     // carrier thread when mounted, accessed by VM
 181     private volatile Thread carrierThread;
 182 
 183     // termination object when joining, created lazily if needed
 184     private volatile CountDownLatch termination;
 185 
 186     /**
 187      * Returns the default scheduler.
 188      */
 189     static Executor defaultScheduler() {
 190         return DEFAULT_SCHEDULER;
 191     }
 192 







 193     /**
 194      * Returns the continuation scope used for virtual threads.
 195      */
 196     static ContinuationScope continuationScope() {
 197         return VTHREAD_SCOPE;
 198     }
 199 
 200     /**
 201      * Creates a new {@code VirtualThread} to run the given task with the given
 202      * scheduler. If the given scheduler is {@code null} and the current thread
 203      * is a platform thread then the newly created virtual thread will use the
 204      * default scheduler. If given scheduler is {@code null} and the current
 205      * thread is a virtual thread then the current thread's scheduler is used.








 206      *
 207      * @param scheduler the scheduler or null
 208      * @param name thread name
 209      * @param characteristics characteristics
 210      * @param task the task to execute
 211      */
 212     VirtualThread(Executor scheduler, String name, int characteristics, Runnable task) {



 213         super(name, characteristics, /*bound*/ false);
 214         Objects.requireNonNull(task);
 215 
 216         // choose scheduler if not specified
 217         if (scheduler == null) {
 218             Thread parent = Thread.currentThread();
 219             if (parent instanceof VirtualThread vparent) {
 220                 scheduler = vparent.scheduler;
 221             } else {
 222                 scheduler = DEFAULT_SCHEDULER;
 223             }
 224         }
 225 
 226         this.scheduler = scheduler;
 227         this.cont = new VThreadContinuation(this, task);
 228         this.runContinuation = this::runContinuation;
 229     }
 230 
 231     /**
 232      * The continuation that a virtual thread executes.
 233      */
 234     private static class VThreadContinuation extends Continuation {
 235         VThreadContinuation(VirtualThread vthread, Runnable task) {
 236             super(VTHREAD_SCOPE, wrap(vthread, task));
 237         }
 238         @Override
 239         protected void onPinned(Continuation.Pinned reason) {
 240         }
 241         private static Runnable wrap(VirtualThread vthread, Runnable task) {
 242             return new Runnable() {
 243                 @Hidden

 296             if (cont.isDone()) {
 297                 afterDone();
 298             } else {
 299                 afterYield();
 300             }
 301         }
 302     }
 303 
 304     /**
 305      * Cancel timeout task when continuing after timed-park or timed-wait.
 306      * The timeout task may be executing, or may have already completed.
 307      */
 308     private void cancelTimeoutTask() {
 309         if (timeoutTask != null) {
 310             timeoutTask.cancel(false);
 311             timeoutTask = null;
 312         }
 313     }
 314 
 315     /**
 316      * Submits the runContinuation task to the scheduler. For the default scheduler,
 317      * and calling it on a worker thread, the task will be pushed to the local queue,
 318      * otherwise it will be pushed to an external submission queue.
 319      * @param scheduler the scheduler
 320      * @param retryOnOOME true to retry indefinitely if OutOfMemoryError is thrown
 321      * @throws RejectedExecutionException
 322      */
 323     private void submitRunContinuation(Executor scheduler, boolean retryOnOOME) {
 324         boolean done = false;
 325         while (!done) {
 326             try {
 327                 // Pin the continuation to prevent the virtual thread from unmounting
 328                 // when submitting a task. For the default scheduler this ensures that
 329                 // the carrier doesn't change when pushing a task. For other schedulers
 330                 // it avoids deadlock that could arise due to carriers and virtual
 331                 // threads contending for a lock.
 332                 if (currentThread().isVirtual()) {
 333                     Continuation.pin();
 334                     try {
 335                         scheduler.execute(runContinuation);
 336                     } finally {
 337                         Continuation.unpin();
 338                     }
 339                 } else {
 340                     scheduler.execute(runContinuation);
 341                 }
 342                 done = true;
 343             } catch (RejectedExecutionException ree) {
 344                 submitFailed(ree);
 345                 throw ree;
 346             } catch (OutOfMemoryError e) {
 347                 if (retryOnOOME) {
 348                     U.park(false, 100_000_000); // 100ms
 349                 } else {
 350                     throw e;
 351                 }
 352             }
 353         }
 354     }
 355 
 356     /**
 357      * Submits the runContinuation task to the given scheduler as an external submit.
 358      * If OutOfMemoryError is thrown then the submit will be retried until it succeeds.
 359      * @throws RejectedExecutionException
 360      * @see ForkJoinPool#externalSubmit(ForkJoinTask)
 361      */
 362     private void externalSubmitRunContinuation(ForkJoinPool pool) {
 363         assert Thread.currentThread() instanceof CarrierThread;
 364         try {
 365             pool.externalSubmit(ForkJoinTask.adapt(runContinuation));
 366         } catch (RejectedExecutionException ree) {
 367             submitFailed(ree);
 368             throw ree;
 369         } catch (OutOfMemoryError e) {
 370             submitRunContinuation(pool, true);
 371         }
 372     }
 373 
 374     /**
 375      * Submits the runContinuation task to the scheduler. For the default scheduler,
 376      * and calling it on a worker thread, the task will be pushed to the local queue,
 377      * otherwise it will be pushed to an external submission queue.
 378      * If OutOfMemoryError is thrown then the submit will be retried until it succeeds.
 379      * @throws RejectedExecutionException
 380      */
 381     private void submitRunContinuation() {
 382         submitRunContinuation(scheduler, true);
 383     }
 384 
 385     /**
 386      * Lazy submit the runContinuation task if invoked on a carrier thread and its local
 387      * queue is empty. If not empty, or invoked by another thread, then this method works
 388      * like submitRunContinuation and just submits the task to the scheduler.
 389      * If OutOfMemoryError is thrown then the submit will be retried until it succeeds.
 390      * @throws RejectedExecutionException
 391      * @see ForkJoinPool#lazySubmit(ForkJoinTask)
 392      */
 393     private void lazySubmitRunContinuation() {

 394         if (currentThread() instanceof CarrierThread ct && ct.getQueuedTaskCount() == 0) {
 395             ForkJoinPool pool = ct.getPool();
 396             try {
 397                 pool.lazySubmit(ForkJoinTask.adapt(runContinuation));
 398             } catch (RejectedExecutionException ree) {
 399                 submitFailed(ree);
 400                 throw ree;
 401             } catch (OutOfMemoryError e) {
 402                 submitRunContinuation();
 403             }
 404         } else {
 405             submitRunContinuation();
 406         }
 407     }
 408 
 409     /**
 410      * Submits the runContinuation task to the scheduler. For the default scheduler, and
 411      * calling it a virtual thread that uses the default scheduler, the task will be
 412      * pushed to an external submission queue. This method may throw OutOfMemoryError.

 413      * @throws RejectedExecutionException
 414      * @throws OutOfMemoryError
 415      */
 416     private void externalSubmitRunContinuationOrThrow() {
 417         if (scheduler == DEFAULT_SCHEDULER && currentCarrierThread() instanceof CarrierThread ct) {

 418             try {
 419                 ct.getPool().externalSubmit(ForkJoinTask.adapt(runContinuation));
 420             } catch (RejectedExecutionException ree) {
 421                 submitFailed(ree);
 422                 throw ree;


 423             }
 424         } else {
 425             submitRunContinuation(scheduler, false);




































 426         }
 427     }
 428 
 429     /**
 430      * If enabled, emits a JFR VirtualThreadSubmitFailedEvent.
 431      */
 432     private void submitFailed(RejectedExecutionException ree) {
 433         var event = new VirtualThreadSubmitFailedEvent();
 434         if (event.isEnabled()) {
 435             event.javaThreadId = threadId();
 436             event.exceptionMessage = ree.getMessage();
 437             event.commit();
 438         }
 439     }
 440 
 441     /**
 442      * Runs a task in the context of this virtual thread.
 443      */
 444     private void run(Runnable task) {
 445         assert Thread.currentThread() == this && state == RUNNING;

 560                 long timeout = this.timeout;
 561                 assert timeout > 0;
 562                 timeoutTask = schedule(this::parkTimeoutExpired, timeout, NANOSECONDS);
 563                 setState(newState = TIMED_PARKED);
 564             }
 565 
 566             // may have been unparked while parking
 567             if (parkPermit && compareAndSetState(newState, UNPARKED)) {
 568                 // lazy submit if local queue is empty
 569                 lazySubmitRunContinuation();
 570             }
 571             return;
 572         }
 573 
 574         // Thread.yield
 575         if (s == YIELDING) {
 576             setState(YIELDED);
 577 
 578             // external submit if there are no tasks in the local task queue
 579             if (currentThread() instanceof CarrierThread ct && ct.getQueuedTaskCount() == 0) {
 580                 externalSubmitRunContinuation(ct.getPool());
 581             } else {
 582                 submitRunContinuation();
 583             }
 584             return;
 585         }
 586 
 587         // blocking on monitorenter
 588         if (s == BLOCKING) {
 589             setState(BLOCKED);
 590 
 591             // may have been unblocked while blocking
 592             if (blockPermit && compareAndSetState(BLOCKED, UNBLOCKED)) {
 593                 // lazy submit if local queue is empty
 594                 lazySubmitRunContinuation();
 595             }
 596             return;
 597         }
 598 
 599         // Object.wait
 600         if (s == WAITING || s == TIMED_WAITING) {
 601             int newState;

 602             if (s == WAITING) {
 603                 setState(newState = WAIT);
 604             } else {
 605                 // For timed-wait, a timeout task is scheduled to execute. The timeout
 606                 // task will change the thread state to UNBLOCKED and submit the thread
 607                 // to the scheduler. A sequence number is used to ensure that the timeout
 608                 // task only unblocks the thread for this timed-wait. We synchronize with
 609                 // the timeout task to coordinate access to the sequence number and to
 610                 // ensure the timeout task doesn't execute until the thread has got to
 611                 // the TIMED_WAIT state.
 612                 long timeout = this.timeout;
 613                 assert timeout > 0;
 614                 synchronized (timedWaitLock()) {
 615                     byte seqNo = ++timedWaitSeqNo;
 616                     timeoutTask = schedule(() -> waitTimeoutExpired(seqNo), timeout, MILLISECONDS);
 617                     setState(newState = TIMED_WAIT);
 618                 }
 619             }
 620 
 621             // may have been notified while in transition to wait state
 622             if (notified && compareAndSetState(newState, BLOCKED)) {
 623                 // may have even been unblocked already
 624                 if (blockPermit && compareAndSetState(BLOCKED, UNBLOCKED)) {
 625                     submitRunContinuation();
 626                 }
 627                 return;
 628             }
 629 
 630             // may have been interrupted while in transition to wait state
 631             if (interrupted && compareAndSetState(newState, UNBLOCKED)) {
 632                 submitRunContinuation();
 633                 return;
 634             }
 635             return;
 636         }
 637 
 638         assert false;
 639     }
 640 
 641     /**
 642      * Invoked after the continuation completes.
 643      */
 644     private void afterDone() {
 645         afterDone(true);
 646     }
 647 
 648     /**
 649      * Invoked after the continuation completes (or start failed). Sets the thread
 650      * state to TERMINATED and notifies anyone waiting for the thread to terminate.
 651      *

1398     @IntrinsicCandidate
1399     @JvmtiMountTransition
1400     private native void notifyJvmtiMount(boolean hide);
1401 
1402     @IntrinsicCandidate
1403     @JvmtiMountTransition
1404     private native void notifyJvmtiUnmount(boolean hide);
1405 
1406     @IntrinsicCandidate
1407     private static native void notifyJvmtiDisableSuspend(boolean enter);
1408 
1409     private static native void registerNatives();
1410     static {
1411         registerNatives();
1412 
1413         // ensure VTHREAD_GROUP is created, may be accessed by JVMTI
1414         var group = Thread.virtualThreadGroup();
1415     }
1416 
1417     /**
1418      * Creates the default ForkJoinPool scheduler.




1419      */
1420     private static ForkJoinPool createDefaultScheduler() {
1421         ForkJoinWorkerThreadFactory factory = pool -> new CarrierThread(pool);
























1422         int parallelism, maxPoolSize, minRunnable;
1423         String parallelismValue = System.getProperty("jdk.virtualThreadScheduler.parallelism");
1424         String maxPoolSizeValue = System.getProperty("jdk.virtualThreadScheduler.maxPoolSize");
1425         String minRunnableValue = System.getProperty("jdk.virtualThreadScheduler.minRunnable");
1426         if (parallelismValue != null) {
1427             parallelism = Integer.parseInt(parallelismValue);
1428         } else {
1429             parallelism = Runtime.getRuntime().availableProcessors();
1430         }
1431         if (maxPoolSizeValue != null) {
1432             maxPoolSize = Integer.parseInt(maxPoolSizeValue);
1433             parallelism = Integer.min(parallelism, maxPoolSize);
1434         } else {
1435             maxPoolSize = Integer.max(parallelism, 256);
1436         }
1437         if (minRunnableValue != null) {
1438             minRunnable = Integer.parseInt(minRunnableValue);
1439         } else {
1440             minRunnable = Integer.max(parallelism / 2, 1);
1441         }
1442         Thread.UncaughtExceptionHandler handler = (t, e) -> { };
1443         boolean asyncMode = true; // FIFO
1444         return new ForkJoinPool(parallelism, factory, handler, asyncMode,
1445                      0, maxPoolSize, minRunnable, pool -> true, 30, SECONDS);






























































1446     }
1447 
1448     /**
1449      * Schedule a runnable task to run after a delay.
1450      */
1451     private Future<?> schedule(Runnable command, long delay, TimeUnit unit) {
1452         if (scheduler instanceof ForkJoinPool pool) {
1453             return pool.schedule(command, delay, unit);
1454         } else {
1455             return DelayedTaskSchedulers.schedule(command, delay, unit);
1456         }
1457     }
1458 
1459     /**
1460      * Supports scheduling a runnable task to run after a delay. It uses a number
1461      * of ScheduledThreadPoolExecutor instances to reduce contention on the delayed
1462      * work queue used. This class is used when using a custom scheduler.
1463      */
1464     private static class DelayedTaskSchedulers {
1465         private static final ScheduledExecutorService[] INSTANCE = createDelayedTaskSchedulers();

1515                 assert changed;
1516                 vthread.unblock();
1517 
1518                 vthread = nextThread;
1519             }
1520         }
1521     }
1522 
1523     /**
1524      * Retrieves the list of virtual threads that are waiting to be unblocked, waiting
1525      * if necessary until a list of one or more threads becomes available.
1526      */
1527     private static native VirtualThread takeVirtualThreadListToUnblock();
1528 
1529     static {
1530         var unblocker = InnocuousThread.newThread("VirtualThread-unblocker",
1531                 VirtualThread::unblockVirtualThreads);
1532         unblocker.setDaemon(true);
1533         unblocker.start();
1534     }
1535 }

   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.lang.reflect.Constructor;
  28 import java.util.Locale;
  29 import java.util.Objects;
  30 import java.util.concurrent.CountDownLatch;

  31 import java.util.concurrent.Executors;
  32 import java.util.concurrent.ForkJoinPool;

  33 import java.util.concurrent.ForkJoinTask;
  34 import java.util.concurrent.Future;
  35 import java.util.concurrent.RejectedExecutionException;
  36 import java.util.concurrent.ScheduledExecutorService;
  37 import java.util.concurrent.ScheduledThreadPoolExecutor;
  38 import java.util.concurrent.TimeUnit;
  39 import jdk.internal.event.VirtualThreadEndEvent;
  40 import jdk.internal.event.VirtualThreadStartEvent;
  41 import jdk.internal.event.VirtualThreadSubmitFailedEvent;
  42 import jdk.internal.misc.CarrierThread;
  43 import jdk.internal.misc.InnocuousThread;
  44 import jdk.internal.misc.Unsafe;
  45 import jdk.internal.vm.Continuation;
  46 import jdk.internal.vm.ContinuationScope;
  47 import jdk.internal.vm.StackableScope;
  48 import jdk.internal.vm.ThreadContainer;
  49 import jdk.internal.vm.ThreadContainers;
  50 import jdk.internal.vm.annotation.ChangesCurrentThread;
  51 import jdk.internal.vm.annotation.Hidden;
  52 import jdk.internal.vm.annotation.IntrinsicCandidate;
  53 import jdk.internal.vm.annotation.JvmtiHideEvents;
  54 import jdk.internal.vm.annotation.JvmtiMountTransition;
  55 import jdk.internal.vm.annotation.ReservedStackAccess;
  56 import sun.nio.ch.Interruptible;
  57 import static java.util.concurrent.TimeUnit.*;
  58 
  59 /**
  60  * A thread that is scheduled by the Java virtual machine rather than the operating system.
  61  */
  62 final class VirtualThread extends BaseVirtualThread {
  63     private static final Unsafe U = Unsafe.getUnsafe();
  64     private static final ContinuationScope VTHREAD_SCOPE = new ContinuationScope("VirtualThreads");
  65 
  66     private static final ForkJoinPool BUILTIN_DEFAULT_SCHEDULER;
  67     private static final VirtualThreadScheduler DEFAULT_SCHEDULER;
  68     static {
  69         // experimental
  70         String propValue = System.getProperty("jdk.virtualThreadScheduler.implClass");
  71         if (propValue != null) {
  72             var builtinScheduler = createBuiltinDefaultScheduler(true);
  73             VirtualThreadScheduler defaultScheduler = builtinScheduler.externalView();
  74             for (String cn: propValue.split(",")) {
  75                 defaultScheduler = loadCustomScheduler(defaultScheduler, cn);
  76             }
  77             BUILTIN_DEFAULT_SCHEDULER = builtinScheduler;
  78             DEFAULT_SCHEDULER = defaultScheduler;
  79         } else {
  80             var builtinScheduler = createBuiltinDefaultScheduler(false);
  81             BUILTIN_DEFAULT_SCHEDULER = builtinScheduler;
  82             DEFAULT_SCHEDULER = builtinScheduler;
  83         }
  84     }
  85 
  86     private static final long STATE = U.objectFieldOffset(VirtualThread.class, "state");
  87     private static final long PARK_PERMIT = U.objectFieldOffset(VirtualThread.class, "parkPermit");
  88     private static final long CARRIER_THREAD = U.objectFieldOffset(VirtualThread.class, "carrierThread");
  89     private static final long TERMINATION = U.objectFieldOffset(VirtualThread.class, "termination");
  90     private static final long ON_WAITING_LIST = U.objectFieldOffset(VirtualThread.class, "onWaitingList");
  91 
  92     // scheduler and continuation
  93     private final VirtualThreadScheduler scheduler;
  94     private final Continuation cont;
  95     private final Runnable runContinuation;
  96 
  97     // virtual thread state, accessed by VM
  98     private volatile int state;
  99 
 100     /*
 101      * Virtual thread state transitions:
 102      *
 103      *      NEW -> STARTED         // Thread.start, schedule to run
 104      *  STARTED -> TERMINATED      // failed to start
 105      *  STARTED -> RUNNING         // first run
 106      *  RUNNING -> TERMINATED      // done
 107      *
 108      *  RUNNING -> PARKING         // Thread parking with LockSupport.park
 109      *  PARKING -> PARKED          // cont.yield successful, parked indefinitely
 110      *  PARKING -> PINNED          // cont.yield failed, parked indefinitely on carrier
 111      *   PARKED -> UNPARKED        // unparked, may be scheduled to continue
 112      *   PINNED -> RUNNING         // unparked, continue execution on same carrier
 113      * UNPARKED -> RUNNING         // continue execution after park

 169     private static final int TERMINATED = 99;  // final state
 170 
 171     // can be suspended from scheduling when unmounted
 172     private static final int SUSPENDED = 1 << 8;
 173 
 174     // parking permit made available by LockSupport.unpark
 175     private volatile boolean parkPermit;
 176 
 177     // blocking permit made available by unblocker thread when another thread exits monitor
 178     private volatile boolean blockPermit;
 179 
 180     // true when on the list of virtual threads waiting to be unblocked
 181     private volatile boolean onWaitingList;
 182 
 183     // next virtual thread on the list of virtual threads waiting to be unblocked
 184     private volatile VirtualThread next;
 185 
 186     // notified by Object.notify/notifyAll while waiting in Object.wait
 187     private volatile boolean notified;
 188 
 189     // true when waiting in Object.wait, false for VM internal uninterruptible Object.wait
 190     private volatile boolean interruptibleWait;
 191 
 192     // timed-wait support
 193     private byte timedWaitSeqNo;
 194 
 195     // timeout for timed-park and timed-wait, only accessed on current/carrier thread
 196     private long timeout;
 197 
 198     // timer task for timed-park and timed-wait, only accessed on current/carrier thread
 199     private Future<?> timeoutTask;
 200 
 201     // carrier thread when mounted, accessed by VM
 202     private volatile Thread carrierThread;
 203 
 204     // termination object when joining, created lazily if needed
 205     private volatile CountDownLatch termination;
 206 
 207     /**
 208      * Returns the default scheduler.
 209      */
 210     static VirtualThreadScheduler defaultScheduler() {
 211         return DEFAULT_SCHEDULER;
 212     }
 213 
 214     /**
 215      * Returns true if using a custom default scheduler.
 216      */
 217     static boolean isCustomDefaultScheduler() {
 218         return DEFAULT_SCHEDULER != BUILTIN_DEFAULT_SCHEDULER;
 219     }
 220 
 221     /**
 222      * Returns the continuation scope used for virtual threads.
 223      */
 224     static ContinuationScope continuationScope() {
 225         return VTHREAD_SCOPE;
 226     }
 227 
 228     /**
 229      * Return the scheduler for this thread.
 230      * @param revealBuiltin true to reveal the built-in default scheduler, false to hide
 231      */
 232     VirtualThreadScheduler scheduler(boolean revealBuiltin) {
 233         if (scheduler instanceof BuiltinDefaultScheduler builtin && !revealBuiltin) {
 234             return builtin.externalView();
 235         } else {
 236             return scheduler;
 237         }
 238     }
 239 
 240     /**
 241      * Creates a new {@code VirtualThread} to run the given task with the given scheduler.
 242      *
 243      * @param scheduler the scheduler or null for default scheduler
 244      * @param name thread name
 245      * @param characteristics characteristics
 246      * @param task the task to execute
 247      */
 248     VirtualThread(VirtualThreadScheduler scheduler,
 249                   String name,
 250                   int characteristics,
 251                   Runnable task) {
 252         super(name, characteristics, /*bound*/ false);
 253         Objects.requireNonNull(task);
 254 
 255         // use default scheduler if not provided
 256         if (scheduler == null) {
 257             scheduler = DEFAULT_SCHEDULER;





 258         }
 259 
 260         this.scheduler = scheduler;
 261         this.cont = new VThreadContinuation(this, task);
 262         this.runContinuation = this::runContinuation;
 263     }
 264 
 265     /**
 266      * The continuation that a virtual thread executes.
 267      */
 268     private static class VThreadContinuation extends Continuation {
 269         VThreadContinuation(VirtualThread vthread, Runnable task) {
 270             super(VTHREAD_SCOPE, wrap(vthread, task));
 271         }
 272         @Override
 273         protected void onPinned(Continuation.Pinned reason) {
 274         }
 275         private static Runnable wrap(VirtualThread vthread, Runnable task) {
 276             return new Runnable() {
 277                 @Hidden

 330             if (cont.isDone()) {
 331                 afterDone();
 332             } else {
 333                 afterYield();
 334             }
 335         }
 336     }
 337 
 338     /**
 339      * Cancel timeout task when continuing after timed-park or timed-wait.
 340      * The timeout task may be executing, or may have already completed.
 341      */
 342     private void cancelTimeoutTask() {
 343         if (timeoutTask != null) {
 344             timeoutTask.cancel(false);
 345             timeoutTask = null;
 346         }
 347     }
 348 
 349     /**
 350      * Submits the runContinuation task to the scheduler. For the built-in default
 351      * scheduler, the task will be pushed to the local queue if possible, otherwise it
 352      * will be pushed to an external submission queue.

 353      * @param retryOnOOME true to retry indefinitely if OutOfMemoryError is thrown
 354      * @throws RejectedExecutionException
 355      */
 356     private void submitRunContinuation(boolean retryOnOOME) {
 357         boolean done = false;
 358         while (!done) {
 359             try {
 360                 // Pin the continuation to prevent the virtual thread from unmounting
 361                 // when submitting a task. For the default scheduler this ensures that
 362                 // the carrier doesn't change when pushing a task. For other schedulers
 363                 // it avoids deadlock that could arise due to carriers and virtual
 364                 // threads contending for a lock.
 365                 if (currentThread().isVirtual()) {
 366                     Continuation.pin();
 367                     try {
 368                         scheduler.onContinue(this, runContinuation);
 369                     } finally {
 370                         Continuation.unpin();
 371                     }
 372                 } else {
 373                     scheduler.onContinue(this, runContinuation);
 374                 }
 375                 done = true;
 376             } catch (RejectedExecutionException ree) {
 377                 submitFailed(ree);
 378                 throw ree;
 379             } catch (OutOfMemoryError e) {
 380                 if (retryOnOOME) {
 381                     U.park(false, 100_000_000); // 100ms
 382                 } else {
 383                     throw e;
 384                 }
 385             }
 386         }
 387     }
 388 


















 389     /**
 390      * Submits the runContinuation task to the scheduler. For the default scheduler,
 391      * and calling it on a worker thread, the task will be pushed to the local queue,
 392      * otherwise it will be pushed to an external submission queue.
 393      * If OutOfMemoryError is thrown then the submit will be retried until it succeeds.
 394      * @throws RejectedExecutionException
 395      */
 396     private void submitRunContinuation() {
 397         submitRunContinuation(true);
 398     }
 399 
 400     /**
 401      * Invoked from a carrier thread to lazy submit the runContinuation task to the
 402      * carrier's local queue if the queue is empty. If not empty, or invoked by a thread
 403      * for a custom scheduler, then it just submits the task to the scheduler.
 404      * If OutOfMemoryError is thrown then the submit will be retried until it succeeds.
 405      * @throws RejectedExecutionException
 406      * @see ForkJoinPool#lazySubmit(ForkJoinTask)
 407      */
 408     private void lazySubmitRunContinuation() {
 409         assert !currentThread().isVirtual();
 410         if (currentThread() instanceof CarrierThread ct && ct.getQueuedTaskCount() == 0) {

 411             try {
 412                 ct.getPool().lazySubmit(ForkJoinTask.adapt(runContinuation));
 413             } catch (RejectedExecutionException ree) {
 414                 submitFailed(ree);
 415                 throw ree;
 416             } catch (OutOfMemoryError e) {
 417                 submitRunContinuation();
 418             }
 419         } else {
 420             submitRunContinuation();
 421         }
 422     }
 423 
 424     /**
 425      * Invoked from a carrier thread to externally submit the runContinuation task to the
 426      * scheduler. If invoked by a thread for a custom scheduler, then it just submits the
 427      * task to the scheduler.
 428      * If OutOfMemoryError is thrown then the submit will be retried until it succeeds.
 429      * @throws RejectedExecutionException
 430      * @see ForkJoinPool#externalSubmit(ForkJoinTask)
 431      */
 432     private void externalSubmitRunContinuation() {
 433         assert !currentThread().isVirtual();
 434         if (currentThread() instanceof CarrierThread ct) {
 435             try {
 436                 ct.getPool().externalSubmit(ForkJoinTask.adapt(runContinuation));
 437             } catch (RejectedExecutionException ree) {
 438                 submitFailed(ree);
 439                 throw ree;
 440             } catch (OutOfMemoryError e) {
 441                 submitRunContinuation();
 442             }
 443         } else {
 444             submitRunContinuation();
 445         }
 446     }
 447 
 448     /**
 449      * Invoked from Thread.start to externally submit the runContinuation task to the
 450      * scheduler. If this virtual thread is scheduled by the built-in default scheduler,
 451      * and this method is called from a virtual thread scheduled by the built-in default
 452      * scheduler, then it uses externalSubmit to ensure that the task is pushed to an
 453      * external submission queue rather than the local queue.
 454      * @throws RejectedExecutionException
 455      * @throws OutOfMemoryError
 456      * @see ForkJoinPool#externalSubmit(ForkJoinTask)
 457      */
 458     private void externalSubmitRunContinuationOrThrow() {
 459         try {
 460             if (currentThread().isVirtual()) {
 461                 // Pin the continuation to prevent the virtual thread from unmounting
 462                 // when submitting a task. This avoids deadlock that could arise due to
 463                 // carriers and virtual threads contending for a lock.
 464                 Continuation.pin();
 465                 try {
 466                     if (scheduler == BUILTIN_DEFAULT_SCHEDULER
 467                             && currentCarrierThread() instanceof CarrierThread ct) {
 468                         ct.getPool().externalSubmit(ForkJoinTask.adapt(runContinuation));
 469                     } else {
 470                         scheduler.onStart(this, runContinuation);
 471                     }
 472                 } finally {
 473                     Continuation.unpin();
 474                 }
 475             } else {
 476                 scheduler.onStart(this, runContinuation);
 477             }
 478         } catch (RejectedExecutionException ree) {
 479             submitFailed(ree);
 480             throw ree;
 481         }
 482     }
 483 
 484     /**
 485      * If enabled, emits a JFR VirtualThreadSubmitFailedEvent.
 486      */
 487     private void submitFailed(RejectedExecutionException ree) {
 488         var event = new VirtualThreadSubmitFailedEvent();
 489         if (event.isEnabled()) {
 490             event.javaThreadId = threadId();
 491             event.exceptionMessage = ree.getMessage();
 492             event.commit();
 493         }
 494     }
 495 
 496     /**
 497      * Runs a task in the context of this virtual thread.
 498      */
 499     private void run(Runnable task) {
 500         assert Thread.currentThread() == this && state == RUNNING;

 615                 long timeout = this.timeout;
 616                 assert timeout > 0;
 617                 timeoutTask = schedule(this::parkTimeoutExpired, timeout, NANOSECONDS);
 618                 setState(newState = TIMED_PARKED);
 619             }
 620 
 621             // may have been unparked while parking
 622             if (parkPermit && compareAndSetState(newState, UNPARKED)) {
 623                 // lazy submit if local queue is empty
 624                 lazySubmitRunContinuation();
 625             }
 626             return;
 627         }
 628 
 629         // Thread.yield
 630         if (s == YIELDING) {
 631             setState(YIELDED);
 632 
 633             // external submit if there are no tasks in the local task queue
 634             if (currentThread() instanceof CarrierThread ct && ct.getQueuedTaskCount() == 0) {
 635                 externalSubmitRunContinuation();
 636             } else {
 637                 submitRunContinuation();
 638             }
 639             return;
 640         }
 641 
 642         // blocking on monitorenter
 643         if (s == BLOCKING) {
 644             setState(BLOCKED);
 645 
 646             // may have been unblocked while blocking
 647             if (blockPermit && compareAndSetState(BLOCKED, UNBLOCKED)) {
 648                 // lazy submit if local queue is empty
 649                 lazySubmitRunContinuation();
 650             }
 651             return;
 652         }
 653 
 654         // Object.wait
 655         if (s == WAITING || s == TIMED_WAITING) {
 656             int newState;
 657             boolean interruptible = interruptibleWait;
 658             if (s == WAITING) {
 659                 setState(newState = WAIT);
 660             } else {
 661                 // For timed-wait, a timeout task is scheduled to execute. The timeout
 662                 // task will change the thread state to UNBLOCKED and submit the thread
 663                 // to the scheduler. A sequence number is used to ensure that the timeout
 664                 // task only unblocks the thread for this timed-wait. We synchronize with
 665                 // the timeout task to coordinate access to the sequence number and to
 666                 // ensure the timeout task doesn't execute until the thread has got to
 667                 // the TIMED_WAIT state.
 668                 long timeout = this.timeout;
 669                 assert timeout > 0;
 670                 synchronized (timedWaitLock()) {
 671                     byte seqNo = ++timedWaitSeqNo;
 672                     timeoutTask = schedule(() -> waitTimeoutExpired(seqNo), timeout, MILLISECONDS);
 673                     setState(newState = TIMED_WAIT);
 674                 }
 675             }
 676 
 677             // may have been notified while in transition to wait state
 678             if (notified && compareAndSetState(newState, BLOCKED)) {
 679                 // may have even been unblocked already
 680                 if (blockPermit && compareAndSetState(BLOCKED, UNBLOCKED)) {
 681                     submitRunContinuation();
 682                 }
 683                 return;
 684             }
 685 
 686             // may have been interrupted while in transition to wait state
 687             if (interruptible && interrupted && compareAndSetState(newState, UNBLOCKED)) {
 688                 submitRunContinuation();
 689                 return;
 690             }
 691             return;
 692         }
 693 
 694         assert false;
 695     }
 696 
 697     /**
 698      * Invoked after the continuation completes.
 699      */
 700     private void afterDone() {
 701         afterDone(true);
 702     }
 703 
 704     /**
 705      * Invoked after the continuation completes (or start failed). Sets the thread
 706      * state to TERMINATED and notifies anyone waiting for the thread to terminate.
 707      *

1454     @IntrinsicCandidate
1455     @JvmtiMountTransition
1456     private native void notifyJvmtiMount(boolean hide);
1457 
1458     @IntrinsicCandidate
1459     @JvmtiMountTransition
1460     private native void notifyJvmtiUnmount(boolean hide);
1461 
1462     @IntrinsicCandidate
1463     private static native void notifyJvmtiDisableSuspend(boolean enter);
1464 
1465     private static native void registerNatives();
1466     static {
1467         registerNatives();
1468 
1469         // ensure VTHREAD_GROUP is created, may be accessed by JVMTI
1470         var group = Thread.virtualThreadGroup();
1471     }
1472 
1473     /**
1474      * Loads a VirtualThreadScheduler with the given class name. The class must be public
1475      * in an exported package, with public one-arg or no-arg constructor, and be visible
1476      * to the system class loader.
1477      * @param delegate the scheduler that the custom scheduler may delegate to
1478      * @param cn the class name of the custom scheduler
1479      */
1480     private static VirtualThreadScheduler loadCustomScheduler(VirtualThreadScheduler delegate, String cn) {
1481         try {
1482             Class<?> clazz = Class.forName(cn, true, ClassLoader.getSystemClassLoader());
1483             VirtualThreadScheduler scheduler;
1484             try {
1485                 // 1-arg constructor
1486                 Constructor<?> ctor = clazz.getConstructor(VirtualThreadScheduler.class);
1487                 scheduler = (VirtualThreadScheduler) ctor.newInstance(delegate);
1488             } catch (NoSuchMethodException e) {
1489                 // 0-arg constructor
1490                 Constructor<?> ctor = clazz.getConstructor();
1491                 scheduler = (VirtualThreadScheduler) ctor.newInstance();
1492             }
1493             System.err.println("""
1494                 WARNING: Using custom default scheduler, this is an experimental feature!""");
1495             return scheduler;
1496         } catch (Exception ex) {
1497             throw new Error(ex);
1498         }
1499     }
1500 
1501     /**
1502      * Creates the built-in default ForkJoinPool scheduler.
1503      * @param wrapped true if wrapped by a custom default scheduler
1504      */
1505     private static BuiltinDefaultScheduler createBuiltinDefaultScheduler(boolean wrapped) {
1506         int parallelism, maxPoolSize, minRunnable;
1507         String parallelismValue = System.getProperty("jdk.virtualThreadScheduler.parallelism");
1508         String maxPoolSizeValue = System.getProperty("jdk.virtualThreadScheduler.maxPoolSize");
1509         String minRunnableValue = System.getProperty("jdk.virtualThreadScheduler.minRunnable");
1510         if (parallelismValue != null) {
1511             parallelism = Integer.parseInt(parallelismValue);
1512         } else {
1513             parallelism = Runtime.getRuntime().availableProcessors();
1514         }
1515         if (maxPoolSizeValue != null) {
1516             maxPoolSize = Integer.parseInt(maxPoolSizeValue);
1517             parallelism = Integer.min(parallelism, maxPoolSize);
1518         } else {
1519             maxPoolSize = Integer.max(parallelism, 256);
1520         }
1521         if (minRunnableValue != null) {
1522             minRunnable = Integer.parseInt(minRunnableValue);
1523         } else {
1524             minRunnable = Integer.max(parallelism / 2, 1);
1525         }
1526         return new BuiltinDefaultScheduler(parallelism, maxPoolSize, minRunnable, wrapped);
1527     }
1528 
1529     /**
1530      * The built-in default ForkJoinPool scheduler.
1531      */
1532     private static class BuiltinDefaultScheduler
1533             extends ForkJoinPool implements VirtualThreadScheduler {
1534 
1535         private static final StableValue<VirtualThreadScheduler> VIEW = StableValue.of();
1536 
1537         BuiltinDefaultScheduler(int parallelism, int maxPoolSize, int minRunnable, boolean wrapped) {
1538             ForkJoinWorkerThreadFactory factory = wrapped
1539                     ? ForkJoinPool.defaultForkJoinWorkerThreadFactory
1540                     : CarrierThread::new;
1541             Thread.UncaughtExceptionHandler handler = (t, e) -> { };
1542             boolean asyncMode = true; // FIFO
1543             super(parallelism, factory, handler, asyncMode,
1544                     0, maxPoolSize, minRunnable, pool -> true, 30, SECONDS);
1545         }
1546 
1547         private void adaptAndExecute(Runnable task) {
1548             execute(ForkJoinTask.adapt(task));
1549         }
1550 
1551         @Override
1552         public void onStart(Thread vthread, Runnable task) {
1553             adaptAndExecute(task);
1554         }
1555 
1556         @Override
1557         public void onContinue(Thread vthread, Runnable task) {
1558             adaptAndExecute(task);
1559         }
1560 
1561         /**
1562          * Wraps the scheduler to avoid leaking a direct reference.
1563          */
1564         VirtualThreadScheduler externalView() {
1565             BuiltinDefaultScheduler builtin = this;
1566             return VIEW.orElseSet(() -> {
1567                 return new VirtualThreadScheduler() {
1568                     private void execute(Thread thread, Runnable task) {
1569                         Objects.requireNonNull(thread);
1570                         if (thread instanceof VirtualThread vthread) {
1571                             VirtualThreadScheduler scheduler = vthread.scheduler;
1572                             if (scheduler == this || scheduler == DEFAULT_SCHEDULER) {
1573                                 builtin.adaptAndExecute(task);
1574                             } else {
1575                                 throw new IllegalArgumentException();
1576                             }
1577                         } else {
1578                             throw new UnsupportedOperationException();
1579                         }
1580                     }
1581                     @Override
1582                     public void onStart(Thread thread, Runnable task) {
1583                         execute(thread, task);
1584                     }
1585                     @Override
1586                     public void onContinue(Thread thread, Runnable task) {
1587                         execute(thread, task);
1588                     }
1589                 };
1590             });
1591         }
1592     }
1593 
1594     /**
1595      * Schedule a runnable task to run after a delay.
1596      */
1597     private Future<?> schedule(Runnable command, long delay, TimeUnit unit) {
1598         if (scheduler instanceof ForkJoinPool pool) {
1599             return pool.schedule(command, delay, unit);
1600         } else {
1601             return DelayedTaskSchedulers.schedule(command, delay, unit);
1602         }
1603     }
1604 
1605     /**
1606      * Supports scheduling a runnable task to run after a delay. It uses a number
1607      * of ScheduledThreadPoolExecutor instances to reduce contention on the delayed
1608      * work queue used. This class is used when using a custom scheduler.
1609      */
1610     private static class DelayedTaskSchedulers {
1611         private static final ScheduledExecutorService[] INSTANCE = createDelayedTaskSchedulers();

1661                 assert changed;
1662                 vthread.unblock();
1663 
1664                 vthread = nextThread;
1665             }
1666         }
1667     }
1668 
1669     /**
1670      * Retrieves the list of virtual threads that are waiting to be unblocked, waiting
1671      * if necessary until a list of one or more threads becomes available.
1672      */
1673     private static native VirtualThread takeVirtualThreadListToUnblock();
1674 
1675     static {
1676         var unblocker = InnocuousThread.newThread("VirtualThread-unblocker",
1677                 VirtualThread::unblockVirtualThreads);
1678         unblocker.setDaemon(true);
1679         unblocker.start();
1680     }
1681 }
< prev index next >