Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/share/jdi/ThreadState.java
41161 views
1
/*
2
* Copyright (c) 2014, 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.share.jdi;
25
26
import java.util.concurrent.locks.Lock;
27
import java.util.concurrent.locks.ReentrantLock;
28
import java.util.concurrent.locks.Condition;
29
import java.util.concurrent.TimeUnit;
30
31
import nsk.share.Failure;
32
33
/**
34
* Functions to set and wait for states in threads.
35
* Used to sync main thread and debuggee thread.
36
*/
37
public class ThreadState {
38
private final Lock lock = new ReentrantLock();
39
private final Condition cond = lock.newCondition();
40
private volatile String currentState;
41
private long timeoutMs;
42
43
public ThreadState(String startState, long timeoutMs) {
44
currentState = startState;
45
this.timeoutMs = timeoutMs;
46
}
47
48
/**
49
* Set new state.
50
*/
51
public void setState(String newState) {
52
lock.lock();
53
try {
54
log(MSG_SET_STATE, newState);
55
currentState = newState;
56
cond.signalAll();
57
} finally {
58
lock.unlock();
59
}
60
}
61
62
/**
63
* Wait for the specified state.
64
* Throws Failure if timeout.
65
*/
66
public void waitForState(String waitState) {
67
lock.lock();
68
try {
69
log(MSG_WAIT_STATE, waitState);
70
while (!currentState.equals(waitState)) {
71
if (!cond.await(timeoutMs, TimeUnit.MILLISECONDS)) {
72
throw new Failure(format(MSG_TIMEOUT, waitState));
73
}
74
}
75
log(MSG_GOT_STATE, waitState);
76
} catch (InterruptedException e) {
77
e.printStackTrace();
78
throw new Failure(e);
79
} finally {
80
lock.unlock();
81
}
82
}
83
84
/**
85
* Simple helper that sets a new state and then wait for another state.
86
*/
87
public void setAndWait(String newState, String waitState) {
88
setState(newState);
89
waitForState(waitState);
90
}
91
92
private static final String MSG_TIMEOUT = "ThreadState(thread='%s', state='%s') timeout waiting for %s";
93
private static final String MSG_SET_STATE = "ThreadState(thread='%s', state='%s') set state to %s";
94
private static final String MSG_WAIT_STATE = "ThreadState(thread='%s', state='%s') waiting for state %s";
95
private static final String MSG_GOT_STATE = "ThreadState(thread='%s', state='%s') got state %s";
96
97
private String format(String pattern, String state) {
98
final String threadName = Thread.currentThread().getName();
99
return String.format(pattern, threadName, currentState, state);
100
}
101
102
private void log(String pattern, String state) {
103
System.out.println(format(pattern, state));
104
}
105
}
106
107