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) 1996, 2018, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1996, 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,8 +25,8 @@
package java.io;
import java.util.Objects;
import jdk.internal.misc.InternalLock;
/**
* Abstract class for writing to character streams. The only methods that a
@ -162,6 +162,21 @@ public abstract class Writer implements Appendable, Closeable, Flushable {
this.lock = this;
}
/**
* For use by BufferedWriter to create a character-stream writer that uses an
* internal lock when BufferedWriter is not extended and the given writer is
* trusted, otherwise critical sections will synchronize on the given writer.
*/
Writer(Writer writer) {
Class<?> clazz = writer.getClass();
if (getClass() == BufferedWriter.class &&
(clazz == OutputStreamWriter.class || clazz == FileWriter.class)) {
this.lock = InternalLock.newLockOr(writer);
} else {
this.lock = writer;
}
}
/**
* Creates a new character-stream writer whose critical sections will
* synchronize on the given object.
@ -191,15 +206,29 @@ public abstract class Writer implements Appendable, Closeable, Flushable {
* If an I/O error occurs
*/
public void write(int c) throws IOException {
synchronized (lock) {
if (writeBuffer == null){
writeBuffer = new char[WRITE_BUFFER_SIZE];
Object lock = this.lock;
if (lock instanceof InternalLock locker) {
locker.lock();
try {
implWrite(c);
} finally {
locker.unlock();
}
} else {
synchronized (lock) {
implWrite(c);
}
writeBuffer[0] = (char) c;
write(writeBuffer, 0, 1);
}
}
private void implWrite(int c) throws IOException {
if (writeBuffer == null){
writeBuffer = new char[WRITE_BUFFER_SIZE];
}
writeBuffer[0] = (char) c;
write(writeBuffer, 0, 1);
}
/**
* Writes an array of characters.
*
@ -276,21 +305,35 @@ public abstract class Writer implements Appendable, Closeable, Flushable {
* If an I/O error occurs
*/
public void write(String str, int off, int len) throws IOException {
synchronized (lock) {
char cbuf[];
if (len <= WRITE_BUFFER_SIZE) {
if (writeBuffer == null) {
writeBuffer = new char[WRITE_BUFFER_SIZE];
}
cbuf = writeBuffer;
} else { // Don't permanently allocate very large buffers.
cbuf = new char[len];
Object lock = this.lock;
if (lock instanceof InternalLock locker) {
locker.lock();
try {
implWrite(str, off, len);
} finally {
locker.unlock();
}
} else {
synchronized (lock) {
implWrite(str, off, len);
}
str.getChars(off, (off + len), cbuf, 0);
write(cbuf, 0, len);
}
}
private void implWrite(String str, int off, int len) throws IOException {
char cbuf[];
if (len <= WRITE_BUFFER_SIZE) {
if (writeBuffer == null) {
writeBuffer = new char[WRITE_BUFFER_SIZE];
}
cbuf = writeBuffer;
} else { // Don't permanently allocate very large buffers.
cbuf = new char[len];
}
str.getChars(off, (off + len), cbuf, 0);
write(cbuf, 0, len);
}
/**
* Appends the specified character sequence to this writer.
*