8284161: Implementation of Virtual Threads (Preview)

Co-authored-by: Ron Pressler <rpressler@openjdk.org>
Co-authored-by: Alan Bateman <alanb@openjdk.org>
Co-authored-by: Erik Österlund <eosterlund@openjdk.org>
Co-authored-by: Andrew Haley <aph@openjdk.org>
Co-authored-by: Rickard Bäckman <rbackman@openjdk.org>
Co-authored-by: Markus Grönlund <mgronlun@openjdk.org>
Co-authored-by: Leonid Mesnik <lmesnik@openjdk.org>
Co-authored-by: Serguei Spitsyn <sspitsyn@openjdk.org>
Co-authored-by: Chris Plummer <cjplummer@openjdk.org>
Co-authored-by: Coleen Phillimore <coleenp@openjdk.org>
Co-authored-by: Robbin Ehn <rehn@openjdk.org>
Co-authored-by: Stefan Karlsson <stefank@openjdk.org>
Co-authored-by: Thomas Schatzl <tschatzl@openjdk.org>
Co-authored-by: Sergey Kuksenko <skuksenko@openjdk.org>
Reviewed-by: lancea, eosterlund, rehn, sspitsyn, stefank, tschatzl, dfuchs, lmesnik, dcubed, kevinw, amenkov, dlong, mchung, psandoz, bpb, coleenp, smarks, egahlin, mseledtsov, coffeys, darcy
This commit is contained in:
Alan Bateman 2022-05-07 08:06:16 +00:00
parent 5212535a27
commit 9583e3657e
1133 changed files with 95935 additions and 8335 deletions

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 1994, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1994, 2022, 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,10 @@
package java.io;
import java.util.Arrays;
import jdk.internal.misc.InternalLock;
import jdk.internal.misc.VM;
/**
* The class implements a buffered output stream. By setting up such
* an output stream, an application can write bytes to the underlying
@ -35,6 +39,12 @@ package java.io;
* @since 1.0
*/
public class BufferedOutputStream extends FilterOutputStream {
private static final int DEFAULT_INITIAL_BUFFER_SIZE = 512;
private static final int DEFAULT_MAX_BUFFER_SIZE = 8192;
// initialized to null when BufferedOutputStream is sub-classed
private final InternalLock lock;
/**
* The internal buffer where data is stored.
*/
@ -48,6 +58,44 @@ public class BufferedOutputStream extends FilterOutputStream {
*/
protected int count;
/**
* Max size of the internal buffer.
*/
private final int maxBufSize;
/**
* Returns the buffer size to use when no output buffer size specified.
*/
private static int initialBufferSize() {
if (VM.isBooted() && Thread.currentThread().isVirtual()) {
return DEFAULT_INITIAL_BUFFER_SIZE;
} else {
return DEFAULT_MAX_BUFFER_SIZE;
}
}
/**
* Creates a new buffered output stream.
*/
private BufferedOutputStream(OutputStream out, int initialSize, int maxSize) {
super(out);
if (initialSize <= 0) {
throw new IllegalArgumentException("Buffer size <= 0");
}
if (getClass() == BufferedOutputStream.class) {
// use InternalLock and resizable buffer when not sub-classed
this.lock = InternalLock.newLockOrNull();
this.buf = new byte[initialSize]; // resizable
} else {
// use monitors and no resizing when sub-classed
this.lock = null;
this.buf = new byte[maxSize];
}
this.maxBufSize = maxSize;
}
/**
* Creates a new buffered output stream to write data to the
* specified underlying output stream.
@ -55,7 +103,7 @@ public class BufferedOutputStream extends FilterOutputStream {
* @param out the underlying output stream.
*/
public BufferedOutputStream(OutputStream out) {
this(out, 8192);
this(out, initialBufferSize(), DEFAULT_MAX_BUFFER_SIZE);
}
/**
@ -68,11 +116,7 @@ public class BufferedOutputStream extends FilterOutputStream {
* @throws IllegalArgumentException if size &lt;= 0.
*/
public BufferedOutputStream(OutputStream out, int size) {
super(out);
if (size <= 0) {
throw new IllegalArgumentException("Buffer size <= 0");
}
buf = new byte[size];
this(out, size, size);
}
/** Flush the internal buffer */
@ -83,6 +127,24 @@ public class BufferedOutputStream extends FilterOutputStream {
}
}
/**
* Grow buf to fit an additional len bytes if needed.
* If possible, it grows by len+1 to avoid flushing when len bytes
* are added. A no-op if the buffer is not resizable.
*
* This method should only be called while holding the lock.
*/
private void growIfNeeded(int len) {
int neededSize = count + len + 1;
if (neededSize < 0)
neededSize = Integer.MAX_VALUE;
int bufSize = buf.length;
if (neededSize > bufSize && bufSize < maxBufSize) {
int newSize = Math.min(neededSize, maxBufSize);
buf = Arrays.copyOf(buf, newSize);
}
}
/**
* Writes the specified byte to this buffered output stream.
*
@ -90,7 +152,23 @@ public class BufferedOutputStream extends FilterOutputStream {
* @throws IOException if an I/O error occurs.
*/
@Override
public synchronized void write(int b) throws IOException {
public void write(int b) throws IOException {
if (lock != null) {
lock.lock();
try {
implWrite(b);
} finally {
lock.unlock();
}
} else {
synchronized (this) {
implWrite(b);
}
}
}
private void implWrite(int b) throws IOException {
growIfNeeded(1);
if (count >= buf.length) {
flushBuffer();
}
@ -114,15 +192,31 @@ public class BufferedOutputStream extends FilterOutputStream {
* @throws IOException if an I/O error occurs.
*/
@Override
public synchronized void write(byte[] b, int off, int len) throws IOException {
if (len >= buf.length) {
/* If the request length exceeds the size of the output buffer,
public void write(byte[] b, int off, int len) throws IOException {
if (lock != null) {
lock.lock();
try {
implWrite(b, off, len);
} finally {
lock.unlock();
}
} else {
synchronized (this) {
implWrite(b, off, len);
}
}
}
private void implWrite(byte[] b, int off, int len) throws IOException {
if (len >= maxBufSize) {
/* If the request length exceeds the max size of the output buffer,
flush the output buffer and then write the data directly.
In this way buffered streams will cascade harmlessly. */
flushBuffer();
out.write(b, off, len);
return;
}
growIfNeeded(len);
if (len > buf.length - count) {
flushBuffer();
}
@ -138,7 +232,22 @@ public class BufferedOutputStream extends FilterOutputStream {
* @see java.io.FilterOutputStream#out
*/
@Override
public synchronized void flush() throws IOException {
public void flush() throws IOException {
if (lock != null) {
lock.lock();
try {
implFlush();
} finally {
lock.unlock();
}
} else {
synchronized (this) {
implFlush();
}
}
}
private void implFlush() throws IOException {
flushBuffer();
out.flush();
}