jdk/src/share/classes/java/dyn/VolatileCallSite.java
author jrose
Thu, 16 Dec 2010 15:59:27 -0800
changeset 7562 a0ad195efe2c
parent 7555 a279ebc3b25c
child 8346 3b891698c4ec
permissions -rw-r--r--
7001424: implement JSR 292 EG adjustments, November 2010 Reviewed-by: twisti
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
7051
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents:
diff changeset
     1
/*
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents:
diff changeset
     2
 * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents:
diff changeset
     4
 *
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents:
diff changeset
    10
 *
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents:
diff changeset
    15
 * accompanied this code).
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents:
diff changeset
    16
 *
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents:
diff changeset
    20
 *
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents:
diff changeset
    23
 * questions.
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents:
diff changeset
    24
 */
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents:
diff changeset
    25
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents:
diff changeset
    26
package java.dyn;
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents:
diff changeset
    27
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
    28
import java.util.List;
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
    29
7051
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents:
diff changeset
    30
/**
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
    31
 * A {@code VolatileCallSite} is a {@link CallSite} whose target acts like a volatile variable.
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
    32
 * An {@code invokedynamic} instruction linked to a {@code VolatileCallSite} sees updates
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
    33
 * to its call site target immediately, even if the update occurs in another thread.
7562
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
    34
 * There may be a performance penalty for such tight coupling between threads.
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
    35
 * <p>
7562
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
    36
 * Unlike {@code MutableCallSite}, there is no
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
    37
 * {@linkplain MutableCallSite#sync sync operation} on volatile
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
    38
 * call sites, since every write to a volatile variable is implicitly
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
    39
 * synchronized with reader threads.
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
    40
 * <p>
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
    41
 * In other respects, a {@code VolatileCallSite} is interchangeable
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
    42
 * with {@code MutableCallSite}.
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
    43
 * @see MutableCallSite
7051
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents:
diff changeset
    44
 * @author John Rose, JSR 292 EG
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents:
diff changeset
    45
 */
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
    46
public class VolatileCallSite extends CallSite {
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
    47
    /** Create a call site with a volatile target.
7562
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
    48
     *  The initial target is set to a method handle
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
    49
     *  of the given type which will throw {@code IllegalStateException}.
7562
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
    50
     * @throws NullPointerException if the proposed type is null
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
    51
     */
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
    52
    public VolatileCallSite(MethodType type) {
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
    53
        super(type);
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
    54
    }
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
    55
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
    56
    /** Create a call site with a volatile target.
7562
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
    57
     *  The target is set to the given value.
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
    58
     * @throws NullPointerException if the proposed target is null
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
    59
     */
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
    60
    public VolatileCallSite(MethodHandle target) {
7051
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents:
diff changeset
    61
        super(target);
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
    62
    }
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
    63
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
    64
    /** Internal override to nominally final getTarget. */
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
    65
    @Override
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
    66
    MethodHandle getTarget0() {
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
    67
        return getTargetVolatile();
7051
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents:
diff changeset
    68
    }
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
    69
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
    70
    /**
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
    71
     * Set the target method of this call site, as a volatile variable.
7562
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
    72
     * Has the same effect as {@link CallSite#setTarget CallSite.setTarget}, with the additional
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
    73
     * effects associated with volatiles, in the Java Memory Model.
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
    74
     */
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
    75
    @Override public void setTarget(MethodHandle newTarget) {
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
    76
        checkTargetChange(getTargetVolatile(), newTarget);
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
    77
        setTargetVolatile(newTarget);
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
    78
    }
7051
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents:
diff changeset
    79
}