rt/emul/compact/src/main/java/java/lang/ClassValue.java
changeset 1985 cd1cc103a03c
parent 1984 87bb69774b64
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/rt/emul/compact/src/main/java/java/lang/ClassValue.java	Tue Jan 17 07:04:06 2017 +0100
     1.3 @@ -0,0 +1,179 @@
     1.4 +/*
     1.5 + * Copyright (c) 2010, 2011, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + */
    1.28 +
    1.29 +package java.lang;
    1.30 +
    1.31 +import java.lang.Class;
    1.32 +import org.apidesign.bck2brwsr.core.JavaScriptBody;
    1.33 +
    1.34 +/**
    1.35 + * Lazily associate a computed value with (potentially) every type.
    1.36 + * For example, if a dynamic language needs to construct a message dispatch
    1.37 + * table for each class encountered at a message send call site,
    1.38 + * it can use a {@code ClassValue} to cache information needed to
    1.39 + * perform the message send quickly, for each class encountered.
    1.40 + * @author John Rose, JSR 292 EG
    1.41 + * @since 1.7
    1.42 + */
    1.43 +public abstract class ClassValue<T> {
    1.44 +    /**
    1.45 +     * Sole constructor.  (For invocation by subclass constructors, typically
    1.46 +     * implicit.)
    1.47 +     */
    1.48 +    protected ClassValue() {
    1.49 +    }
    1.50 +
    1.51 +    /**
    1.52 +     * Computes the given class's derived value for this {@code ClassValue}.
    1.53 +     * <p>
    1.54 +     * This method will be invoked within the first thread that accesses
    1.55 +     * the value with the {@link #get get} method.
    1.56 +     * <p>
    1.57 +     * Normally, this method is invoked at most once per class,
    1.58 +     * but it may be invoked again if there has been a call to
    1.59 +     * {@link #remove remove}.
    1.60 +     * <p>
    1.61 +     * If this method throws an exception, the corresponding call to {@code get}
    1.62 +     * will terminate abnormally with that exception, and no class value will be recorded.
    1.63 +     *
    1.64 +     * @param type the type whose class value must be computed
    1.65 +     * @return the newly computed value associated with this {@code ClassValue}, for the given class or interface
    1.66 +     * @see #get
    1.67 +     * @see #remove
    1.68 +     */
    1.69 +    protected abstract T computeValue(Class<?> type);
    1.70 +
    1.71 +    /**
    1.72 +     * Returns the value for the given class.
    1.73 +     * If no value has yet been computed, it is obtained by
    1.74 +     * an invocation of the {@link #computeValue computeValue} method.
    1.75 +     * <p>
    1.76 +     * The actual installation of the value on the class
    1.77 +     * is performed atomically.
    1.78 +     * At that point, if several racing threads have
    1.79 +     * computed values, one is chosen, and returned to
    1.80 +     * all the racing threads.
    1.81 +     * <p>
    1.82 +     * The {@code type} parameter is typically a class, but it may be any type,
    1.83 +     * such as an interface, a primitive type (like {@code int.class}), or {@code void.class}.
    1.84 +     * <p>
    1.85 +     * In the absence of {@code remove} calls, a class value has a simple
    1.86 +     * state diagram:  uninitialized and initialized.
    1.87 +     * When {@code remove} calls are made,
    1.88 +     * the rules for value observation are more complex.
    1.89 +     * See the documentation for {@link #remove remove} for more information.
    1.90 +     *
    1.91 +     * @param type the type whose class value must be computed or retrieved
    1.92 +     * @return the current value associated with this {@code ClassValue}, for the given class or interface
    1.93 +     * @throws NullPointerException if the argument is null
    1.94 +     * @see #remove
    1.95 +     * @see #computeValue
    1.96 +     */
    1.97 +    public T get(Class<?> type) {
    1.98 +        T value = access(type, id, false, null);
    1.99 +        if (value == undefined()) {
   1.100 +            value = access(type, id, true, computeValue(type));
   1.101 +        }
   1.102 +        return value;
   1.103 +    }
   1.104 +
   1.105 +    /**
   1.106 +     * Removes the associated value for the given class.
   1.107 +     * If this value is subsequently {@linkplain #get read} for the same class,
   1.108 +     * its value will be reinitialized by invoking its {@link #computeValue computeValue} method.
   1.109 +     * This may result in an additional invocation of the
   1.110 +     * {@code computeValue} method for the given class.
   1.111 +     * <p>
   1.112 +     * In order to explain the interaction between {@code get} and {@code remove} calls,
   1.113 +     * we must model the state transitions of a class value to take into account
   1.114 +     * the alternation between uninitialized and initialized states.
   1.115 +     * To do this, number these states sequentially from zero, and note that
   1.116 +     * uninitialized (or removed) states are numbered with even numbers,
   1.117 +     * while initialized (or re-initialized) states have odd numbers.
   1.118 +     * <p>
   1.119 +     * When a thread {@code T} removes a class value in state {@code 2N},
   1.120 +     * nothing happens, since the class value is already uninitialized.
   1.121 +     * Otherwise, the state is advanced atomically to {@code 2N+1}.
   1.122 +     * <p>
   1.123 +     * When a thread {@code T} queries a class value in state {@code 2N},
   1.124 +     * the thread first attempts to initialize the class value to state {@code 2N+1}
   1.125 +     * by invoking {@code computeValue} and installing the resulting value.
   1.126 +     * <p>
   1.127 +     * When {@code T} attempts to install the newly computed value,
   1.128 +     * if the state is still at {@code 2N}, the class value will be initialized
   1.129 +     * with the computed value, advancing it to state {@code 2N+1}.
   1.130 +     * <p>
   1.131 +     * Otherwise, whether the new state is even or odd,
   1.132 +     * {@code T} will discard the newly computed value
   1.133 +     * and retry the {@code get} operation.
   1.134 +     * <p>
   1.135 +     * Discarding and retrying is an important proviso,
   1.136 +     * since otherwise {@code T} could potentially install
   1.137 +     * a disastrously stale value.  For example:
   1.138 +     * <ul>
   1.139 +     * <li>{@code T} calls {@code CV.get(C)} and sees state {@code 2N}
   1.140 +     * <li>{@code T} quickly computes a time-dependent value {@code V0} and gets ready to install it
   1.141 +     * <li>{@code T} is hit by an unlucky paging or scheduling event, and goes to sleep for a long time
   1.142 +     * <li>...meanwhile, {@code T2} also calls {@code CV.get(C)} and sees state {@code 2N}
   1.143 +     * <li>{@code T2} quickly computes a similar time-dependent value {@code V1} and installs it on {@code CV.get(C)}
   1.144 +     * <li>{@code T2} (or a third thread) then calls {@code CV.remove(C)}, undoing {@code T2}'s work
   1.145 +     * <li> the previous actions of {@code T2} are repeated several times
   1.146 +     * <li> also, the relevant computed values change over time: {@code V1}, {@code V2}, ...
   1.147 +     * <li>...meanwhile, {@code T} wakes up and attempts to install {@code V0}; <em>this must fail</em>
   1.148 +     * </ul>
   1.149 +     * We can assume in the above scenario that {@code CV.computeValue} uses locks to properly
   1.150 +     * observe the time-dependent states as it computes {@code V1}, etc.
   1.151 +     * This does not remove the threat of a stale value, since there is a window of time
   1.152 +     * between the return of {@code computeValue} in {@code T} and the installation
   1.153 +     * of the the new value.  No user synchronization is possible during this time.
   1.154 +     *
   1.155 +     * @param type the type whose class value must be removed
   1.156 +     * @throws NullPointerException if the argument is null
   1.157 +     */
   1.158 +    public void remove(Class<?> type) {
   1.159 +        access(type, id, true, undefined());
   1.160 +    }
   1.161 +
   1.162 +    /// Implementation...
   1.163 +
   1.164 +    private static int COUNTER = 0;
   1.165 +    private final int id = COUNTER++;
   1.166 +
   1.167 +    @JavaScriptBody(args = {}, body = "return undefined;")
   1.168 +    private static native Object undefined();
   1.169 +
   1.170 +    @JavaScriptBody(args = { "where", "index", "set", "newValue" }, body =
   1.171 +        "var data = where['values'];\n" +
   1.172 +        "if (!data) {\n" +
   1.173 +        "  data = where['values'] = [];\n" +
   1.174 +        "}\n" +
   1.175 +        "if (set) {\n" +
   1.176 +        "  data[index] = newValue;\n" +
   1.177 +        "}\n" +
   1.178 +        "return data[index];\n" +
   1.179 +        ""
   1.180 +    )
   1.181 +    private static native <T> T access(Class<?> where, int index, boolean set, T newValue);
   1.182 +}