jaroslav@1646: /* jaroslav@1646: * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. jaroslav@1646: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. jaroslav@1646: * jaroslav@1646: * This code is free software; you can redistribute it and/or modify it jaroslav@1646: * under the terms of the GNU General Public License version 2 only, as jaroslav@1646: * published by the Free Software Foundation. Oracle designates this jaroslav@1646: * particular file as subject to the "Classpath" exception as provided jaroslav@1646: * by Oracle in the LICENSE file that accompanied this code. jaroslav@1646: * jaroslav@1646: * This code is distributed in the hope that it will be useful, but WITHOUT jaroslav@1646: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or jaroslav@1646: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License jaroslav@1646: * version 2 for more details (a copy is included in the LICENSE file that jaroslav@1646: * accompanied this code). jaroslav@1646: * jaroslav@1646: * You should have received a copy of the GNU General Public License version jaroslav@1646: * 2 along with this work; if not, write to the Free Software Foundation, jaroslav@1646: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. jaroslav@1646: * jaroslav@1646: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA jaroslav@1646: * or visit www.oracle.com if you need additional information or have any jaroslav@1646: * questions. jaroslav@1646: */ jaroslav@1646: jaroslav@1646: package java.lang.invoke; jaroslav@1646: jaroslav@1646: /** jaroslav@1646: *

jaroslav@1646: * A {@code SwitchPoint} is an object which can publish state transitions to other threads. jaroslav@1646: * A switch point is initially in the valid state, but may at any time be jaroslav@1646: * changed to the invalid state. Invalidation cannot be reversed. jaroslav@1646: * A switch point can combine a guarded pair of method handles into a jaroslav@1646: * guarded delegator. jaroslav@1646: * The guarded delegator is a method handle which delegates to one of the old method handles. jaroslav@1646: * The state of the switch point determines which of the two gets the delegation. jaroslav@1646: *

jaroslav@1646: * A single switch point may be used to control any number of method handles. jaroslav@1646: * (Indirectly, therefore, it can control any number of call sites.) jaroslav@1646: * This is done by using the single switch point as a factory for combining jaroslav@1646: * any number of guarded method handle pairs into guarded delegators. jaroslav@1646: *

jaroslav@1646: * When a guarded delegator is created from a guarded pair, the pair jaroslav@1646: * is wrapped in a new method handle {@code M}, jaroslav@1646: * which is permanently associated with the switch point that created it. jaroslav@1646: * Each pair consists of a target {@code T} and a fallback {@code F}. jaroslav@1646: * While the switch point is valid, invocations to {@code M} are delegated to {@code T}. jaroslav@1646: * After it is invalidated, invocations are delegated to {@code F}. jaroslav@1646: *

jaroslav@1646: * Invalidation is global and immediate, as if the switch point contained a jaroslav@1646: * volatile boolean variable consulted on every call to {@code M}. jaroslav@1646: * The invalidation is also permanent, which means the switch point jaroslav@1646: * can change state only once. jaroslav@1646: * The switch point will always delegate to {@code F} after being invalidated. jaroslav@1646: * At that point {@code guardWithTest} may ignore {@code T} and return {@code F}. jaroslav@1646: *

jaroslav@1646: * Here is an example of a switch point in action: jaroslav@1646: *

{@code
jaroslav@1646: MethodHandle MH_strcat = MethodHandles.lookup()
jaroslav@1646:     .findVirtual(String.class, "concat", MethodType.methodType(String.class, String.class));
jaroslav@1646: SwitchPoint spt = new SwitchPoint();
jaroslav@1646: assert(!spt.hasBeenInvalidated());
jaroslav@1646: // the following steps may be repeated to re-use the same switch point:
jaroslav@1646: MethodHandle worker1 = MH_strcat;
jaroslav@1646: MethodHandle worker2 = MethodHandles.permuteArguments(MH_strcat, MH_strcat.type(), 1, 0);
jaroslav@1646: MethodHandle worker = spt.guardWithTest(worker1, worker2);
jaroslav@1646: assertEquals("method", (String) worker.invokeExact("met", "hod"));
jaroslav@1646: SwitchPoint.invalidateAll(new SwitchPoint[]{ spt });
jaroslav@1646: assert(spt.hasBeenInvalidated());
jaroslav@1646: assertEquals("hodmet", (String) worker.invokeExact("met", "hod"));
jaroslav@1646:  * }
jaroslav@1646: *

jaroslav@1646: * Discussion: jaroslav@1646: * Switch points are useful without subclassing. They may also be subclassed. jaroslav@1646: * This may be useful in order to associate application-specific invalidation logic jaroslav@1646: * with the switch point. jaroslav@1646: * Notice that there is no permanent association between a switch point and jaroslav@1646: * the method handles it produces and consumes. jaroslav@1646: * The garbage collector may collect method handles produced or consumed jaroslav@1646: * by a switch point independently of the lifetime of the switch point itself. jaroslav@1646: *

jaroslav@1646: * Implementation Note: jaroslav@1646: * A switch point behaves as if implemented on top of {@link MutableCallSite}, jaroslav@1646: * approximately as follows: jaroslav@1646: *

{@code
jaroslav@1646: public class SwitchPoint {
jaroslav@1646:   private static final MethodHandle
jaroslav@1646:     K_true  = MethodHandles.constant(boolean.class, true),
jaroslav@1646:     K_false = MethodHandles.constant(boolean.class, false);
jaroslav@1646:   private final MutableCallSite mcs;
jaroslav@1646:   private final MethodHandle mcsInvoker;
jaroslav@1646:   public SwitchPoint() {
jaroslav@1646:     this.mcs = new MutableCallSite(K_true);
jaroslav@1646:     this.mcsInvoker = mcs.dynamicInvoker();
jaroslav@1646:   }
jaroslav@1646:   public MethodHandle guardWithTest(
jaroslav@1646:                 MethodHandle target, MethodHandle fallback) {
jaroslav@1646:     // Note:  mcsInvoker is of type ()boolean.
jaroslav@1646:     // Target and fallback may take any arguments, but must have the same type.
jaroslav@1646:     return MethodHandles.guardWithTest(this.mcsInvoker, target, fallback);
jaroslav@1646:   }
jaroslav@1646:   public static void invalidateAll(SwitchPoint[] spts) {
jaroslav@1646:     List<MutableCallSite> mcss = new ArrayList<>();
jaroslav@1646:     for (SwitchPoint spt : spts)  mcss.add(spt.mcs);
jaroslav@1646:     for (MutableCallSite mcs : mcss)  mcs.setTarget(K_false);
jaroslav@1646:     MutableCallSite.syncAll(mcss.toArray(new MutableCallSite[0]));
jaroslav@1646:   }
jaroslav@1646: }
jaroslav@1646:  * }
jaroslav@1646: * @author Remi Forax, JSR 292 EG jaroslav@1646: */ jaroslav@1646: public class SwitchPoint { jaroslav@1646: private static final MethodHandle jaroslav@1646: K_true = MethodHandles.constant(boolean.class, true), jaroslav@1646: K_false = MethodHandles.constant(boolean.class, false); jaroslav@1646: jaroslav@1646: private final MutableCallSite mcs; jaroslav@1646: private final MethodHandle mcsInvoker; jaroslav@1646: jaroslav@1646: /** jaroslav@1646: * Creates a new switch point. jaroslav@1646: */ jaroslav@1646: public SwitchPoint() { jaroslav@1646: this.mcs = new MutableCallSite(K_true); jaroslav@1646: this.mcsInvoker = mcs.dynamicInvoker(); jaroslav@1646: } jaroslav@1646: jaroslav@1646: /** jaroslav@1646: * Determines if this switch point has been invalidated yet. jaroslav@1646: * jaroslav@1646: *

jaroslav@1646: * Discussion: jaroslav@1646: * Because of the one-way nature of invalidation, once a switch point begins jaroslav@1646: * to return true for {@code hasBeenInvalidated}, jaroslav@1646: * it will always do so in the future. jaroslav@1646: * On the other hand, a valid switch point visible to other threads may jaroslav@1646: * be invalidated at any moment, due to a request by another thread. jaroslav@1646: *

jaroslav@1646: * Since invalidation is a global and immediate operation, jaroslav@1646: * the execution of this query, on a valid switchpoint, jaroslav@1646: * must be internally sequenced with any jaroslav@1646: * other threads that could cause invalidation. jaroslav@1646: * This query may therefore be expensive. jaroslav@1646: * The recommended way to build a boolean-valued method handle jaroslav@1646: * which queries the invalidation state of a switch point {@code s} is jaroslav@1646: * to call {@code s.guardWithTest} on jaroslav@1646: * {@link MethodHandles#constant constant} true and false method handles. jaroslav@1646: * jaroslav@1646: * @return true if this switch point has been invalidated jaroslav@1646: */ jaroslav@1646: public boolean hasBeenInvalidated() { jaroslav@1646: return (mcs.getTarget() != K_true); jaroslav@1646: } jaroslav@1646: jaroslav@1646: /** jaroslav@1646: * Returns a method handle which always delegates either to the target or the fallback. jaroslav@1646: * The method handle will delegate to the target exactly as long as the switch point is valid. jaroslav@1646: * After that, it will permanently delegate to the fallback. jaroslav@1646: *

jaroslav@1646: * The target and fallback must be of exactly the same method type, jaroslav@1646: * and the resulting combined method handle will also be of this type. jaroslav@1646: * jaroslav@1646: * @param target the method handle selected by the switch point as long as it is valid jaroslav@1646: * @param fallback the method handle selected by the switch point after it is invalidated jaroslav@1646: * @return a combined method handle which always calls either the target or fallback jaroslav@1646: * @throws NullPointerException if either argument is null jaroslav@1646: * @throws IllegalArgumentException if the two method types do not match jaroslav@1646: * @see MethodHandles#guardWithTest jaroslav@1646: */ jaroslav@1646: public MethodHandle guardWithTest(MethodHandle target, MethodHandle fallback) { jaroslav@1646: if (mcs.getTarget() == K_false) jaroslav@1646: return fallback; // already invalid jaroslav@1646: return MethodHandles.guardWithTest(mcsInvoker, target, fallback); jaroslav@1646: } jaroslav@1646: jaroslav@1646: /** jaroslav@1646: * Sets all of the given switch points into the invalid state. jaroslav@1646: * After this call executes, no thread will observe any of the jaroslav@1646: * switch points to be in a valid state. jaroslav@1646: *

jaroslav@1646: * This operation is likely to be expensive and should be used sparingly. jaroslav@1646: * If possible, it should be buffered for batch processing on sets of switch points. jaroslav@1646: *

jaroslav@1646: * If {@code switchPoints} contains a null element, jaroslav@1646: * a {@code NullPointerException} will be raised. jaroslav@1646: * In this case, some non-null elements in the array may be jaroslav@1646: * processed before the method returns abnormally. jaroslav@1646: * Which elements these are (if any) is implementation-dependent. jaroslav@1646: * jaroslav@1646: *

jaroslav@1646: * Discussion: jaroslav@1646: * For performance reasons, {@code invalidateAll} is not a virtual method jaroslav@1646: * on a single switch point, but rather applies to a set of switch points. jaroslav@1646: * Some implementations may incur a large fixed overhead cost jaroslav@1646: * for processing one or more invalidation operations, jaroslav@1646: * but a small incremental cost for each additional invalidation. jaroslav@1646: * In any case, this operation is likely to be costly, since jaroslav@1646: * other threads may have to be somehow interrupted jaroslav@1646: * in order to make them notice the updated switch point state. jaroslav@1646: * However, it may be observed that a single call to invalidate jaroslav@1646: * several switch points has the same formal effect as many calls, jaroslav@1646: * each on just one of the switch points. jaroslav@1646: * jaroslav@1646: *

jaroslav@1646: * Implementation Note: jaroslav@1646: * Simple implementations of {@code SwitchPoint} may use jaroslav@1646: * a private {@link MutableCallSite} to publish the state of a switch point. jaroslav@1646: * In such an implementation, the {@code invalidateAll} method can jaroslav@1646: * simply change the call site's target, and issue one call to jaroslav@1646: * {@linkplain MutableCallSite#syncAll synchronize} all the jaroslav@1646: * private call sites. jaroslav@1646: * jaroslav@1646: * @param switchPoints an array of call sites to be synchronized jaroslav@1646: * @throws NullPointerException if the {@code switchPoints} array reference is null jaroslav@1646: * or the array contains a null jaroslav@1646: */ jaroslav@1646: public static void invalidateAll(SwitchPoint[] switchPoints) { jaroslav@1646: if (switchPoints.length == 0) return; jaroslav@1646: MutableCallSite[] sites = new MutableCallSite[switchPoints.length]; jaroslav@1646: for (int i = 0; i < switchPoints.length; i++) { jaroslav@1646: SwitchPoint spt = switchPoints[i]; jaroslav@1646: if (spt == null) break; // MSC.syncAll will trigger a NPE jaroslav@1646: sites[i] = spt.mcs; jaroslav@1646: spt.mcs.setTarget(K_false); jaroslav@1646: } jaroslav@1646: MutableCallSite.syncAll(sites); jaroslav@1646: } jaroslav@1646: }