diff -r 87bb69774b64 -r cd1cc103a03c rt/emul/compact/src/main/java/java/lang/ClassValue.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/rt/emul/compact/src/main/java/java/lang/ClassValue.java Tue Jan 17 07:04:06 2017 +0100 @@ -0,0 +1,179 @@ +/* + * Copyright (c) 2010, 2011, 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 + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +package java.lang; + +import java.lang.Class; +import org.apidesign.bck2brwsr.core.JavaScriptBody; + +/** + * Lazily associate a computed value with (potentially) every type. + * For example, if a dynamic language needs to construct a message dispatch + * table for each class encountered at a message send call site, + * it can use a {@code ClassValue} to cache information needed to + * perform the message send quickly, for each class encountered. + * @author John Rose, JSR 292 EG + * @since 1.7 + */ +public abstract class ClassValue { + /** + * Sole constructor. (For invocation by subclass constructors, typically + * implicit.) + */ + protected ClassValue() { + } + + /** + * Computes the given class's derived value for this {@code ClassValue}. + *

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

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

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

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

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

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

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

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

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

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

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

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

+ * We can assume in the above scenario that {@code CV.computeValue} uses locks to properly + * observe the time-dependent states as it computes {@code V1}, etc. + * This does not remove the threat of a stale value, since there is a window of time + * between the return of {@code computeValue} in {@code T} and the installation + * of the the new value. No user synchronization is possible during this time. + * + * @param type the type whose class value must be removed + * @throws NullPointerException if the argument is null + */ + public void remove(Class type) { + access(type, id, true, undefined()); + } + + /// Implementation... + + private static int COUNTER = 0; + private final int id = COUNTER++; + + @JavaScriptBody(args = {}, body = "return undefined;") + private static native Object undefined(); + + @JavaScriptBody(args = { "where", "index", "set", "newValue" }, body = + "var data = where['values'];\n" + + "if (!data) {\n" + + " data = where['values'] = [];\n" + + "}\n" + + "if (set) {\n" + + " data[index] = newValue;\n" + + "}\n" + + "return data[index];\n" + + "" + ) + private static native T access(Class where, int index, boolean set, T newValue); +}