1 /*
   2  * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.lang.invoke;
  27 
  28 /**
  29  * <p>
  30  * A {@code SwitchPoint} is an object which can publish state transitions to other threads.
  31  * A switch point is initially in the <em>valid</em> state, but may at any time be
  32  * changed to the <em>invalid</em> state.  Invalidation cannot be reversed.
  33  * A switch point can combine a <em>guarded pair</em> of method handles into a
  34  * <em>guarded delegator</em>.
  35  * The guarded delegator is a method handle which delegates to one of the old method handles.
  36  * The state of the switch point determines which of the two gets the delegation.
  37  * <p>
  38  * A single switch point may be used to control any number of method handles.
  39  * (Indirectly, therefore, it can control any number of call sites.)
  40  * This is done by using the single switch point as a factory for combining
  41  * any number of guarded method handle pairs into guarded delegators.
  42  * <p>
  43  * When a guarded delegator is created from a guarded pair, the pair
  44  * is wrapped in a new method handle {@code M},
  45  * which is permanently associated with the switch point that created it.
  46  * Each pair consists of a target {@code T} and a fallback {@code F}.
  47  * While the switch point is valid, invocations to {@code M} are delegated to {@code T}.
  48  * After it is invalidated, invocations are delegated to {@code F}.
  49  * <p>
  50  * Invalidation is global and immediate, as if the switch point contained a
  51  * volatile boolean variable consulted on every call to {@code M}.
  52  * The invalidation is also permanent, which means the switch point
  53  * can change state only once.
  54  * The switch point will always delegate to {@code F} after being invalidated.
  55  * At that point {@code guardWithTest} may ignore {@code T} and return {@code F}.
  56  * <p>
  57  * Here is an example of a switch point in action:
  58  * <blockquote><pre>{@code
  59 MethodHandle MH_strcat = MethodHandles.lookup()
  60     .findVirtual(String.class, "concat", MethodType.methodType(String.class, String.class));
  61 SwitchPoint spt = new SwitchPoint();
  62 assert(!spt.hasBeenInvalidated());
  63 // the following steps may be repeated to re-use the same switch point:
  64 MethodHandle worker1 = MH_strcat;
  65 MethodHandle worker2 = MethodHandles.permuteArguments(MH_strcat, MH_strcat.type(), 1, 0);
  66 MethodHandle worker = spt.guardWithTest(worker1, worker2);
  67 assertEquals("method", (String) worker.invokeExact("met", "hod"));
  68 SwitchPoint.invalidateAll(new SwitchPoint[]{ spt });
  69 assert(spt.hasBeenInvalidated());
  70 assertEquals("hodmet", (String) worker.invokeExact("met", "hod"));
  71  * }</pre></blockquote>
  72  * <p style="font-size:smaller;">
  73  * <em>Discussion:</em>
  74  * Switch points are useful without subclassing.  They may also be subclassed.
  75  * This may be useful in order to associate application-specific invalidation logic
  76  * with the switch point.
  77  * Notice that there is no permanent association between a switch point and
  78  * the method handles it produces and consumes.
  79  * The garbage collector may collect method handles produced or consumed
  80  * by a switch point independently of the lifetime of the switch point itself.
  81  * <p style="font-size:smaller;">
  82  * <em>Implementation Note:</em>
  83  * A switch point behaves as if implemented on top of {@link MutableCallSite},
  84  * approximately as follows:
  85  * <blockquote><pre>{@code
  86 public class SwitchPoint {
  87   private static final MethodHandle
  88     K_true  = MethodHandles.constant(boolean.class, true),
  89     K_false = MethodHandles.constant(boolean.class, false);
  90   private final MutableCallSite mcs;
  91   private final MethodHandle mcsInvoker;
  92   public SwitchPoint() {
  93     this.mcs = new MutableCallSite(K_true);
  94     this.mcsInvoker = mcs.dynamicInvoker();
  95   }
  96   public MethodHandle guardWithTest(
  97                 MethodHandle target, MethodHandle fallback) {
  98     // Note:  mcsInvoker is of type ()boolean.
  99     // Target and fallback may take any arguments, but must have the same type.
 100     return MethodHandles.guardWithTest(this.mcsInvoker, target, fallback);
 101   }
 102   public static void invalidateAll(SwitchPoint[] spts) {
 103     List&lt;MutableCallSite&gt; mcss = new ArrayList&lt;&gt;();
 104     for (SwitchPoint spt : spts)  mcss.add(spt.mcs);
 105     for (MutableCallSite mcs : mcss)  mcs.setTarget(K_false);
 106     MutableCallSite.syncAll(mcss.toArray(new MutableCallSite[0]));
 107   }
 108 }
 109  * }</pre></blockquote>
 110  * @author Remi Forax, JSR 292 EG
 111  */
 112 public class SwitchPoint {
 113     private static final MethodHandle
 114         K_true  = MethodHandles.constant(boolean.class, true),
 115         K_false = MethodHandles.constant(boolean.class, false);
 116 
 117     private final MutableCallSite mcs;
 118     private final MethodHandle mcsInvoker;
 119 
 120     /**
 121      * Creates a new switch point.
 122      */
 123     public SwitchPoint() {
 124         this.mcs = new MutableCallSite(K_true);
 125         this.mcsInvoker = mcs.dynamicInvoker();
 126     }
 127 
 128     /**
 129      * Determines if this switch point has been invalidated yet.
 130      *
 131      * <p style="font-size:smaller;">
 132      * <em>Discussion:</em>
 133      * Because of the one-way nature of invalidation, once a switch point begins
 134      * to return true for {@code hasBeenInvalidated},
 135      * it will always do so in the future.
 136      * On the other hand, a valid switch point visible to other threads may
 137      * be invalidated at any moment, due to a request by another thread.
 138      * <p style="font-size:smaller;">
 139      * Since invalidation is a global and immediate operation,
 140      * the execution of this query, on a valid switchpoint,
 141      * must be internally sequenced with any
 142      * other threads that could cause invalidation.
 143      * This query may therefore be expensive.
 144      * The recommended way to build a boolean-valued method handle
 145      * which queries the invalidation state of a switch point {@code s} is
 146      * to call {@code s.guardWithTest} on
 147      * {@link MethodHandles#constant constant} true and false method handles.
 148      *
 149      * @return true if this switch point has been invalidated
 150      */
 151     public boolean hasBeenInvalidated() {
 152         return (mcs.getTarget() != K_true);
 153     }
 154 
 155     /**
 156      * Returns a method handle which always delegates either to the target or the fallback.
 157      * The method handle will delegate to the target exactly as long as the switch point is valid.
 158      * After that, it will permanently delegate to the fallback.
 159      * <p>
 160      * The target and fallback must be of exactly the same method type,
 161      * and the resulting combined method handle will also be of this type.
 162      *
 163      * @param target the method handle selected by the switch point as long as it is valid
 164      * @param fallback the method handle selected by the switch point after it is invalidated
 165      * @return a combined method handle which always calls either the target or fallback
 166      * @throws NullPointerException if either argument is null
 167      * @throws IllegalArgumentException if the two method types do not match
 168      * @see MethodHandles#guardWithTest
 169      */
 170     public MethodHandle guardWithTest(MethodHandle target, MethodHandle fallback) {
 171         if (mcs.getTarget() == K_false)
 172             return fallback;  // already invalid
 173         return MethodHandles.guardWithTest(mcsInvoker, target, fallback);
 174     }
 175 
 176     /**
 177      * Sets all of the given switch points into the invalid state.
 178      * After this call executes, no thread will observe any of the
 179      * switch points to be in a valid state.
 180      * <p>
 181      * This operation is likely to be expensive and should be used sparingly.
 182      * If possible, it should be buffered for batch processing on sets of switch points.
 183      * <p>
 184      * If {@code switchPoints} contains a null element,
 185      * a {@code NullPointerException} will be raised.
 186      * In this case, some non-null elements in the array may be
 187      * processed before the method returns abnormally.
 188      * Which elements these are (if any) is implementation-dependent.
 189      *
 190      * <p style="font-size:smaller;">
 191      * <em>Discussion:</em>
 192      * For performance reasons, {@code invalidateAll} is not a virtual method
 193      * on a single switch point, but rather applies to a set of switch points.
 194      * Some implementations may incur a large fixed overhead cost
 195      * for processing one or more invalidation operations,
 196      * but a small incremental cost for each additional invalidation.
 197      * In any case, this operation is likely to be costly, since
 198      * other threads may have to be somehow interrupted
 199      * in order to make them notice the updated switch point state.
 200      * However, it may be observed that a single call to invalidate
 201      * several switch points has the same formal effect as many calls,
 202      * each on just one of the switch points.
 203      *
 204      * <p style="font-size:smaller;">
 205      * <em>Implementation Note:</em>
 206      * Simple implementations of {@code SwitchPoint} may use
 207      * a private {@link MutableCallSite} to publish the state of a switch point.
 208      * In such an implementation, the {@code invalidateAll} method can
 209      * simply change the call site's target, and issue one call to
 210      * {@linkplain MutableCallSite#syncAll synchronize} all the
 211      * private call sites.
 212      *
 213      * @param switchPoints an array of call sites to be synchronized
 214      * @throws NullPointerException if the {@code switchPoints} array reference is null
 215      *                              or the array contains a null
 216      */
 217     public static void invalidateAll(SwitchPoint[] switchPoints) {
 218         if (switchPoints.length == 0)  return;
 219         MutableCallSite[] sites = new MutableCallSite[switchPoints.length];
 220         for (int i = 0; i < switchPoints.length; i++) {
 221             SwitchPoint spt = switchPoints[i];
 222             if (spt == null)  break;  // MSC.syncAll will trigger a NPE
 223             sites[i] = spt.mcs;
 224             spt.mcs.setTarget(K_false);
 225         }
 226         MutableCallSite.syncAll(sites);
 227     }
 228 }