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