Skip to content

Commit 073c99c

Browse files
committed
Add JCL support for Java 8-17
Signed-off-by: Jack Lu <Jack.S.Lu@ibm.com>
1 parent 48103b1 commit 073c99c

File tree

1 file changed

+55
-23
lines changed

1 file changed

+55
-23
lines changed

jcl/src/java.base/share/classes/java/lang/Thread.java

Lines changed: 55 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -62,26 +62,26 @@ public class Thread implements Runnable {
6262
/**
6363
* The maximum priority value for a Thread.
6464
*/
65-
public final static int MAX_PRIORITY = 10; // Maximum allowed priority for a thread
65+
public final static int MAX_PRIORITY = 10; // Maximum allowed priority for a thread
6666
/**
6767
* The minimum priority value for a Thread.
6868
*/
69-
public final static int MIN_PRIORITY = 1; // Minimum allowed priority for a thread
69+
public final static int MIN_PRIORITY = 1; // Minimum allowed priority for a thread
7070
/**
7171
* The default priority value for a Thread.
7272
*/
73-
public final static int NORM_PRIORITY = 5; // Normal priority for a thread
73+
public final static int NORM_PRIORITY = 5; // Normal priority for a thread
7474
/*[PR 97331] Initial thread name should be Thread-0 */
75-
private static int createCount; // Used internally to compute Thread names that comply with the Java specification
75+
private static int createCount; // Used internally to compute Thread names that comply with the Java specification
7676
/*[PR 122459] LIR646 - Remove use of generic object for synchronization */
7777
private static final class TidLock {
7878
TidLock() {}
7979
}
8080
private static Object tidLock = new TidLock();
8181
private static long tidCount = 1;
82-
private static final int NANOS_MAX = 999999; // Max value for nanoseconds parameter to sleep and join
83-
private static final int INITIAL_LOCAL_STORAGE_CAPACITY = 5; // Initial number of local storages when the Thread is created
84-
static final long NO_REF = 0; // Symbolic constant, no threadRef assigned or already cleaned up
82+
private static final int NANOS_MAX = 999999; // Max value for nanoseconds parameter to sleep and join
83+
private static final int INITIAL_LOCAL_STORAGE_CAPACITY = 5; // Initial number of local storages when the Thread is created
84+
static final long NO_REF = 0; // Symbolic constant, no threadRef assigned or already cleaned up
8585

8686
// Instance variables
8787
private volatile long threadRef; // Used by the VM
@@ -94,14 +94,15 @@ private static final class TidLock {
9494
*/
9595
private volatile boolean deadInterrupt;
9696
/*[ENDIF] JAVA_SPEC_VERSION >= 14 */
97-
private volatile boolean started; // If !isAlive(), tells if Thread died already or hasn't even started
98-
private String name; // The Thread's name
99-
private int priority = NORM_PRIORITY; // The Thread's current priority
100-
private boolean isDaemon; // Tells if the Thread is a daemon thread or not.
101-
102-
ThreadGroup group; // A Thread belongs to exactly one ThreadGroup
103-
private Runnable runnable; // Target (optional) runnable object
104-
private boolean stopCalled = false; // Used by the VM
97+
private volatile boolean started; // If !isAlive(), tells if Thread died already or hasn't even started
98+
private String name; // The Thread's name
99+
private int priority = NORM_PRIORITY; // The Thread's current priority
100+
private boolean isDaemon; // Tells if the Thread is a daemon thread or not.
101+
private volatile int threadStatus; // The Thread's state.
102+
103+
ThreadGroup group; // A Thread belongs to exactly one ThreadGroup
104+
private Runnable runnable; // Target (optional) runnable object
105+
private boolean stopCalled = false; // Used by the VM
105106
/*[PR 1FENTZW]*/
106107
private ClassLoader contextClassLoader; // Used to find classes and resources in this Thread
107108
ThreadLocal.ThreadLocalMap threadLocals;
@@ -124,8 +125,8 @@ private static final class ThreadLock {}
124125

125126
volatile Object parkBlocker;
126127

127-
private static ThreadGroup systemThreadGroup; // Assigned by the vm
128-
private static ThreadGroup mainGroup; // ThreadGroup where the "main" Thread starts
128+
private static ThreadGroup systemThreadGroup; // Assigned by the vm
129+
private static ThreadGroup mainGroup; // ThreadGroup where the "main" Thread starts
129130

130131
/*[PR 113602] Thread fields should be volatile */
131132
private volatile static UncaughtExceptionHandler defaultExceptionHandler;
@@ -1488,6 +1489,39 @@ public static enum State {
14881489
*/
14891490
TERMINATED }
14901491

1492+
/**
1493+
* Returns the translation from a J9VMThread state to a Thread::State.
1494+
*
1495+
* @param status thread status value set by VM.
1496+
* @return this thread's state.
1497+
*
1498+
* @see State
1499+
*/
1500+
private State translateJ9VMThreadStateToThreadState(int status) {
1501+
switch (status) {
1502+
case 1: // J9VMTHREAD_STATE_RUNNING
1503+
return State.RUNNABLE;
1504+
case 2: // J9VMTHREAD_STATE_BLOCKED
1505+
return State.BLOCKED;
1506+
case 4: // J9VMTHREAD_STATE_WAITING
1507+
case 0x80: // J9VMTHREAD_STATE_PARKED
1508+
return State.WAITING;
1509+
case 8: // J9VMTHREAD_STATE_SLEEPING
1510+
case 64: // J9VMTHREAD_STATE_WAITING_TIMED
1511+
case 0x100: // J9VMTHREAD_STATE_PARKED_TIMED
1512+
return State.TIMED_WAITING;
1513+
case 32: // J9VMTHREAD_STATE_DEAD
1514+
return State.TERMINATED;
1515+
default:
1516+
synchronized (lock) {
1517+
if (threadRef == NO_REF) {
1518+
return State.TERMINATED;
1519+
}
1520+
return State.values()[getStateImpl(threadRef)];
1521+
}
1522+
}
1523+
}
1524+
14911525
/**
14921526
* Returns the current Thread state.
14931527
*
@@ -1496,15 +1530,13 @@ public static enum State {
14961530
* @see State
14971531
*/
14981532
public State getState() {
1499-
synchronized(lock) {
1533+
if (started) {
15001534
if (threadRef == NO_REF) {
1501-
if (isDead()) {
1502-
return State.TERMINATED;
1503-
}
1504-
return State.NEW;
1535+
return State.TERMINATED;
15051536
}
1506-
return State.values()[getStateImpl(threadRef)];
1537+
return translateJ9VMThreadStateToThreadState(threadStatus);
15071538
}
1539+
return State.NEW;
15081540
}
15091541

15101542
private native int getStateImpl(long threadRef);

0 commit comments

Comments
 (0)