jaroslav@1981: /* jaroslav@1981: * Copyright (c) 2010, 2011, Oracle and/or its affiliates. All rights reserved. jaroslav@1981: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. jaroslav@1981: * jaroslav@1981: * This code is free software; you can redistribute it and/or modify it jaroslav@1981: * under the terms of the GNU General Public License version 2 only, as jaroslav@1981: * published by the Free Software Foundation. Oracle designates this jaroslav@1981: * particular file as subject to the "Classpath" exception as provided jaroslav@1981: * by Oracle in the LICENSE file that accompanied this code. jaroslav@1981: * jaroslav@1981: * This code is distributed in the hope that it will be useful, but WITHOUT jaroslav@1981: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or jaroslav@1981: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License jaroslav@1981: * version 2 for more details (a copy is included in the LICENSE file that jaroslav@1981: * accompanied this code). jaroslav@1981: * jaroslav@1981: * You should have received a copy of the GNU General Public License version jaroslav@1981: * 2 along with this work; if not, write to the Free Software Foundation, jaroslav@1981: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. jaroslav@1981: * jaroslav@1981: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA jaroslav@1981: * or visit www.oracle.com if you need additional information or have any jaroslav@1981: * questions. jaroslav@1981: */ jaroslav@1981: jaroslav@1981: package java.lang; jaroslav@1981: jaroslav@1981: import java.util.WeakHashMap; jaroslav@1981: import java.util.concurrent.atomic.AtomicInteger; jaroslav@1981: jaroslav@1981: /** jaroslav@1981: * Lazily associate a computed value with (potentially) every type. jaroslav@1981: * For example, if a dynamic language needs to construct a message dispatch jaroslav@1981: * table for each class encountered at a message send call site, jaroslav@1981: * it can use a {@code ClassValue} to cache information needed to jaroslav@1981: * perform the message send quickly, for each class encountered. jaroslav@1981: * @author John Rose, JSR 292 EG jaroslav@1981: * @since 1.7 jaroslav@1981: */ jaroslav@1981: public abstract class ClassValue { jaroslav@1981: /** jaroslav@1981: * Sole constructor. (For invocation by subclass constructors, typically jaroslav@1981: * implicit.) jaroslav@1981: */ jaroslav@1981: protected ClassValue() { jaroslav@1981: } jaroslav@1981: jaroslav@1981: /** jaroslav@1981: * Computes the given class's derived value for this {@code ClassValue}. jaroslav@1981: *

jaroslav@1981: * This method will be invoked within the first thread that accesses jaroslav@1981: * the value with the {@link #get get} method. jaroslav@1981: *

jaroslav@1981: * Normally, this method is invoked at most once per class, jaroslav@1981: * but it may be invoked again if there has been a call to jaroslav@1981: * {@link #remove remove}. jaroslav@1981: *

jaroslav@1981: * If this method throws an exception, the corresponding call to {@code get} jaroslav@1981: * will terminate abnormally with that exception, and no class value will be recorded. jaroslav@1981: * jaroslav@1981: * @param type the type whose class value must be computed jaroslav@1981: * @return the newly computed value associated with this {@code ClassValue}, for the given class or interface jaroslav@1981: * @see #get jaroslav@1981: * @see #remove jaroslav@1981: */ jaroslav@1981: protected abstract T computeValue(Class type); jaroslav@1981: jaroslav@1981: /** jaroslav@1981: * Returns the value for the given class. jaroslav@1981: * If no value has yet been computed, it is obtained by jaroslav@1981: * an invocation of the {@link #computeValue computeValue} method. jaroslav@1981: *

jaroslav@1981: * The actual installation of the value on the class jaroslav@1981: * is performed atomically. jaroslav@1981: * At that point, if several racing threads have jaroslav@1981: * computed values, one is chosen, and returned to jaroslav@1981: * all the racing threads. jaroslav@1981: *

jaroslav@1981: * The {@code type} parameter is typically a class, but it may be any type, jaroslav@1981: * such as an interface, a primitive type (like {@code int.class}), or {@code void.class}. jaroslav@1981: *

jaroslav@1981: * In the absence of {@code remove} calls, a class value has a simple jaroslav@1981: * state diagram: uninitialized and initialized. jaroslav@1981: * When {@code remove} calls are made, jaroslav@1981: * the rules for value observation are more complex. jaroslav@1981: * See the documentation for {@link #remove remove} for more information. jaroslav@1981: * jaroslav@1981: * @param type the type whose class value must be computed or retrieved jaroslav@1981: * @return the current value associated with this {@code ClassValue}, for the given class or interface jaroslav@1981: * @throws NullPointerException if the argument is null jaroslav@1981: * @see #remove jaroslav@1981: * @see #computeValue jaroslav@1981: */ jaroslav@1981: public T get(Class type) { jaroslav@1981: ClassValueMap map = getMap(type); jaroslav@1981: if (map != null) { jaroslav@1981: Object x = map.get(this); jaroslav@1981: if (x != null) { jaroslav@1981: return (T) map.unmaskNull(x); jaroslav@1981: } jaroslav@1981: } jaroslav@1981: return setComputedValue(type); jaroslav@1981: } jaroslav@1981: jaroslav@1981: /** jaroslav@1981: * Removes the associated value for the given class. jaroslav@1981: * If this value is subsequently {@linkplain #get read} for the same class, jaroslav@1981: * its value will be reinitialized by invoking its {@link #computeValue computeValue} method. jaroslav@1981: * This may result in an additional invocation of the jaroslav@1981: * {@code computeValue} method for the given class. jaroslav@1981: *

jaroslav@1981: * In order to explain the interaction between {@code get} and {@code remove} calls, jaroslav@1981: * we must model the state transitions of a class value to take into account jaroslav@1981: * the alternation between uninitialized and initialized states. jaroslav@1981: * To do this, number these states sequentially from zero, and note that jaroslav@1981: * uninitialized (or removed) states are numbered with even numbers, jaroslav@1981: * while initialized (or re-initialized) states have odd numbers. jaroslav@1981: *

jaroslav@1981: * When a thread {@code T} removes a class value in state {@code 2N}, jaroslav@1981: * nothing happens, since the class value is already uninitialized. jaroslav@1981: * Otherwise, the state is advanced atomically to {@code 2N+1}. jaroslav@1981: *

jaroslav@1981: * When a thread {@code T} queries a class value in state {@code 2N}, jaroslav@1981: * the thread first attempts to initialize the class value to state {@code 2N+1} jaroslav@1981: * by invoking {@code computeValue} and installing the resulting value. jaroslav@1981: *

jaroslav@1981: * When {@code T} attempts to install the newly computed value, jaroslav@1981: * if the state is still at {@code 2N}, the class value will be initialized jaroslav@1981: * with the computed value, advancing it to state {@code 2N+1}. jaroslav@1981: *

jaroslav@1981: * Otherwise, whether the new state is even or odd, jaroslav@1981: * {@code T} will discard the newly computed value jaroslav@1981: * and retry the {@code get} operation. jaroslav@1981: *

jaroslav@1981: * Discarding and retrying is an important proviso, jaroslav@1981: * since otherwise {@code T} could potentially install jaroslav@1981: * a disastrously stale value. For example: jaroslav@1981: *

jaroslav@1981: * We can assume in the above scenario that {@code CV.computeValue} uses locks to properly jaroslav@1981: * observe the time-dependent states as it computes {@code V1}, etc. jaroslav@1981: * This does not remove the threat of a stale value, since there is a window of time jaroslav@1981: * between the return of {@code computeValue} in {@code T} and the installation jaroslav@1981: * of the the new value. No user synchronization is possible during this time. jaroslav@1981: * jaroslav@1981: * @param type the type whose class value must be removed jaroslav@1981: * @throws NullPointerException if the argument is null jaroslav@1981: */ jaroslav@1981: public void remove(Class type) { jaroslav@1981: ClassValueMap map = getMap(type); jaroslav@1981: if (map != null) { jaroslav@1981: synchronized (map) { jaroslav@1981: map.remove(this); jaroslav@1981: } jaroslav@1981: } jaroslav@1981: } jaroslav@1981: jaroslav@1981: /// Implementation... jaroslav@1981: // FIXME: Use a data structure here similar that of ThreadLocal (7030453). jaroslav@1981: jaroslav@1981: private static final AtomicInteger STORE_BARRIER = new AtomicInteger(); jaroslav@1981: jaroslav@1981: /** Slow path for {@link #get}. */ jaroslav@1981: private T setComputedValue(Class type) { jaroslav@1981: ClassValueMap map = getMap(type); jaroslav@1981: if (map == null) { jaroslav@1981: map = initializeMap(type); jaroslav@1981: } jaroslav@1981: T value = computeValue(type); jaroslav@1981: STORE_BARRIER.lazySet(0); jaroslav@1981: // All stores pending from computeValue are completed. jaroslav@1981: synchronized (map) { jaroslav@1981: // Warm up the table with a null entry. jaroslav@1981: map.preInitializeEntry(this); jaroslav@1981: } jaroslav@1981: STORE_BARRIER.lazySet(0); jaroslav@1981: // All stores pending from table expansion are completed. jaroslav@1981: synchronized (map) { jaroslav@1981: value = (T) map.initializeEntry(this, value); jaroslav@1981: // One might fear a possible race condition here jaroslav@1981: // if the code for map.put has flushed the write jaroslav@1981: // to map.table[*] before the writes to the Map.Entry jaroslav@1981: // are done. This is not possible, since we have jaroslav@1981: // warmed up the table with an empty entry. jaroslav@1981: } jaroslav@1981: return value; jaroslav@1981: } jaroslav@1981: jaroslav@1981: // Replace this map by a per-class slot. jaroslav@1981: private static final WeakHashMap, ClassValueMap> ROOT jaroslav@1981: = new WeakHashMap, ClassValueMap>(); jaroslav@1981: jaroslav@1981: private static ClassValueMap getMap(Class type) { jaroslav@1981: type.getClass(); // test for null jaroslav@1981: return ROOT.get(type); jaroslav@1981: } jaroslav@1981: jaroslav@1981: private static ClassValueMap initializeMap(Class type) { jaroslav@1981: synchronized (ClassValue.class) { jaroslav@1981: ClassValueMap map = ROOT.get(type); jaroslav@1981: if (map == null) jaroslav@1981: ROOT.put(type, map = new ClassValueMap()); jaroslav@1981: return map; jaroslav@1981: } jaroslav@1981: } jaroslav@1981: jaroslav@1981: static class ClassValueMap extends WeakHashMap { jaroslav@1981: /** Make sure this table contains an Entry for the given key, even if it is empty. */ jaroslav@1981: void preInitializeEntry(ClassValue key) { jaroslav@1981: if (!this.containsKey(key)) jaroslav@1981: this.put(key, null); jaroslav@1981: } jaroslav@1981: /** Make sure this table contains a non-empty Entry for the given key. */ jaroslav@1981: Object initializeEntry(ClassValue key, Object value) { jaroslav@1981: Object prior = this.get(key); jaroslav@1981: if (prior != null) { jaroslav@1981: return unmaskNull(prior); jaroslav@1981: } jaroslav@1981: this.put(key, maskNull(value)); jaroslav@1981: return value; jaroslav@1981: } jaroslav@1981: jaroslav@1981: Object maskNull(Object x) { jaroslav@1981: return x == null ? this : x; jaroslav@1981: } jaroslav@1981: Object unmaskNull(Object x) { jaroslav@1981: return x == this ? null : x; jaroslav@1981: } jaroslav@1981: } jaroslav@1981: }