Skip to content

Commit e4f91a8

Browse files
committed
[GR-58004] Add JavaMonitorQueuedSynchronizer#reacquire (adopt JDK-8325397)
PullRequest: graal/20278
2 parents d8d7b2f + a94e80d commit e4f91a8

File tree

1 file changed

+32
-3
lines changed

1 file changed

+32
-3
lines changed

substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/monitor/JavaMonitorQueuedSynchronizer.java

+32-3
Original file line numberDiff line numberDiff line change
@@ -281,10 +281,39 @@ protected int getSpinAttempts(int parks) {
281281
return (1 << parks) - 1;
282282
}
283283

284+
// see AbstractQueuedLongSynchronizer.reacquire(Node, long)
285+
private void reacquire(Node node, long arg) {
286+
try {
287+
acquire(node, arg);
288+
} catch (Error | RuntimeException firstEx) {
289+
// While we currently do not emit an JFR events in this situation, mainly
290+
// because the conditions under which this happens are such that it
291+
// cannot be presumed to be possible to actually allocate an event, and
292+
// using a preconstructed one would have limited value in serviceability.
293+
// Having said that, the following place would be the more appropriate
294+
// place to put such logic:
295+
// emit JFR event
296+
297+
for (long nanos = 1L;;) {
298+
U.park(false, nanos); // must use Unsafe park to sleep
299+
if (nanos < 1L << 30) { // max about 1 second
300+
nanos <<= 1;
301+
}
302+
try {
303+
acquire(node, arg);
304+
} catch (Error | RuntimeException ignored) {
305+
continue;
306+
}
307+
308+
throw firstEx;
309+
}
310+
}
311+
}
312+
284313
// see AbstractQueuedLongSynchronizer.acquire(Node, long, false, false, false, 0L)
285314
@SuppressWarnings("all")
286315
@BasedOnJDKFile("https://github.com/openjdk/jdk/blob/jdk-24+24/src/hotspot/share/runtime/objectMonitor.cpp#L895-L930")
287-
final int acquire(Node node, long arg) {
316+
private int acquire(Node node, long arg) {
288317
Thread current = Thread.currentThread();
289318
/* Spinning logic is SVM-specific. */
290319
int parks = 0;
@@ -624,7 +653,7 @@ public void await(Object obj) throws InterruptedException {
624653
node.clearStatus();
625654
// waiting is done, emit wait event
626655
JavaMonitorWaitEvent.emit(startTicks, obj, node.notifierJfrTid, 0L, false);
627-
acquire(node, savedAcquisitions);
656+
reacquire(node, savedAcquisitions);
628657
if (interrupted) {
629658
if (cancelled) {
630659
unlinkCancelledWaiters(node);
@@ -665,7 +694,7 @@ public boolean await(Object obj, long time, TimeUnit unit) throws InterruptedExc
665694
node.clearStatus();
666695
// waiting is done, emit wait event
667696
JavaMonitorWaitEvent.emit(startTicks, obj, node.notifierJfrTid, time, cancelled);
668-
acquire(node, savedAcquisitions);
697+
reacquire(node, savedAcquisitions);
669698
if (cancelled) {
670699
unlinkCancelledWaiters(node);
671700
if (interrupted) {

0 commit comments

Comments
 (0)