1 /* 2 * Copyright (c) 2008, 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 // Android-changed: removed references to MutableCallSite.syncAll(). 29 /** 30 * A {@code MutableCallSite} is a {@link CallSite} whose target variable 31 * behaves like an ordinary field. 32 * An {@code invokedynamic} instruction linked to a {@code MutableCallSite} delegates 33 * all calls to the site's current target. 34 * The {@linkplain CallSite#dynamicInvoker dynamic invoker} of a mutable call site 35 * also delegates each call to the site's current target. 36 * <p> 37 * Here is an example of a mutable call site which introduces a 38 * state variable into a method handle chain. 39 * <!-- JavaDocExamplesTest.testMutableCallSite --> 40 * <blockquote><pre>{@code 41 MutableCallSite name = new MutableCallSite(MethodType.methodType(String.class)); 42 MethodHandle MH_name = name.dynamicInvoker(); 43 MethodType MT_str1 = MethodType.methodType(String.class); 44 MethodHandle MH_upcase = MethodHandles.lookup() 45 .findVirtual(String.class, "toUpperCase", MT_str1); 46 MethodHandle worker1 = MethodHandles.filterReturnValue(MH_name, MH_upcase); 47 name.setTarget(MethodHandles.constant(String.class, "Rocky")); 48 assertEquals("ROCKY", (String) worker1.invokeExact()); 49 name.setTarget(MethodHandles.constant(String.class, "Fred")); 50 assertEquals("FRED", (String) worker1.invokeExact()); 51 // (mutation can be continued indefinitely) 52 * }</pre></blockquote> 53 * <p> 54 * The same call site may be used in several places at once. 55 * <blockquote><pre>{@code 56 MethodType MT_str2 = MethodType.methodType(String.class, String.class); 57 MethodHandle MH_cat = lookup().findVirtual(String.class, 58 "concat", methodType(String.class, String.class)); 59 MethodHandle MH_dear = MethodHandles.insertArguments(MH_cat, 1, ", dear?"); 60 MethodHandle worker2 = MethodHandles.filterReturnValue(MH_name, MH_dear); 61 assertEquals("Fred, dear?", (String) worker2.invokeExact()); 62 name.setTarget(MethodHandles.constant(String.class, "Wilma")); 63 assertEquals("WILMA", (String) worker1.invokeExact()); 64 assertEquals("Wilma, dear?", (String) worker2.invokeExact()); 65 * }</pre></blockquote> 66 * <p> 67 * <em>Non-synchronization of target values:</em> 68 * A write to a mutable call site's target does not force other threads 69 * to become aware of the updated value. Threads which do not perform 70 * suitable synchronization actions relative to the updated call site 71 * may cache the old target value and delay their use of the new target 72 * value indefinitely. 73 * (This is a normal consequence of the Java Memory Model as applied 74 * to object fields.) 75 * <p> 76 * For target values which will be frequently updated, consider using 77 * a {@linkplain VolatileCallSite volatile call site} instead. 78 * @author John Rose, JSR 292 EG 79 */ 80 public class MutableCallSite extends CallSite { 81 /** 82 * Creates a blank call site object with the given method type. 83 * The initial target is set to a method handle of the given type 84 * which will throw an {@link IllegalStateException} if called. 85 * <p> 86 * The type of the call site is permanently set to the given type. 87 * <p> 88 * Before this {@code CallSite} object is returned from a bootstrap method, 89 * or invoked in some other manner, 90 * it is usually provided with a more useful target method, 91 * via a call to {@link CallSite#setTarget(MethodHandle) setTarget}. 92 * @param type the method type that this call site will have 93 * @throws NullPointerException if the proposed type is null 94 */ MutableCallSite(MethodType type)95 public MutableCallSite(MethodType type) { 96 super(type); 97 } 98 99 /** 100 * Creates a call site object with an initial target method handle. 101 * The type of the call site is permanently set to the initial target's type. 102 * @param target the method handle that will be the initial target of the call site 103 * @throws NullPointerException if the proposed target is null 104 */ MutableCallSite(MethodHandle target)105 public MutableCallSite(MethodHandle target) { 106 super(target); 107 } 108 109 /** 110 * Returns the target method of the call site, which behaves 111 * like a normal field of the {@code MutableCallSite}. 112 * <p> 113 * The interactions of {@code getTarget} with memory are the same 114 * as of a read from an ordinary variable, such as an array element or a 115 * non-volatile, non-final field. 116 * <p> 117 * In particular, the current thread may choose to reuse the result 118 * of a previous read of the target from memory, and may fail to see 119 * a recent update to the target by another thread. 120 * 121 * @return the linkage state of this call site, a method handle which can change over time 122 * @see #setTarget 123 */ getTarget()124 @Override public final MethodHandle getTarget() { 125 return target; 126 } 127 128 /** 129 * Updates the target method of this call site, as a normal variable. 130 * The type of the new target must agree with the type of the old target. 131 * <p> 132 * The interactions with memory are the same 133 * as of a write to an ordinary variable, such as an array element or a 134 * non-volatile, non-final field. 135 * <p> 136 * In particular, unrelated threads may fail to see the updated target 137 * until they perform a read from memory. 138 * Stronger guarantees can be created by putting appropriate operations 139 * into the bootstrap method and/or the target methods used 140 * at any given call site. 141 * 142 * @param newTarget the new target 143 * @throws NullPointerException if the proposed new target is null 144 * @throws WrongMethodTypeException if the proposed new target 145 * has a method type that differs from the previous target 146 * @see #getTarget 147 */ setTarget(MethodHandle newTarget)148 @Override public void setTarget(MethodHandle newTarget) { 149 checkTargetChange(this.target, newTarget); 150 setTargetNormal(newTarget); 151 } 152 153 /** 154 * {@inheritDoc} 155 */ 156 @Override dynamicInvoker()157 public final MethodHandle dynamicInvoker() { 158 return makeDynamicInvoker(); 159 } 160 161 // BEGIN Android-removed: syncAll() implementation is incomplete. 162 /** 163 * Performs a synchronization operation on each call site in the given array, 164 * forcing all other threads to throw away any cached values previously 165 * loaded from the target of any of the call sites. 166 * <p> 167 * This operation does not reverse any calls that have already started 168 * on an old target value. 169 * (Java supports {@linkplain java.lang.Object#wait() forward time travel} only.) 170 * <p> 171 * The overall effect is to force all future readers of each call site's target 172 * to accept the most recently stored value. 173 * ("Most recently" is reckoned relative to the {@code syncAll} itself.) 174 * Conversely, the {@code syncAll} call may block until all readers have 175 * (somehow) decached all previous versions of each call site's target. 176 * <p> 177 * To avoid race conditions, calls to {@code setTarget} and {@code syncAll} 178 * should generally be performed under some sort of mutual exclusion. 179 * Note that reader threads may observe an updated target as early 180 * as the {@code setTarget} call that install the value 181 * (and before the {@code syncAll} that confirms the value). 182 * On the other hand, reader threads may observe previous versions of 183 * the target until the {@code syncAll} call returns 184 * (and after the {@code setTarget} that attempts to convey the updated version). 185 * <p> 186 * This operation is likely to be expensive and should be used sparingly. 187 * If possible, it should be buffered for batch processing on sets of call sites. 188 * <p> 189 * If {@code sites} contains a null element, 190 * a {@code NullPointerException} will be raised. 191 * In this case, some non-null elements in the array may be 192 * processed before the method returns abnormally. 193 * Which elements these are (if any) is implementation-dependent. 194 * 195 * <h1>Java Memory Model details</h1> 196 * In terms of the Java Memory Model, this operation performs a synchronization 197 * action which is comparable in effect to the writing of a volatile variable 198 * by the current thread, and an eventual volatile read by every other thread 199 * that may access one of the affected call sites. 200 * <p> 201 * The following effects are apparent, for each individual call site {@code S}: 202 * <ul> 203 * <li>A new volatile variable {@code V} is created, and written by the current thread. 204 * As defined by the JMM, this write is a global synchronization event. 205 * <li>As is normal with thread-local ordering of write events, 206 * every action already performed by the current thread is 207 * taken to happen before the volatile write to {@code V}. 208 * (In some implementations, this means that the current thread 209 * performs a global release operation.) 210 * <li>Specifically, the write to the current target of {@code S} is 211 * taken to happen before the volatile write to {@code V}. 212 * <li>The volatile write to {@code V} is placed 213 * (in an implementation specific manner) 214 * in the global synchronization order. 215 * <li>Consider an arbitrary thread {@code T} (other than the current thread). 216 * If {@code T} executes a synchronization action {@code A} 217 * after the volatile write to {@code V} (in the global synchronization order), 218 * it is therefore required to see either the current target 219 * of {@code S}, or a later write to that target, 220 * if it executes a read on the target of {@code S}. 221 * (This constraint is called "synchronization-order consistency".) 222 * <li>The JMM specifically allows optimizing compilers to elide 223 * reads or writes of variables that are known to be useless. 224 * Such elided reads and writes have no effect on the happens-before 225 * relation. Regardless of this fact, the volatile {@code V} 226 * will not be elided, even though its written value is 227 * indeterminate and its read value is not used. 228 * </ul> 229 * Because of the last point, the implementation behaves as if a 230 * volatile read of {@code V} were performed by {@code T} 231 * immediately after its action {@code A}. In the local ordering 232 * of actions in {@code T}, this read happens before any future 233 * read of the target of {@code S}. It is as if the 234 * implementation arbitrarily picked a read of {@code S}'s target 235 * by {@code T}, and forced a read of {@code V} to precede it, 236 * thereby ensuring communication of the new target value. 237 * <p> 238 * As long as the constraints of the Java Memory Model are obeyed, 239 * implementations may delay the completion of a {@code syncAll} 240 * operation while other threads ({@code T} above) continue to 241 * use previous values of {@code S}'s target. 242 * However, implementations are (as always) encouraged to avoid 243 * livelock, and to eventually require all threads to take account 244 * of the updated target. 245 * 246 * <p style="font-size:smaller;"> 247 * <em>Discussion:</em> 248 * For performance reasons, {@code syncAll} is not a virtual method 249 * on a single call site, but rather applies to a set of call sites. 250 * Some implementations may incur a large fixed overhead cost 251 * for processing one or more synchronization operations, 252 * but a small incremental cost for each additional call site. 253 * In any case, this operation is likely to be costly, since 254 * other threads may have to be somehow interrupted 255 * in order to make them notice the updated target value. 256 * However, it may be observed that a single call to synchronize 257 * several sites has the same formal effect as many calls, 258 * each on just one of the sites. 259 * 260 * <p style="font-size:smaller;"> 261 * <em>Implementation Note:</em> 262 * Simple implementations of {@code MutableCallSite} may use 263 * a volatile variable for the target of a mutable call site. 264 * In such an implementation, the {@code syncAll} method can be a no-op, 265 * and yet it will conform to the JMM behavior documented above. 266 * 267 * @param sites an array of call sites to be synchronized 268 * @throws NullPointerException if the {@code sites} array reference is null 269 * or the array contains a null 270 * 271 public static void syncAll(MutableCallSite[] sites) { 272 if (sites.length == 0) return; 273 STORE_BARRIER.lazySet(0); 274 for (int i = 0; i < sites.length; i++) { 275 sites[i].getClass(); // trigger NPE on first null 276 } 277 // FIXME: NYI 278 } 279 private static final AtomicInteger STORE_BARRIER = new AtomicInteger(); 280 */ 281 // END Android-removed: syncAll() implementation is incomplete. 282 } 283