jdk/src/share/classes/java/util/concurrent/locks/AbstractQueuedLongSynchronizer.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 2165 98373487fcf4
permissions -rw-r--r--
Initial load
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * by Sun in the LICENSE file that accompanied this code.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * have any questions.
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * http://creativecommons.org/licenses/publicdomain
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.locks;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.util.concurrent.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.util.concurrent.atomic.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import sun.misc.Unsafe;
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 version of {@link AbstractQueuedSynchronizer} in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * which synchronization state is maintained as a <tt>long</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * This class has exactly the same structure, properties, and methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * as <tt>AbstractQueuedSynchronizer</tt> with the exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * that all state-related parameters and results are defined
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * as <tt>long</tt> rather than <tt>int</tt>. This class
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * may be useful when creating synchronizers such as
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * multilevel locks and barriers that require
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * 64 bits of state.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * <p>See {@link AbstractQueuedSynchronizer} for usage
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * notes and examples.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * @author Doug Lea
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
public abstract class AbstractQueuedLongSynchronizer
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    extends AbstractOwnableSynchronizer
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    implements java.io.Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    private static final long serialVersionUID = 7373984972572414692L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
      To keep sources in sync, the remainder of this source file is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
      exactly cloned from AbstractQueuedSynchronizer, replacing class
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
      name and changing ints related with sync state to longs. Please
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
      keep it that way.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * Creates a new <tt>AbstractQueuedLongSynchronizer</tt> instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * with initial synchronization state of zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    protected AbstractQueuedLongSynchronizer() { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * Wait queue node class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * <p>The wait queue is a variant of a "CLH" (Craig, Landin, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * Hagersten) lock queue. CLH locks are normally used for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * spinlocks.  We instead use them for blocking synchronizers, but
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * use the same basic tactic of holding some of the control
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * information about a thread in the predecessor of its node.  A
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * "status" field in each node keeps track of whether a thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * should block.  A node is signalled when its predecessor
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * releases.  Each node of the queue otherwise serves as a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * specific-notification-style monitor holding a single waiting
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * thread. The status field does NOT control whether threads are
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * granted locks etc though.  A thread may try to acquire if it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * first in the queue. But being first does not guarantee success;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * it only gives the right to contend.  So the currently released
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * contender thread may need to rewait.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * <p>To enqueue into a CLH lock, you atomically splice it in as new
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * tail. To dequeue, you just set the head field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     *      +------+  prev +-----+       +-----+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * head |      | <---- |     | <---- |     |  tail
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     *      +------+       +-----+       +-----+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * <p>Insertion into a CLH queue requires only a single atomic
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * operation on "tail", so there is a simple atomic point of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * demarcation from unqueued to queued. Similarly, dequeing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * involves only updating the "head". However, it takes a bit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * more work for nodes to determine who their successors are,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * in part to deal with possible cancellation due to timeouts
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * and interrupts.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * <p>The "prev" links (not used in original CLH locks), are mainly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * needed to handle cancellation. If a node is cancelled, its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * successor is (normally) relinked to a non-cancelled
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * predecessor. For explanation of similar mechanics in the case
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * of spin locks, see the papers by Scott and Scherer at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * http://www.cs.rochester.edu/u/scott/synchronization/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * <p>We also use "next" links to implement blocking mechanics.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * The thread id for each node is kept in its own node, so a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * predecessor signals the next node to wake up by traversing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * next link to determine which thread it is.  Determination of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * successor must avoid races with newly queued nodes to set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * the "next" fields of their predecessors.  This is solved
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * when necessary by checking backwards from the atomically
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * updated "tail" when a node's successor appears to be null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * (Or, said differently, the next-links are an optimization
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * so that we don't usually need a backward scan.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * <p>Cancellation introduces some conservatism to the basic
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * algorithms.  Since we must poll for cancellation of other
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * nodes, we can miss noticing whether a cancelled node is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * ahead or behind us. This is dealt with by always unparking
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * successors upon cancellation, allowing them to stabilize on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * a new predecessor, unless we can identify an uncancelled
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * predecessor who will carry this responsibility.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * <p>CLH queues need a dummy header node to get started. But
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * we don't create them on construction, because it would be wasted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * effort if there is never contention. Instead, the node
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * is constructed and head and tail pointers are set upon first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * contention.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * <p>Threads waiting on Conditions use the same nodes, but
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * use an additional link. Conditions only need to link nodes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * in simple (non-concurrent) linked queues because they are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * only accessed when exclusively held.  Upon await, a node is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * inserted into a condition queue.  Upon signal, the node is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * transferred to the main queue.  A special value of status
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * field is used to mark which queue a node is on.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * <p>Thanks go to Dave Dice, Mark Moir, Victor Luchangco, Bill
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * Scherer and Michael Scott, along with members of JSR-166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * expert group, for helpful ideas, discussions, and critiques
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * on the design of this class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    static final class Node {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        /** Marker to indicate a node is waiting in shared mode */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        static final Node SHARED = new Node();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        /** Marker to indicate a node is waiting in exclusive mode */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        static final Node EXCLUSIVE = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        /** waitStatus value to indicate thread has cancelled */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        static final int CANCELLED =  1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        /** waitStatus value to indicate successor's thread needs unparking */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        static final int SIGNAL    = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        /** waitStatus value to indicate thread is waiting on condition */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        static final int CONDITION = -2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
         * Status field, taking on only the values:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
         *   SIGNAL:     The successor of this node is (or will soon be)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
         *               blocked (via park), so the current node must
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
         *               unpark its successor when it releases or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
         *               cancels. To avoid races, acquire methods must
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
         *               first indicate they need a signal,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
         *               then retry the atomic acquire, and then,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
         *               on failure, block.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
         *   CANCELLED:  This node is cancelled due to timeout or interrupt.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
         *               Nodes never leave this state. In particular,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
         *               a thread with cancelled node never again blocks.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
         *   CONDITION:  This node is currently on a condition queue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
         *               It will not be used as a sync queue node until
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
         *               transferred. (Use of this value here
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
         *               has nothing to do with the other uses
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
         *               of the field, but simplifies mechanics.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
         *   0:          None of the above
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
         * The values are arranged numerically to simplify use.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
         * Non-negative values mean that a node doesn't need to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
         * signal. So, most code doesn't need to check for particular
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
         * values, just for sign.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
         * The field is initialized to 0 for normal sync nodes, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
         * CONDITION for condition nodes.  It is modified using CAS
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
         * (or when possible, unconditional volatile writes).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        volatile int waitStatus;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
         * Link to predecessor node that current node/thread relies on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
         * for checking waitStatus. Assigned during enqueing, and nulled
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
         * out (for sake of GC) only upon dequeuing.  Also, upon
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
         * cancellation of a predecessor, we short-circuit while
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
         * finding a non-cancelled one, which will always exist
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
         * because the head node is never cancelled: A node becomes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
         * head only as a result of successful acquire. A
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
         * cancelled thread never succeeds in acquiring, and a thread only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
         * cancels itself, not any other node.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        volatile Node prev;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
         * Link to the successor node that the current node/thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
         * unparks upon release. Assigned during enqueuing, adjusted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
         * when bypassing cancelled predecessors, and nulled out (for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
         * sake of GC) when dequeued.  The enq operation does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
         * assign next field of a predecessor until after attachment,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
         * so seeing a null next field does not necessarily mean that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
         * node is at end of queue. However, if a next field appears
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
         * to be null, we can scan prev's from the tail to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
         * double-check.  The next field of cancelled nodes is set to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
         * point to the node itself instead of null, to make life
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
         * easier for isOnSyncQueue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        volatile Node next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
         * The thread that enqueued this node.  Initialized on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
         * construction and nulled out after use.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        volatile Thread thread;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
         * Link to next node waiting on condition, or the special
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
         * value SHARED.  Because condition queues are accessed only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
         * when holding in exclusive mode, we just need a simple
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
         * linked queue to hold nodes while they are waiting on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
         * conditions. They are then transferred to the queue to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
         * re-acquire. And because conditions can only be exclusive,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
         * we save a field by using special value to indicate shared
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
         * mode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        Node nextWaiter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
         * Returns true if node is waiting in shared mode
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        final boolean isShared() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            return nextWaiter == SHARED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
         * Returns previous node, or throws NullPointerException if null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
         * Use when predecessor cannot be null.  The null check could
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
         * be elided, but is present to help the VM.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
         * @return the predecessor of this node
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        final Node predecessor() throws NullPointerException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            Node p = prev;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            if (p == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                return p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        Node() {    // Used to establish initial head or SHARED marker
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        Node(Thread thread, Node mode) {     // Used by addWaiter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            this.nextWaiter = mode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            this.thread = thread;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        Node(Thread thread, int waitStatus) { // Used by Condition
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            this.waitStatus = waitStatus;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            this.thread = thread;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     * Head of the wait queue, lazily initialized.  Except for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * initialization, it is modified only via method setHead.  Note:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * If head exists, its waitStatus is guaranteed not to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * CANCELLED.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    private transient volatile Node head;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * Tail of the wait queue, lazily initialized.  Modified only via
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * method enq to add new wait node.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    private transient volatile Node tail;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * The synchronization state.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    private volatile long state;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * Returns the current value of synchronization state.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * This operation has memory semantics of a <tt>volatile</tt> read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * @return current state value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    protected final long getState() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        return state;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * Sets the value of synchronization state.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * This operation has memory semantics of a <tt>volatile</tt> write.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     * @param newState the new state value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    protected final void setState(long newState) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        state = newState;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * Atomically sets synchronization state to the given updated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     * value if the current state value equals the expected value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     * This operation has memory semantics of a <tt>volatile</tt> read
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     * and write.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     * @param expect the expected value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     * @param update the new value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     * @return true if successful. False return indicates that the actual
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     *         value was not equal to the expected value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    protected final boolean compareAndSetState(long expect, long update) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        // See below for intrinsics setup to support this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        return unsafe.compareAndSwapLong(this, stateOffset, expect, update);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    // Queuing utilities
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     * The number of nanoseconds for which it is faster to spin
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     * rather than to use timed park. A rough estimate suffices
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     * to improve responsiveness with very short timeouts.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
    static final long spinForTimeoutThreshold = 1000L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     * Inserts node into queue, initializing if necessary. See picture above.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     * @param node the node to insert
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * @return node's predecessor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    private Node enq(final Node node) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
            Node t = tail;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
            if (t == null) { // Must initialize
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                if (compareAndSetHead(new Node()))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                    tail = head;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                node.prev = t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                if (compareAndSetTail(t, node)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                    t.next = node;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                    return t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     * Creates and enqueues node for current thread and given mode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     * @param mode Node.EXCLUSIVE for exclusive, Node.SHARED for shared
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     * @return the new node
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
    private Node addWaiter(Node mode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        Node node = new Node(Thread.currentThread(), mode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        // Try the fast path of enq; backup to full enq on failure
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        Node pred = tail;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        if (pred != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
            node.prev = pred;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
            if (compareAndSetTail(pred, node)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
                pred.next = node;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
                return node;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        enq(node);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        return node;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     * Sets head of queue to be node, thus dequeuing. Called only by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     * acquire methods.  Also nulls out unused fields for sake of GC
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     * and to suppress unnecessary signals and traversals.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     * @param node the node
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
    private void setHead(Node node) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        head = node;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        node.thread = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        node.prev = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     * Wakes up node's successor, if one exists.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     * @param node the node
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
    private void unparkSuccessor(Node node) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
         * Try to clear status in anticipation of signalling.  It is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
         * OK if this fails or if status is changed by waiting thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        compareAndSetWaitStatus(node, Node.SIGNAL, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
         * Thread to unpark is held in successor, which is normally
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
         * just the next node.  But if cancelled or apparently null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
         * traverse backwards from tail to find the actual
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
         * non-cancelled successor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        Node s = node.next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        if (s == null || s.waitStatus > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
            s = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
            for (Node t = tail; t != null && t != node; t = t.prev)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                if (t.waitStatus <= 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                    s = t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
        if (s != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
            LockSupport.unpark(s.thread);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     * Sets head of queue, and checks if successor may be waiting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
     * in shared mode, if so propagating if propagate > 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     * @param pred the node holding waitStatus for node
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     * @param node the node
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
     * @param propagate the return value from a tryAcquireShared
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
    private void setHeadAndPropagate(Node node, long propagate) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
        setHead(node);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
        if (propagate > 0 && node.waitStatus != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
             * Don't bother fully figuring out successor.  If it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
             * looks null, call unparkSuccessor anyway to be safe.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
            Node s = node.next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
            if (s == null || s.isShared())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
                unparkSuccessor(node);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
    // Utilities for various versions of acquire
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
     * Cancels an ongoing attempt to acquire.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
     * @param node the node
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
    private void cancelAcquire(Node node) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        // Ignore if node doesn't exist
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
        if (node == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        node.thread = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
        // Skip cancelled predecessors
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
        Node pred = node.prev;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
        while (pred.waitStatus > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
            node.prev = pred = pred.prev;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
        // Getting this before setting waitStatus ensures staleness
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
        Node predNext = pred.next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
        // Can use unconditional write instead of CAS here
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
        node.waitStatus = Node.CANCELLED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        // If we are the tail, remove ourselves
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
        if (node == tail && compareAndSetTail(node, pred)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
            compareAndSetNext(pred, predNext, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
            // If "active" predecessor found...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
            if (pred != head
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
                && (pred.waitStatus == Node.SIGNAL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
                    || compareAndSetWaitStatus(pred, 0, Node.SIGNAL))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
                && pred.thread != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
                // If successor is active, set predecessor's next link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
                Node next = node.next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
                if (next != null && next.waitStatus <= 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
                    compareAndSetNext(pred, predNext, next);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
                unparkSuccessor(node);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
            node.next = node; // help GC
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
     * Checks and updates status for a node that failed to acquire.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
     * Returns true if thread should block. This is the main signal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
     * control in all acquire loops.  Requires that pred == node.prev
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
     * @param pred node's predecessor holding status
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
     * @param node the node
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
     * @return {@code true} if thread should block
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
    private static boolean shouldParkAfterFailedAcquire(Node pred, Node node) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
        int s = pred.waitStatus;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
        if (s < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
             * This node has already set status asking a release
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
             * to signal it, so it can safely park.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
        if (s > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
             * Predecessor was cancelled. Skip over predecessors and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
             * indicate retry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
            do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
                node.prev = pred = pred.prev;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
            } while (pred.waitStatus > 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
            pred.next = node;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
             * Indicate that we need a signal, but don't park yet. Caller
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
             * will need to retry to make sure it cannot acquire before
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
             * parking.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
            compareAndSetWaitStatus(pred, 0, Node.SIGNAL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
     * Convenience method to interrupt current thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
    private static void selfInterrupt() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
        Thread.currentThread().interrupt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
     * Convenience method to park and then check if interrupted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
     * @return {@code true} if interrupted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
    private final boolean parkAndCheckInterrupt() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
        LockSupport.park(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
        return Thread.interrupted();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
     * Various flavors of acquire, varying in exclusive/shared and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
     * control modes.  Each is mostly the same, but annoyingly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
     * different.  Only a little bit of factoring is possible due to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
     * interactions of exception mechanics (including ensuring that we
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
     * cancel if tryAcquire throws exception) and other control, at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
     * least not without hurting performance too much.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
     * Acquires in exclusive uninterruptible mode for thread already in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
     * queue. Used by condition wait methods as well as acquire.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
     * @param node the node
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
     * @param arg the acquire argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
     * @return {@code true} if interrupted while waiting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
    final boolean acquireQueued(final Node node, long arg) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
        boolean failed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
            boolean interrupted = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
            for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
                final Node p = node.predecessor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
                if (p == head && tryAcquire(arg)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
                    setHead(node);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
                    p.next = null; // help GC
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
                    failed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
                    return interrupted;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
                if (shouldParkAfterFailedAcquire(p, node) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
                    parkAndCheckInterrupt())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
                    interrupted = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
            if (failed)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
                cancelAcquire(node);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
     * Acquires in exclusive interruptible mode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
     * @param arg the acquire argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
    private void doAcquireInterruptibly(long arg)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
        throws InterruptedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
        final Node node = addWaiter(Node.EXCLUSIVE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
        boolean failed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
            for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
                final Node p = node.predecessor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
                if (p == head && tryAcquire(arg)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
                    setHead(node);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
                    p.next = null; // help GC
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
                    failed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
                if (shouldParkAfterFailedAcquire(p, node) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
                    parkAndCheckInterrupt())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
                    throw new InterruptedException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
            if (failed)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
                cancelAcquire(node);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
     * Acquires in exclusive timed mode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
     * @param arg the acquire argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
     * @param nanosTimeout max wait time
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
     * @return {@code true} if acquired
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
    private boolean doAcquireNanos(long arg, long nanosTimeout)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
        throws InterruptedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
        long lastTime = System.nanoTime();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
        final Node node = addWaiter(Node.EXCLUSIVE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
        boolean failed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
            for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
                final Node p = node.predecessor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
                if (p == head && tryAcquire(arg)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
                    setHead(node);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
                    p.next = null; // help GC
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
                    failed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
                if (nanosTimeout <= 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
                if (shouldParkAfterFailedAcquire(p, node) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
                    nanosTimeout > spinForTimeoutThreshold)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
                    LockSupport.parkNanos(this, nanosTimeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
                long now = System.nanoTime();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
                nanosTimeout -= now - lastTime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
                lastTime = now;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
                if (Thread.interrupted())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
                    throw new InterruptedException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
            if (failed)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
                cancelAcquire(node);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
     * Acquires in shared uninterruptible mode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
     * @param arg the acquire argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
    private void doAcquireShared(long arg) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
        final Node node = addWaiter(Node.SHARED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
        boolean failed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
            boolean interrupted = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
            for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
                final Node p = node.predecessor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
                if (p == head) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
                    long r = tryAcquireShared(arg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
                    if (r >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
                        setHeadAndPropagate(node, r);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
                        p.next = null; // help GC
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
                        if (interrupted)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
                            selfInterrupt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
                        failed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
                        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
                if (shouldParkAfterFailedAcquire(p, node) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
                    parkAndCheckInterrupt())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
                    interrupted = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
            if (failed)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
                cancelAcquire(node);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
     * Acquires in shared interruptible mode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
     * @param arg the acquire argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
    private void doAcquireSharedInterruptibly(long arg)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
        throws InterruptedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
        final Node node = addWaiter(Node.SHARED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
        boolean failed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
            for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
                final Node p = node.predecessor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
                if (p == head) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
                    long r = tryAcquireShared(arg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
                    if (r >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
                        setHeadAndPropagate(node, r);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
                        p.next = null; // help GC
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
                        failed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
                        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
                if (shouldParkAfterFailedAcquire(p, node) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
                    parkAndCheckInterrupt())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
                    throw new InterruptedException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
            if (failed)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
                cancelAcquire(node);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
     * Acquires in shared timed mode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
     * @param arg the acquire argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
     * @param nanosTimeout max wait time
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
     * @return {@code true} if acquired
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
    private boolean doAcquireSharedNanos(long arg, long nanosTimeout)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
        throws InterruptedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
        long lastTime = System.nanoTime();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
        final Node node = addWaiter(Node.SHARED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
        boolean failed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
            for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
                final Node p = node.predecessor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
                if (p == head) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
                    long r = tryAcquireShared(arg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
                    if (r >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
                        setHeadAndPropagate(node, r);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
                        p.next = null; // help GC
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
                        failed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
                        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
                if (nanosTimeout <= 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
                if (shouldParkAfterFailedAcquire(p, node) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
                    nanosTimeout > spinForTimeoutThreshold)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
                    LockSupport.parkNanos(this, nanosTimeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
                long now = System.nanoTime();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
                nanosTimeout -= now - lastTime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
                lastTime = now;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
                if (Thread.interrupted())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
                    throw new InterruptedException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
            if (failed)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
                cancelAcquire(node);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
    // Main exported methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
     * Attempts to acquire in exclusive mode. This method should query
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
     * if the state of the object permits it to be acquired in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
     * exclusive mode, and if so to acquire it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
     * <p>This method is always invoked by the thread performing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
     * acquire.  If this method reports failure, the acquire method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
     * may queue the thread, if it is not already queued, until it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
     * signalled by a release from some other thread. This can be used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
     * to implement method {@link Lock#tryLock()}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
     * <p>The default
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
     * implementation throws {@link UnsupportedOperationException}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
     * @param arg the acquire argument. This value is always the one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
     *        passed to an acquire method, or is the value saved on entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
     *        to a condition wait.  The value is otherwise uninterpreted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
     *        and can represent anything you like.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
     * @return {@code true} if successful. Upon success, this object has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
     *         been acquired.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
     * @throws IllegalMonitorStateException if acquiring would place this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
     *         synchronizer in an illegal state. This exception must be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
     *         thrown in a consistent fashion for synchronization to work
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
     *         correctly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
     * @throws UnsupportedOperationException if exclusive mode is not supported
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
    protected boolean tryAcquire(long arg) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
        throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
     * Attempts to set the state to reflect a release in exclusive
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
     * mode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
     * <p>This method is always invoked by the thread performing release.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
     * <p>The default implementation throws
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
     * {@link UnsupportedOperationException}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
     * @param arg the release argument. This value is always the one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
     *        passed to a release method, or the current state value upon
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
     *        entry to a condition wait.  The value is otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
     *        uninterpreted and can represent anything you like.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
     * @return {@code true} if this object is now in a fully released
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
     *         state, so that any waiting threads may attempt to acquire;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
     *         and {@code false} otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
     * @throws IllegalMonitorStateException if releasing would place this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
     *         synchronizer in an illegal state. This exception must be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
     *         thrown in a consistent fashion for synchronization to work
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
     *         correctly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
     * @throws UnsupportedOperationException if exclusive mode is not supported
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
    protected boolean tryRelease(long arg) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
        throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
     * Attempts to acquire in shared mode. This method should query if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
     * the state of the object permits it to be acquired in the shared
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
     * mode, and if so to acquire it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
     * <p>This method is always invoked by the thread performing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
     * acquire.  If this method reports failure, the acquire method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
     * may queue the thread, if it is not already queued, until it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
     * signalled by a release from some other thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
     * <p>The default implementation throws {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
     * UnsupportedOperationException}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
     * @param arg the acquire argument. This value is always the one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
     *        passed to an acquire method, or is the value saved on entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
     *        to a condition wait.  The value is otherwise uninterpreted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
     *        and can represent anything you like.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
     * @return a negative value on failure; zero if acquisition in shared
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
     *         mode succeeded but no subsequent shared-mode acquire can
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
     *         succeed; and a positive value if acquisition in shared
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
     *         mode succeeded and subsequent shared-mode acquires might
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
     *         also succeed, in which case a subsequent waiting thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
     *         must check availability. (Support for three different
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
     *         return values enables this method to be used in contexts
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
     *         where acquires only sometimes act exclusively.)  Upon
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
     *         success, this object has been acquired.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
     * @throws IllegalMonitorStateException if acquiring would place this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
     *         synchronizer in an illegal state. This exception must be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
     *         thrown in a consistent fashion for synchronization to work
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
     *         correctly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
     * @throws UnsupportedOperationException if shared mode is not supported
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
    protected long tryAcquireShared(long arg) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
        throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
     * Attempts to set the state to reflect a release in shared mode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
     * <p>This method is always invoked by the thread performing release.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
     * <p>The default implementation throws
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
     * {@link UnsupportedOperationException}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
     * @param arg the release argument. This value is always the one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
     *        passed to a release method, or the current state value upon
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
     *        entry to a condition wait.  The value is otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
     *        uninterpreted and can represent anything you like.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
     * @return {@code true} if this release of shared mode may permit a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
     *         waiting acquire (shared or exclusive) to succeed; and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
     *         {@code false} otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
     * @throws IllegalMonitorStateException if releasing would place this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
     *         synchronizer in an illegal state. This exception must be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
     *         thrown in a consistent fashion for synchronization to work
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
     *         correctly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
     * @throws UnsupportedOperationException if shared mode is not supported
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
    protected boolean tryReleaseShared(long arg) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
        throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
     * Returns {@code true} if synchronization is held exclusively with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
     * respect to the current (calling) thread.  This method is invoked
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
     * upon each call to a non-waiting {@link ConditionObject} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
     * (Waiting methods instead invoke {@link #release}.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
     * <p>The default implementation throws {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
     * UnsupportedOperationException}. This method is invoked
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
     * internally only within {@link ConditionObject} methods, so need
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
     * not be defined if conditions are not used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
     * @return {@code true} if synchronization is held exclusively;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
     *         {@code false} otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
     * @throws UnsupportedOperationException if conditions are not supported
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
    protected boolean isHeldExclusively() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
        throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
     * Acquires in exclusive mode, ignoring interrupts.  Implemented
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
     * by invoking at least once {@link #tryAcquire},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
     * returning on success.  Otherwise the thread is queued, possibly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
     * repeatedly blocking and unblocking, invoking {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
     * #tryAcquire} until success.  This method can be used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
     * to implement method {@link Lock#lock}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
     * @param arg the acquire argument.  This value is conveyed to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
     *        {@link #tryAcquire} but is otherwise uninterpreted and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
     *        can represent anything you like.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
    public final void acquire(long arg) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
        if (!tryAcquire(arg) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
            acquireQueued(addWaiter(Node.EXCLUSIVE), arg))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
            selfInterrupt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
     * Acquires in exclusive mode, aborting if interrupted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
     * Implemented by first checking interrupt status, then invoking
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
     * at least once {@link #tryAcquire}, returning on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
     * success.  Otherwise the thread is queued, possibly repeatedly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
     * blocking and unblocking, invoking {@link #tryAcquire}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
     * until success or the thread is interrupted.  This method can be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
     * used to implement method {@link Lock#lockInterruptibly}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
     * @param arg the acquire argument.  This value is conveyed to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
     *        {@link #tryAcquire} but is otherwise uninterpreted and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
     *        can represent anything you like.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
     * @throws InterruptedException if the current thread is interrupted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
    public final void acquireInterruptibly(long arg) throws InterruptedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
        if (Thread.interrupted())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
            throw new InterruptedException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
        if (!tryAcquire(arg))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
            doAcquireInterruptibly(arg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
     * Attempts to acquire in exclusive mode, aborting if interrupted,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
     * and failing if the given timeout elapses.  Implemented by first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
     * checking interrupt status, then invoking at least once {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
     * #tryAcquire}, returning on success.  Otherwise, the thread is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
     * queued, possibly repeatedly blocking and unblocking, invoking
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
     * {@link #tryAcquire} until success or the thread is interrupted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
     * or the timeout elapses.  This method can be used to implement
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
     * method {@link Lock#tryLock(long, TimeUnit)}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
     * @param arg the acquire argument.  This value is conveyed to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
     *        {@link #tryAcquire} but is otherwise uninterpreted and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
     *        can represent anything you like.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
     * @param nanosTimeout the maximum number of nanoseconds to wait
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
     * @return {@code true} if acquired; {@code false} if timed out
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
     * @throws InterruptedException if the current thread is interrupted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
    public final boolean tryAcquireNanos(long arg, long nanosTimeout) throws InterruptedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
        if (Thread.interrupted())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
            throw new InterruptedException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
        return tryAcquire(arg) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
            doAcquireNanos(arg, nanosTimeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
     * Releases in exclusive mode.  Implemented by unblocking one or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
     * more threads if {@link #tryRelease} returns true.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
     * This method can be used to implement method {@link Lock#unlock}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
     * @param arg the release argument.  This value is conveyed to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
     *        {@link #tryRelease} but is otherwise uninterpreted and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
     *        can represent anything you like.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
     * @return the value returned from {@link #tryRelease}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
    public final boolean release(long arg) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
        if (tryRelease(arg)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
            Node h = head;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
            if (h != null && h.waitStatus != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
                unparkSuccessor(h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
     * Acquires in shared mode, ignoring interrupts.  Implemented by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
     * first invoking at least once {@link #tryAcquireShared},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
     * returning on success.  Otherwise the thread is queued, possibly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
     * repeatedly blocking and unblocking, invoking {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
     * #tryAcquireShared} until success.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
     * @param arg the acquire argument.  This value is conveyed to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
     *        {@link #tryAcquireShared} but is otherwise uninterpreted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
     *        and can represent anything you like.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
    public final void acquireShared(long arg) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
        if (tryAcquireShared(arg) < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
            doAcquireShared(arg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
     * Acquires in shared mode, aborting if interrupted.  Implemented
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
     * by first checking interrupt status, then invoking at least once
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
     * {@link #tryAcquireShared}, returning on success.  Otherwise the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
     * thread is queued, possibly repeatedly blocking and unblocking,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
     * invoking {@link #tryAcquireShared} until success or the thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
     * is interrupted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
     * @param arg the acquire argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
     * This value is conveyed to {@link #tryAcquireShared} but is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
     * otherwise uninterpreted and can represent anything
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
     * you like.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
     * @throws InterruptedException if the current thread is interrupted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
    public final void acquireSharedInterruptibly(long arg) throws InterruptedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
        if (Thread.interrupted())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
            throw new InterruptedException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
        if (tryAcquireShared(arg) < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
            doAcquireSharedInterruptibly(arg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
     * Attempts to acquire in shared mode, aborting if interrupted, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
     * failing if the given timeout elapses.  Implemented by first
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
     * checking interrupt status, then invoking at least once {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
     * #tryAcquireShared}, returning on success.  Otherwise, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
     * thread is queued, possibly repeatedly blocking and unblocking,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
     * invoking {@link #tryAcquireShared} until success or the thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
     * is interrupted or the timeout elapses.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
     * @param arg the acquire argument.  This value is conveyed to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
     *        {@link #tryAcquireShared} but is otherwise uninterpreted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
     *        and can represent anything you like.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
     * @param nanosTimeout the maximum number of nanoseconds to wait
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
     * @return {@code true} if acquired; {@code false} if timed out
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
     * @throws InterruptedException if the current thread is interrupted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
    public final boolean tryAcquireSharedNanos(long arg, long nanosTimeout) throws InterruptedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
        if (Thread.interrupted())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
            throw new InterruptedException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
        return tryAcquireShared(arg) >= 0 ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
            doAcquireSharedNanos(arg, nanosTimeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
     * Releases in shared mode.  Implemented by unblocking one or more
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
     * threads if {@link #tryReleaseShared} returns true.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
     * @param arg the release argument.  This value is conveyed to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
     *        {@link #tryReleaseShared} but is otherwise uninterpreted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
     *        and can represent anything you like.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
     * @return the value returned from {@link #tryReleaseShared}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
    public final boolean releaseShared(long arg) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
        if (tryReleaseShared(arg)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
            Node h = head;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
            if (h != null && h.waitStatus != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
                unparkSuccessor(h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
    // Queue inspection methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
     * Queries whether any threads are waiting to acquire. Note that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
     * because cancellations due to interrupts and timeouts may occur
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
     * at any time, a {@code true} return does not guarantee that any
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
     * other thread will ever acquire.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
     * <p>In this implementation, this operation returns in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
     * constant time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
     * @return {@code true} if there may be other threads waiting to acquire
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
    public final boolean hasQueuedThreads() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
        return head != tail;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
     * Queries whether any threads have ever contended to acquire this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
     * synchronizer; that is if an acquire method has ever blocked.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
     * <p>In this implementation, this operation returns in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
     * constant time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
     * @return {@code true} if there has ever been contention
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
    public final boolean hasContended() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
        return head != null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
     * Returns the first (longest-waiting) thread in the queue, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
     * {@code null} if no threads are currently queued.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
     * <p>In this implementation, this operation normally returns in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
     * constant time, but may iterate upon contention if other threads are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
     * concurrently modifying the queue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
     * @return the first (longest-waiting) thread in the queue, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
     *         {@code null} if no threads are currently queued
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
    public final Thread getFirstQueuedThread() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
        // handle only fast path, else relay
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
        return (head == tail) ? null : fullGetFirstQueuedThread();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
     * Version of getFirstQueuedThread called when fastpath fails
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
    private Thread fullGetFirstQueuedThread() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
         * The first node is normally head.next. Try to get its
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
         * thread field, ensuring consistent reads: If thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
         * field is nulled out or s.prev is no longer head, then
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
         * some other thread(s) concurrently performed setHead in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
         * between some of our reads. We try this twice before
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
         * resorting to traversal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
        Node h, s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
        Thread st;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
        if (((h = head) != null && (s = h.next) != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
             s.prev == head && (st = s.thread) != null) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
            ((h = head) != null && (s = h.next) != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
             s.prev == head && (st = s.thread) != null))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
            return st;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
         * Head's next field might not have been set yet, or may have
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
         * been unset after setHead. So we must check to see if tail
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
         * is actually first node. If not, we continue on, safely
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
         * traversing from tail back to head to find first,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
         * guaranteeing termination.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
        Node t = tail;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
        Thread firstThread = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
        while (t != null && t != head) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
            Thread tt = t.thread;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
            if (tt != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
                firstThread = tt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
            t = t.prev;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
        return firstThread;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
     * Returns true if the given thread is currently queued.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
     * <p>This implementation traverses the queue to determine
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
     * presence of the given thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
     * @param thread the thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
     * @return {@code true} if the given thread is on the queue
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
     * @throws NullPointerException if the thread is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
    public final boolean isQueued(Thread thread) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
        if (thread == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
        for (Node p = tail; p != null; p = p.prev)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
            if (p.thread == thread)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
     * Returns {@code true} if the apparent first queued thread, if one
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
     * exists, is waiting in exclusive mode.  If this method returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
     * {@code true}, and the current thread is attempting to acquire in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
     * shared mode (that is, this method is invoked from {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
     * #tryAcquireShared}) then it is guaranteed that the current thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
     * is not the first queued thread.  Used only as a heuristic in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
     * ReentrantReadWriteLock.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
    final boolean apparentlyFirstQueuedIsExclusive() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
        Node h, s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
        return (h = head) != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
            (s = h.next)  != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
            !s.isShared()         &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
            s.thread != null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
     * Queries whether any threads have been waiting to acquire longer
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
     * than the current thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
     * <p>An invocation of this method is equivalent to (but may be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
     * more efficient than):
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
     *  <pre> {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
     * getFirstQueuedThread() != Thread.currentThread() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
     * hasQueuedThreads()}</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
     * <p>Note that because cancellations due to interrupts and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
     * timeouts may occur at any time, a {@code true} return does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
     * guarantee that some other thread will acquire before the current
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
     * thread.  Likewise, it is possible for another thread to win a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
     * race to enqueue after this method has returned {@code false},
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
     * due to the queue being empty.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
     * <p>This method is designed to be used by a fair synchronizer to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
     * avoid <a href="AbstractQueuedSynchronizer#barging">barging</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
     * Such a synchronizer's {@link #tryAcquire} method should return
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
     * {@code false}, and its {@link #tryAcquireShared} method should
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
     * return a negative value, if this method returns {@code true}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
     * (unless this is a reentrant acquire).  For example, the {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
     * tryAcquire} method for a fair, reentrant, exclusive mode
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
     * synchronizer might look like this:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
     *  <pre> {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
     * protected boolean tryAcquire(int arg) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
     *   if (isHeldExclusively()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
     *     // A reentrant acquire; increment hold count
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
     *     return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
     *   } else if (hasQueuedPredecessors()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
     *     return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
     *   } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
     *     // try to acquire normally
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
     *   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
     * }}</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
     * @return {@code true} if there is a queued thread preceding the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
     *         current thread, and {@code false} if the current thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
     *         is at the head of the queue or the queue is empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
     * @since 1.7
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
    public final boolean hasQueuedPredecessors() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
        // The correctness of this depends on head being initialized
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
        // before tail and on head.next being accurate if the current
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
        // thread is first in queue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
        Node h, s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
        return (h = head) != tail &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
            ((s = h.next) == null || s.thread != Thread.currentThread());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
    // Instrumentation and monitoring methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
     * Returns an estimate of the number of threads waiting to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
     * acquire.  The value is only an estimate because the number of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
     * threads may change dynamically while this method traverses
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
     * internal data structures.  This method is designed for use in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
     * monitoring system state, not for synchronization
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
     * control.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
     * @return the estimated number of threads waiting to acquire
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
    public final int getQueueLength() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
        int n = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
        for (Node p = tail; p != null; p = p.prev) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
            if (p.thread != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
                ++n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
        return n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
     * Returns a collection containing threads that may be waiting to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
     * acquire.  Because the actual set of threads may change
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
     * dynamically while constructing this result, the returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
     * collection is only a best-effort estimate.  The elements of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
     * returned collection are in no particular order.  This method is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
     * designed to facilitate construction of subclasses that provide
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
     * more extensive monitoring facilities.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
     * @return the collection of threads
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
    public final Collection<Thread> getQueuedThreads() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
        ArrayList<Thread> list = new ArrayList<Thread>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
        for (Node p = tail; p != null; p = p.prev) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
            Thread t = p.thread;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
            if (t != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
                list.add(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
        return list;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
     * Returns a collection containing threads that may be waiting to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
     * acquire in exclusive mode. This has the same properties
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
     * as {@link #getQueuedThreads} except that it only returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
     * those threads waiting due to an exclusive acquire.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
     * @return the collection of threads
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
    public final Collection<Thread> getExclusiveQueuedThreads() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
        ArrayList<Thread> list = new ArrayList<Thread>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
        for (Node p = tail; p != null; p = p.prev) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
            if (!p.isShared()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
                Thread t = p.thread;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
                if (t != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
                    list.add(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
        return list;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
     * Returns a collection containing threads that may be waiting to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
     * acquire in shared mode. This has the same properties
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
     * as {@link #getQueuedThreads} except that it only returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
     * those threads waiting due to a shared acquire.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
     * @return the collection of threads
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
    public final Collection<Thread> getSharedQueuedThreads() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
        ArrayList<Thread> list = new ArrayList<Thread>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
        for (Node p = tail; p != null; p = p.prev) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
            if (p.isShared()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
                Thread t = p.thread;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
                if (t != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
                    list.add(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
        return list;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
     * Returns a string identifying this synchronizer, as well as its state.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
     * The state, in brackets, includes the String {@code "State ="}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
     * followed by the current value of {@link #getState}, and either
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
     * {@code "nonempty"} or {@code "empty"} depending on whether the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
     * queue is empty.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
     * @return a string identifying this synchronizer, as well as its state
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
        long s = getState();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
        String q  = hasQueuedThreads() ? "non" : "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
        return super.toString() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
            "[State = " + s + ", " + q + "empty queue]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
    // Internal support methods for Conditions
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
     * Returns true if a node, always one that was initially placed on
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
     * a condition queue, is now waiting to reacquire on sync queue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
     * @param node the node
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
     * @return true if is reacquiring
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
    final boolean isOnSyncQueue(Node node) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
        if (node.waitStatus == Node.CONDITION || node.prev == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
        if (node.next != null) // If has successor, it must be on queue
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
         * node.prev can be non-null, but not yet on queue because
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
         * the CAS to place it on queue can fail. So we have to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
         * traverse from tail to make sure it actually made it.  It
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
         * will always be near the tail in calls to this method, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
         * unless the CAS failed (which is unlikely), it will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
         * there, so we hardly ever traverse much.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
        return findNodeFromTail(node);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
     * Returns true if node is on sync queue by searching backwards from tail.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
     * Called only when needed by isOnSyncQueue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
     * @return true if present
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
    private boolean findNodeFromTail(Node node) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
        Node t = tail;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
        for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
            if (t == node)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
            if (t == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
            t = t.prev;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
     * Transfers a node from a condition queue onto sync queue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
     * Returns true if successful.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
     * @param node the node
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
     * @return true if successfully transferred (else the node was
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
     * cancelled before signal).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
    final boolean transferForSignal(Node node) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
         * If cannot change waitStatus, the node has been cancelled.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
        if (!compareAndSetWaitStatus(node, Node.CONDITION, 0))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
         * Splice onto queue and try to set waitStatus of predecessor to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
         * indicate that thread is (probably) waiting. If cancelled or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
         * attempt to set waitStatus fails, wake up to resync (in which
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
         * case the waitStatus can be transiently and harmlessly wrong).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
        Node p = enq(node);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
        int c = p.waitStatus;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
        if (c > 0 || !compareAndSetWaitStatus(p, c, Node.SIGNAL))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
            LockSupport.unpark(node.thread);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
     * Transfers node, if necessary, to sync queue after a cancelled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
     * wait. Returns true if thread was cancelled before being
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
     * signalled.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
     * @param current the waiting thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
     * @param node its node
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
     * @return true if cancelled before the node was signalled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
    final boolean transferAfterCancelledWait(Node node) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1406
        if (compareAndSetWaitStatus(node, Node.CONDITION, 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1407
            enq(node);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1408
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1410
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
         * If we lost out to a signal(), then we can't proceed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
         * until it finishes its enq().  Cancelling during an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1413
         * incomplete transfer is both rare and transient, so just
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1414
         * spin.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
        while (!isOnSyncQueue(node))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
            Thread.yield();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1418
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1421
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
     * Invokes release with current state value; returns saved state.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
     * Cancels node and throws exception on failure.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
     * @param node the condition node for this wait
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1425
     * @return previous sync state
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1427
    final long fullyRelease(Node node) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1428
        boolean failed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1429
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1430
            long savedState = getState();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1431
            if (release(savedState)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1432
                failed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1433
                return savedState;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1434
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1435
                throw new IllegalMonitorStateException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1436
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1437
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1438
            if (failed)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1439
                node.waitStatus = Node.CANCELLED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1440
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1441
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1442
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1443
    // Instrumentation methods for conditions
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1444
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1445
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1446
     * Queries whether the given ConditionObject
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1447
     * uses this synchronizer as its lock.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1448
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1449
     * @param condition the condition
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1450
     * @return <tt>true</tt> if owned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1451
     * @throws NullPointerException if the condition is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1452
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1453
    public final boolean owns(ConditionObject condition) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1454
        if (condition == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1455
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1456
        return condition.isOwnedBy(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1457
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1458
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1459
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1460
     * Queries whether any threads are waiting on the given condition
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1461
     * associated with this synchronizer. Note that because timeouts
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1462
     * and interrupts may occur at any time, a <tt>true</tt> return
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1463
     * does not guarantee that a future <tt>signal</tt> will awaken
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1464
     * any threads.  This method is designed primarily for use in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1465
     * monitoring of the system state.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1466
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1467
     * @param condition the condition
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1468
     * @return <tt>true</tt> if there are any waiting threads
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1469
     * @throws IllegalMonitorStateException if exclusive synchronization
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1470
     *         is not held
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1471
     * @throws IllegalArgumentException if the given condition is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1472
     *         not associated with this synchronizer
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1473
     * @throws NullPointerException if the condition is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1474
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1475
    public final boolean hasWaiters(ConditionObject condition) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1476
        if (!owns(condition))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1477
            throw new IllegalArgumentException("Not owner");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1478
        return condition.hasWaiters();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1479
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1480
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1481
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1482
     * Returns an estimate of the number of threads waiting on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1483
     * given condition associated with this synchronizer. Note that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1484
     * because timeouts and interrupts may occur at any time, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1485
     * estimate serves only as an upper bound on the actual number of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1486
     * waiters.  This method is designed for use in monitoring of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1487
     * system state, not for synchronization control.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1488
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1489
     * @param condition the condition
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1490
     * @return the estimated number of waiting threads
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1491
     * @throws IllegalMonitorStateException if exclusive synchronization
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1492
     *         is not held
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1493
     * @throws IllegalArgumentException if the given condition is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1494
     *         not associated with this synchronizer
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1495
     * @throws NullPointerException if the condition is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1496
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1497
    public final int getWaitQueueLength(ConditionObject condition) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1498
        if (!owns(condition))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1499
            throw new IllegalArgumentException("Not owner");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1500
        return condition.getWaitQueueLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1501
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1502
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1503
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1504
     * Returns a collection containing those threads that may be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1505
     * waiting on the given condition associated with this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1506
     * synchronizer.  Because the actual set of threads may change
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1507
     * dynamically while constructing this result, the returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1508
     * collection is only a best-effort estimate. The elements of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1509
     * returned collection are in no particular order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1510
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1511
     * @param condition the condition
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1512
     * @return the collection of threads
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1513
     * @throws IllegalMonitorStateException if exclusive synchronization
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1514
     *         is not held
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1515
     * @throws IllegalArgumentException if the given condition is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1516
     *         not associated with this synchronizer
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1517
     * @throws NullPointerException if the condition is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1518
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1519
    public final Collection<Thread> getWaitingThreads(ConditionObject condition) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1520
        if (!owns(condition))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1521
            throw new IllegalArgumentException("Not owner");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1522
        return condition.getWaitingThreads();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1523
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1524
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1525
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1526
     * Condition implementation for a {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1527
     * AbstractQueuedLongSynchronizer} serving as the basis of a {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1528
     * Lock} implementation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1529
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1530
     * <p>Method documentation for this class describes mechanics,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1531
     * not behavioral specifications from the point of view of Lock
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1532
     * and Condition users. Exported versions of this class will in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1533
     * general need to be accompanied by documentation describing
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1534
     * condition semantics that rely on those of the associated
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1535
     * <tt>AbstractQueuedLongSynchronizer</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1536
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1537
     * <p>This class is Serializable, but all fields are transient,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1538
     * so deserialized conditions have no waiters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1539
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1540
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1541
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1542
    public class ConditionObject implements Condition, java.io.Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1543
        private static final long serialVersionUID = 1173984872572414699L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1544
        /** First node of condition queue. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1545
        private transient Node firstWaiter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1546
        /** Last node of condition queue. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1547
        private transient Node lastWaiter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1548
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1549
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1550
         * Creates a new <tt>ConditionObject</tt> instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1551
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1552
        public ConditionObject() { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1553
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1554
        // Internal methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1555
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1556
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1557
         * Adds a new waiter to wait queue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1558
         * @return its new wait node
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1559
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1560
        private Node addConditionWaiter() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1561
            Node t = lastWaiter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1562
            // If lastWaiter is cancelled, clean out.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1563
            if (t != null && t.waitStatus != Node.CONDITION) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1564
                unlinkCancelledWaiters();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1565
                t = lastWaiter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1566
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1567
            Node node = new Node(Thread.currentThread(), Node.CONDITION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1568
            if (t == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1569
                firstWaiter = node;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1570
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1571
                t.nextWaiter = node;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1572
            lastWaiter = node;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1573
            return node;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1574
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1575
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1576
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1577
         * Removes and transfers nodes until hit non-cancelled one or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1578
         * null. Split out from signal in part to encourage compilers
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1579
         * to inline the case of no waiters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1580
         * @param first (non-null) the first node on condition queue
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1581
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1582
        private void doSignal(Node first) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1583
            do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1584
                if ( (firstWaiter = first.nextWaiter) == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1585
                    lastWaiter = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1586
                first.nextWaiter = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1587
            } while (!transferForSignal(first) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1588
                     (first = firstWaiter) != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1589
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1590
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1591
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1592
         * Removes and transfers all nodes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1593
         * @param first (non-null) the first node on condition queue
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1594
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1595
        private void doSignalAll(Node first) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1596
            lastWaiter = firstWaiter = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1597
            do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1598
                Node next = first.nextWaiter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1599
                first.nextWaiter = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1600
                transferForSignal(first);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1601
                first = next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1602
            } while (first != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1603
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1604
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1605
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1606
         * Unlinks cancelled waiter nodes from condition queue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1607
         * Called only while holding lock. This is called when
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1608
         * cancellation occurred during condition wait, and upon
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1609
         * insertion of a new waiter when lastWaiter is seen to have
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1610
         * been cancelled. This method is needed to avoid garbage
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1611
         * retention in the absence of signals. So even though it may
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1612
         * require a full traversal, it comes into play only when
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1613
         * timeouts or cancellations occur in the absence of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1614
         * signals. It traverses all nodes rather than stopping at a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1615
         * particular target to unlink all pointers to garbage nodes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1616
         * without requiring many re-traversals during cancellation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1617
         * storms.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1618
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1619
        private void unlinkCancelledWaiters() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1620
            Node t = firstWaiter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1621
            Node trail = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1622
            while (t != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1623
                Node next = t.nextWaiter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1624
                if (t.waitStatus != Node.CONDITION) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1625
                    t.nextWaiter = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1626
                    if (trail == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1627
                        firstWaiter = next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1628
                    else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1629
                        trail.nextWaiter = next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1630
                    if (next == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1631
                        lastWaiter = trail;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1632
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1633
                else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1634
                    trail = t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1635
                t = next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1636
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1637
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1638
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1639
        // public methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1640
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1641
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1642
         * Moves the longest-waiting thread, if one exists, from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1643
         * wait queue for this condition to the wait queue for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1644
         * owning lock.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1645
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1646
         * @throws IllegalMonitorStateException if {@link #isHeldExclusively}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1647
         *         returns {@code false}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1648
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1649
        public final void signal() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1650
            if (!isHeldExclusively())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1651
                throw new IllegalMonitorStateException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1652
            Node first = firstWaiter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1653
            if (first != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1654
                doSignal(first);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1655
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1656
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1657
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1658
         * Moves all threads from the wait queue for this condition to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1659
         * the wait queue for the owning lock.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1660
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1661
         * @throws IllegalMonitorStateException if {@link #isHeldExclusively}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1662
         *         returns {@code false}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1663
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1664
        public final void signalAll() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1665
            if (!isHeldExclusively())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1666
                throw new IllegalMonitorStateException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1667
            Node first = firstWaiter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1668
            if (first != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1669
                doSignalAll(first);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1670
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1671
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1672
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1673
         * Implements uninterruptible condition wait.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1674
         * <ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1675
         * <li> Save lock state returned by {@link #getState}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1676
         * <li> Invoke {@link #release} with
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1677
         *      saved state as argument, throwing
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1678
         *      IllegalMonitorStateException if it fails.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1679
         * <li> Block until signalled.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1680
         * <li> Reacquire by invoking specialized version of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1681
         *      {@link #acquire} with saved state as argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1682
         * </ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1683
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1684
        public final void awaitUninterruptibly() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1685
            Node node = addConditionWaiter();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1686
            long savedState = fullyRelease(node);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1687
            boolean interrupted = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1688
            while (!isOnSyncQueue(node)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1689
                LockSupport.park(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1690
                if (Thread.interrupted())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1691
                    interrupted = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1692
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1693
            if (acquireQueued(node, savedState) || interrupted)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1694
                selfInterrupt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1695
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1696
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1697
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1698
         * For interruptible waits, we need to track whether to throw
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1699
         * InterruptedException, if interrupted while blocked on
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1700
         * condition, versus reinterrupt current thread, if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1701
         * interrupted while blocked waiting to re-acquire.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1702
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1703
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1704
        /** Mode meaning to reinterrupt on exit from wait */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1705
        private static final int REINTERRUPT =  1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1706
        /** Mode meaning to throw InterruptedException on exit from wait */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1707
        private static final int THROW_IE    = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1708
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1709
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1710
         * Checks for interrupt, returning THROW_IE if interrupted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1711
         * before signalled, REINTERRUPT if after signalled, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1712
         * 0 if not interrupted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1713
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1714
        private int checkInterruptWhileWaiting(Node node) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1715
            return Thread.interrupted() ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1716
                (transferAfterCancelledWait(node) ? THROW_IE : REINTERRUPT) :
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1717
                0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1718
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1719
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1720
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1721
         * Throws InterruptedException, reinterrupts current thread, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1722
         * does nothing, depending on mode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1723
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1724
        private void reportInterruptAfterWait(int interruptMode)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1725
            throws InterruptedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1726
            if (interruptMode == THROW_IE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1727
                throw new InterruptedException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1728
            else if (interruptMode == REINTERRUPT)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1729
                selfInterrupt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1730
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1731
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1732
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1733
         * Implements interruptible condition wait.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1734
         * <ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1735
         * <li> If current thread is interrupted, throw InterruptedException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1736
         * <li> Save lock state returned by {@link #getState}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1737
         * <li> Invoke {@link #release} with
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1738
         *      saved state as argument, throwing
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1739
         *      IllegalMonitorStateException if it fails.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1740
         * <li> Block until signalled or interrupted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1741
         * <li> Reacquire by invoking specialized version of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1742
         *      {@link #acquire} with saved state as argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1743
         * <li> If interrupted while blocked in step 4, throw InterruptedException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1744
         * </ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1745
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1746
        public final void await() throws InterruptedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1747
            if (Thread.interrupted())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1748
                throw new InterruptedException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1749
            Node node = addConditionWaiter();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1750
            long savedState = fullyRelease(node);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1751
            int interruptMode = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1752
            while (!isOnSyncQueue(node)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1753
                LockSupport.park(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1754
                if ((interruptMode = checkInterruptWhileWaiting(node)) != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1755
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1756
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1757
            if (acquireQueued(node, savedState) && interruptMode != THROW_IE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1758
                interruptMode = REINTERRUPT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1759
            if (node.nextWaiter != null) // clean up if cancelled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1760
                unlinkCancelledWaiters();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1761
            if (interruptMode != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1762
                reportInterruptAfterWait(interruptMode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1763
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1764
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1765
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1766
         * Implements timed condition wait.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1767
         * <ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1768
         * <li> If current thread is interrupted, throw InterruptedException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1769
         * <li> Save lock state returned by {@link #getState}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1770
         * <li> Invoke {@link #release} with
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1771
         *      saved state as argument, throwing
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1772
         *      IllegalMonitorStateException if it fails.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1773
         * <li> Block until signalled, interrupted, or timed out.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1774
         * <li> Reacquire by invoking specialized version of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1775
         *      {@link #acquire} with saved state as argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1776
         * <li> If interrupted while blocked in step 4, throw InterruptedException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1777
         * </ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1778
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1779
        public final long awaitNanos(long nanosTimeout) throws InterruptedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1780
            if (Thread.interrupted())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1781
                throw new InterruptedException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1782
            Node node = addConditionWaiter();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1783
            long savedState = fullyRelease(node);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1784
            long lastTime = System.nanoTime();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1785
            int interruptMode = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1786
            while (!isOnSyncQueue(node)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1787
                if (nanosTimeout <= 0L) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1788
                    transferAfterCancelledWait(node);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1789
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1790
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1791
                LockSupport.parkNanos(this, nanosTimeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1792
                if ((interruptMode = checkInterruptWhileWaiting(node)) != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1793
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1794
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1795
                long now = System.nanoTime();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1796
                nanosTimeout -= now - lastTime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1797
                lastTime = now;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1798
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1799
            if (acquireQueued(node, savedState) && interruptMode != THROW_IE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1800
                interruptMode = REINTERRUPT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1801
            if (node.nextWaiter != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1802
                unlinkCancelledWaiters();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1803
            if (interruptMode != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1804
                reportInterruptAfterWait(interruptMode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1805
            return nanosTimeout - (System.nanoTime() - lastTime);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1806
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1807
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1808
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1809
         * Implements absolute timed condition wait.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1810
         * <ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1811
         * <li> If current thread is interrupted, throw InterruptedException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1812
         * <li> Save lock state returned by {@link #getState}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1813
         * <li> Invoke {@link #release} with
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1814
         *      saved state as argument, throwing
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1815
         *      IllegalMonitorStateException if it fails.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1816
         * <li> Block until signalled, interrupted, or timed out.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1817
         * <li> Reacquire by invoking specialized version of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1818
         *      {@link #acquire} with saved state as argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1819
         * <li> If interrupted while blocked in step 4, throw InterruptedException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1820
         * <li> If timed out while blocked in step 4, return false, else true.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1821
         * </ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1822
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1823
        public final boolean awaitUntil(Date deadline) throws InterruptedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1824
            if (deadline == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1825
                throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1826
            long abstime = deadline.getTime();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1827
            if (Thread.interrupted())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1828
                throw new InterruptedException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1829
            Node node = addConditionWaiter();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1830
            long savedState = fullyRelease(node);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1831
            boolean timedout = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1832
            int interruptMode = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1833
            while (!isOnSyncQueue(node)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1834
                if (System.currentTimeMillis() > abstime) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1835
                    timedout = transferAfterCancelledWait(node);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1836
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1837
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1838
                LockSupport.parkUntil(this, abstime);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1839
                if ((interruptMode = checkInterruptWhileWaiting(node)) != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1840
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1841
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1842
            if (acquireQueued(node, savedState) && interruptMode != THROW_IE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1843
                interruptMode = REINTERRUPT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1844
            if (node.nextWaiter != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1845
                unlinkCancelledWaiters();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1846
            if (interruptMode != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1847
                reportInterruptAfterWait(interruptMode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1848
            return !timedout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1849
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1850
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1851
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1852
         * Implements timed condition wait.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1853
         * <ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1854
         * <li> If current thread is interrupted, throw InterruptedException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1855
         * <li> Save lock state returned by {@link #getState}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1856
         * <li> Invoke {@link #release} with
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1857
         *      saved state as argument, throwing
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1858
         *      IllegalMonitorStateException if it fails.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1859
         * <li> Block until signalled, interrupted, or timed out.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1860
         * <li> Reacquire by invoking specialized version of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1861
         *      {@link #acquire} with saved state as argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1862
         * <li> If interrupted while blocked in step 4, throw InterruptedException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1863
         * <li> If timed out while blocked in step 4, return false, else true.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1864
         * </ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1865
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1866
        public final boolean await(long time, TimeUnit unit) throws InterruptedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1867
            if (unit == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1868
                throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1869
            long nanosTimeout = unit.toNanos(time);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1870
            if (Thread.interrupted())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1871
                throw new InterruptedException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1872
            Node node = addConditionWaiter();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1873
            long savedState = fullyRelease(node);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1874
            long lastTime = System.nanoTime();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1875
            boolean timedout = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1876
            int interruptMode = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1877
            while (!isOnSyncQueue(node)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1878
                if (nanosTimeout <= 0L) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1879
                    timedout = transferAfterCancelledWait(node);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1880
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1881
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1882
                if (nanosTimeout >= spinForTimeoutThreshold)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1883
                    LockSupport.parkNanos(this, nanosTimeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1884
                if ((interruptMode = checkInterruptWhileWaiting(node)) != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1885
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1886
                long now = System.nanoTime();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1887
                nanosTimeout -= now - lastTime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1888
                lastTime = now;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1889
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1890
            if (acquireQueued(node, savedState) && interruptMode != THROW_IE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1891
                interruptMode = REINTERRUPT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1892
            if (node.nextWaiter != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1893
                unlinkCancelledWaiters();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1894
            if (interruptMode != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1895
                reportInterruptAfterWait(interruptMode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1896
            return !timedout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1897
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1898
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1899
        //  support for instrumentation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1900
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1901
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1902
         * Returns true if this condition was created by the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1903
         * synchronization object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1904
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1905
         * @return {@code true} if owned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1906
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1907
        final boolean isOwnedBy(AbstractQueuedLongSynchronizer sync) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1908
            return sync == AbstractQueuedLongSynchronizer.this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1909
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1910
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1911
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1912
         * Queries whether any threads are waiting on this condition.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1913
         * Implements {@link AbstractQueuedLongSynchronizer#hasWaiters}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1914
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1915
         * @return {@code true} if there are any waiting threads
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1916
         * @throws IllegalMonitorStateException if {@link #isHeldExclusively}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1917
         *         returns {@code false}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1918
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1919
        protected final boolean hasWaiters() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1920
            if (!isHeldExclusively())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1921
                throw new IllegalMonitorStateException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1922
            for (Node w = firstWaiter; w != null; w = w.nextWaiter) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1923
                if (w.waitStatus == Node.CONDITION)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1924
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1925
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1926
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1927
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1928
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1929
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1930
         * Returns an estimate of the number of threads waiting on
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1931
         * this condition.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1932
         * Implements {@link AbstractQueuedLongSynchronizer#getWaitQueueLength}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1933
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1934
         * @return the estimated number of waiting threads
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1935
         * @throws IllegalMonitorStateException if {@link #isHeldExclusively}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1936
         *         returns {@code false}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1937
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1938
        protected final int getWaitQueueLength() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1939
            if (!isHeldExclusively())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1940
                throw new IllegalMonitorStateException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1941
            int n = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1942
            for (Node w = firstWaiter; w != null; w = w.nextWaiter) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1943
                if (w.waitStatus == Node.CONDITION)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1944
                    ++n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1945
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1946
            return n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1947
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1948
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1949
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1950
         * Returns a collection containing those threads that may be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1951
         * waiting on this Condition.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1952
         * Implements {@link AbstractQueuedLongSynchronizer#getWaitingThreads}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1953
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1954
         * @return the collection of threads
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1955
         * @throws IllegalMonitorStateException if {@link #isHeldExclusively}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1956
         *         returns {@code false}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1957
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1958
        protected final Collection<Thread> getWaitingThreads() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1959
            if (!isHeldExclusively())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1960
                throw new IllegalMonitorStateException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1961
            ArrayList<Thread> list = new ArrayList<Thread>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1962
            for (Node w = firstWaiter; w != null; w = w.nextWaiter) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1963
                if (w.waitStatus == Node.CONDITION) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1964
                    Thread t = w.thread;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1965
                    if (t != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1966
                        list.add(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1967
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1968
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1969
            return list;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1970
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1971
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1972
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1973
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1974
     * Setup to support compareAndSet. We need to natively implement
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1975
     * this here: For the sake of permitting future enhancements, we
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1976
     * cannot explicitly subclass AtomicLong, which would be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1977
     * efficient and useful otherwise. So, as the lesser of evils, we
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1978
     * natively implement using hotspot intrinsics API. And while we
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1979
     * are at it, we do the same for other CASable fields (which could
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1980
     * otherwise be done with atomic field updaters).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1981
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1982
    private static final Unsafe unsafe = Unsafe.getUnsafe();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1983
    private static final long stateOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1984
    private static final long headOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1985
    private static final long tailOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1986
    private static final long waitStatusOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1987
    private static final long nextOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1988
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1989
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1990
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1991
            stateOffset = unsafe.objectFieldOffset
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1992
                (AbstractQueuedLongSynchronizer.class.getDeclaredField("state"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1993
            headOffset = unsafe.objectFieldOffset
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1994
                (AbstractQueuedLongSynchronizer.class.getDeclaredField("head"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1995
            tailOffset = unsafe.objectFieldOffset
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1996
                (AbstractQueuedLongSynchronizer.class.getDeclaredField("tail"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1997
            waitStatusOffset = unsafe.objectFieldOffset
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1998
                (Node.class.getDeclaredField("waitStatus"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1999
            nextOffset = unsafe.objectFieldOffset
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2000
                (Node.class.getDeclaredField("next"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2001
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2002
        } catch (Exception ex) { throw new Error(ex); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2003
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2004
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2005
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2006
     * CAS head field. Used only by enq.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2007
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2008
    private final boolean compareAndSetHead(Node update) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2009
        return unsafe.compareAndSwapObject(this, headOffset, null, update);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2010
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2011
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2012
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2013
     * CAS tail field. Used only by enq.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2014
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2015
    private final boolean compareAndSetTail(Node expect, Node update) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2016
        return unsafe.compareAndSwapObject(this, tailOffset, expect, update);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2017
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2018
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2019
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2020
     * CAS waitStatus field of a node.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2021
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2022
    private final static boolean compareAndSetWaitStatus(Node node,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2023
                                                         int expect,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2024
                                                         int update) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2025
        return unsafe.compareAndSwapInt(node, waitStatusOffset,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2026
                                        expect, update);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2027
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2028
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2029
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2030
     * CAS next field of a node.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2031
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2032
    private final static boolean compareAndSetNext(Node node,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2033
                                                   Node expect,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2034
                                                   Node update) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2035
        return unsafe.compareAndSwapObject(node, nextOffset, expect, update);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2036
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2037
}