7051206: JSR 292 method name SwitchPoint.isValid is misleading to unwary users; should be hasBeenInvalidated

Reviewed-by: kvn, never, ysr
This commit is contained in:
John R Rose 2011-06-03 11:20:20 -07:00
parent c0d9c39aa2
commit ad33af1a1c
2 changed files with 26 additions and 12 deletions

View File

@ -59,14 +59,14 @@ package java.lang.invoke;
MethodHandle MH_strcat = MethodHandles.lookup() MethodHandle MH_strcat = MethodHandles.lookup()
.findVirtual(String.class, "concat", MethodType.methodType(String.class, String.class)); .findVirtual(String.class, "concat", MethodType.methodType(String.class, String.class));
SwitchPoint spt = new SwitchPoint(); SwitchPoint spt = new SwitchPoint();
assert(spt.isValid()); assert(!spt.hasBeenInvalidated());
// the following steps may be repeated to re-use the same switch point: // the following steps may be repeated to re-use the same switch point:
MethodHandle worker1 = MH_strcat; MethodHandle worker1 = MH_strcat;
MethodHandle worker2 = MethodHandles.permuteArguments(MH_strcat, MH_strcat.type(), 1, 0); MethodHandle worker2 = MethodHandles.permuteArguments(MH_strcat, MH_strcat.type(), 1, 0);
MethodHandle worker = spt.guardWithTest(worker1, worker2); MethodHandle worker = spt.guardWithTest(worker1, worker2);
assertEquals("method", (String) worker.invokeExact("met", "hod")); assertEquals("method", (String) worker.invokeExact("met", "hod"));
SwitchPoint.invalidateAll(new SwitchPoint[]{ spt }); SwitchPoint.invalidateAll(new SwitchPoint[]{ spt });
assert(!spt.isValid()); assert(spt.hasBeenInvalidated());
assertEquals("hodmet", (String) worker.invokeExact("met", "hod")); assertEquals("hodmet", (String) worker.invokeExact("met", "hod"));
* </pre></blockquote> * </pre></blockquote>
* <p style="font-size:smaller;"> * <p style="font-size:smaller;">
@ -126,16 +126,30 @@ public class SwitchPoint {
} }
/** /**
* Determines if this switch point is still valid. * Determines if this switch point has been invalidated yet.
* <p> *
* <p style="font-size:smaller;">
* <em>Discussion:</em>
* Because of the one-way nature of invalidation, once a switch point begins
* to return true for {@code hasBeenInvalidated},
* it will always do so in the future.
* On the other hand, a valid switch point visible to other threads may
* invalidated at any moment, due to a request by another thread.
* <p style="font-size:smaller;">
* Since invalidation is a global and immediate operation, * Since invalidation is a global and immediate operation,
* this query must be sequenced with any * the execution of this query, on a valid switchpoint,
* other threads that could invalidate this switch point. * must be internally sequenced with any
* It may therefore be expensive. * other threads that could cause invalidation.
* @return true if this switch point has never been invalidated * This query may therefore be expensive.
* The recommended way to build a boolean-valued method handle
* which queries the invalidation state of a switch point {@code s} is
* to call {@code s.guardWithTest} on
* {@link MethodHandles#constant constant} true and false method handles.
*
* @return true if this switch point has been invalidated
*/ */
public boolean isValid() { public boolean hasBeenInvalidated() {
return (mcs.getTarget() == K_true); return (mcs.getTarget() != K_true);
} }
/** /**

View File

@ -477,14 +477,14 @@ assertEquals("Wilma, dear?", (String) worker2.invokeExact());
MethodHandle MH_strcat = MethodHandles.lookup() MethodHandle MH_strcat = MethodHandles.lookup()
.findVirtual(String.class, "concat", MethodType.methodType(String.class, String.class)); .findVirtual(String.class, "concat", MethodType.methodType(String.class, String.class));
SwitchPoint spt = new SwitchPoint(); SwitchPoint spt = new SwitchPoint();
assert(spt.isValid()); assert(!spt.hasBeenInvalidated());
// the following steps may be repeated to re-use the same switch point: // the following steps may be repeated to re-use the same switch point:
MethodHandle worker1 = MH_strcat; MethodHandle worker1 = MH_strcat;
MethodHandle worker2 = MethodHandles.permuteArguments(MH_strcat, MH_strcat.type(), 1, 0); MethodHandle worker2 = MethodHandles.permuteArguments(MH_strcat, MH_strcat.type(), 1, 0);
MethodHandle worker = spt.guardWithTest(worker1, worker2); MethodHandle worker = spt.guardWithTest(worker1, worker2);
assertEquals("method", (String) worker.invokeExact("met", "hod")); assertEquals("method", (String) worker.invokeExact("met", "hod"));
SwitchPoint.invalidateAll(new SwitchPoint[]{ spt }); SwitchPoint.invalidateAll(new SwitchPoint[]{ spt });
assert(!spt.isValid()); assert(spt.hasBeenInvalidated());
assertEquals("hodmet", (String) worker.invokeExact("met", "hod")); assertEquals("hodmet", (String) worker.invokeExact("met", "hod"));
{} {}
}} }}