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  * <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>
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  * <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<MutableCallSite> mcss = new ArrayList<>();
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>
110  * @author Remi Forax, JSR 292 EG
111  * @since 1.7
112  */
113 public class SwitchPoint {
114     private static final MethodHandle
115         K_true  = MethodHandles.constant(boolean.class, true),
116         K_false = MethodHandles.constant(boolean.class, false);
117 
118     private final MutableCallSite mcs;
119     private final MethodHandle mcsInvoker;
120 
121     /**
122      * Creates a new switch point.
123      */
SwitchPoint()124     public SwitchPoint() {
125         this.mcs = new MutableCallSite(K_true);
126         this.mcsInvoker = mcs.dynamicInvoker();
127     }
128 
129     /**
130      * Determines if this switch point has been invalidated yet.
131      *
132      * <p style="font-size:smaller;">
133      * <em>Discussion:</em>
134      * Because of the one-way nature of invalidation, once a switch point begins
135      * to return true for {@code hasBeenInvalidated},
136      * it will always do so in the future.
137      * On the other hand, a valid switch point visible to other threads may
138      * be invalidated at any moment, due to a request by another thread.
139      * <p style="font-size:smaller;">
140      * Since invalidation is a global and immediate operation,
141      * the execution of this query, on a valid switchpoint,
142      * must be internally sequenced with any
143      * other threads that could cause invalidation.
144      * This query may therefore be expensive.
145      * The recommended way to build a boolean-valued method handle
146      * which queries the invalidation state of a switch point {@code s} is
147      * to call {@code s.guardWithTest} on
148      * {@link MethodHandles#constant constant} true and false method handles.
149      *
150      * @return true if this switch point has been invalidated
151      */
hasBeenInvalidated()152     public boolean hasBeenInvalidated() {
153         return (mcs.getTarget() != K_true);
154     }
155 
156     /**
157      * Returns a method handle which always delegates either to the target or the fallback.
158      * The method handle will delegate to the target exactly as long as the switch point is valid.
159      * After that, it will permanently delegate to the fallback.
160      * <p>
161      * The target and fallback must be of exactly the same method type,
162      * and the resulting combined method handle will also be of this type.
163      *
164      * @param target the method handle selected by the switch point as long as it is valid
165      * @param fallback the method handle selected by the switch point after it is invalidated
166      * @return a combined method handle which always calls either the target or fallback
167      * @throws NullPointerException if either argument is null
168      * @throws IllegalArgumentException if the two method types do not match
169      * @see MethodHandles#guardWithTest
170      */
guardWithTest(MethodHandle target, MethodHandle fallback)171     public MethodHandle guardWithTest(MethodHandle target, MethodHandle fallback) {
172         if (mcs.getTarget() == K_false)
173             return fallback;  // already invalid
174         return MethodHandles.guardWithTest(mcsInvoker, target, fallback);
175     }
176 
177     /**
178      * Sets all of the given switch points into the invalid state.
179      * After this call executes, no thread will observe any of the
180      * switch points to be in a valid state.
181      * <p>
182      * This operation is likely to be expensive and should be used sparingly.
183      * If possible, it should be buffered for batch processing on sets of switch points.
184      * <p>
185      * If {@code switchPoints} contains a null element,
186      * a {@code NullPointerException} will be raised.
187      * In this case, some non-null elements in the array may be
188      * processed before the method returns abnormally.
189      * Which elements these are (if any) is implementation-dependent.
190      *
191      * <p style="font-size:smaller;">
192      * <em>Discussion:</em>
193      * For performance reasons, {@code invalidateAll} is not a virtual method
194      * on a single switch point, but rather applies to a set of switch points.
195      * Some implementations may incur a large fixed overhead cost
196      * for processing one or more invalidation operations,
197      * but a small incremental cost for each additional invalidation.
198      * In any case, this operation is likely to be costly, since
199      * other threads may have to be somehow interrupted
200      * in order to make them notice the updated switch point state.
201      * However, it may be observed that a single call to invalidate
202      * several switch points has the same formal effect as many calls,
203      * each on just one of the switch points.
204      *
205      * <p style="font-size:smaller;">
206      * <em>Implementation Note:</em>
207      * Simple implementations of {@code SwitchPoint} may use
208      * a private {@link MutableCallSite} to publish the state of a switch point.
209      * In such an implementation, the {@code invalidateAll} method can
210      * simply change the call site's target, and issue one call to
211      * {@linkplain MutableCallSite#syncAll synchronize} all the
212      * private call sites.
213      *
214      * @param switchPoints an array of call sites to be synchronized
215      * @throws NullPointerException if the {@code switchPoints} array reference is null
216      *                              or the array contains a null
217      */
invalidateAll(SwitchPoint[] switchPoints)218     public static void invalidateAll(SwitchPoint[] switchPoints) {
219         if (switchPoints.length == 0)  return;
220         MutableCallSite[] sites = new MutableCallSite[switchPoints.length];
221         for (int i = 0; i < switchPoints.length; i++) {
222             SwitchPoint spt = switchPoints[i];
223             if (spt == null)  break;  // MSC.syncAll will trigger a NPE
224             sites[i] = spt.mcs;
225             spt.mcs.setTarget(K_false);
226         }
227         MutableCallSite.syncAll(sites);
228     }
229 }
230