rt/emul/compact/src/main/java/java/lang/invoke/VolatileCallSite.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sat, 09 Aug 2014 11:11:13 +0200
branchjdk8-b132
changeset 1646 c880a8a8803b
permissions -rw-r--r--
Batch of classes necessary to implement invoke dynamic interfaces. Taken from JDK8 build 132
jaroslav@1646
     1
/*
jaroslav@1646
     2
 * Copyright (c) 2010, 2011, Oracle and/or its affiliates. All rights reserved.
jaroslav@1646
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jaroslav@1646
     4
 *
jaroslav@1646
     5
 * This code is free software; you can redistribute it and/or modify it
jaroslav@1646
     6
 * under the terms of the GNU General Public License version 2 only, as
jaroslav@1646
     7
 * published by the Free Software Foundation.  Oracle designates this
jaroslav@1646
     8
 * particular file as subject to the "Classpath" exception as provided
jaroslav@1646
     9
 * by Oracle in the LICENSE file that accompanied this code.
jaroslav@1646
    10
 *
jaroslav@1646
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
jaroslav@1646
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jaroslav@1646
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
jaroslav@1646
    14
 * version 2 for more details (a copy is included in the LICENSE file that
jaroslav@1646
    15
 * accompanied this code).
jaroslav@1646
    16
 *
jaroslav@1646
    17
 * You should have received a copy of the GNU General Public License version
jaroslav@1646
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
jaroslav@1646
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jaroslav@1646
    20
 *
jaroslav@1646
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
jaroslav@1646
    22
 * or visit www.oracle.com if you need additional information or have any
jaroslav@1646
    23
 * questions.
jaroslav@1646
    24
 */
jaroslav@1646
    25
jaroslav@1646
    26
package java.lang.invoke;
jaroslav@1646
    27
jaroslav@1646
    28
/**
jaroslav@1646
    29
 * A {@code VolatileCallSite} is a {@link CallSite} whose target acts like a volatile variable.
jaroslav@1646
    30
 * An {@code invokedynamic} instruction linked to a {@code VolatileCallSite} sees updates
jaroslav@1646
    31
 * to its call site target immediately, even if the update occurs in another thread.
jaroslav@1646
    32
 * There may be a performance penalty for such tight coupling between threads.
jaroslav@1646
    33
 * <p>
jaroslav@1646
    34
 * Unlike {@code MutableCallSite}, there is no
jaroslav@1646
    35
 * {@linkplain MutableCallSite#syncAll syncAll operation} on volatile
jaroslav@1646
    36
 * call sites, since every write to a volatile variable is implicitly
jaroslav@1646
    37
 * synchronized with reader threads.
jaroslav@1646
    38
 * <p>
jaroslav@1646
    39
 * In other respects, a {@code VolatileCallSite} is interchangeable
jaroslav@1646
    40
 * with {@code MutableCallSite}.
jaroslav@1646
    41
 * @see MutableCallSite
jaroslav@1646
    42
 * @author John Rose, JSR 292 EG
jaroslav@1646
    43
 */
jaroslav@1646
    44
public class VolatileCallSite extends CallSite {
jaroslav@1646
    45
    /**
jaroslav@1646
    46
     * Creates a call site with a volatile binding to its target.
jaroslav@1646
    47
     * The initial target is set to a method handle
jaroslav@1646
    48
     * of the given type which will throw an {@code IllegalStateException} if called.
jaroslav@1646
    49
     * @param type the method type that this call site will have
jaroslav@1646
    50
     * @throws NullPointerException if the proposed type is null
jaroslav@1646
    51
     */
jaroslav@1646
    52
    public VolatileCallSite(MethodType type) {
jaroslav@1646
    53
        super(type);
jaroslav@1646
    54
    }
jaroslav@1646
    55
jaroslav@1646
    56
    /**
jaroslav@1646
    57
     * Creates a call site with a volatile binding to its target.
jaroslav@1646
    58
     * The target is set to the given value.
jaroslav@1646
    59
     * @param target the method handle that will be the initial target of the call site
jaroslav@1646
    60
     * @throws NullPointerException if the proposed target is null
jaroslav@1646
    61
     */
jaroslav@1646
    62
    public VolatileCallSite(MethodHandle target) {
jaroslav@1646
    63
        super(target);
jaroslav@1646
    64
    }
jaroslav@1646
    65
jaroslav@1646
    66
    /**
jaroslav@1646
    67
     * Returns the target method of the call site, which behaves
jaroslav@1646
    68
     * like a {@code volatile} field of the {@code VolatileCallSite}.
jaroslav@1646
    69
     * <p>
jaroslav@1646
    70
     * The interactions of {@code getTarget} with memory are the same
jaroslav@1646
    71
     * as of a read from a {@code volatile} field.
jaroslav@1646
    72
     * <p>
jaroslav@1646
    73
     * In particular, the current thread is required to issue a fresh
jaroslav@1646
    74
     * read of the target from memory, and must not fail to see
jaroslav@1646
    75
     * a recent update to the target by another thread.
jaroslav@1646
    76
     *
jaroslav@1646
    77
     * @return the linkage state of this call site, a method handle which can change over time
jaroslav@1646
    78
     * @see #setTarget
jaroslav@1646
    79
     */
jaroslav@1646
    80
    @Override public final MethodHandle getTarget() {
jaroslav@1646
    81
        return getTargetVolatile();
jaroslav@1646
    82
    }
jaroslav@1646
    83
jaroslav@1646
    84
    /**
jaroslav@1646
    85
     * Updates the target method of this call site, as a volatile variable.
jaroslav@1646
    86
     * The type of the new target must agree with the type of the old target.
jaroslav@1646
    87
     * <p>
jaroslav@1646
    88
     * The interactions with memory are the same as of a write to a volatile field.
jaroslav@1646
    89
     * In particular, any threads is guaranteed to see the updated target
jaroslav@1646
    90
     * the next time it calls {@code getTarget}.
jaroslav@1646
    91
     * @param newTarget the new target
jaroslav@1646
    92
     * @throws NullPointerException if the proposed new target is null
jaroslav@1646
    93
     * @throws WrongMethodTypeException if the proposed new target
jaroslav@1646
    94
     *         has a method type that differs from the previous target
jaroslav@1646
    95
     * @see #getTarget
jaroslav@1646
    96
     */
jaroslav@1646
    97
    @Override public void setTarget(MethodHandle newTarget) {
jaroslav@1646
    98
        checkTargetChange(getTargetVolatile(), newTarget);
jaroslav@1646
    99
        setTargetVolatile(newTarget);
jaroslav@1646
   100
    }
jaroslav@1646
   101
jaroslav@1646
   102
    /**
jaroslav@1646
   103
     * {@inheritDoc}
jaroslav@1646
   104
     */
jaroslav@1646
   105
    @Override
jaroslav@1646
   106
    public final MethodHandle dynamicInvoker() {
jaroslav@1646
   107
        return makeDynamicInvoker();
jaroslav@1646
   108
    }
jaroslav@1646
   109
}