1 /*
 2  * Copyright (c) 2019, 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.
 8  *
 9  * This code is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * version 2 for more details (a copy is included in the LICENSE file that
13  * accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License version
16  * 2 along with this work; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  */
23 
24 import java.lang.reflect.Constructor;
25 import java.lang.reflect.InvocationTargetException;
26 import java.util.concurrent.Executor;
27 import java.util.concurrent.Executors;
28 
29 /**
30  * Helper class for creating Thread buidlers.
31  *
32  * Tests using this class need to open java.base/java.lang.
33  */
34 class ThreadBuilders {
35     private ThreadBuilders() { }
36 
37     private static final Constructor<?> VTBUILDER_CTOR;
38     static {
39         try {
40             Class<?> clazz = Class.forName("java.lang.ThreadBuilders$VirtualThreadBuilder");
41             Constructor<?> ctor = clazz.getDeclaredConstructor(Executor.class);
42             ctor.setAccessible(true);
43             VTBUILDER_CTOR = ctor;
44         } catch (Exception e) {
45             throw new InternalError(e);
46         }
47     }
48 
49     /**
50      * Returns a builder to create virtual threads that use the given scheduler.
51      * @throws UnsupportedOperationException if custom schedulers are not supported
52      */
53     static Thread.Builder.OfVirtual virtualThreadBuilder(Executor scheduler) {
54         try {
55             return (Thread.Builder.OfVirtual) VTBUILDER_CTOR.newInstance(scheduler);
56         } catch (InvocationTargetException e) {
57             Throwable cause = e.getCause();
58             if (cause instanceof RuntimeException re) {
59                 throw re;
60             }
61             throw new RuntimeException(e);
62         } catch (Exception e) {
63             throw new RuntimeException(e);
64         }
65     }
66 
67     /**
68      * Return true if custom schedulers are supported.
69      */
70     static boolean supportsCustomScheduler() {
71         try (var pool = Executors.newCachedThreadPool()) {
72             try {
73                 virtualThreadBuilder(pool);
74                 return true;
75             } catch (UnsupportedOperationException e) {
76                 return false;
77             }
78         }
79     }
80 }