blob: 475f5440ab12b8c13e021706758497b376697f30 [file] [log] [blame]
Roland Levillain379e5732017-08-21 19:26:22 +01001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
jeffhao5d1ac922011-09-29 17:41:15 -070017import sun.misc.Unsafe;
18
19import java.lang.reflect.Field;
20
21public class Main {
22 private static Unsafe UNSAFE;
23
24 public static void main(String[] args) throws Exception {
25 setUp();
26
27 ParkTester test = new ParkTester();
28
29 System.out.println("Test starting");
30
31 test.start();
32 UNSAFE.unpark(test);
jeffhao5d1ac922011-09-29 17:41:15 -070033
34 System.out.println("GC'ing");
35 System.gc();
Mathieu Chartier7befd0e2014-02-03 17:48:41 -080036 System.runFinalization();
jeffhao5d1ac922011-09-29 17:41:15 -070037 System.gc();
38
39 System.out.println("Asking thread to park");
40 test.parkNow = true;
41
42 try {
Roland Levillain379e5732017-08-21 19:26:22 +010043 // Give some time to the ParkTester thread to honor the park command.
44 Thread.sleep(3000);
jeffhao5d1ac922011-09-29 17:41:15 -070045 } catch (InterruptedException ex) {
Roland Levillain379e5732017-08-21 19:26:22 +010046 System.out.println("Main thread interrupted!");
47 System.exit(1);
jeffhao5d1ac922011-09-29 17:41:15 -070048 }
49
50 if (test.success) {
51 System.out.println("Test succeeded!");
52 } else {
53 System.out.println("Test failed.");
Hans Boehm75c3ff32020-04-20 16:44:27 -070054 test.printTimes();
55 System.out.println("Value of success = " + test.success);
56 Thread.sleep(3000);
57 System.out.println("Value of success after sleeping = " + test.success);
58 test.printTimes(); // In case they weren't ready the first time.
jeffhao5d1ac922011-09-29 17:41:15 -070059 }
60 }
61
62 /**
63 * Set up {@link #UNSAFE}.
64 */
Brian Carlstrom5f57d2d2015-06-02 21:53:14 -070065 public static void setUp() throws Exception{
jeffhao5d1ac922011-09-29 17:41:15 -070066 /*
67 * Subvert the access check to get the unique Unsafe instance.
68 * We can do this because there's no security manager
69 * installed when running the test.
70 */
Brian Carlstrom5f57d2d2015-06-02 21:53:14 -070071 Field field = null;
jeffhao5d1ac922011-09-29 17:41:15 -070072 try {
Brian Carlstrom5f57d2d2015-06-02 21:53:14 -070073 field = Unsafe.class.getDeclaredField("THE_ONE");
74 } catch (NoSuchFieldException e1) {
75 try {
76 field = Unsafe.class.getDeclaredField("theUnsafe");
77 } catch (NoSuchFieldException e2) {
78 throw new RuntimeException("Failed to find THE_ONE or theUnsafe");
79 }
jeffhao5d1ac922011-09-29 17:41:15 -070080 }
Brian Carlstrom5f57d2d2015-06-02 21:53:14 -070081 field.setAccessible(true);
82 UNSAFE = (Unsafe) field.get(null);
jeffhao5d1ac922011-09-29 17:41:15 -070083 }
84
jeffhao5d1ac922011-09-29 17:41:15 -070085 private static class ParkTester extends Thread {
86 public volatile boolean parkNow = false;
87 public volatile boolean success = false;
Hans Boehm75c3ff32020-04-20 16:44:27 -070088 public volatile long startTime = 0;
89 public volatile long elapsedTime = 0;
90 public volatile long finishTime = 0;
jeffhao5d1ac922011-09-29 17:41:15 -070091
92 public void run() {
93 while (!parkNow) {
94 try {
95 Thread.sleep(500);
96 } catch (InterruptedException ex) {
97 // Ignore it.
98 }
99 }
100
101 long start = System.currentTimeMillis();
102 UNSAFE.park(false, 500 * 1000000); // 500 msec
103 long elapsed = System.currentTimeMillis() - start;
104
105 if (elapsed > 200) {
jeffhao5d1ac922011-09-29 17:41:15 -0700106 success = false;
Hans Boehm61280972020-05-13 18:47:20 -0700107 System.out.println("park()ed for " + elapsed + " msec");
jeffhao5d1ac922011-09-29 17:41:15 -0700108 } else {
jeffhao5d1ac922011-09-29 17:41:15 -0700109 success = true;
Hans Boehm61280972020-05-13 18:47:20 -0700110 // println is occasionally very slow.
111 // But output still appears before main thread output.
112 System.out.println("park() returned quickly");
Hans Boehm75c3ff32020-04-20 16:44:27 -0700113 finishTime = System.currentTimeMillis();
114 startTime = start;
115 elapsedTime = elapsed;
jeffhao5d1ac922011-09-29 17:41:15 -0700116 }
117 }
Hans Boehm75c3ff32020-04-20 16:44:27 -0700118
119 public void printTimes() {
120 System.out.println("Started at " + startTime + "ms, took " + elapsedTime
121 + "ms, signalled at " + finishTime + "ms");
122 }
jeffhao5d1ac922011-09-29 17:41:15 -0700123 }
124}