jdk/src/java.base/share/classes/java/util/concurrent/FutureTask.java
author dl
Wed, 21 Dec 2016 14:26:52 -0800
changeset 42927 1d31e540bfcb
parent 40734 48879ea67e2a
permissions -rw-r--r--
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12 Reviewed-by: martin, smarks, psandoz
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     6
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     8
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * This file is available under and governed by the GNU General Public
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * License version 2 only, as published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * However, the following notice accompanied the original version of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * file:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * Written by Doug Lea with assistance from members of JCP JSR-166
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * Expert Group and released to the public domain, as explained at
9242
ef138d47df58 7034657: Update Creative Commons license URL in legal notices
dl
parents: 7518
diff changeset
    33
 * http://creativecommons.org/publicdomain/zero/1.0/
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
package java.util.concurrent;
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 27032
diff changeset
    37
39725
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
    38
import java.lang.invoke.MethodHandles;
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
    39
import java.lang.invoke.VarHandle;
11689
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
    40
import java.util.concurrent.locks.LockSupport;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * A cancellable asynchronous computation.  This class provides a base
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * implementation of {@link Future}, with methods to start and cancel
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * a computation, query to see if the computation is complete, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * retrieve the result of the computation.  The result can only be
11689
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
    47
 * retrieved when the computation has completed; the {@code get}
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
    48
 * methods will block if the computation has not yet completed.  Once
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * the computation has completed, the computation cannot be restarted
11689
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
    50
 * or cancelled (unless the computation is invoked using
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
    51
 * {@link #runAndReset}).
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 *
11689
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
    53
 * <p>A {@code FutureTask} can be used to wrap a {@link Callable} or
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
    54
 * {@link Runnable} object.  Because {@code FutureTask} implements
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
    55
 * {@code Runnable}, a {@code FutureTask} can be submitted to an
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
    56
 * {@link Executor} for execution.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * <p>In addition to serving as a standalone class, this class provides
11689
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
    59
 * {@code protected} functionality that may be useful when creating
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * customized task classes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * @author Doug Lea
11689
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
    64
 * @param <V> The result type returned by this FutureTask's {@code get} methods
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
public class FutureTask<V> implements RunnableFuture<V> {
11689
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
    67
    /*
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
    68
     * Revision notes: This differs from previous versions of this
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
    69
     * class that relied on AbstractQueuedSynchronizer, mainly to
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
    70
     * avoid surprising users about retaining interrupt status during
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
    71
     * cancellation races. Sync control in the current design relies
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
    72
     * on a "state" field updated via CAS to track completion, along
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
    73
     * with a simple Treiber stack to hold waiting threads.
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
    74
     */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    /**
11689
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
    77
     * The run state of this task, initially NEW.  The run state
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
    78
     * transitions to a terminal state only in methods set,
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
    79
     * setException, and cancel.  During completion, state may take on
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
    80
     * transient values of COMPLETING (while outcome is being set) or
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
    81
     * INTERRUPTING (only while interrupting the runner to satisfy a
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
    82
     * cancel(true)). Transitions from these intermediate to final
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
    83
     * states use cheaper ordered/lazy writes because values are unique
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
    84
     * and cannot be further modified.
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
    85
     *
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
    86
     * Possible state transitions:
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
    87
     * NEW -> COMPLETING -> NORMAL
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
    88
     * NEW -> COMPLETING -> EXCEPTIONAL
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
    89
     * NEW -> CANCELLED
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
    90
     * NEW -> INTERRUPTING -> INTERRUPTED
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
    91
     */
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
    92
    private volatile int state;
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
    93
    private static final int NEW          = 0;
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
    94
    private static final int COMPLETING   = 1;
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
    95
    private static final int NORMAL       = 2;
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
    96
    private static final int EXCEPTIONAL  = 3;
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
    97
    private static final int CANCELLED    = 4;
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
    98
    private static final int INTERRUPTING = 5;
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
    99
    private static final int INTERRUPTED  = 6;
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   100
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   101
    /** The underlying callable; nulled out after running */
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   102
    private Callable<V> callable;
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   103
    /** The result to return or exception to throw from get() */
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   104
    private Object outcome; // non-volatile, protected by state reads/writes
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   105
    /** The thread running the callable; CASed during run() */
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   106
    private volatile Thread runner;
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   107
    /** Treiber stack of waiting threads */
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   108
    private volatile WaitNode waiters;
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   109
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   110
    /**
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   111
     * Returns result or throws exception for completed task.
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   112
     *
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   113
     * @param s completed state value
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   114
     */
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   115
    @SuppressWarnings("unchecked")
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   116
    private V report(int s) throws ExecutionException {
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   117
        Object x = outcome;
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   118
        if (s == NORMAL)
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   119
            return (V)x;
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   120
        if (s >= CANCELLED)
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   121
            throw new CancellationException();
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   122
        throw new ExecutionException((Throwable)x);
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   123
    }
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   124
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   125
    /**
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   126
     * Creates a {@code FutureTask} that will, upon running, execute the
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   127
     * given {@code Callable}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * @param  callable the callable task
11689
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   130
     * @throws NullPointerException if the callable is null
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    public FutureTask(Callable<V> callable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        if (callable == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            throw new NullPointerException();
11689
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   135
        this.callable = callable;
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   136
        this.state = NEW;       // ensure visibility of callable
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    /**
11689
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   140
     * Creates a {@code FutureTask} that will, upon running, execute the
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   141
     * given {@code Runnable}, and arrange that {@code get} will return the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * given result on successful completion.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * @param runnable the runnable task
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * @param result the result to return on successful completion. If
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * you don't need a particular result, consider using
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * constructions of the form:
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   148
     * {@code Future<?> f = new FutureTask<Void>(runnable, null)}
11689
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   149
     * @throws NullPointerException if the runnable is null
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    public FutureTask(Runnable runnable, V result) {
11689
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   152
        this.callable = Executors.callable(runnable, result);
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   153
        this.state = NEW;       // ensure visibility of callable
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    public boolean isCancelled() {
11689
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   157
        return state >= CANCELLED;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    public boolean isDone() {
11689
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   161
        return state != NEW;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    public boolean cancel(boolean mayInterruptIfRunning) {
39725
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   165
        if (!(state == NEW && STATE.compareAndSet
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   166
              (this, NEW, mayInterruptIfRunning ? INTERRUPTING : CANCELLED)))
11689
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   167
            return false;
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 11689
diff changeset
   168
        try {    // in case call to interrupt throws exception
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 11689
diff changeset
   169
            if (mayInterruptIfRunning) {
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 11689
diff changeset
   170
                try {
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 11689
diff changeset
   171
                    Thread t = runner;
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 11689
diff changeset
   172
                    if (t != null)
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 11689
diff changeset
   173
                        t.interrupt();
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 11689
diff changeset
   174
                } finally { // final state
39725
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   175
                    STATE.setRelease(this, INTERRUPTED);
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 11689
diff changeset
   176
                }
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 11689
diff changeset
   177
            }
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 11689
diff changeset
   178
        } finally {
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 11689
diff changeset
   179
            finishCompletion();
11689
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   180
        }
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   181
        return true;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * @throws CancellationException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    public V get() throws InterruptedException, ExecutionException {
11689
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   188
        int s = state;
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   189
        if (s <= COMPLETING)
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   190
            s = awaitDone(false, 0L);
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   191
        return report(s);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * @throws CancellationException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    public V get(long timeout, TimeUnit unit)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        throws InterruptedException, ExecutionException, TimeoutException {
11689
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   199
        if (unit == null)
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   200
            throw new NullPointerException();
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   201
        int s = state;
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   202
        if (s <= COMPLETING &&
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   203
            (s = awaitDone(true, unit.toNanos(timeout))) <= COMPLETING)
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   204
            throw new TimeoutException();
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   205
        return report(s);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * Protected method invoked when this task transitions to state
11689
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   210
     * {@code isDone} (whether normally or via cancellation). The
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * default implementation does nothing.  Subclasses may override
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * this method to invoke completion callbacks or perform
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * bookkeeping. Note that you can query status inside the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * implementation of this method to determine whether this task
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * has been cancelled.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    protected void done() { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    /**
11689
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   220
     * Sets the result of this future to the given value unless
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * this future has already been set or has been cancelled.
11689
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   222
     *
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   223
     * <p>This method is invoked internally by the {@link #run} method
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * upon successful completion of the computation.
11689
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   225
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * @param v the value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    protected void set(V v) {
39725
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   229
        if (STATE.compareAndSet(this, NEW, COMPLETING)) {
11689
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   230
            outcome = v;
39725
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   231
            STATE.setRelease(this, NORMAL); // final state
11689
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   232
            finishCompletion();
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   233
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    /**
11689
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   237
     * Causes this future to report an {@link ExecutionException}
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   238
     * with the given throwable as its cause, unless this future has
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * already been set or has been cancelled.
11689
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   240
     *
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   241
     * <p>This method is invoked internally by the {@link #run} method
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * upon failure of the computation.
11689
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   243
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * @param t the cause of failure
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    protected void setException(Throwable t) {
39725
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   247
        if (STATE.compareAndSet(this, NEW, COMPLETING)) {
11689
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   248
            outcome = t;
39725
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   249
            STATE.setRelease(this, EXCEPTIONAL); // final state
11689
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   250
            finishCompletion();
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   251
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    public void run() {
11689
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   255
        if (state != NEW ||
39725
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   256
            !RUNNER.compareAndSet(this, null, Thread.currentThread()))
11689
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   257
            return;
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   258
        try {
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   259
            Callable<V> c = callable;
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   260
            if (c != null && state == NEW) {
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   261
                V result;
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   262
                boolean ran;
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   263
                try {
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   264
                    result = c.call();
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   265
                    ran = true;
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   266
                } catch (Throwable ex) {
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   267
                    result = null;
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   268
                    ran = false;
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   269
                    setException(ex);
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   270
                }
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   271
                if (ran)
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   272
                    set(result);
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   273
            }
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   274
        } finally {
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   275
            // runner must be non-null until state is settled to
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   276
            // prevent concurrent calls to run()
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   277
            runner = null;
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   278
            // state must be re-read after nulling runner to prevent
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   279
            // leaked interrupts
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   280
            int s = state;
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   281
            if (s >= INTERRUPTING)
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   282
                handlePossibleCancellationInterrupt(s);
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   283
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * Executes the computation without setting its result, and then
11689
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   288
     * resets this future to initial state, failing to do so if the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * computation encounters an exception or is cancelled.  This is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * designed for use with tasks that intrinsically execute more
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * than once.
11689
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   292
     *
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 11689
diff changeset
   293
     * @return {@code true} if successfully run and reset
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    protected boolean runAndReset() {
11689
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   296
        if (state != NEW ||
39725
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   297
            !RUNNER.compareAndSet(this, null, Thread.currentThread()))
11689
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   298
            return false;
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   299
        boolean ran = false;
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   300
        int s = state;
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   301
        try {
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   302
            Callable<V> c = callable;
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   303
            if (c != null && s == NEW) {
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   304
                try {
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   305
                    c.call(); // don't set result
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   306
                    ran = true;
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   307
                } catch (Throwable ex) {
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   308
                    setException(ex);
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   309
                }
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   310
            }
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   311
        } finally {
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   312
            // runner must be non-null until state is settled to
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   313
            // prevent concurrent calls to run()
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   314
            runner = null;
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   315
            // state must be re-read after nulling runner to prevent
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   316
            // leaked interrupts
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   317
            s = state;
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   318
            if (s >= INTERRUPTING)
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   319
                handlePossibleCancellationInterrupt(s);
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   320
        }
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   321
        return ran && s == NEW;
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   322
    }
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   323
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   324
    /**
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   325
     * Ensures that any interrupt from a possible cancel(true) is only
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   326
     * delivered to a task while in run or runAndReset.
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   327
     */
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   328
    private void handlePossibleCancellationInterrupt(int s) {
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   329
        // It is possible for our interrupter to stall before getting a
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   330
        // chance to interrupt us.  Let's spin-wait patiently.
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   331
        if (s == INTERRUPTING)
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   332
            while (state == INTERRUPTING)
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   333
                Thread.yield(); // wait out pending interrupt
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   334
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   335
        // assert state == INTERRUPTED;
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   336
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   337
        // We want to clear any interrupt we may have received from
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   338
        // cancel(true).  However, it is permissible to use interrupts
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   339
        // as an independent mechanism for a task to communicate with
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   340
        // its caller, and there is no way to clear only the
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   341
        // cancellation interrupt.
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   342
        //
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   343
        // Thread.interrupted();
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   344
    }
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   345
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   346
    /**
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   347
     * Simple linked list nodes to record waiting threads in a Treiber
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   348
     * stack.  See other classes such as Phaser and SynchronousQueue
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   349
     * for more detailed explanation.
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   350
     */
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   351
    static final class WaitNode {
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   352
        volatile Thread thread;
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   353
        volatile WaitNode next;
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   354
        WaitNode() { thread = Thread.currentThread(); }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
    /**
11689
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   358
     * Removes and signals all waiting threads, invokes done(), and
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   359
     * nulls out callable.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     */
11689
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   361
    private void finishCompletion() {
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   362
        // assert state > COMPLETING;
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   363
        for (WaitNode q; (q = waiters) != null;) {
40734
48879ea67e2a 8162108: Rename weakCompareAndSetVolatile to weakCompareAndSet
psandoz
parents: 39725
diff changeset
   364
            if (WAITERS.weakCompareAndSet(this, q, null)) {
11689
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   365
                for (;;) {
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   366
                    Thread t = q.thread;
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   367
                    if (t != null) {
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   368
                        q.thread = null;
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   369
                        LockSupport.unpark(t);
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   370
                    }
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   371
                    WaitNode next = q.next;
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   372
                    if (next == null)
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   373
                        break;
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   374
                    q.next = null; // unlink to help gc
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   375
                    q = next;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                }
11689
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   377
                break;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
11689
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   381
        done();
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   382
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   383
        callable = null;        // to reduce footprint
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   384
    }
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   385
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   386
    /**
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   387
     * Awaits completion or aborts on interrupt or timeout.
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   388
     *
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   389
     * @param timed true if use timed waits
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   390
     * @param nanos time to wait, if timed
27032
b7bdee8519d9 8060052: FutureTask; fix underflow when timeout = Long.MIN_VALUE
chegar
parents: 25859
diff changeset
   391
     * @return state upon completion or at timeout
11689
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   392
     */
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   393
    private int awaitDone(boolean timed, long nanos)
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   394
        throws InterruptedException {
27032
b7bdee8519d9 8060052: FutureTask; fix underflow when timeout = Long.MIN_VALUE
chegar
parents: 25859
diff changeset
   395
        // The code below is very delicate, to achieve these goals:
b7bdee8519d9 8060052: FutureTask; fix underflow when timeout = Long.MIN_VALUE
chegar
parents: 25859
diff changeset
   396
        // - call nanoTime exactly once for each call to park
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 27032
diff changeset
   397
        // - if nanos <= 0L, return promptly without allocation or nanoTime
27032
b7bdee8519d9 8060052: FutureTask; fix underflow when timeout = Long.MIN_VALUE
chegar
parents: 25859
diff changeset
   398
        // - if nanos == Long.MIN_VALUE, don't underflow
b7bdee8519d9 8060052: FutureTask; fix underflow when timeout = Long.MIN_VALUE
chegar
parents: 25859
diff changeset
   399
        // - if nanos == Long.MAX_VALUE, and nanoTime is non-monotonic
b7bdee8519d9 8060052: FutureTask; fix underflow when timeout = Long.MIN_VALUE
chegar
parents: 25859
diff changeset
   400
        //   and we suffer a spurious wakeup, we will do no worse than
b7bdee8519d9 8060052: FutureTask; fix underflow when timeout = Long.MIN_VALUE
chegar
parents: 25859
diff changeset
   401
        //   to park-spin for a while
b7bdee8519d9 8060052: FutureTask; fix underflow when timeout = Long.MIN_VALUE
chegar
parents: 25859
diff changeset
   402
        long startTime = 0L;    // Special value 0L means not yet parked
11689
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   403
        WaitNode q = null;
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   404
        boolean queued = false;
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   405
        for (;;) {
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   406
            int s = state;
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   407
            if (s > COMPLETING) {
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   408
                if (q != null)
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   409
                    q.thread = null;
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   410
                return s;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
            }
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 27032
diff changeset
   412
            else if (s == COMPLETING)
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 27032
diff changeset
   413
                // We may have already promised (via isDone) that we are done
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 27032
diff changeset
   414
                // so never return empty-handed or throw InterruptedException
11689
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   415
                Thread.yield();
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 27032
diff changeset
   416
            else if (Thread.interrupted()) {
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 27032
diff changeset
   417
                removeWaiter(q);
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 27032
diff changeset
   418
                throw new InterruptedException();
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 27032
diff changeset
   419
            }
27032
b7bdee8519d9 8060052: FutureTask; fix underflow when timeout = Long.MIN_VALUE
chegar
parents: 25859
diff changeset
   420
            else if (q == null) {
b7bdee8519d9 8060052: FutureTask; fix underflow when timeout = Long.MIN_VALUE
chegar
parents: 25859
diff changeset
   421
                if (timed && nanos <= 0L)
b7bdee8519d9 8060052: FutureTask; fix underflow when timeout = Long.MIN_VALUE
chegar
parents: 25859
diff changeset
   422
                    return s;
11689
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   423
                q = new WaitNode();
27032
b7bdee8519d9 8060052: FutureTask; fix underflow when timeout = Long.MIN_VALUE
chegar
parents: 25859
diff changeset
   424
            }
11689
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   425
            else if (!queued)
40734
48879ea67e2a 8162108: Rename weakCompareAndSetVolatile to weakCompareAndSet
psandoz
parents: 39725
diff changeset
   426
                queued = WAITERS.weakCompareAndSet(this, q.next = waiters, q);
11689
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   427
            else if (timed) {
27032
b7bdee8519d9 8060052: FutureTask; fix underflow when timeout = Long.MIN_VALUE
chegar
parents: 25859
diff changeset
   428
                final long parkNanos;
b7bdee8519d9 8060052: FutureTask; fix underflow when timeout = Long.MIN_VALUE
chegar
parents: 25859
diff changeset
   429
                if (startTime == 0L) { // first time
b7bdee8519d9 8060052: FutureTask; fix underflow when timeout = Long.MIN_VALUE
chegar
parents: 25859
diff changeset
   430
                    startTime = System.nanoTime();
b7bdee8519d9 8060052: FutureTask; fix underflow when timeout = Long.MIN_VALUE
chegar
parents: 25859
diff changeset
   431
                    if (startTime == 0L)
b7bdee8519d9 8060052: FutureTask; fix underflow when timeout = Long.MIN_VALUE
chegar
parents: 25859
diff changeset
   432
                        startTime = 1L;
b7bdee8519d9 8060052: FutureTask; fix underflow when timeout = Long.MIN_VALUE
chegar
parents: 25859
diff changeset
   433
                    parkNanos = nanos;
b7bdee8519d9 8060052: FutureTask; fix underflow when timeout = Long.MIN_VALUE
chegar
parents: 25859
diff changeset
   434
                } else {
b7bdee8519d9 8060052: FutureTask; fix underflow when timeout = Long.MIN_VALUE
chegar
parents: 25859
diff changeset
   435
                    long elapsed = System.nanoTime() - startTime;
b7bdee8519d9 8060052: FutureTask; fix underflow when timeout = Long.MIN_VALUE
chegar
parents: 25859
diff changeset
   436
                    if (elapsed >= nanos) {
b7bdee8519d9 8060052: FutureTask; fix underflow when timeout = Long.MIN_VALUE
chegar
parents: 25859
diff changeset
   437
                        removeWaiter(q);
b7bdee8519d9 8060052: FutureTask; fix underflow when timeout = Long.MIN_VALUE
chegar
parents: 25859
diff changeset
   438
                        return state;
b7bdee8519d9 8060052: FutureTask; fix underflow when timeout = Long.MIN_VALUE
chegar
parents: 25859
diff changeset
   439
                    }
b7bdee8519d9 8060052: FutureTask; fix underflow when timeout = Long.MIN_VALUE
chegar
parents: 25859
diff changeset
   440
                    parkNanos = nanos - elapsed;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
                }
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 27032
diff changeset
   442
                // nanoTime may be slow; recheck before parking
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 27032
diff changeset
   443
                if (state < COMPLETING)
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 27032
diff changeset
   444
                    LockSupport.parkNanos(this, parkNanos);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
            }
11689
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   446
            else
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   447
                LockSupport.park(this);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
        }
11689
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   449
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
11689
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   451
    /**
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   452
     * Tries to unlink a timed-out or interrupted wait node to avoid
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   453
     * accumulating garbage.  Internal nodes are simply unspliced
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   454
     * without CAS since it is harmless if they are traversed anyway
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   455
     * by releasers.  To avoid effects of unsplicing from already
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   456
     * removed nodes, the list is retraversed in case of an apparent
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   457
     * race.  This is slow when there are a lot of nodes, but we don't
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   458
     * expect lists to be long enough to outweigh higher-overhead
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   459
     * schemes.
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   460
     */
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   461
    private void removeWaiter(WaitNode node) {
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   462
        if (node != null) {
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   463
            node.thread = null;
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   464
            retry:
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   465
            for (;;) {          // restart on removeWaiter race
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   466
                for (WaitNode pred = null, q = waiters, s; q != null; q = s) {
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   467
                    s = q.next;
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   468
                    if (q.thread != null)
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   469
                        pred = q;
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   470
                    else if (pred != null) {
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   471
                        pred.next = s;
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   472
                        if (pred.thread == null) // check for race
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   473
                            continue retry;
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   474
                    }
39725
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   475
                    else if (!WAITERS.compareAndSet(this, q, s))
11689
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   476
                        continue retry;
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   477
                }
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   478
                break;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
    }
11689
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   482
39725
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   483
    // VarHandle mechanics
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   484
    private static final VarHandle STATE;
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   485
    private static final VarHandle RUNNER;
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   486
    private static final VarHandle WAITERS;
11689
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   487
    static {
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   488
        try {
39725
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   489
            MethodHandles.Lookup l = MethodHandles.lookup();
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   490
            STATE = l.findVarHandle(FutureTask.class, "state", int.class);
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   491
            RUNNER = l.findVarHandle(FutureTask.class, "runner", Thread.class);
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   492
            WAITERS = l.findVarHandle(FutureTask.class, "waiters", WaitNode.class);
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 27032
diff changeset
   493
        } catch (ReflectiveOperationException e) {
11689
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   494
            throw new Error(e);
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   495
        }
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 27032
diff changeset
   496
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 27032
diff changeset
   497
        // Reduce the risk of rare disastrous classloading in first call to
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 27032
diff changeset
   498
        // LockSupport.park: https://bugs.openjdk.java.net/browse/JDK-8074773
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 27032
diff changeset
   499
        Class<?> ensureLoaded = LockSupport.class;
11689
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   500
    }
ffbfda5c3886 7132378: Race in FutureTask if used with explicit set ( not Runnable )
dl
parents: 9242
diff changeset
   501
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
}