Path: blob/master/src/java.desktop/share/classes/sun/java2d/ReentrantContextProvider.java
41152 views
/*1* Copyright (c) 2016, 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*/24package sun.java2d;2526import java.lang.ref.Reference;27import java.lang.ref.SoftReference;28import java.lang.ref.WeakReference;2930/**31* This abstract ReentrantContextProvider helper class manages the creation,32* storage, and retrieval of concrete ReentrantContext instances which can be33* subclassed to hold cached contextual data.34*35* It supports reentrancy as every call to acquire() provides a new unique context36* instance that must later be returned for reuse by a call to release(ctx)37* (typically in a try/finally block).38*39* It has a couple of abstract implementations which store references in a queue40* and/or thread-local storage.41* The Providers can be configured to hold ReentrantContext instances in memory42* using hard, soft or weak references.43*44* The acquire() and release() methods are used to retrieve and return the contexts.45*46* The {@code newContext()} method remains abstract in all implementations and47* must be provided by the module to create a new subclass of ReentrantContext48* with the appropriate contextual data in it.49*50* Sample Usage:51* - create a subclass ReentrantContextImpl to hold the thread state:52*53* static final class ReentrantContextImpl extends ReentrantContext {54* // specific cached data55* }56*57* - create the appropriate ReentrantContextProvider:58*59* private static final ReentrantContextProvider<ReentrantContextImpl> contextProvider =60* new ReentrantContextProviderTL<ReentrantContextImpl>(ReentrantContextProvider.REF_WEAK)61* {62* @Override63* protected ReentrantContextImpl newContext() {64* return new ReentrantContextImpl();65* }66* };67* ...68* void someMethod() {69* ReentrantContextImpl ctx = contextProvider.acquire();70* try {71* // use the context72* } finally {73* contextProvider.release(ctx);74* }75* }76*77* @param <K> ReentrantContext subclass78*79* @see ReentrantContext80*/81public abstract class ReentrantContextProvider<K extends ReentrantContext>82{83// thread-local storage: inactive84static final byte USAGE_TL_INACTIVE = 0;85// thread-local storage: in use86static final byte USAGE_TL_IN_USE = 1;87// CLQ storage88static final byte USAGE_CLQ = 2;8990// hard reference91public static final int REF_HARD = 0;92// soft reference93public static final int REF_SOFT = 1;94// weak reference95public static final int REF_WEAK = 2;9697/* members */98// internal reference type99private final int refType;100101/**102* Create a new ReentrantContext provider using the given reference type103* among hard, soft or weak104*105* @param refType reference type106*/107protected ReentrantContextProvider(final int refType) {108this.refType = refType;109}110111/**112* Create a new ReentrantContext instance113*114* @return new ReentrantContext instance115*/116protected abstract K newContext();117118/**119* Give a ReentrantContext instance for the current thread120*121* @return ReentrantContext instance122*/123public abstract K acquire();124125/**126* Restore the given ReentrantContext instance for reuse127*128* @param ctx ReentrantContext instance129*/130public abstract void release(K ctx);131132@SuppressWarnings("unchecked")133protected final Reference<K> getOrCreateReference(final K ctx) {134if (ctx.reference == null) {135// Create the reference:136switch (refType) {137case REF_HARD:138ctx.reference = new HardReference<K>(ctx);139break;140case REF_SOFT:141ctx.reference = new SoftReference<K>(ctx);142break;143default:144case REF_WEAK:145ctx.reference = new WeakReference<K>(ctx);146break;147}148}149return (Reference<K>) ctx.reference;150}151152/* Missing HardReference implementation */153static final class HardReference<V> extends WeakReference<V> {154// kept strong reference:155private final V strongRef;156157HardReference(final V referent) {158// no referent needed for the parent WeakReference:159super(null);160this.strongRef = referent;161}162163@Override164public V get() {165return strongRef;166}167}168}169170171