1 /*
 2  * Copyright (c) 2018, 2021, 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 
26 package jdk.internal.misc;
27 
28 import java.util.concurrent.TimeUnit;
29 import java.util.concurrent.RejectedExecutionException;
30 import jdk.internal.access.JavaLangAccess;
31 import jdk.internal.access.SharedSecrets;
32 
33 /**
34  * Defines static methods to support execution in the context of a virtual thread.
35  */
36 public final class VirtualThreads {
37     private static final JavaLangAccess JLA;
38     static {
39         JLA = SharedSecrets.getJavaLangAccess();
40         if (JLA == null) {
41             throw new InternalError("JavaLangAccess not setup");
42         }
43     }
44     private VirtualThreads() { }
45 
46     /**
47      * Returns the current carrier thread.
48      */
49     public static Thread currentCarrierThread() {
50         return JLA.currentCarrierThread();
51     }
52 
53     /**
54      * Parks the current virtual thread until it is unparked or interrupted.
55      * If already unparked then the parking permit is consumed and this method
56      * completes immediately (meaning it doesn't yield). It also completes
57      * immediately if the interrupt status is set.
58      */
59     public static void park() {
60         JLA.parkVirtualThread();
61     }
62 
63     /**
64      * Parks the current virtual thread up to the given waiting time or until it
65      * is unparked or interrupted. If already unparked then the parking permit is
66      * consumed and this method completes immediately (meaning it doesn't yield).
67      * It also completes immediately if the interrupt status is set or the waiting
68      * time is {@code <= 0}.
69      *
70      * @param nanos the maximum number of nanoseconds to wait.
71      */
72     public static void park(long nanos) {
73         JLA.parkVirtualThread(nanos);
74     }
75 
76     /**
77      * Parks the current virtual thread until the given deadline or until is is
78      * unparked or interrupted. If already unparked then the parking permit is
79      * consumed and this method completes immediately (meaning it doesn't yield).
80      * It also completes immediately if the interrupt status is set or the
81      * deadline has past.
82      *
83      * @param deadline absolute time, in milliseconds, from the epoch
84      */
85     public static void parkUntil(long deadline) {
86         long millis = deadline - System.currentTimeMillis();
87         long nanos = TimeUnit.NANOSECONDS.convert(millis, TimeUnit.MILLISECONDS);
88         park(nanos);
89     }
90 
91     /**
92      * Unparks a virtual thread. The thread's task is pushed to the current carrier
93      * thread's work queue when invoked from a virtual thread.
94      * @throws RejectedExecutionException if the scheduler cannot accept a task
95      */
96     public static void unpark(Thread thread) {
97         JLA.unparkVirtualThread(thread);
98     }
99 }