1 /*
  2  * Copyright (c) 2003, 2018, 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 package nsk.jvmti.GetThreadInfo;
 25 
 26 import java.io.PrintStream;
 27 import java.util.concurrent.ThreadFactory;
 28 
 29 public class thrinfo001 {
 30 
 31     final static int JCK_STATUS_BASE = 95;
 32 
 33     static {
 34         try {
 35             System.loadLibrary("thrinfo001");
 36         } catch (UnsatisfiedLinkError ule) {
 37             System.err.println("Could not load thrinfo001 library");
 38             System.err.println("java.library.path:"
 39                 + System.getProperty("java.library.path"));
 40             throw ule;
 41         }
 42     }
 43 
 44     native static void checkInfo(Thread thr, ThreadGroup thr_group, int ind);
 45     native static int getRes();
 46 
 47     public static void main(String args[]) {
 48         args = nsk.share.jvmti.JVMTITest.commonInit(args);
 49 
 50         // produce JCK-like exit status.
 51         System.exit(run(args, System.out) + JCK_STATUS_BASE);
 52     }
 53 
 54     public static int run(String argv[], PrintStream ref) {
 55         Thread currThr = Thread.currentThread();
 56         checkInfo(currThr, currThr.getThreadGroup(), 0);
 57 
 58         ThreadGroup tg = new ThreadGroup("tg1");
 59         thrinfo001a t_a = new thrinfo001a(tg, "thread1");
 60         t_a.setPriority(Thread.MIN_PRIORITY + 2);
 61         t_a.setDaemon(true);
 62         checkInfo(t_a, tg, 1);
 63         t_a.start();
 64         try {
 65             t_a.join();
 66         } catch (InterruptedException e) {}
 67         checkInfo(t_a, t_a.getThreadGroup(), 1);
 68 
 69         thrinfo001b t_b = new thrinfo001b();
 70         t_b.setPriority(Thread.MIN_PRIORITY);
 71         t_b.setDaemon(true);
 72         checkInfo(t_b, t_b.getThreadGroup(), 2);
 73         t_b.start();
 74         try {
 75             t_b.join();
 76         } catch (InterruptedException e) {}
 77         checkInfo(t_b, t_b.getThreadGroup(), 2);
 78 
 79 
 80         Thread t_c = virtualThreadFactory().newThread(new thrinfo001c());
 81         t_c.setName("vthread");
 82 
 83         checkInfo(t_c, t_c.getThreadGroup(), 3);
 84         t_c.start();
 85         try {
 86             t_c.join();
 87         } catch (InterruptedException e) {}
 88         checkInfo(t_c, t_c.getThreadGroup(), 3);
 89 
 90         return getRes();
 91     }
 92 
 93     private static ThreadFactory virtualThreadFactory() {
 94         try {
 95             Object builder = Thread.class.getMethod("ofVirtual").invoke(null);
 96             Class<?> clazz = Class.forName("java.lang.Thread$Builder");
 97             java.lang.reflect.Method factory = clazz.getMethod("factory");
 98             return (ThreadFactory) factory.invoke(builder);
 99         } catch (RuntimeException | Error e) {
100             throw e;
101         } catch (Exception e) {
102             throw new RuntimeException(e);
103         }
104     }
105 }
106 
107 class thrinfo001a extends Thread {
108     thrinfo001a(ThreadGroup tg, String name) {
109         super(tg, name);
110     }
111 
112     public void run() {
113         Thread currThr = Thread.currentThread();
114         thrinfo001.checkInfo(currThr, currThr.getThreadGroup(), 1);
115     }
116 }
117 
118 class thrinfo001b extends Thread {
119     public void run() {
120         Thread currThr = Thread.currentThread();
121         thrinfo001.checkInfo(currThr, currThr.getThreadGroup(), 2);
122     }
123 }
124 
125 class thrinfo001c implements Runnable {
126     public void run() {
127         Thread currThr = Thread.currentThread();
128         thrinfo001.checkInfo(currThr, currThr.getThreadGroup(), 3);
129     }
130 }