Skip to content

Commit

Permalink
Reduce lock contention in Thread.getAndClearInterrupt
Browse files Browse the repository at this point in the history
Lock contention in Thread.getAndClearInterrupt is reduced by
acquiring the lock only when the value of the Thread.interrupted
field is true.

Related: eclipse-openj9/openj9#20414

Signed-off-by: Babneet Singh <[email protected]>
  • Loading branch information
babsingh committed Nov 6, 2024
1 parent d655244 commit 72d6365
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
21 changes: 12 additions & 9 deletions src/java.base/share/classes/java/lang/Thread.java
Original file line number Diff line number Diff line change
Expand Up @@ -1800,17 +1800,20 @@ boolean getAndClearInterrupt() {
if (com.ibm.oti.vm.VM.isJVMInSingleThreadedMode()) {
return interruptedImpl();
}
synchronized (interruptLock) {
boolean oldValue = interrupted;
// We may have been interrupted the moment after we read the field,
// so only clear the field if we saw that it was set and will return
// true; otherwise we could lose an interrupt.
if (oldValue) {
interrupted = false;
clearInterruptEvent();
boolean oldValue = interrupted;
if (oldValue) {
synchronized (interruptLock) {
oldValue = interrupted;
// We may have been interrupted the moment after we read the field,
// so only clear the field if we saw that it was set and will return
// true; otherwise we could lose an interrupt.
if (oldValue) {
interrupted = false;
clearInterruptEvent();
}
}
return oldValue;
}
return oldValue;
}

/**
Expand Down
9 changes: 6 additions & 3 deletions src/java.base/share/classes/java/lang/VirtualThread.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
*/
/*
* ===========================================================================
* (c) Copyright IBM Corp. 2022, 2023 All Rights Reserved
* (c) Copyright IBM Corp. 2022, 2024 All Rights Reserved
* ===========================================================================
*/
package java.lang;
Expand Down Expand Up @@ -952,8 +952,11 @@ boolean getAndClearInterrupt() {
disableSuspendAndPreempt();
try {
synchronized (interruptLock) {
interrupted = false;
carrierThread.clearInterrupt();
oldValue = interrupted;
if (oldValue) {
interrupted = false;
carrierThread.clearInterrupt();
}
}
} finally {
enableSuspendAndPreempt();
Expand Down

0 comments on commit 72d6365

Please sign in to comment.