Skip to content

Commit

Permalink
8256167: Convert JDK use of Reference::get to Reference::refersTo
Browse files Browse the repository at this point in the history
Reviewed-by: sspitsyn, shade, dfuchs, alanb, kbarrett
  • Loading branch information
Mandy Chung committed Dec 6, 2020
1 parent 78be334 commit 972bc3b
Show file tree
Hide file tree
Showing 12 changed files with 49 additions and 53 deletions.
6 changes: 3 additions & 3 deletions src/java.base/share/classes/java/io/ObjectStreamClass.java
Original file line number Diff line number Diff line change
Expand Up @@ -2411,7 +2411,7 @@ public boolean equals(Object obj) {
Class<?> referent;
return (nullClass ? other.nullClass
: ((referent = get()) != null) &&
(referent == other.get())) &&
(other.refersTo(referent))) &&
Arrays.equals(sigs, other.sigs);
} else {
return false;
Expand Down Expand Up @@ -2532,9 +2532,9 @@ public boolean equals(Object obj) {
}

if (obj instanceof WeakClassKey) {
Object referent = get();
Class<?> referent = get();
return (referent != null) &&
(referent == ((WeakClassKey) obj).get());
(((WeakClassKey) obj).refersTo(referent));
} else {
return false;
}
Expand Down
4 changes: 2 additions & 2 deletions src/java.base/share/classes/java/lang/Thread.java
Original file line number Diff line number Diff line change
Expand Up @@ -2044,9 +2044,9 @@ public boolean equals(Object obj) {
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;
}
Expand Down
31 changes: 13 additions & 18 deletions src/java.base/share/classes/java/lang/ThreadLocal.java
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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;
Expand Down Expand Up @@ -433,7 +433,7 @@ private ThreadLocalMap(ThreadLocalMap parentMap) {
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);
Expand All @@ -453,10 +453,9 @@ private Entry getEntryAfterMiss(ThreadLocal<?> key, int i, Entry e) {
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);
Expand Down Expand Up @@ -485,14 +484,12 @@ private void set(ThreadLocal<?> key, Object value) {
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;
}
Expand All @@ -514,7 +511,7 @@ private void remove(ThreadLocal<?> key) {
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;
Expand Down Expand Up @@ -551,22 +548,20 @@ private void replaceStaleEntry(ThreadLocal<?> key, Object value,
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
// occurs first
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];
Expand All @@ -582,7 +577,7 @@ private void replaceStaleEntry(ThreadLocal<?> key, Object value,
// 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;
}

Expand Down Expand Up @@ -673,7 +668,7 @@ private boolean cleanSomeSlots(int i, int n) {
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);
Expand Down Expand Up @@ -733,7 +728,7 @@ private void expungeStaleEntries() {
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);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -402,9 +402,8 @@ private static boolean checkInitialized(MemberName member) {
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.
Expand Down
2 changes: 1 addition & 1 deletion src/java.base/share/classes/java/lang/ref/Reference.java
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ public void runFinalization() {
*
* @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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -642,7 +642,7 @@ private static class Cache {
}

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) {
Expand Down Expand Up @@ -674,7 +674,7 @@ private boolean isAccessChecked(Class<?> caller) {
if (cache instanceof WeakReference) {
@SuppressWarnings("unchecked")
WeakReference<Class<?>> ref = (WeakReference<Class<?>>) cache;
return ref.get() == caller;
return ref.refersTo(caller);
}
return false;
}
Expand Down
4 changes: 2 additions & 2 deletions src/java.base/share/classes/java/util/ResourceBundle.java
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -1755,7 +1755,7 @@ private static ResourceBundle findBundle(Module callerModule,
// 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);
}
}
Expand Down
24 changes: 14 additions & 10 deletions src/java.base/share/classes/java/util/WeakHashMap.java
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -27,7 +27,6 @@

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;
Expand Down Expand Up @@ -283,8 +282,14 @@ static Object unmaskNull(Object key) {
* 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);
}

/**
Expand Down Expand Up @@ -399,7 +404,7 @@ public V get(Object key) {
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;
}
Expand Down Expand Up @@ -428,7 +433,7 @@ Entry<K,V> getEntry(Object key) {
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;
}
Expand All @@ -452,7 +457,7 @@ public V put(K key, V value) {
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;
Expand Down Expand Up @@ -515,8 +520,7 @@ private void transfer(Entry<K,V>[] src, Entry<K,V>[] dest) {
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--;
Expand Down Expand Up @@ -597,7 +601,7 @@ public V remove(Object key) {

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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -181,12 +181,10 @@ static void awaitPendingTasks() {
// 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);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -777,7 +777,7 @@ synchronized boolean addLocalLogger(Logger logger, boolean addDefaultLoggersIfNe
}
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.
Expand Down
3 changes: 1 addition & 2 deletions src/java.logging/share/classes/java/util/logging/Logger.java
Original file line number Diff line number Diff line change
Expand Up @@ -2404,8 +2404,7 @@ private void doSetParent(Logger newParent) {
// 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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -120,9 +120,10 @@ public boolean equals(Object o) {
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() {
Expand Down

1 comment on commit 972bc3b

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.