jaroslav@1646: /* jaroslav@1646: * Copyright (c) 2010, 2011, Oracle and/or its affiliates. All rights reserved. jaroslav@1646: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. jaroslav@1646: * jaroslav@1646: * This code is free software; you can redistribute it and/or modify it jaroslav@1646: * under the terms of the GNU General Public License version 2 only, as jaroslav@1646: * published by the Free Software Foundation. Oracle designates this jaroslav@1646: * particular file as subject to the "Classpath" exception as provided jaroslav@1646: * by Oracle in the LICENSE file that accompanied this code. jaroslav@1646: * jaroslav@1646: * This code is distributed in the hope that it will be useful, but WITHOUT jaroslav@1646: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or jaroslav@1646: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License jaroslav@1646: * version 2 for more details (a copy is included in the LICENSE file that jaroslav@1646: * accompanied this code). jaroslav@1646: * jaroslav@1646: * You should have received a copy of the GNU General Public License version jaroslav@1646: * 2 along with this work; if not, write to the Free Software Foundation, jaroslav@1646: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. jaroslav@1646: * jaroslav@1646: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA jaroslav@1646: * or visit www.oracle.com if you need additional information or have any jaroslav@1646: * questions. jaroslav@1646: */ jaroslav@1646: jaroslav@1646: package java.lang.invoke; jaroslav@1646: jaroslav@1646: /** jaroslav@1646: * A {@code VolatileCallSite} is a {@link CallSite} whose target acts like a volatile variable. jaroslav@1646: * An {@code invokedynamic} instruction linked to a {@code VolatileCallSite} sees updates jaroslav@1646: * to its call site target immediately, even if the update occurs in another thread. jaroslav@1646: * There may be a performance penalty for such tight coupling between threads. jaroslav@1646: *

jaroslav@1646: * Unlike {@code MutableCallSite}, there is no jaroslav@1646: * {@linkplain MutableCallSite#syncAll syncAll operation} on volatile jaroslav@1646: * call sites, since every write to a volatile variable is implicitly jaroslav@1646: * synchronized with reader threads. jaroslav@1646: *

jaroslav@1646: * In other respects, a {@code VolatileCallSite} is interchangeable jaroslav@1646: * with {@code MutableCallSite}. jaroslav@1646: * @see MutableCallSite jaroslav@1646: * @author John Rose, JSR 292 EG jaroslav@1646: */ jaroslav@1646: public class VolatileCallSite extends CallSite { jaroslav@1646: /** jaroslav@1646: * Creates a call site with a volatile binding to its target. jaroslav@1646: * The initial target is set to a method handle jaroslav@1646: * of the given type which will throw an {@code IllegalStateException} if called. jaroslav@1646: * @param type the method type that this call site will have jaroslav@1646: * @throws NullPointerException if the proposed type is null jaroslav@1646: */ jaroslav@1646: public VolatileCallSite(MethodType type) { jaroslav@1646: super(type); jaroslav@1646: } jaroslav@1646: jaroslav@1646: /** jaroslav@1646: * Creates a call site with a volatile binding to its target. jaroslav@1646: * The target is set to the given value. jaroslav@1646: * @param target the method handle that will be the initial target of the call site jaroslav@1646: * @throws NullPointerException if the proposed target is null jaroslav@1646: */ jaroslav@1646: public VolatileCallSite(MethodHandle target) { jaroslav@1646: super(target); jaroslav@1646: } jaroslav@1646: jaroslav@1646: /** jaroslav@1646: * Returns the target method of the call site, which behaves jaroslav@1646: * like a {@code volatile} field of the {@code VolatileCallSite}. jaroslav@1646: *

jaroslav@1646: * The interactions of {@code getTarget} with memory are the same jaroslav@1646: * as of a read from a {@code volatile} field. jaroslav@1646: *

jaroslav@1646: * In particular, the current thread is required to issue a fresh jaroslav@1646: * read of the target from memory, and must not fail to see jaroslav@1646: * a recent update to the target by another thread. jaroslav@1646: * jaroslav@1646: * @return the linkage state of this call site, a method handle which can change over time jaroslav@1646: * @see #setTarget jaroslav@1646: */ jaroslav@1646: @Override public final MethodHandle getTarget() { jaroslav@1646: return getTargetVolatile(); jaroslav@1646: } jaroslav@1646: jaroslav@1646: /** jaroslav@1646: * Updates the target method of this call site, as a volatile variable. jaroslav@1646: * The type of the new target must agree with the type of the old target. jaroslav@1646: *

jaroslav@1646: * The interactions with memory are the same as of a write to a volatile field. jaroslav@1646: * In particular, any threads is guaranteed to see the updated target jaroslav@1646: * the next time it calls {@code getTarget}. jaroslav@1646: * @param newTarget the new target jaroslav@1646: * @throws NullPointerException if the proposed new target is null jaroslav@1646: * @throws WrongMethodTypeException if the proposed new target jaroslav@1646: * has a method type that differs from the previous target jaroslav@1646: * @see #getTarget jaroslav@1646: */ jaroslav@1646: @Override public void setTarget(MethodHandle newTarget) { jaroslav@1646: checkTargetChange(getTargetVolatile(), newTarget); jaroslav@1646: setTargetVolatile(newTarget); jaroslav@1646: } jaroslav@1646: jaroslav@1646: /** jaroslav@1646: * {@inheritDoc} jaroslav@1646: */ jaroslav@1646: @Override jaroslav@1646: public final MethodHandle dynamicInvoker() { jaroslav@1646: return makeDynamicInvoker(); jaroslav@1646: } jaroslav@1646: }