Synchronization
Items that have been cached are returned directly, until invalidated by
the action of SwitchPoint
.
When an item has not been cached or the cached value has been invalidated,
its lazy recomputation at next use takes place within doInPG
,
that is, "on the PG thread". (An extended discussion of what that really
means can be found at DualState
.) The PG thread must be the
only thread where the SwitchPoint
s will be invalidated, and an old
SwitchPoint
must be replaced in its field by a newly-constructed one
before the old one is invalidated.
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic class
Builder for use during the static initialization of a class that usesSwitchPointCache
. -
Method Summary
Modifier and TypeMethodDescriptionstatic void
Called from a computation method to prevent caching of the value being returned.static MethodHandle
fix
(MethodHandle m) Transform aMethodHandle
into one with a reference to itself.static void
setConstant
(MethodHandle[] slots, int index, Object v) Replaceslots[index]
with a constant returningv
forever, immune to invalidation.
-
Method Details
-
doNotCache
public static void doNotCache()Called from a computation method to prevent caching of the value being returned.This can be useful in cases where a not-yet-determined value should not 'stick'. Whatever the computation method returns will be returned to the caller, but the computation method will be reinvoked the next time a caller wants the value.
This state is reset on entry and after return of any computation method. Therefore, if there are actions within a computation method that could involve calling other
SwitchPointCache
-based methods, this method must be called after all of those to have reliable effect. By convention, it should be called immediately before returning. -
fix
Transform aMethodHandle
into one with a reference to itself.- Parameters:
m
- MethodHandle with methodType(r,MethodHandle,p0,...,pk) where the expected first parameter is a MethodHandle h of methodType(r,p0,...,pk) that invokes m with inserted first argument h.- Returns:
- h
-
setConstant
Replaceslots[index]
with a constant returningv
forever, immune to invalidation.The slot must already be populated, as by the initializer created by a
Builder
; this method adapts the supplied constant to the slot's existingmethodType
.
-