MutableCallSite is a CallSite whose target variable
behaves like an ordinary field.
An invokedynamic instruction linked to a MutableCallSite delegates
all calls to the site's current target.
The dynamic invoker of a mutable call site
also delegates each call to the site's current target.
Here is an example of a mutable call site which introduces a state variable into a method handle chain.
MutableCallSite name = new MutableCallSite(MethodType.methodType(String.class)); MethodHandle MH_name = name.dynamicInvoker(); MethodType MT_str1 = MethodType.methodType(String.class); MethodHandle MH_upcase = MethodHandles.lookup() .findVirtual(String.class, "toUpperCase", MT_str1); MethodHandle worker1 = MethodHandles.filterReturnValue(MH_name, MH_upcase); name.setTarget(MethodHandles.constant(String.class, "Rocky")); assertEquals("ROCKY", (String) worker1.invokeExact()); name.setTarget(MethodHandles.constant(String.class, "Fred")); assertEquals("FRED", (String) worker1.invokeExact()); // (mutation can be continued indefinitely)
The same call site may be used in several places at once.
MethodType MT_str2 = MethodType.methodType(String.class, String.class); MethodHandle MH_cat = lookup().findVirtual(String.class, "concat", methodType(String.class, String.class)); MethodHandle MH_dear = MethodHandles.insertArguments(MH_cat, 1, ", dear?"); MethodHandle worker2 = MethodHandles.filterReturnValue(MH_name, MH_dear); assertEquals("Fred, dear?", (String) worker2.invokeExact()); name.setTarget(MethodHandles.constant(String.class, "Wilma")); assertEquals("WILMA", (String) worker1.invokeExact()); assertEquals("Wilma, dear?", (String) worker2.invokeExact());
Non-synchronization of target values: A write to a mutable call site's target does not force other threads to become aware of the updated value. Threads which do not perform suitable synchronization actions relative to the updated call site may cache the old target value and delay their use of the new target value indefinitely. (This is a normal consequence of the Java Memory Model as applied to object fields.)
The syncAll operation provides a way to force threads
to accept a new target value, even if there is no other synchronization.
For target values which will be frequently updated, consider using a volatile call site instead.
| Access | Constructor and Description |
|---|---|
| public | MutableCallSite(MethodType
the method type that this call site will have type)Creates a blank call site object with the given method type. |
| public | MutableCallSite(MethodHandle
the method handle that will be the initial target of the call site target)Creates a call site object with an initial target method handle. |
| Modifier and Type | Method and Description |
|---|---|
| public final MethodHandle | dynamicInvoker()
Implements abstract java. Produces a method handle equivalent to an invokedynamic instruction which has been linked to this call site. |
| public final MethodHandle | Returns: the linkage state of this call site, a method handle which can change over timeImplements abstract java. Returns the target method of the call site, which behaves
like a normal field of the |
| public void | setTarget(MethodHandle
the new target newTarget)Implements abstract java. Updates the target method of this call site, as a normal variable. |
| public static void | syncAll(MutableCallSite[]
an array of call sites to be synchronized sites)Performs a synchronization operation on each call site in the given array, forcing all other threads to throw away any cached values previously loaded from the target of any of the call sites. |
| MutableCallSite | back to summary |
|---|---|
| public MutableCallSite(MethodType type) Creates a blank call site object with the given method type.
The initial target is set to a method handle of the given type
which will throw an The type of the call site is permanently set to the given type.
Before this
| |
| MutableCallSite | back to summary |
|---|---|
| public MutableCallSite(MethodHandle target) Creates a call site object with an initial target method handle. The type of the call site is permanently set to the initial target's type.
| |
| dynamicInvoker | back to summary |
|---|---|
| public final MethodHandle dynamicInvoker() Implements abstract java. Doc from java. Produces a method handle equivalent to an invokedynamic instruction which has been linked to this call site. This method is equivalent to the following code:
| |
| getTarget | back to summary |
|---|---|
| public final MethodHandle getTarget() Implements abstract java. Returns the target method of the call site, which behaves
like a normal field of the
The interactions of In particular, the current thread may choose to reuse the result of a previous read of the target from memory, and may fail to see a recent update to the target by another thread.
| |
| setTarget | back to summary |
|---|---|
| public void setTarget(MethodHandle newTarget) Implements abstract java. Updates the target method of this call site, as a normal variable. The type of the new target must agree with the type of the old target. The interactions with memory are the same as of a write to an ordinary variable, such as an array element or a non-volatile, non-final field. In particular, unrelated threads may fail to see the updated target until they perform a read from memory. Stronger guarantees can be created by putting appropriate operations into the bootstrap method and/or the target methods used at any given call site.
| |
| syncAll | back to summary |
|---|---|
| public static void syncAll(MutableCallSite[] sites) Performs a synchronization operation on each call site in the given array, forcing all other threads to throw away any cached values previously loaded from the target of any of the call sites. This operation does not reverse any calls that have already started on an old target value. (Java supports forward time travel only.)
The overall effect is to force all future readers of each call site's target
to accept the most recently stored value.
("Most recently" is reckoned relative to the
To avoid race conditions, calls to This operation is likely to be expensive and should be used sparingly. If possible, it should be buffered for batch processing on sets of call sites.
If Java Memory Model detailsIn terms of the Java Memory Model, this operation performs a synchronization action which is comparable in effect to the writing of a volatile variable by the current thread, and an eventual volatile read by every other thread that may access one of the affected call sites.
The following effects are apparent, for each individual call site
V were performed by T
immediately after its action A. In the local ordering
of actions in T, this read happens before any future
read of the target of S. It is as if the
implementation arbitrarily picked a read of S's target
by T, and forced a read of V to precede it,
thereby ensuring communication of the new target value.
As long as the constraints of the Java Memory Model are obeyed,
implementations may delay the completion of a
Discussion:
For performance reasons, Implementation Note Simple implementations of
| |