8329593: Drop adjustments to target parallelism when virtual threads do I/O on files opened for buffered I/O

Reviewed-by: bpb, jpai
This commit is contained in:
Alan Bateman 2024-04-23 16:10:13 +00:00
parent b07e1531b3
commit 412e306d81
28 changed files with 461 additions and 870 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
@ -370,16 +370,21 @@ public class Object {
* @see #wait(long, int)
*/
public final void wait(long timeoutMillis) throws InterruptedException {
long comp = Blocker.begin();
if (!Thread.currentThread().isVirtual()) {
wait0(timeoutMillis);
return;
}
// virtual thread waiting
boolean attempted = Blocker.begin();
try {
wait0(timeoutMillis);
} catch (InterruptedException e) {
Thread thread = Thread.currentThread();
if (thread.isVirtual())
thread.getAndClearInterrupt();
// virtual thread's interrupt status needs to be cleared
Thread.currentThread().getAndClearInterrupt();
throw e;
} finally {
Blocker.end(comp);
Blocker.end(attempted);
}
}

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 1995, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1995, 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
@ -25,6 +25,7 @@
package java.lang;
import jdk.internal.misc.Blocker;
import jdk.internal.util.StaticProperty;
import java.io.*;
@ -839,6 +840,75 @@ public abstract class Process {
return n - remaining;
}
@Override
public int read() throws IOException {
boolean attempted = Blocker.begin();
try {
return super.read();
} finally {
Blocker.end(attempted);
}
}
@Override
public int read(byte[] b) throws IOException {
boolean attempted = Blocker.begin();
try {
return super.read(b);
} finally {
Blocker.end(attempted);
}
}
@Override
public int read(byte[] b, int off, int len) throws IOException {
boolean attempted = Blocker.begin();
try {
return super.read(b, off, len);
} finally {
Blocker.end(attempted);
}
}
}
/**
* An output stream for a subprocess pipe.
*/
static class PipeOutputStream extends FileOutputStream {
PipeOutputStream(FileDescriptor fd) {
super(fd);
}
@Override
public void write(int b) throws IOException {
boolean attempted = Blocker.begin();
try {
super.write(b);
} finally {
Blocker.end(attempted);
}
}
@Override
public void write(byte[] b) throws IOException {
boolean attempted = Blocker.begin();
try {
super.write(b);
} finally {
Blocker.end(attempted);
}
}
@Override
public void write(byte[] b, int off, int len) throws IOException {
boolean attempted = Blocker.begin();
try {
super.write(b, off, len);
} finally {
Blocker.end(attempted);
}
}
}
/**

View file

@ -71,6 +71,7 @@ import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Stream;
import jdk.internal.logger.LoggerFinderLoader.TemporaryLoggerFinder;
import jdk.internal.misc.Blocker;
import jdk.internal.misc.CarrierThreadLocal;
import jdk.internal.misc.Unsafe;
import jdk.internal.util.StaticProperty;
@ -2190,9 +2191,9 @@ public final class System {
lineSeparator = props.getProperty("line.separator");
FileInputStream fdIn = new FileInputStream(FileDescriptor.in);
FileOutputStream fdOut = new FileOutputStream(FileDescriptor.out);
FileOutputStream fdErr = new FileOutputStream(FileDescriptor.err);
FileInputStream fdIn = new In(FileDescriptor.in);
FileOutputStream fdOut = new Out(FileDescriptor.out);
FileOutputStream fdErr = new Out(FileDescriptor.err);
initialIn = new BufferedInputStream(fdIn);
setIn0(initialIn);
// stdout/err.encoding are set when the VM is associated with the terminal,
@ -2217,6 +2218,83 @@ public final class System {
VM.initLevel(1);
}
/**
* System.in.
*/
private static class In extends FileInputStream {
In(FileDescriptor fd) {
super(fd);
}
@Override
public int read() throws IOException {
boolean attempted = Blocker.begin();
try {
return super.read();
} finally {
Blocker.end(attempted);
}
}
@Override
public int read(byte[] b) throws IOException {
boolean attempted = Blocker.begin();
try {
return super.read(b);
} finally {
Blocker.end(attempted);
}
}
@Override
public int read(byte[] b, int off, int len) throws IOException {
boolean attempted = Blocker.begin();
try {
return super.read(b, off, len);
} finally {
Blocker.end(attempted);
}
}
}
/**
* System.out/System.err wrap this output stream.
*/
private static class Out extends FileOutputStream {
Out(FileDescriptor fd) {
super(fd);
}
public void write(int b) throws IOException {
boolean attempted = Blocker.begin();
try {
super.write(b);
} finally {
Blocker.end(attempted);
}
}
@Override
public void write(byte[] b) throws IOException {
boolean attempted = Blocker.begin();
try {
super.write(b);
} finally {
Blocker.end(attempted);
}
}
@Override
public void write(byte[] b, int off, int len) throws IOException {
boolean attempted = Blocker.begin();
try {
super.write(b, off, len);
} finally {
Blocker.end(attempted);
}
}
}
// @see #initPhase2()
static ModuleLayer bootLayer;

View file

@ -460,6 +460,11 @@ final class VirtualThread extends BaseVirtualThread {
private void afterYield() {
assert carrierThread == null;
// re-adjust parallelism if the virtual thread yielded when compensating
if (currentThread() instanceof CarrierThread ct) {
ct.endBlocking();
}
int s = state();
// LockSupport.park/parkNanos