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) 1997, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1997, 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
@ -35,7 +35,7 @@ final class Finalizer extends FinalReference<Object> { /* Package-private; must
same package as the Reference
class */
private static ReferenceQueue<Object> queue = new ReferenceQueue<>();
private static ReferenceQueue<Object> queue = new NativeReferenceQueue<>();
/** Head of doubly linked list of Finalizers awaiting finalization. */
private static Finalizer unfinalized = null;
@ -166,16 +166,6 @@ final class Finalizer extends FinalReference<Object> { /* Package-private; must
if (running)
return;
// Finalizer thread starts before System.initializeSystemClass
// is called. Wait until JavaLangAccess is available
while (VM.initLevel() == 0) {
// delay until VM completes initialization
try {
VM.awaitInitLevel(1);
} catch (InterruptedException x) {
// ignore and continue
}
}
final JavaLangAccess jla = SharedSecrets.getJavaLangAccess();
running = true;
for (;;) {
@ -189,17 +179,15 @@ final class Finalizer extends FinalReference<Object> { /* Package-private; must
}
}
static {
/**
* Start the Finalizer thread as a daemon thread.
*/
static void startFinalizerThread(ThreadGroup tg) {
if (ENABLED) {
ThreadGroup tg = Thread.currentThread().getThreadGroup();
for (ThreadGroup tgn = tg;
tgn != null;
tg = tgn, tgn = tg.getParent());
Thread finalizer = new FinalizerThread(tg);
finalizer.setPriority(Thread.MAX_PRIORITY - 2);
finalizer.setDaemon(true);
finalizer.start();
}
}
}

View file

@ -0,0 +1,92 @@
/*
* Copyright (c) 2021, 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
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package java.lang.ref;
/**
* An implementation of a ReferenceQueue that uses native monitors.
* The use of java.util.concurrent.lock locks interacts with various mechanisms,
* such as virtual threads and ForkJoinPool, that might not be appropriate for some
* low-level mechanisms, in particular MethodType's weak intern set.
*/
final class NativeReferenceQueue<T> extends ReferenceQueue<T> {
public NativeReferenceQueue() {
super(0);
}
private static class Lock { };
private final Lock lock = new Lock();
@Override
void signal() {
lock.notifyAll();
}
@Override
void await() throws InterruptedException {
lock.wait();
}
@Override
void await(long timeoutMillis) throws InterruptedException {
lock.wait(timeoutMillis);
}
@Override
boolean enqueue(Reference<? extends T> r) {
synchronized(lock) {
return enqueue0(r);
}
}
@Override
public Reference<? extends T> poll() {
if (headIsNull())
return null;
synchronized(lock) {
return poll0();
}
}
@Override
public Reference<? extends T> remove(long timeout)
throws IllegalArgumentException, InterruptedException {
if (timeout < 0)
throw new IllegalArgumentException("Negative timeout value");
if (timeout == 0)
return remove();
synchronized(lock) {
return remove0(timeout);
}
}
@Override
public Reference<? extends T> remove() throws InterruptedException {
synchronized(lock) {
return remove0();
}
}
}

View file

@ -25,6 +25,7 @@
package java.lang.ref;
import jdk.internal.misc.Unsafe;
import jdk.internal.vm.annotation.ForceInline;
import jdk.internal.vm.annotation.IntrinsicCandidate;
import jdk.internal.access.JavaLangRefAccess;
@ -192,27 +193,16 @@ public abstract sealed class Reference<T>
/* High-priority thread to enqueue pending References
*/
private static class ReferenceHandler extends Thread {
private static void ensureClassInitialized(Class<?> clazz) {
try {
Class.forName(clazz.getName(), true, clazz.getClassLoader());
} catch (ClassNotFoundException e) {
throw (Error) new NoClassDefFoundError(e.getMessage()).initCause(e);
}
}
static {
// pre-load and initialize Cleaner class so that we don't
// get into trouble later in the run loop if there's
// memory shortage while loading/initializing it lazily.
ensureClassInitialized(Cleaner.class);
}
ReferenceHandler(ThreadGroup g, String name) {
super(g, null, name, 0, false);
}
public void run() {
// pre-load and initialize Cleaner class so that we don't
// get into trouble later in the run loop if there's
// memory shortage while loading/initializing it lazily.
Unsafe.getUnsafe().ensureClassInitialized(Cleaner.class);
while (true) {
processPendingReferences();
}
@ -302,11 +292,10 @@ public abstract sealed class Reference<T>
}
}
static {
ThreadGroup tg = Thread.currentThread().getThreadGroup();
for (ThreadGroup tgn = tg;
tgn != null;
tg = tgn, tgn = tg.getParent());
/**
* Start the Reference Handler thread as a daemon thread.
*/
static void startReferenceHandlerThread(ThreadGroup tg) {
Thread handler = new ReferenceHandler(tg, "Reference Handler");
/* If there were a special system-only priority greater than
* MAX_PRIORITY, it would be used here
@ -314,9 +303,21 @@ public abstract sealed class Reference<T>
handler.setPriority(Thread.MAX_PRIORITY);
handler.setDaemon(true);
handler.start();
}
static {
// provide access in SharedSecrets
SharedSecrets.setJavaLangRefAccess(new JavaLangRefAccess() {
@Override
public void startThreads() {
ThreadGroup tg = Thread.currentThread().getThreadGroup();
for (ThreadGroup tgn = tg;
tgn != null;
tg = tgn, tgn = tg.getParent());
Reference.startReferenceHandlerThread(tg);
Finalizer.startFinalizerThread(tg);
}
@Override
public boolean waitForReferenceProcessing()
throws InterruptedException
@ -328,6 +329,11 @@ public abstract sealed class Reference<T>
public void runFinalization() {
Finalizer.runFinalization();
}
@Override
public <T> ReferenceQueue<T> newNativeReferenceQueue() {
return new NativeReferenceQueue<T>();
}
});
}

View file

@ -25,6 +25,9 @@
package java.lang.ref;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;
import java.util.function.Consumer;
import jdk.internal.misc.VM;
@ -38,13 +41,10 @@ import jdk.internal.misc.VM;
*/
public class ReferenceQueue<T> {
/**
* Constructs a new reference-object queue.
*/
public ReferenceQueue() { }
private static class Null extends ReferenceQueue<Object> {
public Null() { super(0); }
@Override
boolean enqueue(Reference<?> r) {
return false;
}
@ -53,37 +53,65 @@ public class ReferenceQueue<T> {
static final ReferenceQueue<Object> NULL = new Null();
static final ReferenceQueue<Object> ENQUEUED = new Null();
private static class Lock { };
private final Lock lock = new Lock();
private volatile Reference<? extends T> head;
private long queueLength = 0;
boolean enqueue(Reference<? extends T> r) { /* Called only by Reference class */
synchronized (lock) {
// Check that since getting the lock this reference hasn't already been
// enqueued (and even then removed)
ReferenceQueue<?> queue = r.queue;
if ((queue == NULL) || (queue == ENQUEUED)) {
return false;
}
assert queue == this;
// Self-loop end, so if a FinalReference it remains inactive.
r.next = (head == null) ? r : head;
head = r;
queueLength++;
// Update r.queue *after* adding to list, to avoid race
// with concurrent enqueued checks and fast-path poll().
// Volatiles ensure ordering.
r.queue = ENQUEUED;
if (r instanceof FinalReference) {
VM.addFinalRefCount(1);
}
lock.notifyAll();
return true;
}
private final ReentrantLock lock;
private final Condition notEmpty;
void signal() {
notEmpty.signalAll();
}
private Reference<? extends T> reallyPoll() { /* Must hold lock */
void await() throws InterruptedException {
notEmpty.await();
}
void await(long timeoutMillis) throws InterruptedException {
notEmpty.await(timeoutMillis, TimeUnit.MILLISECONDS);
}
/**
* Constructs a new reference-object queue.
*/
public ReferenceQueue() {
this.lock = new ReentrantLock();
this.notEmpty = lock.newCondition();
}
ReferenceQueue(int dummy) {
this.lock = null;
this.notEmpty = null;
}
final boolean enqueue0(Reference<? extends T> r) { // must hold lock
// Check that since getting the lock this reference hasn't already been
// enqueued (and even then removed)
ReferenceQueue<?> queue = r.queue;
if ((queue == NULL) || (queue == ENQUEUED)) {
return false;
}
assert queue == this;
// Self-loop end, so if a FinalReference it remains inactive.
r.next = (head == null) ? r : head;
head = r;
queueLength++;
// Update r.queue *after* adding to list, to avoid race
// with concurrent enqueued checks and fast-path poll().
// Volatiles ensure ordering.
r.queue = ENQUEUED;
if (r instanceof FinalReference) {
VM.addFinalRefCount(1);
}
signal();
return true;
}
final boolean headIsNull() {
return head == null;
}
final Reference<? extends T> poll0() { // must hold lock
Reference<? extends T> r = head;
if (r != null) {
r.queue = NULL;
@ -106,6 +134,40 @@ public class ReferenceQueue<T> {
return null;
}
final Reference<? extends T> remove0(long timeout)
throws IllegalArgumentException, InterruptedException { // must hold lock
Reference<? extends T> r = poll0();
if (r != null) return r;
long start = System.nanoTime();
for (;;) {
await(timeout);
r = poll0();
if (r != null) return r;
long end = System.nanoTime();
timeout -= (end - start) / 1000_000;
if (timeout <= 0) return null;
start = end;
}
}
final Reference<? extends T> remove0() throws InterruptedException { // must hold lock
for (;;) {
var r = poll0();
if (r != null) return r;
await();
}
}
boolean enqueue(Reference<? extends T> r) { /* Called only by Reference class */
lock.lock();
try {
return enqueue0(r);
} finally {
lock.unlock();
}
}
/**
* Polls this queue to see if a reference object is available. If one is
* available without further delay then it is removed from the queue and
@ -115,10 +177,13 @@ public class ReferenceQueue<T> {
* otherwise {@code null}
*/
public Reference<? extends T> poll() {
if (head == null)
if (headIsNull())
return null;
synchronized (lock) {
return reallyPoll();
lock.lock();
try {
return poll0();
} finally {
lock.unlock();
}
}
@ -142,27 +207,17 @@ public class ReferenceQueue<T> {
* @throws InterruptedException
* If the timeout wait is interrupted
*/
public Reference<? extends T> remove(long timeout)
throws IllegalArgumentException, InterruptedException
{
if (timeout < 0) {
public Reference<? extends T> remove(long timeout) throws InterruptedException {
if (timeout < 0)
throw new IllegalArgumentException("Negative timeout value");
}
synchronized (lock) {
Reference<? extends T> r = reallyPoll();
if (r != null) return r;
long start = (timeout == 0) ? 0 : System.nanoTime();
for (;;) {
lock.wait(timeout);
r = reallyPoll();
if (r != null) return r;
if (timeout != 0) {
long end = System.nanoTime();
timeout -= (end - start) / 1000_000;
if (timeout <= 0) return null;
start = end;
}
}
if (timeout == 0)
return remove();
lock.lock();
try {
return remove0(timeout);
} finally {
lock.unlock();
}
}
@ -174,7 +229,12 @@ public class ReferenceQueue<T> {
* @throws InterruptedException If the wait is interrupted
*/
public Reference<? extends T> remove() throws InterruptedException {
return remove(0);
lock.lock();
try {
return remove0();
} finally {
lock.unlock();
}
}
/**