1 /* 2 * Copyright (c) 2018, 2022, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package jdk.internal.misc; 27 28 import java.util.concurrent.TimeUnit; 29 import java.util.concurrent.RejectedExecutionException; 30 31 /** 32 * Defines static methods to support execution in the context of a virtual thread. 33 * 34 * @hide 35 */ 36 public final class VirtualThreads { 37 // Android-removed: Not used in Android. 38 // private static final JavaLangAccess JLA; 39 // static { 40 // JLA = SharedSecrets.getJavaLangAccess(); 41 // if (JLA == null) { 42 // throw new InternalError("JavaLangAccess not setup"); 43 // } 44 // } VirtualThreads()45 private VirtualThreads() { } 46 47 /** 48 * Parks the current virtual thread until it is unparked or interrupted. 49 * If already unparked then the parking permit is consumed and this method 50 * completes immediately (meaning it doesn't yield). It also completes 51 * immediately if the interrupt status is set. 52 * @throws WrongThreadException if the current thread is not a virtual thread 53 */ park()54 public static void park() { 55 // Android-removed: Not used in Android. 56 // JLA.parkVirtualThread(); 57 } 58 59 /** 60 * Parks the current virtual thread up to the given waiting time or until it 61 * is unparked or interrupted. If already unparked then the parking permit is 62 * consumed and this method completes immediately (meaning it doesn't yield). 63 * It also completes immediately if the interrupt status is set or the waiting 64 * time is {@code <= 0}. 65 * @param nanos the maximum number of nanoseconds to wait 66 * @throws WrongThreadException if the current thread is not a virtual thread 67 */ park(long nanos)68 public static void park(long nanos) { 69 // Android-removed: Not used in Android. 70 // JLA.parkVirtualThread(nanos); 71 } 72 73 /** 74 * Parks the current virtual thread until the given deadline or until is is 75 * unparked or interrupted. If already unparked then the parking permit is 76 * consumed and this method completes immediately (meaning it doesn't yield). 77 * It also completes immediately if the interrupt status is set or the 78 * deadline has past. 79 * @param deadline absolute time, in milliseconds, from the epoch 80 * @throws WrongThreadException if the current thread is not a virtual thread 81 */ parkUntil(long deadline)82 public static void parkUntil(long deadline) { 83 // Android-removed: Not used in Android. 84 // long millis = deadline - System.currentTimeMillis(); 85 // long nanos = TimeUnit.NANOSECONDS.convert(millis, TimeUnit.MILLISECONDS); 86 // park(nanos); 87 } 88 89 /** 90 * Re-enables a virtual thread for scheduling. If the thread was parked then 91 * it will be unblocked, otherwise its next attempt to park will not block 92 * @param thread the virtual thread to unpark 93 * @throws IllegalArgumentException if the thread is not a virtual thread 94 * @throws RejectedExecutionException if the scheduler cannot accept a task 95 */ unpark(Thread thread)96 public static void unpark(Thread thread) { 97 // Android-removed: Not used in Android. 98 // JLA.unparkVirtualThread(thread); 99 } 100 } 101