8322829: Refactor nioBlocker to avoid blocking while holding Thread's interrupt lock

Reviewed-by: jpai
This commit is contained in:
Alan Bateman 2024-01-09 07:05:27 +00:00
parent 07fce8eff2
commit 7286f5291d
6 changed files with 118 additions and 54 deletions

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 1994, 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1994, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -2374,7 +2374,7 @@ public final class System {
return klass.getEnumConstantsShared();
}
public void blockedOn(Interruptible b) {
Thread.blockedOn(b);
Thread.currentThread().blockedOn(b);
}
public void registerShutdownHook(int slot, boolean registerShutdownInProgress, Runnable hook) {
Shutdown.add(slot, registerShutdownInProgress, hook);

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 1994, 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1994, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -345,15 +345,20 @@ public class Thread implements Runnable {
* operation, if any. The blocker's interrupt method should be invoked
* after setting this thread's interrupt status.
*/
volatile Interruptible nioBlocker;
private Interruptible nioBlocker;
Interruptible nioBlocker() {
//assert Thread.holdsLock(interruptLock);
return nioBlocker;
}
/* Set the blocker field; invoked via jdk.internal.access.SharedSecrets
* from java.nio code
*/
static void blockedOn(Interruptible b) {
Thread me = Thread.currentThread();
synchronized (me.interruptLock) {
me.nioBlocker = b;
void blockedOn(Interruptible b) {
//assert Thread.currentThread() == this;
synchronized (interruptLock) {
nioBlocker = b;
}
}
@ -1699,15 +1704,19 @@ public class Thread implements Runnable {
checkAccess();
// thread may be blocked in an I/O operation
Interruptible blocker;
synchronized (interruptLock) {
Interruptible b = nioBlocker;
if (b != null) {
blocker = nioBlocker;
if (blocker != null) {
interrupted = true;
interrupt0(); // inform VM of interrupt
b.interrupt(this);
return;
blocker.interrupt(this);
}
}
if (blocker != null) {
blocker.postInterrupt();
return;
}
}
interrupted = true;
interrupt0(); // inform VM of interrupt

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2018, 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2018, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -852,18 +852,32 @@ final class VirtualThread extends BaseVirtualThread {
return true;
}
@Override
void blockedOn(Interruptible b) {
notifyJvmtiDisableSuspend(true);
try {
super.blockedOn(b);
} finally {
notifyJvmtiDisableSuspend(false);
}
}
@Override
@SuppressWarnings("removal")
public void interrupt() {
if (Thread.currentThread() != this) {
checkAccess();
// if current thread is a virtual thread then prevent it from being
// suspended when entering or holding interruptLock
Interruptible blocker;
notifyJvmtiDisableSuspend(true);
try {
synchronized (interruptLock) {
interrupted = true;
Interruptible b = nioBlocker;
if (b != null) {
b.interrupt(this);
blocker = nioBlocker();
if (blocker != null) {
blocker.interrupt(this);
}
// interrupt carrier thread if mounted
@ -873,6 +887,11 @@ final class VirtualThread extends BaseVirtualThread {
} finally {
notifyJvmtiDisableSuspend(false);
}
// notify blocker after releasing interruptLock
if (blocker != null) {
blocker.postInterrupt();
}
} else {
interrupted = true;
carrierThread.setInterrupt();