Skip to content

Commit

Permalink
Added commentary
Browse files Browse the repository at this point in the history
  • Loading branch information
petermz committed Aug 3, 2023
1 parent c1a0610 commit 0e7a017
Showing 1 changed file with 11 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -211,12 +211,16 @@ private static void slowPathMonitorEnter(Object obj) {
try {
int monitorOffset = getMonitorOffset(obj);
if (monitorOffset == 0) {
/// slow path
// Slow path
singleton().monitorEnter(obj, MonitorInflationCause.MONITOR_ENTER);
return;
}

/// optimized path
/*
* Optimized path takes advantage of the knowledge that, when a new monitor object is created,
* it is not shared with other threads, so we can set its state without CAS. It also has
* acquisitions == 1 by construction, so we don't need to set that too.
*/
long current = JavaThreads.getCurrentThreadId();
JavaMonitor monitor = (JavaMonitor) BarrieredAccess.readObject(obj, monitorOffset);
try {
Expand Down Expand Up @@ -295,11 +299,15 @@ private static void slowPathMonitorExit(Object obj) {
*/
int monitorOffset = getMonitorOffset(obj);
if (monitorOffset == 0) {
// Slow path
singleton().monitorExit(obj, MonitorInflationCause.VM_INTERNAL);
return;
}

/// optimized path
/*
* Optimized path: we know that monitor object exists, due to structured locking,
* so does not need to be created/inflated.
*/
JavaMonitor monitor = (JavaMonitor) BarrieredAccess.readObject(obj, monitorOffset);
if (monitor.acquisitions > 1) {
monitor.acquisitions--;
Expand Down

0 comments on commit 0e7a017

Please sign in to comment.