1 /*
2  * Copyright (c) 2010, 2011, 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 VolatileCallSite} is a {@link CallSite} whose target acts like a volatile variable.
31  * An {@code invokedynamic} instruction linked to a {@code VolatileCallSite} sees updates
32  * to its call site target immediately, even if the update occurs in another thread.
33  * There may be a performance penalty for such tight coupling between threads.
34  * <p>
35  * In other respects, a {@code VolatileCallSite} is interchangeable
36  * with {@code MutableCallSite}.
37  * @see MutableCallSite
38  * @author John Rose, JSR 292 EG
39  */
40 public class VolatileCallSite extends CallSite {
41     /**
42      * Creates a call site with a volatile binding to its target.
43      * The initial target is set to a method handle
44      * of the given type which will throw an {@code IllegalStateException} if called.
45      * @param type the method type that this call site will have
46      * @throws NullPointerException if the proposed type is null
47      */
VolatileCallSite(MethodType type)48     public VolatileCallSite(MethodType type) {
49         super(type);
50     }
51 
52     /**
53      * Creates a call site with a volatile binding to its target.
54      * The target is set to the given value.
55      * @param target the method handle that will be the initial target of the call site
56      * @throws NullPointerException if the proposed target is null
57      */
VolatileCallSite(MethodHandle target)58     public VolatileCallSite(MethodHandle target) {
59         super(target);
60     }
61 
62     /**
63      * Returns the target method of the call site, which behaves
64      * like a {@code volatile} field of the {@code VolatileCallSite}.
65      * <p>
66      * The interactions of {@code getTarget} with memory are the same
67      * as of a read from a {@code volatile} field.
68      * <p>
69      * In particular, the current thread is required to issue a fresh
70      * read of the target from memory, and must not fail to see
71      * a recent update to the target by another thread.
72      *
73      * @return the linkage state of this call site, a method handle which can change over time
74      * @see #setTarget
75      */
getTarget()76     @Override public final MethodHandle getTarget() {
77         return getTargetVolatile();
78     }
79 
80     /**
81      * Updates the target method of this call site, as a volatile variable.
82      * The type of the new target must agree with the type of the old target.
83      * <p>
84      * The interactions with memory are the same as of a write to a volatile field.
85      * In particular, any threads is guaranteed to see the updated target
86      * the next time it calls {@code getTarget}.
87      * @param newTarget the new target
88      * @throws NullPointerException if the proposed new target is null
89      * @throws WrongMethodTypeException if the proposed new target
90      *         has a method type that differs from the previous target
91      * @see #getTarget
92      */
setTarget(MethodHandle newTarget)93     @Override public void setTarget(MethodHandle newTarget) {
94         checkTargetChange(getTargetVolatile(), newTarget);
95         setTargetVolatile(newTarget);
96     }
97 
98     /**
99      * {@inheritDoc}
100      */
101     @Override
dynamicInvoker()102     public final MethodHandle dynamicInvoker() {
103         return makeDynamicInvoker();
104     }
105 }
106