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