public class MonitorSupport extends Object
Most objects used in synchronization operations have a dedicated memory in the object to store a
ReentrantLock
. The static analysis finds out which classes are used for synchronization
(and thus need a monitor) and assigns a monitor offset to point to the slot for the monitor. The
monitor is implemented with a ReentrantLock
.
There are a few exceptions: String
and DynamicHub
objects never have monitor
fields because we want instances in the image heap to be immutable. Arrays never have monitor
fields because it would increase the size of every array and it is not possible to distinguish
between arrays with different header sizes. See
UniverseBuilder.canHaveMonitorFields(AnalysisType) for details.
Synchronization on String
, arrays, and other types not detected by the static analysis
(like synchronization via JNI) fall back to a monitor stored in MonitorSupport.additionalMonitors
.
Because so few objects are receivers of Object.wait()
and Object.notify()
calls[citation
needed], condition variables for those objects are kept in MonitorSupport.additionalConditions
.
Constructor and Description |
---|
MonitorSupport() |
Modifier and Type | Method and Description |
---|---|
ReentrantLock |
getMonitorForTesting(Object obj) |
boolean |
holdsLock(Object obj)
Implements
Thread.holdsLock(java.lang.Object) . |
static int |
maybeAdjustNewParkStatus(int status)
Called from
Unsafe.park when changing the current thread's state before parking the
thread. |
static void |
monitorEnter(Object obj)
Implements the monitorenter bytecode.
|
static void |
monitorEnterWithoutBlockingCheck(Object obj) |
static void |
monitorExit(Object obj)
Implements the monitorexit bytecode.
|
void |
notify(Object obj,
boolean notifyAll)
Implements
Object.notify() and Object.notifyAll() . |
void |
setExclusiveOwnerThread(Object obj,
Thread thread)
This is a highly unsafe method that patches the existing lock of an object so that the object
appears as if it has been locked from a different thread.
|
void |
wait(Object obj,
long timeoutMillis)
Implements
Object.wait(long) . |
public static int maybeAdjustNewParkStatus(int status)
Unsafe.park
when changing the current thread's state before parking the
thread. When the thread is parked due to a monitor operation via ReentrantLock
, we
need to alter the new thread state so Thread.getState()
gives the expected result.public static void monitorEnter(Object obj)
public static void monitorEnterWithoutBlockingCheck(Object obj)
public static void monitorExit(Object obj)
public void setExclusiveOwnerThread(Object obj, Thread thread)
public boolean holdsLock(Object obj)
Thread.holdsLock(java.lang.Object)
.public void wait(Object obj, long timeoutMillis) throws InterruptedException
Object.wait(long)
.InterruptedException
public void notify(Object obj, boolean notifyAll)
Object.notify()
and Object.notifyAll()
.public ReentrantLock getMonitorForTesting(Object obj)