8256167: Convert JDK use of Reference::get
to Reference::refersTo
Reviewed-by: sspitsyn, shade, dfuchs, alanb, kbarrett
This commit is contained in:
parent
78be334c38
commit
972bc3b408
@ -2411,7 +2411,7 @@ public class ObjectStreamClass implements Serializable {
|
||||
Class<?> referent;
|
||||
return (nullClass ? other.nullClass
|
||||
: ((referent = get()) != null) &&
|
||||
(referent == other.get())) &&
|
||||
(other.refersTo(referent))) &&
|
||||
Arrays.equals(sigs, other.sigs);
|
||||
} else {
|
||||
return false;
|
||||
@ -2532,9 +2532,9 @@ public class ObjectStreamClass implements Serializable {
|
||||
}
|
||||
|
||||
if (obj instanceof WeakClassKey) {
|
||||
Object referent = get();
|
||||
Class<?> referent = get();
|
||||
return (referent != null) &&
|
||||
(referent == ((WeakClassKey) obj).get());
|
||||
(((WeakClassKey) obj).refersTo(referent));
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
|
@ -2044,9 +2044,9 @@ public class Thread implements Runnable {
|
||||
return true;
|
||||
|
||||
if (obj instanceof WeakClassKey) {
|
||||
Object referent = get();
|
||||
Class<?> referent = get();
|
||||
return (referent != null) &&
|
||||
(referent == ((WeakClassKey) obj).get());
|
||||
(((WeakClassKey) obj).refersTo(referent));
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
|
||||
* Copyright (c) 1997, 2020, 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
|
||||
@ -26,7 +26,7 @@
|
||||
package java.lang;
|
||||
import jdk.internal.misc.TerminatingThreadLocal;
|
||||
|
||||
import java.lang.ref.*;
|
||||
import java.lang.ref.WeakReference;
|
||||
import java.util.Objects;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.function.Supplier;
|
||||
@ -433,7 +433,7 @@ public class ThreadLocal<T> {
|
||||
private Entry getEntry(ThreadLocal<?> key) {
|
||||
int i = key.threadLocalHashCode & (table.length - 1);
|
||||
Entry e = table[i];
|
||||
if (e != null && e.get() == key)
|
||||
if (e != null && e.refersTo(key))
|
||||
return e;
|
||||
else
|
||||
return getEntryAfterMiss(key, i, e);
|
||||
@ -453,10 +453,9 @@ public class ThreadLocal<T> {
|
||||
int len = tab.length;
|
||||
|
||||
while (e != null) {
|
||||
ThreadLocal<?> k = e.get();
|
||||
if (k == key)
|
||||
if (e.refersTo(key))
|
||||
return e;
|
||||
if (k == null)
|
||||
if (e.refersTo(null))
|
||||
expungeStaleEntry(i);
|
||||
else
|
||||
i = nextIndex(i, len);
|
||||
@ -485,14 +484,12 @@ public class ThreadLocal<T> {
|
||||
for (Entry e = tab[i];
|
||||
e != null;
|
||||
e = tab[i = nextIndex(i, len)]) {
|
||||
ThreadLocal<?> k = e.get();
|
||||
|
||||
if (k == key) {
|
||||
if (e.refersTo(key)) {
|
||||
e.value = value;
|
||||
return;
|
||||
}
|
||||
|
||||
if (k == null) {
|
||||
if (e.refersTo(null)) {
|
||||
replaceStaleEntry(key, value, i);
|
||||
return;
|
||||
}
|
||||
@ -514,7 +511,7 @@ public class ThreadLocal<T> {
|
||||
for (Entry e = tab[i];
|
||||
e != null;
|
||||
e = tab[i = nextIndex(i, len)]) {
|
||||
if (e.get() == key) {
|
||||
if (e.refersTo(key)) {
|
||||
e.clear();
|
||||
expungeStaleEntry(i);
|
||||
return;
|
||||
@ -551,7 +548,7 @@ public class ThreadLocal<T> {
|
||||
for (int i = prevIndex(staleSlot, len);
|
||||
(e = tab[i]) != null;
|
||||
i = prevIndex(i, len))
|
||||
if (e.get() == null)
|
||||
if (e.refersTo(null))
|
||||
slotToExpunge = i;
|
||||
|
||||
// Find either the key or trailing null slot of run, whichever
|
||||
@ -559,14 +556,12 @@ public class ThreadLocal<T> {
|
||||
for (int i = nextIndex(staleSlot, len);
|
||||
(e = tab[i]) != null;
|
||||
i = nextIndex(i, len)) {
|
||||
ThreadLocal<?> k = e.get();
|
||||
|
||||
// If we find key, then we need to swap it
|
||||
// with the stale entry to maintain hash table order.
|
||||
// The newly stale slot, or any other stale slot
|
||||
// encountered above it, can then be sent to expungeStaleEntry
|
||||
// to remove or rehash all of the other entries in run.
|
||||
if (k == key) {
|
||||
if (e.refersTo(key)) {
|
||||
e.value = value;
|
||||
|
||||
tab[i] = tab[staleSlot];
|
||||
@ -582,7 +577,7 @@ public class ThreadLocal<T> {
|
||||
// If we didn't find stale entry on backward scan, the
|
||||
// first stale entry seen while scanning for key is the
|
||||
// first still present in the run.
|
||||
if (k == null && slotToExpunge == staleSlot)
|
||||
if (e.refersTo(null) && slotToExpunge == staleSlot)
|
||||
slotToExpunge = i;
|
||||
}
|
||||
|
||||
@ -673,7 +668,7 @@ public class ThreadLocal<T> {
|
||||
do {
|
||||
i = nextIndex(i, len);
|
||||
Entry e = tab[i];
|
||||
if (e != null && e.get() == null) {
|
||||
if (e != null && e.refersTo(null)) {
|
||||
n = len;
|
||||
removed = true;
|
||||
i = expungeStaleEntry(i);
|
||||
@ -733,7 +728,7 @@ public class ThreadLocal<T> {
|
||||
int len = tab.length;
|
||||
for (int j = 0; j < len; j++) {
|
||||
Entry e = tab[j];
|
||||
if (e != null && e.get() == null)
|
||||
if (e != null && e.refersTo(null))
|
||||
expungeStaleEntry(j);
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2008, 2018, Oracle and/or its affiliates. All rights reserved.
|
||||
* Copyright (c) 2008, 2020, 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
|
||||
@ -402,9 +402,8 @@ class DirectMethodHandle extends MethodHandle {
|
||||
if (ref == null) {
|
||||
return true; // the final state
|
||||
}
|
||||
Thread clinitThread = ref.get();
|
||||
// Somebody may still be running defc.<clinit>.
|
||||
if (clinitThread == Thread.currentThread()) {
|
||||
if (ref.refersTo(Thread.currentThread())) {
|
||||
// If anybody is running defc.<clinit>, it is this thread.
|
||||
if (UNSAFE.shouldBeInitialized(defc))
|
||||
// Yes, we are running it; keep the barrier for now.
|
||||
|
@ -337,7 +337,7 @@ public abstract class Reference<T> {
|
||||
*
|
||||
* @return The object to which this reference refers, or
|
||||
* {@code null} if this reference object has been cleared
|
||||
* @see refersTo
|
||||
* @see #refersTo
|
||||
*/
|
||||
@IntrinsicCandidate
|
||||
public T get() {
|
||||
|
@ -642,7 +642,7 @@ public class AccessibleObject implements AnnotatedElement {
|
||||
}
|
||||
|
||||
boolean isCacheFor(Class<?> caller, Class<?> refc) {
|
||||
return callerRef.get() == caller && targetRef.get() == refc;
|
||||
return callerRef.refersTo(caller) && targetRef.refersTo(refc);
|
||||
}
|
||||
|
||||
static Object protectedMemberCallerCache(Class<?> caller, Class<?> refc) {
|
||||
@ -674,7 +674,7 @@ public class AccessibleObject implements AnnotatedElement {
|
||||
if (cache instanceof WeakReference) {
|
||||
@SuppressWarnings("unchecked")
|
||||
WeakReference<Class<?>> ref = (WeakReference<Class<?>>) cache;
|
||||
return ref.get() == caller;
|
||||
return ref.refersTo(caller);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 1996, 2019, Oracle and/or its affiliates. All rights reserved.
|
||||
* Copyright (c) 1996, 2020, 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
|
||||
@ -1755,7 +1755,7 @@ public abstract class ResourceBundle {
|
||||
// Otherwise, remove the cached one since we can't keep
|
||||
// the same bundles having different parents.
|
||||
BundleReference bundleRef = cacheList.get(cacheKey);
|
||||
if (bundleRef != null && bundleRef.get() == bundle) {
|
||||
if (bundleRef != null && bundleRef.refersTo(bundle)) {
|
||||
cacheList.remove(cacheKey, bundleRef);
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 1998, 2018, Oracle and/or its affiliates. All rights reserved.
|
||||
* Copyright (c) 1998, 2020, 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
|
||||
@ -27,7 +27,6 @@ package java.util;
|
||||
|
||||
import java.lang.ref.WeakReference;
|
||||
import java.lang.ref.ReferenceQueue;
|
||||
import java.util.concurrent.ThreadLocalRandom;
|
||||
import java.util.function.BiConsumer;
|
||||
import java.util.function.BiFunction;
|
||||
import java.util.function.Consumer;
|
||||
@ -283,8 +282,14 @@ public class WeakHashMap<K,V>
|
||||
* Checks for equality of non-null reference x and possibly-null y. By
|
||||
* default uses Object.equals.
|
||||
*/
|
||||
private static boolean eq(Object x, Object y) {
|
||||
return x == y || x.equals(y);
|
||||
private boolean matchesKey(Entry<K,V> e, Object key) {
|
||||
// check if the given entry refers to the given key without
|
||||
// keeping a strong reference to the entry's referent
|
||||
if (e.refersTo(key)) return true;
|
||||
|
||||
// then check for equality if the referent is not cleared
|
||||
Object k = e.get();
|
||||
return k != null && key.equals(k);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -399,7 +404,7 @@ public class WeakHashMap<K,V>
|
||||
int index = indexFor(h, tab.length);
|
||||
Entry<K,V> e = tab[index];
|
||||
while (e != null) {
|
||||
if (e.hash == h && eq(k, e.get()))
|
||||
if (e.hash == h && matchesKey(e, k))
|
||||
return e.value;
|
||||
e = e.next;
|
||||
}
|
||||
@ -428,7 +433,7 @@ public class WeakHashMap<K,V>
|
||||
Entry<K,V>[] tab = getTable();
|
||||
int index = indexFor(h, tab.length);
|
||||
Entry<K,V> e = tab[index];
|
||||
while (e != null && !(e.hash == h && eq(k, e.get())))
|
||||
while (e != null && !(e.hash == h && matchesKey(e, k)))
|
||||
e = e.next;
|
||||
return e;
|
||||
}
|
||||
@ -452,7 +457,7 @@ public class WeakHashMap<K,V>
|
||||
int i = indexFor(h, tab.length);
|
||||
|
||||
for (Entry<K,V> e = tab[i]; e != null; e = e.next) {
|
||||
if (h == e.hash && eq(k, e.get())) {
|
||||
if (h == e.hash && matchesKey(e, k)) {
|
||||
V oldValue = e.value;
|
||||
if (value != oldValue)
|
||||
e.value = value;
|
||||
@ -515,8 +520,7 @@ public class WeakHashMap<K,V>
|
||||
src[j] = null;
|
||||
while (e != null) {
|
||||
Entry<K,V> next = e.next;
|
||||
Object key = e.get();
|
||||
if (key == null) {
|
||||
if (e.refersTo(null)) {
|
||||
e.next = null; // Help GC
|
||||
e.value = null; // " "
|
||||
size--;
|
||||
@ -597,7 +601,7 @@ public class WeakHashMap<K,V>
|
||||
|
||||
while (e != null) {
|
||||
Entry<K,V> next = e.next;
|
||||
if (h == e.hash && eq(k, e.get())) {
|
||||
if (h == e.hash && matchesKey(e, k)) {
|
||||
modCount++;
|
||||
size--;
|
||||
if (prev == e)
|
||||
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
|
||||
* Copyright (c) 2015, 2020, 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
|
||||
@ -181,12 +181,10 @@ public final class BootstrapLogger implements Logger, PlatformLogger.Bridge,
|
||||
// This is used by tests.
|
||||
static boolean isAlive() {
|
||||
WeakReference<ExecutorService> ref = executorRef;
|
||||
ExecutorService executor = ref == null ? null : ref.get();
|
||||
if (executor != null) return true;
|
||||
if (ref != null && !ref.refersTo(null)) return true;
|
||||
synchronized (BootstrapExecutors.class) {
|
||||
ref = executorRef;
|
||||
executor = ref == null ? null : ref.get();
|
||||
return executor != null;
|
||||
return ref != null && !ref.refersTo(null);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -777,7 +777,7 @@ public class LogManager {
|
||||
}
|
||||
LoggerWeakRef ref = namedLoggers.get(name);
|
||||
if (ref != null) {
|
||||
if (ref.get() == null) {
|
||||
if (ref.refersTo(null)) {
|
||||
// It's possible that the Logger was GC'ed after a
|
||||
// drainLoggerRefQueueBounded() call above so allow
|
||||
// a new one to be registered.
|
||||
|
@ -2404,8 +2404,7 @@ public class Logger {
|
||||
// assert parent.kids != null;
|
||||
for (Iterator<LogManager.LoggerWeakRef> iter = parent.kids.iterator(); iter.hasNext(); ) {
|
||||
ref = iter.next();
|
||||
Logger kid = ref.get();
|
||||
if (kid == this) {
|
||||
if (ref.refersTo(this)) {
|
||||
// ref is used down below to complete the reparenting
|
||||
iter.remove();
|
||||
break;
|
||||
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2005, 2008, Oracle and/or its affiliates. All rights reserved.
|
||||
* Copyright (c) 2005, 2020, 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
|
||||
@ -120,9 +120,10 @@ class WeakIdentityHashMap<K, V> {
|
||||
return true;
|
||||
if (!(o instanceof IdentityWeakReference<?>))
|
||||
return false;
|
||||
IdentityWeakReference<?> wr = (IdentityWeakReference<?>) o;
|
||||
Object got = get();
|
||||
return (got != null && got == wr.get());
|
||||
@SuppressWarnings("unchecked")
|
||||
IdentityWeakReference<T> wr = (IdentityWeakReference<T>) o;
|
||||
T got = get();
|
||||
return got != null && wr.refersTo(got);
|
||||
}
|
||||
|
||||
public int hashCode() {
|
||||
|
Loading…
x
Reference in New Issue
Block a user