Path: blob/master/src/java.base/share/classes/java/io/ClassCache.java
67794 views
/*1* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package java.io;2627import java.lang.ref.Reference;28import java.lang.ref.ReferenceQueue;29import java.lang.ref.SoftReference;30import java.util.Objects;3132// Maps Class instances to values of type T. Under memory pressure, the33// mapping is released (under soft references GC policy) and would be34// recomputed the next time it is queried. The mapping is bound to the35// lifetime of the class: when the class is unloaded, the mapping is36// removed too.37abstract class ClassCache<T> {3839private static class CacheRef<T> extends SoftReference<T> {40private final Class<?> type;41private T strongReferent;4243CacheRef(T referent, ReferenceQueue<T> queue, Class<?> type) {44super(referent, queue);45this.type = type;46this.strongReferent = referent;47}4849Class<?> getType() {50return type;51}5253T getStrong() {54return strongReferent;55}5657void clearStrong() {58strongReferent = null;59}60}6162private final ReferenceQueue<T> queue;63private final ClassValue<CacheRef<T>> map;6465protected abstract T computeValue(Class<?> cl);6667protected ClassCache() {68queue = new ReferenceQueue<>();69map = new ClassValue<>() {70@Override71protected CacheRef<T> computeValue(Class<?> type) {72T v = ClassCache.this.computeValue(type);73Objects.requireNonNull(v);74return new CacheRef<>(v, queue, type);75}76};77}7879T get(Class<?> cl) {80while (true) {81processQueue();8283CacheRef<T> ref = map.get(cl);8485// Case 1: A recently created CacheRef.86// We might still have strong referent, and can return it.87// This guarantees progress for at least one thread on every CacheRef.88// Clear the strong referent before returning to make the cache soft.89T strongVal = ref.getStrong();90if (strongVal != null) {91ref.clearStrong();92return strongVal;93}9495// Case 2: Older or recently cleared CacheRef.96// Check if its soft referent is still available, and return it.97T val = ref.get();98if (val != null) {99return val;100}101102// Case 3: The reference was cleared.103// Clear the mapping and retry.104map.remove(cl);105}106}107108private void processQueue() {109Reference<? extends T> ref;110while((ref = queue.poll()) != null) {111CacheRef<? extends T> cacheRef = (CacheRef<? extends T>)ref;112map.remove(cacheRef.getType());113}114}115}116117118