blob: dec49a832034281196ad241118894cf8d698eb39 [file] [log] [blame]
Andreas Gampeaf13ab92017-01-11 20:57:40 -08001/*
2 * Copyright (C) 2017 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
17import java.util.Arrays;
Andreas Gampe72c19832017-01-12 13:22:16 -080018import java.util.ArrayList;
19import java.util.Collections;
Andreas Gampe85807442017-01-13 14:40:58 -080020import java.util.Comparator;
Andreas Gampe72c19832017-01-12 13:22:16 -080021import java.util.concurrent.CountDownLatch;
22import java.util.HashMap;
23import java.util.List;
24import java.util.Map;
Andreas Gampeaf13ab92017-01-11 20:57:40 -080025
26public class Main {
27 public static void main(String[] args) throws Exception {
28 System.loadLibrary(args[1]);
29
30 doTest();
31 }
32
33 private static void doTest() throws Exception {
34 Thread t1 = Thread.currentThread();
35 Thread t2 = getCurrentThread();
36
37 if (t1 != t2) {
38 throw new RuntimeException("Expected " + t1 + " but got " + t2);
39 }
40 System.out.println("currentThread OK");
41
42 printThreadInfo(t1);
43 printThreadInfo(null);
44
45 Thread t3 = new Thread("Daemon Thread");
46 t3.setDaemon(true);
47 // Do not start this thread, yet.
48 printThreadInfo(t3);
49 // Start, and wait for it to die.
50 t3.start();
51 t3.join();
Andreas Gampe72c19832017-01-12 13:22:16 -080052 Thread.sleep(500); // Wait a little bit.
Andreas Gampeaf13ab92017-01-11 20:57:40 -080053 // Thread has died, check that we can still get info.
54 printThreadInfo(t3);
Andreas Gampe72c19832017-01-12 13:22:16 -080055
56 doStateTests();
Andreas Gampe85807442017-01-13 14:40:58 -080057
58 doAllThreadsTests();
Andreas Gampe7b3b3262017-01-19 20:40:42 -080059
60 doTLSTests();
Andreas Gampe72c19832017-01-12 13:22:16 -080061 }
62
63 private static class Holder {
64 volatile boolean flag = false;
65 }
66
67 private static void doStateTests() throws Exception {
68 System.out.println(Integer.toHexString(getThreadState(null)));
69 System.out.println(Integer.toHexString(getThreadState(Thread.currentThread())));
70
71 final CountDownLatch cdl1 = new CountDownLatch(1);
72 final CountDownLatch cdl2 = new CountDownLatch(1);
73 final CountDownLatch cdl3_1 = new CountDownLatch(1);
74 final CountDownLatch cdl3_2 = new CountDownLatch(1);
75 final CountDownLatch cdl4 = new CountDownLatch(1);
76 final CountDownLatch cdl5 = new CountDownLatch(1);
77 final Holder h = new Holder();
78 Runnable r = new Runnable() {
79 @Override
80 public void run() {
81 try {
82 cdl1.countDown();
83 synchronized(cdl1) {
84 cdl1.wait();
85 }
86
87 cdl2.countDown();
88 synchronized(cdl2) {
89 cdl2.wait(1000); // Wait a second.
90 }
91
92 cdl3_1.await();
93 cdl3_2.countDown();
94 synchronized(cdl3_2) {
95 // Nothing, just wanted to block on cdl3.
96 }
97
98 cdl4.countDown();
99 Thread.sleep(1000);
100
101 cdl5.countDown();
102 while (!h.flag) {
103 // Busy-loop.
104 }
105 } catch (Exception e) {
106 throw new RuntimeException(e);
107 }
108 }
109 };
110
111 Thread t = new Thread(r);
112 printThreadState(t);
113 t.start();
114
115 // Waiting.
116 cdl1.await();
117 Thread.yield();
118 Thread.sleep(100);
119 printThreadState(t);
120 synchronized(cdl1) {
121 cdl1.notifyAll();
122 }
123
124 // Timed waiting.
125 cdl2.await();
126 Thread.yield();
127 Thread.sleep(100);
128 printThreadState(t);
129 synchronized(cdl2) {
130 cdl2.notifyAll();
131 }
132
133 // Blocked on monitor.
134 synchronized(cdl3_2) {
135 cdl3_1.countDown();
136 cdl3_2.await();
137 Thread.yield();
138 Thread.sleep(100);
139 printThreadState(t);
140 }
141
142 // Sleeping.
143 cdl4.await();
144 Thread.yield();
145 Thread.sleep(100);
146 printThreadState(t);
147
148 // Running.
149 cdl5.await();
150 Thread.yield();
151 Thread.sleep(100);
152 printThreadState(t);
153 h.flag = true;
154
155 // Dying.
156 t.join();
157 Thread.yield();
158 Thread.sleep(100);
159
160 printThreadState(t);
161 }
162
Andreas Gampe85807442017-01-13 14:40:58 -0800163 private static void doAllThreadsTests() {
164 Thread[] threads = getAllThreads();
165 Arrays.sort(threads, THREAD_COMP);
166 System.out.println(Arrays.toString(threads));
167 }
168
Andreas Gampe7b3b3262017-01-19 20:40:42 -0800169 private static void doTLSTests() throws Exception {
170 doTLSNonLiveTests();
171 doTLSLiveTests();
172 }
173
174 private static void doTLSNonLiveTests() throws Exception {
175 Thread t = new Thread();
176 try {
177 setTLS(t, 1);
178 System.out.println("Expected failure setting TLS for non-live thread");
179 } catch (Exception e) {
180 System.out.println(e.getMessage());
181 }
182 t.start();
183 t.join();
184 try {
185 setTLS(t, 1);
186 System.out.println("Expected failure setting TLS for non-live thread");
187 } catch (Exception e) {
188 System.out.println(e.getMessage());
189 }
190 }
191
192 private static void doTLSLiveTests() throws Exception {
193 setTLS(Thread.currentThread(), 1);
194
195 long l = getTLS(Thread.currentThread());
196 if (l != 1) {
197 throw new RuntimeException("Unexpected TLS value: " + l);
198 };
199
200 final CountDownLatch cdl1 = new CountDownLatch(1);
201 final CountDownLatch cdl2 = new CountDownLatch(1);
202
203 Runnable r = new Runnable() {
204 @Override
205 public void run() {
206 try {
207 cdl1.countDown();
208 cdl2.await();
209 setTLS(Thread.currentThread(), 2);
210 if (getTLS(Thread.currentThread()) != 2) {
211 throw new RuntimeException("Different thread issue");
212 }
213 } catch (Exception e) {
214 throw new RuntimeException(e);
215 }
216 }
217 };
218
219 Thread t = new Thread(r);
220 t.start();
221 cdl1.await();
222 setTLS(Thread.currentThread(), 1);
223 cdl2.countDown();
224
225 t.join();
226 if (getTLS(Thread.currentThread()) != 1) {
227 throw new RuntimeException("Got clobbered");
228 }
229 }
230
Andreas Gampe85807442017-01-13 14:40:58 -0800231 private final static Comparator<Thread> THREAD_COMP = new Comparator<Thread>() {
232 public int compare(Thread o1, Thread o2) {
233 return o1.getName().compareTo(o2.getName());
234 }
235 };
236
Andreas Gampe72c19832017-01-12 13:22:16 -0800237 private final static Map<Integer, String> STATE_NAMES = new HashMap<Integer, String>();
238 private final static List<Integer> STATE_KEYS = new ArrayList<Integer>();
239 static {
240 STATE_NAMES.put(0x1, "ALIVE");
241 STATE_NAMES.put(0x2, "TERMINATED");
242 STATE_NAMES.put(0x4, "RUNNABLE");
243 STATE_NAMES.put(0x400, "BLOCKED_ON_MONITOR_ENTER");
244 STATE_NAMES.put(0x80, "WAITING");
245 STATE_NAMES.put(0x10, "WAITING_INDEFINITELY");
246 STATE_NAMES.put(0x20, "WAITING_WITH_TIMEOUT");
247 STATE_NAMES.put(0x40, "SLEEPING");
248 STATE_NAMES.put(0x100, "IN_OBJECT_WAIT");
249 STATE_NAMES.put(0x200, "PARKED");
250 STATE_NAMES.put(0x100000, "SUSPENDED");
251 STATE_NAMES.put(0x200000, "INTERRUPTED");
252 STATE_NAMES.put(0x400000, "IN_NATIVE");
253 STATE_KEYS.addAll(STATE_NAMES.keySet());
254 Collections.sort(STATE_KEYS);
255 }
256
257 private static void printThreadState(Thread t) {
258 int state = getThreadState(t);
259
260 StringBuilder sb = new StringBuilder();
261
262 for (Integer i : STATE_KEYS) {
263 if ((state & i) != 0) {
264 if (sb.length()>0) {
265 sb.append('|');
266 }
267 sb.append(STATE_NAMES.get(i));
268 }
269 }
270
271 if (sb.length() == 0) {
272 sb.append("NEW");
273 }
274
275 System.out.println(Integer.toHexString(state) + " = " + sb.toString());
Andreas Gampeaf13ab92017-01-11 20:57:40 -0800276 }
277
278 private static void printThreadInfo(Thread t) {
279 Object[] threadInfo = getThreadInfo(t);
280 if (threadInfo == null || threadInfo.length != 5) {
281 System.out.println(Arrays.toString(threadInfo));
282 throw new RuntimeException("threadInfo length wrong");
283 }
284
285 System.out.println(threadInfo[0]); // Name
286 System.out.println(threadInfo[1]); // Priority
287 System.out.println(threadInfo[2]); // Daemon
288 System.out.println(threadInfo[3]); // Threadgroup
289 System.out.println(threadInfo[4] == null ? "null" : threadInfo[4].getClass()); // Context CL.
290 }
291
292 private static native Thread getCurrentThread();
293 private static native Object[] getThreadInfo(Thread t);
Andreas Gampe72c19832017-01-12 13:22:16 -0800294 private static native int getThreadState(Thread t);
Andreas Gampe85807442017-01-13 14:40:58 -0800295 private static native Thread[] getAllThreads();
Andreas Gampe7b3b3262017-01-19 20:40:42 -0800296 private static native void setTLS(Thread t, long l);
297 private static native long getTLS(Thread t);
Andreas Gampeaf13ab92017-01-11 20:57:40 -0800298}