src/java.base/share/classes/java/util/concurrent/locks/AbstractQueuedSynchronizer.java
author dl
Tue, 16 Jan 2018 18:24:32 -0800
changeset 48540 221cf8307606
parent 48046 98801bd22f5b
child 49565 b5705ade8c8d
permissions -rw-r--r--
8191483: AbstractQueuedSynchronizer cancel/cancel race Reviewed-by: martin
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;
32990
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
    37
39725
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 35247
diff changeset
    38
import java.lang.invoke.MethodHandles;
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 35247
diff changeset
    39
import java.lang.invoke.VarHandle;
14325
622c473a21aa 8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents: 9242
diff changeset
    40
import java.util.ArrayList;
622c473a21aa 8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents: 9242
diff changeset
    41
import java.util.Collection;
622c473a21aa 8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents: 9242
diff changeset
    42
import java.util.Date;
32990
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
    43
import java.util.concurrent.TimeUnit;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * Provides a framework for implementing blocking locks and related
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * synchronizers (semaphores, events, etc) that rely on
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * first-in-first-out (FIFO) wait queues.  This class is designed to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * be a useful basis for most kinds of synchronizers that rely on a
18576
7a5c231327af 8019377: Sync j.u.c locks and atomic from 166 to tl
dl
parents: 14325
diff changeset
    50
 * single atomic {@code int} value to represent state. Subclasses
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * must define the protected methods that change this state, and which
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * define what that state means in terms of this object being acquired
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * or released.  Given these, the other methods in this class carry
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * out all queuing and blocking mechanics. Subclasses can maintain
18576
7a5c231327af 8019377: Sync j.u.c locks and atomic from 166 to tl
dl
parents: 14325
diff changeset
    55
 * other state fields, but only the atomically updated {@code int}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * value manipulated using methods {@link #getState}, {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * #setState} and {@link #compareAndSetState} is tracked with respect
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * to synchronization.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * <p>Subclasses should be defined as non-public internal helper
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * classes that are used to implement the synchronization properties
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * of their enclosing class.  Class
18576
7a5c231327af 8019377: Sync j.u.c locks and atomic from 166 to tl
dl
parents: 14325
diff changeset
    63
 * {@code AbstractQueuedSynchronizer} does not implement any
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * synchronization interface.  Instead it defines methods such as
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * {@link #acquireInterruptibly} that can be invoked as
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * appropriate by concrete locks and related synchronizers to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * implement their public methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * <p>This class supports either or both a default <em>exclusive</em>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * mode and a <em>shared</em> mode. When acquired in exclusive mode,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * attempted acquires by other threads cannot succeed. Shared mode
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * acquires by multiple threads may (but need not) succeed. This class
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * does not &quot;understand&quot; these differences except in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * mechanical sense that when a shared mode acquire succeeds, the next
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * waiting thread (if one exists) must also determine whether it can
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * acquire as well. Threads waiting in the different modes share the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * same FIFO queue. Usually, implementation subclasses support only
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * one of these modes, but both can come into play for example in a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * {@link ReadWriteLock}. Subclasses that support only exclusive or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * only shared modes need not define the methods supporting the unused mode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * <p>This class defines a nested {@link ConditionObject} class that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * can be used as a {@link Condition} implementation by subclasses
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * supporting exclusive mode for which method {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * #isHeldExclusively} reports whether synchronization is exclusively
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 * held with respect to the current thread, method {@link #release}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * invoked with the current {@link #getState} value fully releases
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 * this object, and {@link #acquire}, given this saved state value,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 * eventually restores this object to its previous acquired state.  No
18576
7a5c231327af 8019377: Sync j.u.c locks and atomic from 166 to tl
dl
parents: 14325
diff changeset
    90
 * {@code AbstractQueuedSynchronizer} method otherwise creates such a
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 * condition, so if this constraint cannot be met, do not use it.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 * behavior of {@link ConditionObject} depends of course on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 * semantics of its synchronizer implementation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 * <p>This class provides inspection, instrumentation, and monitoring
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 * methods for the internal queue, as well as similar methods for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 * condition objects. These can be exported as desired into classes
18576
7a5c231327af 8019377: Sync j.u.c locks and atomic from 166 to tl
dl
parents: 14325
diff changeset
    98
 * using an {@code AbstractQueuedSynchronizer} for their
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 * synchronization mechanics.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 * <p>Serialization of this class stores only the underlying atomic
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 * integer maintaining state, so deserialized objects have empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 * thread queues. Typical subclasses requiring serializability will
18576
7a5c231327af 8019377: Sync j.u.c locks and atomic from 166 to tl
dl
parents: 14325
diff changeset
   104
 * define a {@code readObject} method that restores this to a known
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 * initial state upon deserialization.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 * <h3>Usage</h3>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
 * <p>To use this class as the basis of a synchronizer, redefine the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
 * following methods, as applicable, by inspecting and/or modifying
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
 * the synchronization state using {@link #getState}, {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
 * #setState} and/or {@link #compareAndSetState}:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
 * <ul>
32990
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
   115
 * <li>{@link #tryAcquire}
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
   116
 * <li>{@link #tryRelease}
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
   117
 * <li>{@link #tryAcquireShared}
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
   118
 * <li>{@link #tryReleaseShared}
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
   119
 * <li>{@link #isHeldExclusively}
18576
7a5c231327af 8019377: Sync j.u.c locks and atomic from 166 to tl
dl
parents: 14325
diff changeset
   120
 * </ul>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
 * Each of these methods by default throws {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
 * UnsupportedOperationException}.  Implementations of these methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
 * must be internally thread-safe, and should in general be short and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
 * not block. Defining these methods is the <em>only</em> supported
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
 * means of using this class. All other methods are declared
18576
7a5c231327af 8019377: Sync j.u.c locks and atomic from 166 to tl
dl
parents: 14325
diff changeset
   127
 * {@code final} because they cannot be independently varied.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
 * <p>You may also find the inherited methods from {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
 * AbstractOwnableSynchronizer} useful to keep track of the thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
 * owning an exclusive synchronizer.  You are encouraged to use them
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
 * -- this enables monitoring and diagnostic tools to assist users in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
 * determining which threads hold locks.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
 * <p>Even though this class is based on an internal FIFO queue, it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
 * does not automatically enforce FIFO acquisition policies.  The core
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
 * of exclusive synchronization takes the form:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
 * Acquire:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
 *     while (!tryAcquire(arg)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
 *        <em>enqueue thread if it is not already queued</em>;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
 *        <em>possibly block current thread</em>;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
 *     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
 * Release:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
 *     if (tryRelease(arg))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
 *        <em>unblock the first queued thread</em>;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
 * (Shared mode is similar but may involve cascading signals.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
 *
18576
7a5c231327af 8019377: Sync j.u.c locks and atomic from 166 to tl
dl
parents: 14325
diff changeset
   153
 * <p id="barging">Because checks in acquire are invoked before
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
 * enqueuing, a newly acquiring thread may <em>barge</em> ahead of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
 * others that are blocked and queued.  However, you can, if desired,
18576
7a5c231327af 8019377: Sync j.u.c locks and atomic from 166 to tl
dl
parents: 14325
diff changeset
   156
 * define {@code tryAcquire} and/or {@code tryAcquireShared} to
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
 * disable barging by internally invoking one or more of the inspection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
 * methods, thereby providing a <em>fair</em> FIFO acquisition order.
18576
7a5c231327af 8019377: Sync j.u.c locks and atomic from 166 to tl
dl
parents: 14325
diff changeset
   159
 * In particular, most fair synchronizers can define {@code tryAcquire}
7a5c231327af 8019377: Sync j.u.c locks and atomic from 166 to tl
dl
parents: 14325
diff changeset
   160
 * to return {@code false} if {@link #hasQueuedPredecessors} (a method
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
 * specifically designed to be used by fair synchronizers) returns
18576
7a5c231327af 8019377: Sync j.u.c locks and atomic from 166 to tl
dl
parents: 14325
diff changeset
   162
 * {@code true}.  Other variations are possible.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
 * <p>Throughput and scalability are generally highest for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
 * default barging (also known as <em>greedy</em>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
 * <em>renouncement</em>, and <em>convoy-avoidance</em>) strategy.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
 * While this is not guaranteed to be fair or starvation-free, earlier
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
 * queued threads are allowed to recontend before later queued
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
 * threads, and each recontention has an unbiased chance to succeed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
 * against incoming threads.  Also, while acquires do not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
 * &quot;spin&quot; in the usual sense, they may perform multiple
18576
7a5c231327af 8019377: Sync j.u.c locks and atomic from 166 to tl
dl
parents: 14325
diff changeset
   172
 * invocations of {@code tryAcquire} interspersed with other
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
 * computations before blocking.  This gives most of the benefits of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
 * spins when exclusive synchronization is only briefly held, without
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
 * most of the liabilities when it isn't. If so desired, you can
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
 * augment this by preceding calls to acquire methods with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
 * "fast-path" checks, possibly prechecking {@link #hasContended}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
 * and/or {@link #hasQueuedThreads} to only do so if the synchronizer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
 * is likely not to be contended.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
 * <p>This class provides an efficient and scalable basis for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
 * synchronization in part by specializing its range of use to
18576
7a5c231327af 8019377: Sync j.u.c locks and atomic from 166 to tl
dl
parents: 14325
diff changeset
   183
 * synchronizers that can rely on {@code int} state, acquire, and
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
 * release parameters, and an internal FIFO wait queue. When this does
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
 * not suffice, you can build synchronizers from a lower level using
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
 * {@link java.util.concurrent.atomic atomic} classes, your own custom
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
 * {@link java.util.Queue} classes, and {@link LockSupport} blocking
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
 * support.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
 * <h3>Usage Examples</h3>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
 * <p>Here is a non-reentrant mutual exclusion lock class that uses
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
 * the value zero to represent the unlocked state, and one to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
 * represent the locked state. While a non-reentrant lock
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
 * does not strictly require recording of the current owner
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
 * thread, this class does so anyway to make usage easier to monitor.
47302
f517fa4f4dc6 8187408: AbstractQueuedSynchronizer wait queue corrupted when thread awaits without holding the lock
dl
parents: 47216
diff changeset
   197
 * It also supports conditions and exposes some instrumentation methods:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
 *
32990
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
   199
 * <pre> {@code
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
 * class Mutex implements Lock, java.io.Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
 *   // Our internal helper class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
 *   private static class Sync extends AbstractQueuedSynchronizer {
18576
7a5c231327af 8019377: Sync j.u.c locks and atomic from 166 to tl
dl
parents: 14325
diff changeset
   204
 *     // Acquires the lock if state is zero
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
 *     public boolean tryAcquire(int acquires) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
 *       assert acquires == 1; // Otherwise unused
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
 *       if (compareAndSetState(0, 1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
 *         setExclusiveOwnerThread(Thread.currentThread());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
 *         return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
 *       }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
 *       return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
 *     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
 *
18576
7a5c231327af 8019377: Sync j.u.c locks and atomic from 166 to tl
dl
parents: 14325
diff changeset
   214
 *     // Releases the lock by setting state to zero
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
 *     protected boolean tryRelease(int releases) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
 *       assert releases == 1; // Otherwise unused
47302
f517fa4f4dc6 8187408: AbstractQueuedSynchronizer wait queue corrupted when thread awaits without holding the lock
dl
parents: 47216
diff changeset
   217
 *       if (!isHeldExclusively())
f517fa4f4dc6 8187408: AbstractQueuedSynchronizer wait queue corrupted when thread awaits without holding the lock
dl
parents: 47216
diff changeset
   218
 *         throw new IllegalMonitorStateException();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
 *       setExclusiveOwnerThread(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
 *       setState(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
 *       return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
 *     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
 *
47302
f517fa4f4dc6 8187408: AbstractQueuedSynchronizer wait queue corrupted when thread awaits without holding the lock
dl
parents: 47216
diff changeset
   224
 *     // Reports whether in locked state
f517fa4f4dc6 8187408: AbstractQueuedSynchronizer wait queue corrupted when thread awaits without holding the lock
dl
parents: 47216
diff changeset
   225
 *     public boolean isLocked() {
f517fa4f4dc6 8187408: AbstractQueuedSynchronizer wait queue corrupted when thread awaits without holding the lock
dl
parents: 47216
diff changeset
   226
 *       return getState() != 0;
f517fa4f4dc6 8187408: AbstractQueuedSynchronizer wait queue corrupted when thread awaits without holding the lock
dl
parents: 47216
diff changeset
   227
 *     }
f517fa4f4dc6 8187408: AbstractQueuedSynchronizer wait queue corrupted when thread awaits without holding the lock
dl
parents: 47216
diff changeset
   228
 *
f517fa4f4dc6 8187408: AbstractQueuedSynchronizer wait queue corrupted when thread awaits without holding the lock
dl
parents: 47216
diff changeset
   229
 *     public boolean isHeldExclusively() {
f517fa4f4dc6 8187408: AbstractQueuedSynchronizer wait queue corrupted when thread awaits without holding the lock
dl
parents: 47216
diff changeset
   230
 *       // a data race, but safe due to out-of-thin-air guarantees
f517fa4f4dc6 8187408: AbstractQueuedSynchronizer wait queue corrupted when thread awaits without holding the lock
dl
parents: 47216
diff changeset
   231
 *       return getExclusiveOwnerThread() == Thread.currentThread();
f517fa4f4dc6 8187408: AbstractQueuedSynchronizer wait queue corrupted when thread awaits without holding the lock
dl
parents: 47216
diff changeset
   232
 *     }
f517fa4f4dc6 8187408: AbstractQueuedSynchronizer wait queue corrupted when thread awaits without holding the lock
dl
parents: 47216
diff changeset
   233
 *
18576
7a5c231327af 8019377: Sync j.u.c locks and atomic from 166 to tl
dl
parents: 14325
diff changeset
   234
 *     // Provides a Condition
47302
f517fa4f4dc6 8187408: AbstractQueuedSynchronizer wait queue corrupted when thread awaits without holding the lock
dl
parents: 47216
diff changeset
   235
 *     public Condition newCondition() {
f517fa4f4dc6 8187408: AbstractQueuedSynchronizer wait queue corrupted when thread awaits without holding the lock
dl
parents: 47216
diff changeset
   236
 *       return new ConditionObject();
f517fa4f4dc6 8187408: AbstractQueuedSynchronizer wait queue corrupted when thread awaits without holding the lock
dl
parents: 47216
diff changeset
   237
 *     }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
 *
18576
7a5c231327af 8019377: Sync j.u.c locks and atomic from 166 to tl
dl
parents: 14325
diff changeset
   239
 *     // Deserializes properly
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
 *     private void readObject(ObjectInputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
 *         throws IOException, ClassNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
 *       s.defaultReadObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
 *       setState(0); // reset to unlocked state
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
 *     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
 *   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
 *   // The sync object does all the hard work. We just forward to it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
 *   private final Sync sync = new Sync();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
 *
47302
f517fa4f4dc6 8187408: AbstractQueuedSynchronizer wait queue corrupted when thread awaits without holding the lock
dl
parents: 47216
diff changeset
   250
 *   public void lock()              { sync.acquire(1); }
f517fa4f4dc6 8187408: AbstractQueuedSynchronizer wait queue corrupted when thread awaits without holding the lock
dl
parents: 47216
diff changeset
   251
 *   public boolean tryLock()        { return sync.tryAcquire(1); }
f517fa4f4dc6 8187408: AbstractQueuedSynchronizer wait queue corrupted when thread awaits without holding the lock
dl
parents: 47216
diff changeset
   252
 *   public void unlock()            { sync.release(1); }
f517fa4f4dc6 8187408: AbstractQueuedSynchronizer wait queue corrupted when thread awaits without holding the lock
dl
parents: 47216
diff changeset
   253
 *   public Condition newCondition() { return sync.newCondition(); }
f517fa4f4dc6 8187408: AbstractQueuedSynchronizer wait queue corrupted when thread awaits without holding the lock
dl
parents: 47216
diff changeset
   254
 *   public boolean isLocked()       { return sync.isLocked(); }
f517fa4f4dc6 8187408: AbstractQueuedSynchronizer wait queue corrupted when thread awaits without holding the lock
dl
parents: 47216
diff changeset
   255
 *   public boolean isHeldByCurrentThread() {
f517fa4f4dc6 8187408: AbstractQueuedSynchronizer wait queue corrupted when thread awaits without holding the lock
dl
parents: 47216
diff changeset
   256
 *     return sync.isHeldExclusively();
f517fa4f4dc6 8187408: AbstractQueuedSynchronizer wait queue corrupted when thread awaits without holding the lock
dl
parents: 47216
diff changeset
   257
 *   }
f517fa4f4dc6 8187408: AbstractQueuedSynchronizer wait queue corrupted when thread awaits without holding the lock
dl
parents: 47216
diff changeset
   258
 *   public boolean hasQueuedThreads() {
f517fa4f4dc6 8187408: AbstractQueuedSynchronizer wait queue corrupted when thread awaits without holding the lock
dl
parents: 47216
diff changeset
   259
 *     return sync.hasQueuedThreads();
f517fa4f4dc6 8187408: AbstractQueuedSynchronizer wait queue corrupted when thread awaits without holding the lock
dl
parents: 47216
diff changeset
   260
 *   }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
 *   public void lockInterruptibly() throws InterruptedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
 *     sync.acquireInterruptibly(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
 *   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
 *   public boolean tryLock(long timeout, TimeUnit unit)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
 *       throws InterruptedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
 *     return sync.tryAcquireNanos(1, unit.toNanos(timeout));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
 *   }
14325
622c473a21aa 8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents: 9242
diff changeset
   268
 * }}</pre>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
 *
14325
622c473a21aa 8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents: 9242
diff changeset
   270
 * <p>Here is a latch class that is like a
622c473a21aa 8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents: 9242
diff changeset
   271
 * {@link java.util.concurrent.CountDownLatch CountDownLatch}
18576
7a5c231327af 8019377: Sync j.u.c locks and atomic from 166 to tl
dl
parents: 14325
diff changeset
   272
 * except that it only requires a single {@code signal} to
7a5c231327af 8019377: Sync j.u.c locks and atomic from 166 to tl
dl
parents: 14325
diff changeset
   273
 * fire. Because a latch is non-exclusive, it uses the {@code shared}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
 * acquire and release methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
 *
32990
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
   276
 * <pre> {@code
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
 * class BooleanLatch {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
 *   private static class Sync extends AbstractQueuedSynchronizer {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
 *     boolean isSignalled() { return getState() != 0; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
 *     protected int tryAcquireShared(int ignore) {
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   283
 *       return isSignalled() ? 1 : -1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
 *     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
 *     protected boolean tryReleaseShared(int ignore) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
 *       setState(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
 *       return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
 *     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
 *   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
 *   private final Sync sync = new Sync();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
 *   public boolean isSignalled() { return sync.isSignalled(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
 *   public void signal()         { sync.releaseShared(1); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
 *   public void await() throws InterruptedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
 *     sync.acquireSharedInterruptibly(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
 *   }
14325
622c473a21aa 8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents: 9242
diff changeset
   298
 * }}</pre>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
 * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
 * @author Doug Lea
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
public abstract class AbstractQueuedSynchronizer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    extends AbstractOwnableSynchronizer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    implements java.io.Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    private static final long serialVersionUID = 7373984972572414691L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
    /**
18576
7a5c231327af 8019377: Sync j.u.c locks and atomic from 166 to tl
dl
parents: 14325
diff changeset
   310
     * Creates a new {@code AbstractQueuedSynchronizer} instance
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * with initial synchronization state of zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    protected AbstractQueuedSynchronizer() { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * Wait queue node class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     * <p>The wait queue is a variant of a "CLH" (Craig, Landin, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * Hagersten) lock queue. CLH locks are normally used for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * spinlocks.  We instead use them for blocking synchronizers, but
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     * use the same basic tactic of holding some of the control
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     * information about a thread in the predecessor of its node.  A
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     * "status" field in each node keeps track of whether a thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     * should block.  A node is signalled when its predecessor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     * releases.  Each node of the queue otherwise serves as a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     * specific-notification-style monitor holding a single waiting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     * thread. The status field does NOT control whether threads are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     * granted locks etc though.  A thread may try to acquire if it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * first in the queue. But being first does not guarantee success;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     * it only gives the right to contend.  So the currently released
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * contender thread may need to rewait.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     * <p>To enqueue into a CLH lock, you atomically splice it in as new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     * tail. To dequeue, you just set the head field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     *      +------+  prev +-----+       +-----+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     * head |      | <---- |     | <---- |     |  tail
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     *      +------+       +-----+       +-----+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     * <p>Insertion into a CLH queue requires only a single atomic
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     * 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
   343
     * demarcation from unqueued to queued. Similarly, dequeuing
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     * involves only updating the "head". However, it takes a bit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     * more work for nodes to determine who their successors are,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     * in part to deal with possible cancellation due to timeouts
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * and interrupts.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     * <p>The "prev" links (not used in original CLH locks), are mainly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     * needed to handle cancellation. If a node is cancelled, its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     * successor is (normally) relinked to a non-cancelled
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     * predecessor. For explanation of similar mechanics in the case
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     * of spin locks, see the papers by Scott and Scherer at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     * http://www.cs.rochester.edu/u/scott/synchronization/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     * <p>We also use "next" links to implement blocking mechanics.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     * The thread id for each node is kept in its own node, so a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     * predecessor signals the next node to wake up by traversing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * next link to determine which thread it is.  Determination of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     * successor must avoid races with newly queued nodes to set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     * the "next" fields of their predecessors.  This is solved
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     * when necessary by checking backwards from the atomically
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     * updated "tail" when a node's successor appears to be null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     * (Or, said differently, the next-links are an optimization
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     * so that we don't usually need a backward scan.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     * <p>Cancellation introduces some conservatism to the basic
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     * algorithms.  Since we must poll for cancellation of other
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     * nodes, we can miss noticing whether a cancelled node is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     * ahead or behind us. This is dealt with by always unparking
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     * successors upon cancellation, allowing them to stabilize on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     * a new predecessor, unless we can identify an uncancelled
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     * predecessor who will carry this responsibility.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     * <p>CLH queues need a dummy header node to get started. But
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     * we don't create them on construction, because it would be wasted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     * effort if there is never contention. Instead, the node
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     * is constructed and head and tail pointers are set upon first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     * contention.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     * <p>Threads waiting on Conditions use the same nodes, but
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     * use an additional link. Conditions only need to link nodes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     * in simple (non-concurrent) linked queues because they are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     * only accessed when exclusively held.  Upon await, a node is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     * inserted into a condition queue.  Upon signal, the node is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     * transferred to the main queue.  A special value of status
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     * field is used to mark which queue a node is on.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     * <p>Thanks go to Dave Dice, Mark Moir, Victor Luchangco, Bill
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     * Scherer and Michael Scott, along with members of JSR-166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     * expert group, for helpful ideas, discussions, and critiques
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     * on the design of this class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
    static final class Node {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        /** Marker to indicate a node is waiting in shared mode */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        static final Node SHARED = new Node();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        /** Marker to indicate a node is waiting in exclusive mode */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        static final Node EXCLUSIVE = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
32990
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
   400
        /** waitStatus value to indicate thread has cancelled. */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        static final int CANCELLED =  1;
32990
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
   402
        /** waitStatus value to indicate successor's thread needs unparking. */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        static final int SIGNAL    = -1;
32990
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
   404
        /** waitStatus value to indicate thread is waiting on condition. */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        static final int CONDITION = -2;
2430
975a98680ae8 6801020: Concurrent Semaphore release may cause some require thread not signaled
dl
parents: 2165
diff changeset
   406
        /**
975a98680ae8 6801020: Concurrent Semaphore release may cause some require thread not signaled
dl
parents: 2165
diff changeset
   407
         * waitStatus value to indicate the next acquireShared should
32990
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
   408
         * unconditionally propagate.
2430
975a98680ae8 6801020: Concurrent Semaphore release may cause some require thread not signaled
dl
parents: 2165
diff changeset
   409
         */
975a98680ae8 6801020: Concurrent Semaphore release may cause some require thread not signaled
dl
parents: 2165
diff changeset
   410
        static final int PROPAGATE = -3;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
         * Status field, taking on only the values:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
         *   SIGNAL:     The successor of this node is (or will soon be)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
         *               blocked (via park), so the current node must
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
         *               unpark its successor when it releases or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
         *               cancels. To avoid races, acquire methods must
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
         *               first indicate they need a signal,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
         *               then retry the atomic acquire, and then,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
         *               on failure, block.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
         *   CANCELLED:  This node is cancelled due to timeout or interrupt.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
         *               Nodes never leave this state. In particular,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
         *               a thread with cancelled node never again blocks.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
         *   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
   425
         *               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
   426
         *               until transferred, at which time the status
975a98680ae8 6801020: Concurrent Semaphore release may cause some require thread not signaled
dl
parents: 2165
diff changeset
   427
         *               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
   428
         *               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
   429
         *               field, but simplifies mechanics.)
975a98680ae8 6801020: Concurrent Semaphore release may cause some require thread not signaled
dl
parents: 2165
diff changeset
   430
         *   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
   431
         *               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
   432
         *               doReleaseShared to ensure propagation
975a98680ae8 6801020: Concurrent Semaphore release may cause some require thread not signaled
dl
parents: 2165
diff changeset
   433
         *               continues, even if other operations have
975a98680ae8 6801020: Concurrent Semaphore release may cause some require thread not signaled
dl
parents: 2165
diff changeset
   434
         *               since intervened.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
         *   0:          None of the above
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
         * The values are arranged numerically to simplify use.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
         * Non-negative values mean that a node doesn't need to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
         * signal. So, most code doesn't need to check for particular
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
         * values, just for sign.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
         * The field is initialized to 0 for normal sync nodes, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
         * CONDITION for condition nodes.  It is modified using CAS
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
         * (or when possible, unconditional volatile writes).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
        volatile int waitStatus;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
         * 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
   450
         * for checking waitStatus. Assigned during enqueuing, and nulled
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
         * out (for sake of GC) only upon dequeuing.  Also, upon
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
         * cancellation of a predecessor, we short-circuit while
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
         * finding a non-cancelled one, which will always exist
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
         * because the head node is never cancelled: A node becomes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
         * head only as a result of successful acquire. A
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
         * cancelled thread never succeeds in acquiring, and a thread only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
         * cancels itself, not any other node.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        volatile Node prev;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
         * Link to the successor node that the current node/thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
         * unparks upon release. Assigned during enqueuing, adjusted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
         * when bypassing cancelled predecessors, and nulled out (for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
         * sake of GC) when dequeued.  The enq operation does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
         * assign next field of a predecessor until after attachment,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
         * so seeing a null next field does not necessarily mean that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
         * node is at end of queue. However, if a next field appears
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
         * to be null, we can scan prev's from the tail to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
         * double-check.  The next field of cancelled nodes is set to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
         * point to the node itself instead of null, to make life
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
         * easier for isOnSyncQueue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        volatile Node next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
         * The thread that enqueued this node.  Initialized on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
         * construction and nulled out after use.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
        volatile Thread thread;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
         * Link to next node waiting on condition, or the special
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
         * value SHARED.  Because condition queues are accessed only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
         * when holding in exclusive mode, we just need a simple
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
         * linked queue to hold nodes while they are waiting on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
         * conditions. They are then transferred to the queue to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
         * re-acquire. And because conditions can only be exclusive,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
         * we save a field by using special value to indicate shared
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
         * mode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
        Node nextWaiter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
        /**
18576
7a5c231327af 8019377: Sync j.u.c locks and atomic from 166 to tl
dl
parents: 14325
diff changeset
   495
         * Returns true if node is waiting in shared mode.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
        final boolean isShared() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
            return nextWaiter == SHARED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
         * Returns previous node, or throws NullPointerException if null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
         * Use when predecessor cannot be null.  The null check could
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
         * be elided, but is present to help the VM.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
         * @return the predecessor of this node
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
         */
48046
98801bd22f5b 8191937: Lost interrupt in AbstractQueuedSynchronizer when tryAcquire methods throw
dl
parents: 47302
diff changeset
   508
        final Node predecessor() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
            Node p = prev;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
            if (p == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
                throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
                return p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
32990
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
   516
        /** Establishes initial head or SHARED marker. */
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
   517
        Node() {}
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
   518
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
   519
        /** Constructor used by addWaiter. */
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
   520
        Node(Node nextWaiter) {
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
   521
            this.nextWaiter = nextWaiter;
39725
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 35247
diff changeset
   522
            THREAD.set(this, Thread.currentThread());
32990
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
   523
        }
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
   524
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
   525
        /** Constructor used by addConditionWaiter. */
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
   526
        Node(int waitStatus) {
39725
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 35247
diff changeset
   527
            WAITSTATUS.set(this, waitStatus);
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 35247
diff changeset
   528
            THREAD.set(this, Thread.currentThread());
32990
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
   529
        }
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
   530
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
   531
        /** CASes waitStatus field. */
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
   532
        final boolean compareAndSetWaitStatus(int expect, int update) {
39725
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 35247
diff changeset
   533
            return WAITSTATUS.compareAndSet(this, expect, update);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
32990
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
   536
        /** CASes next field. */
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
   537
        final boolean compareAndSetNext(Node expect, Node update) {
39725
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 35247
diff changeset
   538
            return NEXT.compareAndSet(this, expect, update);
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 35247
diff changeset
   539
        }
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 35247
diff changeset
   540
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 35247
diff changeset
   541
        final void setPrevRelaxed(Node p) {
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 35247
diff changeset
   542
            PREV.set(this, p);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
39725
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 35247
diff changeset
   545
        // VarHandle mechanics
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 35247
diff changeset
   546
        private static final VarHandle NEXT;
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 35247
diff changeset
   547
        private static final VarHandle PREV;
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 35247
diff changeset
   548
        private static final VarHandle THREAD;
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 35247
diff changeset
   549
        private static final VarHandle WAITSTATUS;
32990
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
   550
        static {
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
   551
            try {
39725
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 35247
diff changeset
   552
                MethodHandles.Lookup l = MethodHandles.lookup();
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 35247
diff changeset
   553
                NEXT = l.findVarHandle(Node.class, "next", Node.class);
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 35247
diff changeset
   554
                PREV = l.findVarHandle(Node.class, "prev", Node.class);
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 35247
diff changeset
   555
                THREAD = l.findVarHandle(Node.class, "thread", Thread.class);
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 35247
diff changeset
   556
                WAITSTATUS = l.findVarHandle(Node.class, "waitStatus", int.class);
32990
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
   557
            } catch (ReflectiveOperationException e) {
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
   558
                throw new Error(e);
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
   559
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
        }
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
     * Head of the wait queue, lazily initialized.  Except for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
     * initialization, it is modified only via method setHead.  Note:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
     * If head exists, its waitStatus is guaranteed not to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
     * CANCELLED.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
    private transient volatile Node head;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
     * Tail of the wait queue, lazily initialized.  Modified only via
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
     * method enq to add new wait node.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
    private transient volatile Node tail;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
     * The synchronization state.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
    private volatile int state;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
     * 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
   584
     * This operation has memory semantics of a {@code volatile} read.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
     * @return current state value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
    protected final int getState() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
        return state;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
     * 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
   593
     * This operation has memory semantics of a {@code volatile} write.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
     * @param newState the new state value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
    protected final void setState(int newState) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
        state = newState;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
     * Atomically sets synchronization state to the given updated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
     * 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
   603
     * This operation has memory semantics of a {@code volatile} read
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
     * and write.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
     * @param expect the expected value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
     * @param update the new value
18576
7a5c231327af 8019377: Sync j.u.c locks and atomic from 166 to tl
dl
parents: 14325
diff changeset
   608
     * @return {@code true} if successful. False return indicates that the actual
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
     *         value was not equal to the expected value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
    protected final boolean compareAndSetState(int expect, int update) {
39725
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 35247
diff changeset
   612
        return STATE.compareAndSet(this, expect, update);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
    // Queuing utilities
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
     * The number of nanoseconds for which it is faster to spin
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
     * rather than to use timed park. A rough estimate suffices
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
     * to improve responsiveness with very short timeouts.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
     */
32990
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
   622
    static final long SPIN_FOR_TIMEOUT_THRESHOLD = 1000L;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
     * Inserts node into queue, initializing if necessary. See picture above.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
     * @param node the node to insert
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
     * @return node's predecessor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
     */
32990
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
   629
    private Node enq(Node node) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
        for (;;) {
32990
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
   631
            Node oldTail = tail;
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
   632
            if (oldTail != null) {
39725
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 35247
diff changeset
   633
                node.setPrevRelaxed(oldTail);
32990
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
   634
                if (compareAndSetTail(oldTail, node)) {
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
   635
                    oldTail.next = node;
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
   636
                    return oldTail;
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
   637
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
            } else {
32990
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
   639
                initializeSyncQueue();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
     * Creates and enqueues node for current thread and given mode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
     * @param mode Node.EXCLUSIVE for exclusive, Node.SHARED for shared
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
     * @return the new node
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
    private Node addWaiter(Node mode) {
32990
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
   651
        Node node = new Node(mode);
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
   652
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
   653
        for (;;) {
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
   654
            Node oldTail = tail;
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
   655
            if (oldTail != null) {
39725
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 35247
diff changeset
   656
                node.setPrevRelaxed(oldTail);
32990
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
   657
                if (compareAndSetTail(oldTail, node)) {
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
   658
                    oldTail.next = node;
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
   659
                    return node;
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
   660
                }
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
   661
            } else {
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
   662
                initializeSyncQueue();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
     * Sets head of queue to be node, thus dequeuing. Called only by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
     * acquire methods.  Also nulls out unused fields for sake of GC
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
     * and to suppress unnecessary signals and traversals.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
     * @param node the node
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
    private void setHead(Node node) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
        head = node;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
        node.thread = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
        node.prev = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
     * Wakes up node's successor, if one exists.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
     * @param node the node
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
    private void unparkSuccessor(Node node) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
        /*
2430
975a98680ae8 6801020: Concurrent Semaphore release may cause some require thread not signaled
dl
parents: 2165
diff changeset
   687
         * 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
   688
         * 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
   689
         * fails or if status is changed by waiting thread.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
         */
2430
975a98680ae8 6801020: Concurrent Semaphore release may cause some require thread not signaled
dl
parents: 2165
diff changeset
   691
        int ws = node.waitStatus;
975a98680ae8 6801020: Concurrent Semaphore release may cause some require thread not signaled
dl
parents: 2165
diff changeset
   692
        if (ws < 0)
32990
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
   693
            node.compareAndSetWaitStatus(ws, 0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
         * Thread to unpark is held in successor, which is normally
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
         * just the next node.  But if cancelled or apparently null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
         * traverse backwards from tail to find the actual
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
         * non-cancelled successor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
        Node s = node.next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
        if (s == null || s.waitStatus > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
            s = null;
32990
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
   704
            for (Node p = tail; p != node && p != null; p = p.prev)
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
   705
                if (p.waitStatus <= 0)
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
   706
                    s = p;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
        if (s != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
            LockSupport.unpark(s.thread);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
    /**
18576
7a5c231327af 8019377: Sync j.u.c locks and atomic from 166 to tl
dl
parents: 14325
diff changeset
   713
     * 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
   714
     * 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
   715
     * 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
   716
     */
975a98680ae8 6801020: Concurrent Semaphore release may cause some require thread not signaled
dl
parents: 2165
diff changeset
   717
    private void doReleaseShared() {
975a98680ae8 6801020: Concurrent Semaphore release may cause some require thread not signaled
dl
parents: 2165
diff changeset
   718
        /*
975a98680ae8 6801020: Concurrent Semaphore release may cause some require thread not signaled
dl
parents: 2165
diff changeset
   719
         * 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
   720
         * 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
   721
         * 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
   722
         * 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
   723
         * ensure that upon release, propagation continues.
975a98680ae8 6801020: Concurrent Semaphore release may cause some require thread not signaled
dl
parents: 2165
diff changeset
   724
         * 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
   725
         * 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
   726
         * 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
   727
         * fails, if so rechecking.
975a98680ae8 6801020: Concurrent Semaphore release may cause some require thread not signaled
dl
parents: 2165
diff changeset
   728
         */
975a98680ae8 6801020: Concurrent Semaphore release may cause some require thread not signaled
dl
parents: 2165
diff changeset
   729
        for (;;) {
975a98680ae8 6801020: Concurrent Semaphore release may cause some require thread not signaled
dl
parents: 2165
diff changeset
   730
            Node h = head;
975a98680ae8 6801020: Concurrent Semaphore release may cause some require thread not signaled
dl
parents: 2165
diff changeset
   731
            if (h != null && h != tail) {
975a98680ae8 6801020: Concurrent Semaphore release may cause some require thread not signaled
dl
parents: 2165
diff changeset
   732
                int ws = h.waitStatus;
975a98680ae8 6801020: Concurrent Semaphore release may cause some require thread not signaled
dl
parents: 2165
diff changeset
   733
                if (ws == Node.SIGNAL) {
32990
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
   734
                    if (!h.compareAndSetWaitStatus(Node.SIGNAL, 0))
2430
975a98680ae8 6801020: Concurrent Semaphore release may cause some require thread not signaled
dl
parents: 2165
diff changeset
   735
                        continue;            // loop to recheck cases
975a98680ae8 6801020: Concurrent Semaphore release may cause some require thread not signaled
dl
parents: 2165
diff changeset
   736
                    unparkSuccessor(h);
975a98680ae8 6801020: Concurrent Semaphore release may cause some require thread not signaled
dl
parents: 2165
diff changeset
   737
                }
975a98680ae8 6801020: Concurrent Semaphore release may cause some require thread not signaled
dl
parents: 2165
diff changeset
   738
                else if (ws == 0 &&
32990
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
   739
                         !h.compareAndSetWaitStatus(0, Node.PROPAGATE))
2430
975a98680ae8 6801020: Concurrent Semaphore release may cause some require thread not signaled
dl
parents: 2165
diff changeset
   740
                    continue;                // loop on failed CAS
975a98680ae8 6801020: Concurrent Semaphore release may cause some require thread not signaled
dl
parents: 2165
diff changeset
   741
            }
975a98680ae8 6801020: Concurrent Semaphore release may cause some require thread not signaled
dl
parents: 2165
diff changeset
   742
            if (h == head)                   // loop if head changed
975a98680ae8 6801020: Concurrent Semaphore release may cause some require thread not signaled
dl
parents: 2165
diff changeset
   743
                break;
975a98680ae8 6801020: Concurrent Semaphore release may cause some require thread not signaled
dl
parents: 2165
diff changeset
   744
        }
975a98680ae8 6801020: Concurrent Semaphore release may cause some require thread not signaled
dl
parents: 2165
diff changeset
   745
    }
975a98680ae8 6801020: Concurrent Semaphore release may cause some require thread not signaled
dl
parents: 2165
diff changeset
   746
975a98680ae8 6801020: Concurrent Semaphore release may cause some require thread not signaled
dl
parents: 2165
diff changeset
   747
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
     * 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
   749
     * 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
   750
     * PROPAGATE status was set.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
     * @param node the node
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
     * @param propagate the return value from a tryAcquireShared
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
    private void setHeadAndPropagate(Node node, int propagate) {
2430
975a98680ae8 6801020: Concurrent Semaphore release may cause some require thread not signaled
dl
parents: 2165
diff changeset
   756
        Node h = head; // Record old head for check below
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
        setHead(node);
2430
975a98680ae8 6801020: Concurrent Semaphore release may cause some require thread not signaled
dl
parents: 2165
diff changeset
   758
        /*
975a98680ae8 6801020: Concurrent Semaphore release may cause some require thread not signaled
dl
parents: 2165
diff changeset
   759
         * Try to signal next queued node if:
975a98680ae8 6801020: Concurrent Semaphore release may cause some require thread not signaled
dl
parents: 2165
diff changeset
   760
         *   Propagation was indicated by caller,
20766
d94cb38041e6 7011859: java/util/concurrent/Semaphore/RacingReleases.java failing
dl
parents: 18576
diff changeset
   761
         *     or was recorded (as h.waitStatus either before
d94cb38041e6 7011859: java/util/concurrent/Semaphore/RacingReleases.java failing
dl
parents: 18576
diff changeset
   762
         *     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
   763
         *     (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
   764
         *      PROPAGATE status may transition to SIGNAL.)
975a98680ae8 6801020: Concurrent Semaphore release may cause some require thread not signaled
dl
parents: 2165
diff changeset
   765
         * and
975a98680ae8 6801020: Concurrent Semaphore release may cause some require thread not signaled
dl
parents: 2165
diff changeset
   766
         *   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
   767
         *     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
   768
         *
975a98680ae8 6801020: Concurrent Semaphore release may cause some require thread not signaled
dl
parents: 2165
diff changeset
   769
         * 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
   770
         * 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
   771
         * 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
   772
         * anyway.
975a98680ae8 6801020: Concurrent Semaphore release may cause some require thread not signaled
dl
parents: 2165
diff changeset
   773
         */
20766
d94cb38041e6 7011859: java/util/concurrent/Semaphore/RacingReleases.java failing
dl
parents: 18576
diff changeset
   774
        if (propagate > 0 || h == null || h.waitStatus < 0 ||
d94cb38041e6 7011859: java/util/concurrent/Semaphore/RacingReleases.java failing
dl
parents: 18576
diff changeset
   775
            (h = head) == null || h.waitStatus < 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
            Node s = node.next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
            if (s == null || s.isShared())
2430
975a98680ae8 6801020: Concurrent Semaphore release may cause some require thread not signaled
dl
parents: 2165
diff changeset
   778
                doReleaseShared();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
    // Utilities for various versions of acquire
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
     * Cancels an ongoing attempt to acquire.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
     * @param node the node
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
    private void cancelAcquire(Node node) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
        // Ignore if node doesn't exist
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
        if (node == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
        node.thread = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
        // Skip cancelled predecessors
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
        Node pred = node.prev;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
        while (pred.waitStatus > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
            node.prev = pred = pred.prev;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
2430
975a98680ae8 6801020: Concurrent Semaphore release may cause some require thread not signaled
dl
parents: 2165
diff changeset
   801
        // 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
   802
        // fail if not, in which case, we lost race vs another cancel
48540
221cf8307606 8191483: AbstractQueuedSynchronizer cancel/cancel race
dl
parents: 48046
diff changeset
   803
        // or signal, so no further action is necessary, although with
221cf8307606 8191483: AbstractQueuedSynchronizer cancel/cancel race
dl
parents: 48046
diff changeset
   804
        // a possibility that a cancelled node may transiently remain
221cf8307606 8191483: AbstractQueuedSynchronizer cancel/cancel race
dl
parents: 48046
diff changeset
   805
        // reachable.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
        Node predNext = pred.next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
2430
975a98680ae8 6801020: Concurrent Semaphore release may cause some require thread not signaled
dl
parents: 2165
diff changeset
   808
        // 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
   809
        // 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
   810
        // Before, we are free of interference from other threads.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
        node.waitStatus = Node.CANCELLED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
2430
975a98680ae8 6801020: Concurrent Semaphore release may cause some require thread not signaled
dl
parents: 2165
diff changeset
   813
        // If we are the tail, remove ourselves.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
        if (node == tail && compareAndSetTail(node, pred)) {
32990
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
   815
            pred.compareAndSetNext(predNext, null);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
        } else {
2430
975a98680ae8 6801020: Concurrent Semaphore release may cause some require thread not signaled
dl
parents: 2165
diff changeset
   817
            // 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
   818
            // 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
   819
            int ws;
975a98680ae8 6801020: Concurrent Semaphore release may cause some require thread not signaled
dl
parents: 2165
diff changeset
   820
            if (pred != head &&
975a98680ae8 6801020: Concurrent Semaphore release may cause some require thread not signaled
dl
parents: 2165
diff changeset
   821
                ((ws = pred.waitStatus) == Node.SIGNAL ||
32990
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
   822
                 (ws <= 0 && pred.compareAndSetWaitStatus(ws, Node.SIGNAL))) &&
2430
975a98680ae8 6801020: Concurrent Semaphore release may cause some require thread not signaled
dl
parents: 2165
diff changeset
   823
                pred.thread != null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
                Node next = node.next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
                if (next != null && next.waitStatus <= 0)
32990
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
   826
                    pred.compareAndSetNext(predNext, next);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
                unparkSuccessor(node);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
            node.next = node; // help GC
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
     * Checks and updates status for a node that failed to acquire.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
     * 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
   838
     * control in all acquire loops.  Requires that pred == node.prev.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
     * @param pred node's predecessor holding status
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
     * @param node the node
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
     * @return {@code true} if thread should block
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
    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
   845
        int ws = pred.waitStatus;
975a98680ae8 6801020: Concurrent Semaphore release may cause some require thread not signaled
dl
parents: 2165
diff changeset
   846
        if (ws == Node.SIGNAL)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
             * This node has already set status asking a release
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
             * to signal it, so it can safely park.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
            return true;
2430
975a98680ae8 6801020: Concurrent Semaphore release may cause some require thread not signaled
dl
parents: 2165
diff changeset
   852
        if (ws > 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
             * Predecessor was cancelled. Skip over predecessors and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
             * indicate retry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
            do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
                node.prev = pred = pred.prev;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
            } while (pred.waitStatus > 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
            pred.next = node;
2430
975a98680ae8 6801020: Concurrent Semaphore release may cause some require thread not signaled
dl
parents: 2165
diff changeset
   861
        } else {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
            /*
2430
975a98680ae8 6801020: Concurrent Semaphore release may cause some require thread not signaled
dl
parents: 2165
diff changeset
   863
             * 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
   864
             * 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
   865
             * retry to make sure it cannot acquire before parking.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
             */
32990
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
   867
            pred.compareAndSetWaitStatus(ws, Node.SIGNAL);
2430
975a98680ae8 6801020: Concurrent Semaphore release may cause some require thread not signaled
dl
parents: 2165
diff changeset
   868
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
     * Convenience method to interrupt current thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
     */
14325
622c473a21aa 8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents: 9242
diff changeset
   875
    static void selfInterrupt() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
        Thread.currentThread().interrupt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
    /**
32990
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
   880
     * Convenience method to park and then check if interrupted.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
     * @return {@code true} if interrupted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
    private final boolean parkAndCheckInterrupt() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
        LockSupport.park(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
        return Thread.interrupted();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
     * Various flavors of acquire, varying in exclusive/shared and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
     * control modes.  Each is mostly the same, but annoyingly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
     * different.  Only a little bit of factoring is possible due to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
     * interactions of exception mechanics (including ensuring that we
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
     * cancel if tryAcquire throws exception) and other control, at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
     * least not without hurting performance too much.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
     * Acquires in exclusive uninterruptible mode for thread already in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
     * queue. Used by condition wait methods as well as acquire.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
     * @param node the node
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
     * @param arg the acquire argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
     * @return {@code true} if interrupted while waiting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
    final boolean acquireQueued(final Node node, int arg) {
48046
98801bd22f5b 8191937: Lost interrupt in AbstractQueuedSynchronizer when tryAcquire methods throw
dl
parents: 47302
diff changeset
   907
        boolean interrupted = false;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
            for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
                final Node p = node.predecessor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
                if (p == head && tryAcquire(arg)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
                    setHead(node);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
                    p.next = null; // help GC
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
                    return interrupted;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
                }
48046
98801bd22f5b 8191937: Lost interrupt in AbstractQueuedSynchronizer when tryAcquire methods throw
dl
parents: 47302
diff changeset
   916
                if (shouldParkAfterFailedAcquire(p, node))
98801bd22f5b 8191937: Lost interrupt in AbstractQueuedSynchronizer when tryAcquire methods throw
dl
parents: 47302
diff changeset
   917
                    interrupted |= parkAndCheckInterrupt();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
            }
32990
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
   919
        } catch (Throwable t) {
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
   920
            cancelAcquire(node);
48046
98801bd22f5b 8191937: Lost interrupt in AbstractQueuedSynchronizer when tryAcquire methods throw
dl
parents: 47302
diff changeset
   921
            if (interrupted)
98801bd22f5b 8191937: Lost interrupt in AbstractQueuedSynchronizer when tryAcquire methods throw
dl
parents: 47302
diff changeset
   922
                selfInterrupt();
32990
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
   923
            throw t;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
     * Acquires in exclusive interruptible mode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
     * @param arg the acquire argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
    private void doAcquireInterruptibly(int arg)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
        throws InterruptedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
        final Node node = addWaiter(Node.EXCLUSIVE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
            for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
                final Node p = node.predecessor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
                if (p == head && tryAcquire(arg)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
                    setHead(node);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
                    p.next = null; // help GC
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
                if (shouldParkAfterFailedAcquire(p, node) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
                    parkAndCheckInterrupt())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
                    throw new InterruptedException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
            }
32990
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
   946
        } catch (Throwable t) {
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
   947
            cancelAcquire(node);
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
   948
            throw t;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
     * Acquires in exclusive timed mode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
     * @param arg the acquire argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
     * @param nanosTimeout max wait time
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
     * @return {@code true} if acquired
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
    private boolean doAcquireNanos(int arg, long nanosTimeout)
14325
622c473a21aa 8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents: 9242
diff changeset
   960
            throws InterruptedException {
622c473a21aa 8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents: 9242
diff changeset
   961
        if (nanosTimeout <= 0L)
622c473a21aa 8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents: 9242
diff changeset
   962
            return false;
622c473a21aa 8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents: 9242
diff changeset
   963
        final long deadline = System.nanoTime() + nanosTimeout;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
        final Node node = addWaiter(Node.EXCLUSIVE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
            for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
                final Node p = node.predecessor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
                if (p == head && tryAcquire(arg)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
                    setHead(node);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
                    p.next = null; // help GC
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
                }
14325
622c473a21aa 8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents: 9242
diff changeset
   973
                nanosTimeout = deadline - System.nanoTime();
32990
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
   974
                if (nanosTimeout <= 0L) {
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
   975
                    cancelAcquire(node);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
                    return false;
32990
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
   977
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
                if (shouldParkAfterFailedAcquire(p, node) &&
32990
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
   979
                    nanosTimeout > SPIN_FOR_TIMEOUT_THRESHOLD)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
                    LockSupport.parkNanos(this, nanosTimeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
                if (Thread.interrupted())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
                    throw new InterruptedException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
            }
32990
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
   984
        } catch (Throwable t) {
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
   985
            cancelAcquire(node);
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
   986
            throw t;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
     * Acquires in shared uninterruptible mode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
     * @param arg the acquire argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
    private void doAcquireShared(int arg) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
        final Node node = addWaiter(Node.SHARED);
48046
98801bd22f5b 8191937: Lost interrupt in AbstractQueuedSynchronizer when tryAcquire methods throw
dl
parents: 47302
diff changeset
   996
        boolean interrupted = false;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
            for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
                final Node p = node.predecessor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
                if (p == head) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
                    int r = tryAcquireShared(arg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
                    if (r >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
                        setHeadAndPropagate(node, r);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
                        p.next = null; // help GC
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
                        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
                }
48046
98801bd22f5b 8191937: Lost interrupt in AbstractQueuedSynchronizer when tryAcquire methods throw
dl
parents: 47302
diff changeset
  1008
                if (shouldParkAfterFailedAcquire(p, node))
98801bd22f5b 8191937: Lost interrupt in AbstractQueuedSynchronizer when tryAcquire methods throw
dl
parents: 47302
diff changeset
  1009
                    interrupted |= parkAndCheckInterrupt();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
            }
32990
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  1011
        } catch (Throwable t) {
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  1012
            cancelAcquire(node);
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  1013
            throw t;
48046
98801bd22f5b 8191937: Lost interrupt in AbstractQueuedSynchronizer when tryAcquire methods throw
dl
parents: 47302
diff changeset
  1014
        } finally {
98801bd22f5b 8191937: Lost interrupt in AbstractQueuedSynchronizer when tryAcquire methods throw
dl
parents: 47302
diff changeset
  1015
            if (interrupted)
98801bd22f5b 8191937: Lost interrupt in AbstractQueuedSynchronizer when tryAcquire methods throw
dl
parents: 47302
diff changeset
  1016
                selfInterrupt();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
     * Acquires in shared interruptible mode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
     * @param arg the acquire argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
    private void doAcquireSharedInterruptibly(int arg)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
        throws InterruptedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
        final Node node = addWaiter(Node.SHARED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
            for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
                final Node p = node.predecessor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
                if (p == head) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
                    int r = tryAcquireShared(arg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
                    if (r >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
                        setHeadAndPropagate(node, r);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
                        p.next = null; // help GC
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
                        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
                if (shouldParkAfterFailedAcquire(p, node) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
                    parkAndCheckInterrupt())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
                    throw new InterruptedException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
            }
32990
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  1042
        } catch (Throwable t) {
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  1043
            cancelAcquire(node);
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  1044
            throw t;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
        }
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 timed mode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
     * @param arg the acquire argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
     * @param nanosTimeout max wait time
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
     * @return {@code true} if acquired
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
    private boolean doAcquireSharedNanos(int arg, long nanosTimeout)
14325
622c473a21aa 8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents: 9242
diff changeset
  1056
            throws InterruptedException {
622c473a21aa 8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents: 9242
diff changeset
  1057
        if (nanosTimeout <= 0L)
622c473a21aa 8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents: 9242
diff changeset
  1058
            return false;
622c473a21aa 8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents: 9242
diff changeset
  1059
        final long deadline = System.nanoTime() + nanosTimeout;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
        final Node node = addWaiter(Node.SHARED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
            for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
                final Node p = node.predecessor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
                if (p == head) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
                    int r = tryAcquireShared(arg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
                    if (r >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
                        setHeadAndPropagate(node, r);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
                        p.next = null; // help GC
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
                        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
                }
14325
622c473a21aa 8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents: 9242
diff changeset
  1072
                nanosTimeout = deadline - System.nanoTime();
32990
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  1073
                if (nanosTimeout <= 0L) {
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  1074
                    cancelAcquire(node);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
                    return false;
32990
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  1076
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
                if (shouldParkAfterFailedAcquire(p, node) &&
32990
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  1078
                    nanosTimeout > SPIN_FOR_TIMEOUT_THRESHOLD)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
                    LockSupport.parkNanos(this, nanosTimeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
                if (Thread.interrupted())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
                    throw new InterruptedException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
            }
32990
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  1083
        } catch (Throwable t) {
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  1084
            cancelAcquire(node);
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  1085
            throw t;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
    // Main exported methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
     * Attempts to acquire in exclusive mode. This method should query
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
     * if the state of the object permits it to be acquired in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
     * exclusive mode, and if so to acquire it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
     * <p>This method is always invoked by the thread performing
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
     * acquire.  If this method reports failure, the acquire method
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
     * may queue the thread, if it is not already queued, until it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
     * signalled by a release from some other thread. This can be used
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
     * to implement method {@link Lock#tryLock()}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
     * <p>The default
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
     * implementation throws {@link UnsupportedOperationException}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
     * @param arg the acquire argument. This value is always the one
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
     *        passed to an acquire method, or is the value saved on entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
     *        to a condition wait.  The value is otherwise uninterpreted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
     *        and can represent anything you like.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
     * @return {@code true} if successful. Upon success, this object has
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
     *         been acquired.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
     * @throws IllegalMonitorStateException if acquiring would place this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
     *         synchronizer in an illegal state. This exception must be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
     *         thrown in a consistent fashion for synchronization to work
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
     *         correctly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
     * @throws UnsupportedOperationException if exclusive mode is not supported
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
    protected boolean tryAcquire(int arg) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
        throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
     * Attempts to set the state to reflect a release in exclusive
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
     * mode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
     * <p>This method is always invoked by the thread performing release.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
     * <p>The default implementation throws
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
     * {@link UnsupportedOperationException}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
     * @param arg the release argument. This value is always the one
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
     *        passed to a release method, or the current state value upon
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
     *        entry to a condition wait.  The value is otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
     *        uninterpreted and can represent anything you like.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
     * @return {@code true} if this object is now in a fully released
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
     *         state, so that any waiting threads may attempt to acquire;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
     *         and {@code false} otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
     * @throws IllegalMonitorStateException if releasing would place this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
     *         synchronizer in an illegal state. This exception must be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
     *         thrown in a consistent fashion for synchronization to work
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
     *         correctly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
     * @throws UnsupportedOperationException if exclusive mode is not supported
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
    protected boolean tryRelease(int arg) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
        throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
     * Attempts to acquire in shared mode. This method should query if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
     * the state of the object permits it to be acquired in the shared
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
     * mode, and if so to acquire it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
     * <p>This method is always invoked by the thread performing
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
     * acquire.  If this method reports failure, the acquire method
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
     * may queue the thread, if it is not already queued, until it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
     * signalled by a release from some other thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
     * <p>The default implementation throws {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
     * UnsupportedOperationException}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
     * @param arg the acquire argument. This value is always the one
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
     *        passed to an acquire method, or is the value saved on entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
     *        to a condition wait.  The value is otherwise uninterpreted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
     *        and can represent anything you like.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
     * @return a negative value on failure; zero if acquisition in shared
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
     *         mode succeeded but no subsequent shared-mode acquire can
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
     *         succeed; and a positive value if acquisition in shared
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
     *         mode succeeded and subsequent shared-mode acquires might
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
     *         also succeed, in which case a subsequent waiting thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
     *         must check availability. (Support for three different
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
     *         return values enables this method to be used in contexts
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
     *         where acquires only sometimes act exclusively.)  Upon
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
     *         success, this object has been acquired.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
     * @throws IllegalMonitorStateException if acquiring would place this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
     *         synchronizer in an illegal state. This exception must be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
     *         thrown in a consistent fashion for synchronization to work
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
     *         correctly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
     * @throws UnsupportedOperationException if shared mode is not supported
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
    protected int tryAcquireShared(int arg) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
        throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
     * Attempts to set the state to reflect a release in shared mode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
     * <p>This method is always invoked by the thread performing release.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
     * <p>The default implementation throws
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
     * {@link UnsupportedOperationException}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
     * @param arg the release argument. This value is always the one
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
     *        passed to a release method, or the current state value upon
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
     *        entry to a condition wait.  The value is otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
     *        uninterpreted and can represent anything you like.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
     * @return {@code true} if this release of shared mode may permit a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
     *         waiting acquire (shared or exclusive) to succeed; and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
     *         {@code false} otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
     * @throws IllegalMonitorStateException if releasing would place this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
     *         synchronizer in an illegal state. This exception must be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
     *         thrown in a consistent fashion for synchronization to work
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
     *         correctly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
     * @throws UnsupportedOperationException if shared mode is not supported
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
    protected boolean tryReleaseShared(int arg) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
        throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
     * Returns {@code true} if synchronization is held exclusively with
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
     * respect to the current (calling) thread.  This method is invoked
47302
f517fa4f4dc6 8187408: AbstractQueuedSynchronizer wait queue corrupted when thread awaits without holding the lock
dl
parents: 47216
diff changeset
  1211
     * upon each call to a {@link ConditionObject} method.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
     * <p>The default implementation throws {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
     * UnsupportedOperationException}. This method is invoked
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
     * internally only within {@link ConditionObject} methods, so need
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
     * not be defined if conditions are not used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
     * @return {@code true} if synchronization is held exclusively;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
     *         {@code false} otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
     * @throws UnsupportedOperationException if conditions are not supported
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
    protected boolean isHeldExclusively() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
        throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
     * Acquires in exclusive mode, ignoring interrupts.  Implemented
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
     * by invoking at least once {@link #tryAcquire},
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
     * returning on success.  Otherwise the thread is queued, possibly
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
     * repeatedly blocking and unblocking, invoking {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
     * #tryAcquire} until success.  This method can be used
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
     * to implement method {@link Lock#lock}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
     * @param arg the acquire argument.  This value is conveyed to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
     *        {@link #tryAcquire} but is otherwise uninterpreted and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
     *        can represent anything you like.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
    public final void acquire(int arg) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
        if (!tryAcquire(arg) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
            acquireQueued(addWaiter(Node.EXCLUSIVE), arg))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
            selfInterrupt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
     * Acquires in exclusive mode, aborting if interrupted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
     * Implemented by first checking interrupt status, then invoking
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
     * at least once {@link #tryAcquire}, returning on
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
     * success.  Otherwise the thread is queued, possibly repeatedly
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
     * blocking and unblocking, invoking {@link #tryAcquire}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
     * until success or the thread is interrupted.  This method can be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
     * used to implement method {@link Lock#lockInterruptibly}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
     * @param arg the acquire argument.  This value is conveyed to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
     *        {@link #tryAcquire} but is otherwise uninterpreted and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
     *        can represent anything you like.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
     * @throws InterruptedException if the current thread is interrupted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
     */
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
  1258
    public final void acquireInterruptibly(int arg)
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
  1259
            throws InterruptedException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
        if (Thread.interrupted())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
            throw new InterruptedException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
        if (!tryAcquire(arg))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
            doAcquireInterruptibly(arg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
     * Attempts to acquire in exclusive mode, aborting if interrupted,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
     * and failing if the given timeout elapses.  Implemented by first
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
     * checking interrupt status, then invoking at least once {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
     * #tryAcquire}, returning on success.  Otherwise, the thread is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
     * queued, possibly repeatedly blocking and unblocking, invoking
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
     * {@link #tryAcquire} until success or the thread is interrupted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
     * or the timeout elapses.  This method can be used to implement
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
     * method {@link Lock#tryLock(long, TimeUnit)}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
     * @param arg the acquire argument.  This value is conveyed to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
     *        {@link #tryAcquire} but is otherwise uninterpreted and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
     *        can represent anything you like.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
     * @param nanosTimeout the maximum number of nanoseconds to wait
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
     * @return {@code true} if acquired; {@code false} if timed out
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
     * @throws InterruptedException if the current thread is interrupted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
     */
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
  1283
    public final boolean tryAcquireNanos(int arg, long nanosTimeout)
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
  1284
            throws InterruptedException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
        if (Thread.interrupted())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
            throw new InterruptedException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
        return tryAcquire(arg) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
            doAcquireNanos(arg, nanosTimeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
     * Releases in exclusive mode.  Implemented by unblocking one or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
     * more threads if {@link #tryRelease} returns true.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
     * This method can be used to implement method {@link Lock#unlock}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
     * @param arg the release argument.  This value is conveyed to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
     *        {@link #tryRelease} but is otherwise uninterpreted and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
     *        can represent anything you like.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
     * @return the value returned from {@link #tryRelease}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
    public final boolean release(int arg) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
        if (tryRelease(arg)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
            Node h = head;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
            if (h != null && h.waitStatus != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
                unparkSuccessor(h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
     * Acquires in shared mode, ignoring interrupts.  Implemented by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
     * first invoking at least once {@link #tryAcquireShared},
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
     * returning on success.  Otherwise the thread is queued, possibly
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
     * repeatedly blocking and unblocking, invoking {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
     * #tryAcquireShared} until success.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
     * @param arg the acquire argument.  This value is conveyed to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
     *        {@link #tryAcquireShared} but is otherwise uninterpreted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
     *        and can represent anything you like.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
    public final void acquireShared(int arg) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
        if (tryAcquireShared(arg) < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
            doAcquireShared(arg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
     * Acquires in shared mode, aborting if interrupted.  Implemented
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
     * by first checking interrupt status, then invoking at least once
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
     * {@link #tryAcquireShared}, returning on success.  Otherwise the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
     * thread is queued, possibly repeatedly blocking and unblocking,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
     * invoking {@link #tryAcquireShared} until success or the thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
     * is interrupted.
18576
7a5c231327af 8019377: Sync j.u.c locks and atomic from 166 to tl
dl
parents: 14325
diff changeset
  1334
     * @param arg the acquire argument.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
     * This value is conveyed to {@link #tryAcquireShared} but is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
     * otherwise uninterpreted and can represent anything
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
     * you like.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
     * @throws InterruptedException if the current thread is interrupted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
     */
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
  1340
    public final void acquireSharedInterruptibly(int arg)
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
  1341
            throws InterruptedException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
        if (Thread.interrupted())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
            throw new InterruptedException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
        if (tryAcquireShared(arg) < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
            doAcquireSharedInterruptibly(arg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
     * Attempts to acquire in shared mode, aborting if interrupted, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
     * failing if the given timeout elapses.  Implemented by first
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
     * checking interrupt status, then invoking at least once {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
     * #tryAcquireShared}, returning on success.  Otherwise, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
     * thread is queued, possibly repeatedly blocking and unblocking,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
     * invoking {@link #tryAcquireShared} until success or the thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
     * is interrupted or the timeout elapses.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
     * @param arg the acquire argument.  This value is conveyed to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
     *        {@link #tryAcquireShared} but is otherwise uninterpreted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
     *        and can represent anything you like.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
     * @param nanosTimeout the maximum number of nanoseconds to wait
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
     * @return {@code true} if acquired; {@code false} if timed out
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
     * @throws InterruptedException if the current thread is interrupted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
     */
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
  1364
    public final boolean tryAcquireSharedNanos(int arg, long nanosTimeout)
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
  1365
            throws InterruptedException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
        if (Thread.interrupted())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
            throw new InterruptedException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
        return tryAcquireShared(arg) >= 0 ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
            doAcquireSharedNanos(arg, nanosTimeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
     * Releases in shared mode.  Implemented by unblocking one or more
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
     * threads if {@link #tryReleaseShared} returns true.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
     * @param arg the release argument.  This value is conveyed to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
     *        {@link #tryReleaseShared} but is otherwise uninterpreted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
     *        and can represent anything you like.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
     * @return the value returned from {@link #tryReleaseShared}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
    public final boolean releaseShared(int arg) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
        if (tryReleaseShared(arg)) {
2430
975a98680ae8 6801020: Concurrent Semaphore release may cause some require thread not signaled
dl
parents: 2165
diff changeset
  1383
            doReleaseShared();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
    // Queue inspection methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
     * Queries whether any threads are waiting to acquire. Note that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
     * because cancellations due to interrupts and timeouts may occur
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
     * at any time, a {@code true} return does not guarantee that any
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
     * other thread will ever acquire.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
     * @return {@code true} if there may be other threads waiting to acquire
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
    public final boolean hasQueuedThreads() {
48540
221cf8307606 8191483: AbstractQueuedSynchronizer cancel/cancel race
dl
parents: 48046
diff changeset
  1400
        for (Node p = tail, h = head; p != h && p != null; p = p.prev)
221cf8307606 8191483: AbstractQueuedSynchronizer cancel/cancel race
dl
parents: 48046
diff changeset
  1401
            if (p.waitStatus <= 0)
221cf8307606 8191483: AbstractQueuedSynchronizer cancel/cancel race
dl
parents: 48046
diff changeset
  1402
                return true;
221cf8307606 8191483: AbstractQueuedSynchronizer cancel/cancel race
dl
parents: 48046
diff changeset
  1403
        return false;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1406
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1407
     * Queries whether any threads have ever contended to acquire this
34348
ba5c2f2fc9d7 8143087: Miscellaneous changes imported from jsr166 CVS 2015-11
dl
parents: 33674
diff changeset
  1408
     * synchronizer; that is, if an acquire method has ever blocked.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1410
     * <p>In this implementation, this operation returns in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
     * constant time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1413
     * @return {@code true} if there has ever been contention
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1414
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
    public final boolean hasContended() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
        return head != null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1418
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
     * Returns the first (longest-waiting) thread in the queue, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1421
     * {@code null} if no threads are currently queued.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
     * <p>In this implementation, this operation normally returns in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
     * constant time, but may iterate upon contention if other threads are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1425
     * concurrently modifying the queue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1427
     * @return the first (longest-waiting) thread in the queue, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1428
     *         {@code null} if no threads are currently queued
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1429
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1430
    public final Thread getFirstQueuedThread() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1431
        // handle only fast path, else relay
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1432
        return (head == tail) ? null : fullGetFirstQueuedThread();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1433
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1434
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1435
    /**
32990
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  1436
     * Version of getFirstQueuedThread called when fastpath fails.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1437
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1438
    private Thread fullGetFirstQueuedThread() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1439
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1440
         * The first node is normally head.next. Try to get its
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1441
         * thread field, ensuring consistent reads: If thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1442
         * field is nulled out or s.prev is no longer head, then
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1443
         * some other thread(s) concurrently performed setHead in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1444
         * between some of our reads. We try this twice before
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1445
         * resorting to traversal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1446
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1447
        Node h, s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1448
        Thread st;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1449
        if (((h = head) != null && (s = h.next) != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1450
             s.prev == head && (st = s.thread) != null) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1451
            ((h = head) != null && (s = h.next) != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1452
             s.prev == head && (st = s.thread) != null))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1453
            return st;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1454
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1455
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1456
         * Head's next field might not have been set yet, or may have
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1457
         * been unset after setHead. So we must check to see if tail
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1458
         * is actually first node. If not, we continue on, safely
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1459
         * traversing from tail back to head to find first,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1460
         * guaranteeing termination.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1461
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1462
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1463
        Thread firstThread = null;
34348
ba5c2f2fc9d7 8143087: Miscellaneous changes imported from jsr166 CVS 2015-11
dl
parents: 33674
diff changeset
  1464
        for (Node p = tail; p != null && p != head; p = p.prev) {
ba5c2f2fc9d7 8143087: Miscellaneous changes imported from jsr166 CVS 2015-11
dl
parents: 33674
diff changeset
  1465
            Thread t = p.thread;
ba5c2f2fc9d7 8143087: Miscellaneous changes imported from jsr166 CVS 2015-11
dl
parents: 33674
diff changeset
  1466
            if (t != null)
ba5c2f2fc9d7 8143087: Miscellaneous changes imported from jsr166 CVS 2015-11
dl
parents: 33674
diff changeset
  1467
                firstThread = t;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1468
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1469
        return firstThread;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1470
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1471
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1472
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1473
     * Returns true if the given thread is currently queued.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1474
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1475
     * <p>This implementation traverses the queue to determine
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1476
     * presence of the given thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1477
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1478
     * @param thread the thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1479
     * @return {@code true} if the given thread is on the queue
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1480
     * @throws NullPointerException if the thread is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1481
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1482
    public final boolean isQueued(Thread thread) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1483
        if (thread == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1484
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1485
        for (Node p = tail; p != null; p = p.prev)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1486
            if (p.thread == thread)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1487
                return true;
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
     * Returns {@code true} if the apparent first queued thread, if one
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1493
     * exists, is waiting in exclusive mode.  If this method returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1494
     * {@code true}, and the current thread is attempting to acquire in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1495
     * shared mode (that is, this method is invoked from {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1496
     * #tryAcquireShared}) then it is guaranteed that the current thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1497
     * is not the first queued thread.  Used only as a heuristic in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1498
     * ReentrantReadWriteLock.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1499
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1500
    final boolean apparentlyFirstQueuedIsExclusive() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1501
        Node h, s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1502
        return (h = head) != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1503
            (s = h.next)  != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1504
            !s.isShared()         &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1505
            s.thread != null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1506
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1507
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1508
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1509
     * Queries whether any threads have been waiting to acquire longer
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1510
     * than the current thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1511
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1512
     * <p>An invocation of this method is equivalent to (but may be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1513
     * more efficient than):
32990
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  1514
     * <pre> {@code
34348
ba5c2f2fc9d7 8143087: Miscellaneous changes imported from jsr166 CVS 2015-11
dl
parents: 33674
diff changeset
  1515
     * getFirstQueuedThread() != Thread.currentThread()
ba5c2f2fc9d7 8143087: Miscellaneous changes imported from jsr166 CVS 2015-11
dl
parents: 33674
diff changeset
  1516
     *   && hasQueuedThreads()}</pre>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1517
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1518
     * <p>Note that because cancellations due to interrupts and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1519
     * timeouts may occur at any time, a {@code true} return does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1520
     * guarantee that some other thread will acquire before the current
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1521
     * thread.  Likewise, it is possible for another thread to win a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1522
     * race to enqueue after this method has returned {@code false},
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1523
     * due to the queue being empty.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1524
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1525
     * <p>This method is designed to be used by a fair synchronizer to
23020
5790862e31f8 7089615: Broken javadoc link in AbstractQueuedSynchronizer
chegar
parents: 20766
diff changeset
  1526
     * avoid <a href="AbstractQueuedSynchronizer.html#barging">barging</a>.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1527
     * Such a synchronizer's {@link #tryAcquire} method should return
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1528
     * {@code false}, and its {@link #tryAcquireShared} method should
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1529
     * return a negative value, if this method returns {@code true}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1530
     * (unless this is a reentrant acquire).  For example, the {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1531
     * tryAcquire} method for a fair, reentrant, exclusive mode
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1532
     * synchronizer might look like this:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1533
     *
32990
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  1534
     * <pre> {@code
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1535
     * protected boolean tryAcquire(int arg) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1536
     *   if (isHeldExclusively()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1537
     *     // A reentrant acquire; increment hold count
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1538
     *     return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1539
     *   } else if (hasQueuedPredecessors()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1540
     *     return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1541
     *   } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1542
     *     // try to acquire normally
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1543
     *   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1544
     * }}</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1545
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1546
     * @return {@code true} if there is a queued thread preceding the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1547
     *         current thread, and {@code false} if the current thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1548
     *         is at the head of the queue or the queue is empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1549
     * @since 1.7
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1550
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1551
    public final boolean hasQueuedPredecessors() {
48540
221cf8307606 8191483: AbstractQueuedSynchronizer cancel/cancel race
dl
parents: 48046
diff changeset
  1552
        Node h, s;
221cf8307606 8191483: AbstractQueuedSynchronizer cancel/cancel race
dl
parents: 48046
diff changeset
  1553
        if ((h = head) != null) {
221cf8307606 8191483: AbstractQueuedSynchronizer cancel/cancel race
dl
parents: 48046
diff changeset
  1554
            if ((s = h.next) == null || s.waitStatus > 0) {
221cf8307606 8191483: AbstractQueuedSynchronizer cancel/cancel race
dl
parents: 48046
diff changeset
  1555
                s = null; // traverse in case of concurrent cancellation
221cf8307606 8191483: AbstractQueuedSynchronizer cancel/cancel race
dl
parents: 48046
diff changeset
  1556
                for (Node p = tail; p != h && p != null; p = p.prev) {
221cf8307606 8191483: AbstractQueuedSynchronizer cancel/cancel race
dl
parents: 48046
diff changeset
  1557
                    if (p.waitStatus <= 0)
221cf8307606 8191483: AbstractQueuedSynchronizer cancel/cancel race
dl
parents: 48046
diff changeset
  1558
                        s = p;
221cf8307606 8191483: AbstractQueuedSynchronizer cancel/cancel race
dl
parents: 48046
diff changeset
  1559
                }
221cf8307606 8191483: AbstractQueuedSynchronizer cancel/cancel race
dl
parents: 48046
diff changeset
  1560
            }
221cf8307606 8191483: AbstractQueuedSynchronizer cancel/cancel race
dl
parents: 48046
diff changeset
  1561
            if (s != null && s.thread != Thread.currentThread())
221cf8307606 8191483: AbstractQueuedSynchronizer cancel/cancel race
dl
parents: 48046
diff changeset
  1562
                return true;
221cf8307606 8191483: AbstractQueuedSynchronizer cancel/cancel race
dl
parents: 48046
diff changeset
  1563
        }
221cf8307606 8191483: AbstractQueuedSynchronizer cancel/cancel race
dl
parents: 48046
diff changeset
  1564
        return false;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1565
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1566
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1567
    // Instrumentation and monitoring methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1568
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1569
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1570
     * Returns an estimate of the number of threads waiting to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1571
     * acquire.  The value is only an estimate because the number of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1572
     * threads may change dynamically while this method traverses
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1573
     * internal data structures.  This method is designed for use in
32990
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  1574
     * monitoring system state, not for synchronization control.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1575
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1576
     * @return the estimated number of threads waiting to acquire
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1577
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1578
    public final int getQueueLength() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1579
        int n = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1580
        for (Node p = tail; p != null; p = p.prev) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1581
            if (p.thread != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1582
                ++n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1583
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1584
        return n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1585
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1586
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1587
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1588
     * Returns a collection containing threads that may be waiting to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1589
     * acquire.  Because the actual set of threads may change
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1590
     * dynamically while constructing this result, the returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1591
     * collection is only a best-effort estimate.  The elements of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1592
     * returned collection are in no particular order.  This method is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1593
     * designed to facilitate construction of subclasses that provide
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1594
     * more extensive monitoring facilities.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1595
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1596
     * @return the collection of threads
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1597
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1598
    public final Collection<Thread> getQueuedThreads() {
32990
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  1599
        ArrayList<Thread> list = new ArrayList<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1600
        for (Node p = tail; p != null; p = p.prev) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1601
            Thread t = p.thread;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1602
            if (t != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1603
                list.add(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1604
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1605
        return list;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1606
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1607
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1608
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1609
     * Returns a collection containing threads that may be waiting to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1610
     * acquire in exclusive mode. This has the same properties
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1611
     * as {@link #getQueuedThreads} except that it only returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1612
     * those threads waiting due to an exclusive acquire.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1613
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1614
     * @return the collection of threads
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1615
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1616
    public final Collection<Thread> getExclusiveQueuedThreads() {
32990
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  1617
        ArrayList<Thread> list = new ArrayList<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1618
        for (Node p = tail; p != null; p = p.prev) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1619
            if (!p.isShared()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1620
                Thread t = p.thread;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1621
                if (t != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1622
                    list.add(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1623
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1624
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1625
        return list;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1626
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1627
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1628
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1629
     * Returns a collection containing threads that may be waiting to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1630
     * acquire in shared mode. This has the same properties
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1631
     * as {@link #getQueuedThreads} except that it only returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1632
     * those threads waiting due to a shared acquire.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1633
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1634
     * @return the collection of threads
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1635
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1636
    public final Collection<Thread> getSharedQueuedThreads() {
32990
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  1637
        ArrayList<Thread> list = new ArrayList<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1638
        for (Node p = tail; p != null; p = p.prev) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1639
            if (p.isShared()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1640
                Thread t = p.thread;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1641
                if (t != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1642
                    list.add(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1643
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1644
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1645
        return list;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1646
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1647
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1648
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1649
     * Returns a string identifying this synchronizer, as well as its state.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1650
     * The state, in brackets, includes the String {@code "State ="}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1651
     * followed by the current value of {@link #getState}, and either
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1652
     * {@code "nonempty"} or {@code "empty"} depending on whether the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1653
     * queue is empty.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1654
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1655
     * @return a string identifying this synchronizer, as well as its state
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1656
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1657
    public String toString() {
32990
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  1658
        return super.toString()
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  1659
            + "[State = " + getState() + ", "
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  1660
            + (hasQueuedThreads() ? "non" : "") + "empty queue]";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1661
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1662
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1663
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1664
    // Internal support methods for Conditions
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1665
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1666
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1667
     * Returns true if a node, always one that was initially placed on
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1668
     * a condition queue, is now waiting to reacquire on sync queue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1669
     * @param node the node
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1670
     * @return true if is reacquiring
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1671
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1672
    final boolean isOnSyncQueue(Node node) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1673
        if (node.waitStatus == Node.CONDITION || node.prev == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1674
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1675
        if (node.next != null) // If has successor, it must be on queue
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1676
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1677
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1678
         * node.prev can be non-null, but not yet on queue because
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1679
         * the CAS to place it on queue can fail. So we have to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1680
         * traverse from tail to make sure it actually made it.  It
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1681
         * will always be near the tail in calls to this method, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1682
         * unless the CAS failed (which is unlikely), it will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1683
         * there, so we hardly ever traverse much.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1684
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1685
        return findNodeFromTail(node);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1686
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1687
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1688
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1689
     * Returns true if node is on sync queue by searching backwards from tail.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1690
     * Called only when needed by isOnSyncQueue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1691
     * @return true if present
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1692
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1693
    private boolean findNodeFromTail(Node node) {
32990
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  1694
        // We check for node first, since it's likely to be at or near tail.
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  1695
        // tail is known to be non-null, so we could re-order to "save"
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  1696
        // one null check, but we leave it this way to help the VM.
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  1697
        for (Node p = tail;;) {
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  1698
            if (p == node)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1699
                return true;
32990
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  1700
            if (p == null)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1701
                return false;
32990
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  1702
            p = p.prev;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1703
        }
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
     * Transfers a node from a condition queue onto sync queue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1708
     * Returns true if successful.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1709
     * @param node the node
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1710
     * @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
  1711
     * cancelled before signal)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1712
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1713
    final boolean transferForSignal(Node node) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1714
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1715
         * If cannot change waitStatus, the node has been cancelled.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1716
         */
32990
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  1717
        if (!node.compareAndSetWaitStatus(Node.CONDITION, 0))
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1718
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1719
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1720
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1721
         * Splice onto queue and try to set waitStatus of predecessor to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1722
         * indicate that thread is (probably) waiting. If cancelled or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1723
         * attempt to set waitStatus fails, wake up to resync (in which
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1724
         * case the waitStatus can be transiently and harmlessly wrong).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1725
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1726
        Node p = enq(node);
2430
975a98680ae8 6801020: Concurrent Semaphore release may cause some require thread not signaled
dl
parents: 2165
diff changeset
  1727
        int ws = p.waitStatus;
32990
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  1728
        if (ws > 0 || !p.compareAndSetWaitStatus(ws, Node.SIGNAL))
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1729
            LockSupport.unpark(node.thread);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1730
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1731
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1732
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1733
    /**
18576
7a5c231327af 8019377: Sync j.u.c locks and atomic from 166 to tl
dl
parents: 14325
diff changeset
  1734
     * 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
  1735
     * 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
  1736
     *
7a5c231327af 8019377: Sync j.u.c locks and atomic from 166 to tl
dl
parents: 14325
diff changeset
  1737
     * @param node the node
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1738
     * @return true if cancelled before the node was signalled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1739
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1740
    final boolean transferAfterCancelledWait(Node node) {
32990
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  1741
        if (node.compareAndSetWaitStatus(Node.CONDITION, 0)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1742
            enq(node);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1743
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1744
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1745
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1746
         * If we lost out to a signal(), then we can't proceed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1747
         * until it finishes its enq().  Cancelling during an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1748
         * incomplete transfer is both rare and transient, so just
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1749
         * spin.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1750
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1751
        while (!isOnSyncQueue(node))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1752
            Thread.yield();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1753
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1754
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1755
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1756
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1757
     * Invokes release with current state value; returns saved state.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1758
     * Cancels node and throws exception on failure.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1759
     * @param node the condition node for this wait
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1760
     * @return previous sync state
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1761
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1762
    final int fullyRelease(Node node) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1763
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1764
            int savedState = getState();
32990
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  1765
            if (release(savedState))
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1766
                return savedState;
32990
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  1767
            throw new IllegalMonitorStateException();
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  1768
        } catch (Throwable t) {
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  1769
            node.waitStatus = Node.CANCELLED;
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  1770
            throw t;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1771
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1772
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1773
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1774
    // Instrumentation methods for conditions
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1775
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1776
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1777
     * Queries whether the given ConditionObject
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1778
     * uses this synchronizer as its lock.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1779
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1780
     * @param condition the condition
18576
7a5c231327af 8019377: Sync j.u.c locks and atomic from 166 to tl
dl
parents: 14325
diff changeset
  1781
     * @return {@code true} if owned
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1782
     * @throws NullPointerException if the condition is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1783
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1784
    public final boolean owns(ConditionObject condition) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1785
        return condition.isOwnedBy(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1786
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1787
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1788
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1789
     * Queries whether any threads are waiting on the given condition
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1790
     * 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
  1791
     * 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
  1792
     * does not guarantee that a future {@code signal} will awaken
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1793
     * any threads.  This method is designed primarily for use in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1794
     * monitoring of the system state.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1795
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1796
     * @param condition the condition
18576
7a5c231327af 8019377: Sync j.u.c locks and atomic from 166 to tl
dl
parents: 14325
diff changeset
  1797
     * @return {@code true} if there are any waiting threads
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1798
     * @throws IllegalMonitorStateException if exclusive synchronization
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1799
     *         is not held
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1800
     * @throws IllegalArgumentException if the given condition is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1801
     *         not associated with this synchronizer
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1802
     * @throws NullPointerException if the condition is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1803
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1804
    public final boolean hasWaiters(ConditionObject condition) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1805
        if (!owns(condition))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1806
            throw new IllegalArgumentException("Not owner");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1807
        return condition.hasWaiters();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1808
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1809
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1810
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1811
     * Returns an estimate of the number of threads waiting on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1812
     * given condition associated with this synchronizer. Note that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1813
     * because timeouts and interrupts may occur at any time, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1814
     * estimate serves only as an upper bound on the actual number of
32990
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  1815
     * waiters.  This method is designed for use in monitoring system
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  1816
     * state, not for synchronization control.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1817
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1818
     * @param condition the condition
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1819
     * @return the estimated number of waiting threads
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1820
     * @throws IllegalMonitorStateException if exclusive synchronization
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1821
     *         is not held
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1822
     * @throws IllegalArgumentException if the given condition is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1823
     *         not associated with this synchronizer
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1824
     * @throws NullPointerException if the condition is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1825
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1826
    public final int getWaitQueueLength(ConditionObject condition) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1827
        if (!owns(condition))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1828
            throw new IllegalArgumentException("Not owner");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1829
        return condition.getWaitQueueLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1830
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1831
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1832
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1833
     * Returns a collection containing those threads that may be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1834
     * waiting on the given condition associated with this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1835
     * synchronizer.  Because the actual set of threads may change
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1836
     * dynamically while constructing this result, the returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1837
     * collection is only a best-effort estimate. The elements of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1838
     * returned collection are in no particular order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1839
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1840
     * @param condition the condition
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1841
     * @return the collection of threads
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1842
     * @throws IllegalMonitorStateException if exclusive synchronization
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1843
     *         is not held
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1844
     * @throws IllegalArgumentException if the given condition is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1845
     *         not associated with this synchronizer
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1846
     * @throws NullPointerException if the condition is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1847
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1848
    public final Collection<Thread> getWaitingThreads(ConditionObject condition) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1849
        if (!owns(condition))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1850
            throw new IllegalArgumentException("Not owner");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1851
        return condition.getWaitingThreads();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1852
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1853
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1854
    /**
47302
f517fa4f4dc6 8187408: AbstractQueuedSynchronizer wait queue corrupted when thread awaits without holding the lock
dl
parents: 47216
diff changeset
  1855
     * Condition implementation for a {@link AbstractQueuedSynchronizer}
f517fa4f4dc6 8187408: AbstractQueuedSynchronizer wait queue corrupted when thread awaits without holding the lock
dl
parents: 47216
diff changeset
  1856
     * serving as the basis of a {@link Lock} implementation.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1857
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1858
     * <p>Method documentation for this class describes mechanics,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1859
     * not behavioral specifications from the point of view of Lock
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1860
     * and Condition users. Exported versions of this class will in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1861
     * general need to be accompanied by documentation describing
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1862
     * 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
  1863
     * {@code AbstractQueuedSynchronizer}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1864
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1865
     * <p>This class is Serializable, but all fields are transient,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1866
     * so deserialized conditions have no waiters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1867
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1868
    public class ConditionObject implements Condition, java.io.Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1869
        private static final long serialVersionUID = 1173984872572414699L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1870
        /** First node of condition queue. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1871
        private transient Node firstWaiter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1872
        /** Last node of condition queue. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1873
        private transient Node lastWaiter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1874
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1875
        /**
18576
7a5c231327af 8019377: Sync j.u.c locks and atomic from 166 to tl
dl
parents: 14325
diff changeset
  1876
         * Creates a new {@code ConditionObject} instance.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1877
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1878
        public ConditionObject() { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1879
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1880
        // Internal methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1881
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1882
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1883
         * Adds a new waiter to wait queue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1884
         * @return its new wait node
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1885
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1886
        private Node addConditionWaiter() {
47302
f517fa4f4dc6 8187408: AbstractQueuedSynchronizer wait queue corrupted when thread awaits without holding the lock
dl
parents: 47216
diff changeset
  1887
            if (!isHeldExclusively())
f517fa4f4dc6 8187408: AbstractQueuedSynchronizer wait queue corrupted when thread awaits without holding the lock
dl
parents: 47216
diff changeset
  1888
                throw new IllegalMonitorStateException();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1889
            Node t = lastWaiter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1890
            // If lastWaiter is cancelled, clean out.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1891
            if (t != null && t.waitStatus != Node.CONDITION) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1892
                unlinkCancelledWaiters();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1893
                t = lastWaiter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1894
            }
32990
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  1895
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  1896
            Node node = new Node(Node.CONDITION);
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  1897
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1898
            if (t == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1899
                firstWaiter = node;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1900
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1901
                t.nextWaiter = node;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1902
            lastWaiter = node;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1903
            return node;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1904
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1905
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1906
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1907
         * Removes and transfers nodes until hit non-cancelled one or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1908
         * null. Split out from signal in part to encourage compilers
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1909
         * to inline the case of no waiters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1910
         * @param first (non-null) the first node on condition queue
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1911
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1912
        private void doSignal(Node first) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1913
            do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1914
                if ( (firstWaiter = first.nextWaiter) == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1915
                    lastWaiter = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1916
                first.nextWaiter = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1917
            } while (!transferForSignal(first) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1918
                     (first = firstWaiter) != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1919
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1920
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1921
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1922
         * Removes and transfers all nodes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1923
         * @param first (non-null) the first node on condition queue
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1924
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1925
        private void doSignalAll(Node first) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1926
            lastWaiter = firstWaiter = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1927
            do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1928
                Node next = first.nextWaiter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1929
                first.nextWaiter = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1930
                transferForSignal(first);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1931
                first = next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1932
            } while (first != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1933
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1934
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1935
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1936
         * Unlinks cancelled waiter nodes from condition queue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1937
         * Called only while holding lock. This is called when
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1938
         * cancellation occurred during condition wait, and upon
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1939
         * insertion of a new waiter when lastWaiter is seen to have
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1940
         * been cancelled. This method is needed to avoid garbage
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1941
         * retention in the absence of signals. So even though it may
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1942
         * require a full traversal, it comes into play only when
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1943
         * timeouts or cancellations occur in the absence of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1944
         * signals. It traverses all nodes rather than stopping at a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1945
         * particular target to unlink all pointers to garbage nodes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1946
         * without requiring many re-traversals during cancellation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1947
         * storms.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1948
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1949
        private void unlinkCancelledWaiters() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1950
            Node t = firstWaiter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1951
            Node trail = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1952
            while (t != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1953
                Node next = t.nextWaiter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1954
                if (t.waitStatus != Node.CONDITION) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1955
                    t.nextWaiter = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1956
                    if (trail == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1957
                        firstWaiter = next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1958
                    else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1959
                        trail.nextWaiter = next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1960
                    if (next == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1961
                        lastWaiter = trail;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1962
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1963
                else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1964
                    trail = t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1965
                t = next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1966
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1967
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1968
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1969
        // public methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1970
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1971
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1972
         * Moves the longest-waiting thread, if one exists, from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1973
         * wait queue for this condition to the wait queue for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1974
         * owning lock.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1975
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1976
         * @throws IllegalMonitorStateException if {@link #isHeldExclusively}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1977
         *         returns {@code false}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1978
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1979
        public final void signal() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1980
            if (!isHeldExclusively())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1981
                throw new IllegalMonitorStateException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1982
            Node first = firstWaiter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1983
            if (first != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1984
                doSignal(first);
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
         * Moves all threads from the wait queue for this condition to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1989
         * the wait queue for the owning lock.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1990
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1991
         * @throws IllegalMonitorStateException if {@link #isHeldExclusively}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1992
         *         returns {@code false}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1993
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1994
        public final void signalAll() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1995
            if (!isHeldExclusively())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1996
                throw new IllegalMonitorStateException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1997
            Node first = firstWaiter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1998
            if (first != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1999
                doSignalAll(first);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2000
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2001
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2002
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2003
         * Implements uninterruptible condition wait.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2004
         * <ol>
32990
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  2005
         * <li>Save lock state returned by {@link #getState}.
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  2006
         * <li>Invoke {@link #release} with saved state as argument,
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  2007
         *     throwing IllegalMonitorStateException if it fails.
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  2008
         * <li>Block until signalled.
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  2009
         * <li>Reacquire by invoking specialized version of
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  2010
         *     {@link #acquire} with saved state as argument.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2011
         * </ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2012
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2013
        public final void awaitUninterruptibly() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2014
            Node node = addConditionWaiter();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2015
            int savedState = fullyRelease(node);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2016
            boolean interrupted = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2017
            while (!isOnSyncQueue(node)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2018
                LockSupport.park(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2019
                if (Thread.interrupted())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2020
                    interrupted = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2021
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2022
            if (acquireQueued(node, savedState) || interrupted)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2023
                selfInterrupt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2024
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2025
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2026
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2027
         * For interruptible waits, we need to track whether to throw
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2028
         * InterruptedException, if interrupted while blocked on
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2029
         * condition, versus reinterrupt current thread, if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2030
         * interrupted while blocked waiting to re-acquire.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2031
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2032
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2033
        /** Mode meaning to reinterrupt on exit from wait */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2034
        private static final int REINTERRUPT =  1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2035
        /** Mode meaning to throw InterruptedException on exit from wait */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2036
        private static final int THROW_IE    = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2037
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2038
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2039
         * Checks for interrupt, returning THROW_IE if interrupted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2040
         * before signalled, REINTERRUPT if after signalled, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2041
         * 0 if not interrupted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2042
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2043
        private int checkInterruptWhileWaiting(Node node) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2044
            return Thread.interrupted() ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2045
                (transferAfterCancelledWait(node) ? THROW_IE : REINTERRUPT) :
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2046
                0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2047
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2048
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2049
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2050
         * Throws InterruptedException, reinterrupts current thread, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2051
         * does nothing, depending on mode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2052
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2053
        private void reportInterruptAfterWait(int interruptMode)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2054
            throws InterruptedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2055
            if (interruptMode == THROW_IE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2056
                throw new InterruptedException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2057
            else if (interruptMode == REINTERRUPT)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2058
                selfInterrupt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2059
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2060
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2061
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2062
         * Implements interruptible condition wait.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2063
         * <ol>
32990
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  2064
         * <li>If current thread is interrupted, throw InterruptedException.
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  2065
         * <li>Save lock state returned by {@link #getState}.
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  2066
         * <li>Invoke {@link #release} with saved state as argument,
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  2067
         *     throwing IllegalMonitorStateException if it fails.
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  2068
         * <li>Block until signalled or interrupted.
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  2069
         * <li>Reacquire by invoking specialized version of
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  2070
         *     {@link #acquire} with saved state as argument.
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  2071
         * <li>If interrupted while blocked in step 4, throw InterruptedException.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2072
         * </ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2073
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2074
        public final void await() throws InterruptedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2075
            if (Thread.interrupted())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2076
                throw new InterruptedException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2077
            Node node = addConditionWaiter();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2078
            int savedState = fullyRelease(node);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2079
            int interruptMode = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2080
            while (!isOnSyncQueue(node)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2081
                LockSupport.park(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2082
                if ((interruptMode = checkInterruptWhileWaiting(node)) != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2083
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2084
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2085
            if (acquireQueued(node, savedState) && interruptMode != THROW_IE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2086
                interruptMode = REINTERRUPT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2087
            if (node.nextWaiter != null) // clean up if cancelled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2088
                unlinkCancelledWaiters();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2089
            if (interruptMode != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2090
                reportInterruptAfterWait(interruptMode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2091
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2092
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2093
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2094
         * Implements timed condition wait.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2095
         * <ol>
32990
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  2096
         * <li>If current thread is interrupted, throw InterruptedException.
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  2097
         * <li>Save lock state returned by {@link #getState}.
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  2098
         * <li>Invoke {@link #release} with saved state as argument,
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  2099
         *     throwing IllegalMonitorStateException if it fails.
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  2100
         * <li>Block until signalled, interrupted, or timed out.
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  2101
         * <li>Reacquire by invoking specialized version of
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  2102
         *     {@link #acquire} with saved state as argument.
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  2103
         * <li>If interrupted while blocked in step 4, throw InterruptedException.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2104
         * </ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2105
         */
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
  2106
        public final long awaitNanos(long nanosTimeout)
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
  2107
                throws InterruptedException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2108
            if (Thread.interrupted())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2109
                throw new InterruptedException();
32990
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  2110
            // We don't check for nanosTimeout <= 0L here, to allow
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  2111
            // awaitNanos(0) as a way to "yield the lock".
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  2112
            final long deadline = System.nanoTime() + nanosTimeout;
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  2113
            long initialNanos = nanosTimeout;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2114
            Node node = addConditionWaiter();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2115
            int savedState = fullyRelease(node);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2116
            int interruptMode = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2117
            while (!isOnSyncQueue(node)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2118
                if (nanosTimeout <= 0L) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2119
                    transferAfterCancelledWait(node);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2120
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2121
                }
34348
ba5c2f2fc9d7 8143087: Miscellaneous changes imported from jsr166 CVS 2015-11
dl
parents: 33674
diff changeset
  2122
                if (nanosTimeout > SPIN_FOR_TIMEOUT_THRESHOLD)
14325
622c473a21aa 8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents: 9242
diff changeset
  2123
                    LockSupport.parkNanos(this, nanosTimeout);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2124
                if ((interruptMode = checkInterruptWhileWaiting(node)) != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2125
                    break;
14325
622c473a21aa 8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents: 9242
diff changeset
  2126
                nanosTimeout = deadline - System.nanoTime();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2127
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2128
            if (acquireQueued(node, savedState) && interruptMode != THROW_IE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2129
                interruptMode = REINTERRUPT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2130
            if (node.nextWaiter != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2131
                unlinkCancelledWaiters();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2132
            if (interruptMode != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2133
                reportInterruptAfterWait(interruptMode);
32990
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  2134
            long remaining = deadline - System.nanoTime(); // avoid overflow
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  2135
            return (remaining <= initialNanos) ? remaining : Long.MIN_VALUE;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2136
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2137
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2138
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2139
         * Implements absolute timed condition wait.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2140
         * <ol>
32990
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  2141
         * <li>If current thread is interrupted, throw InterruptedException.
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  2142
         * <li>Save lock state returned by {@link #getState}.
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  2143
         * <li>Invoke {@link #release} with saved state as argument,
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  2144
         *     throwing IllegalMonitorStateException if it fails.
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  2145
         * <li>Block until signalled, interrupted, or timed out.
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  2146
         * <li>Reacquire by invoking specialized version of
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  2147
         *     {@link #acquire} with saved state as argument.
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  2148
         * <li>If interrupted while blocked in step 4, throw InterruptedException.
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  2149
         * <li>If timed out while blocked in step 4, return false, else true.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2150
         * </ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2151
         */
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
  2152
        public final boolean awaitUntil(Date deadline)
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
  2153
                throws InterruptedException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2154
            long abstime = deadline.getTime();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2155
            if (Thread.interrupted())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2156
                throw new InterruptedException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2157
            Node node = addConditionWaiter();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2158
            int savedState = fullyRelease(node);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2159
            boolean timedout = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2160
            int interruptMode = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2161
            while (!isOnSyncQueue(node)) {
32990
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  2162
                if (System.currentTimeMillis() >= abstime) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2163
                    timedout = transferAfterCancelledWait(node);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2164
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2165
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2166
                LockSupport.parkUntil(this, abstime);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2167
                if ((interruptMode = checkInterruptWhileWaiting(node)) != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2168
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2169
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2170
            if (acquireQueued(node, savedState) && interruptMode != THROW_IE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2171
                interruptMode = REINTERRUPT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2172
            if (node.nextWaiter != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2173
                unlinkCancelledWaiters();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2174
            if (interruptMode != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2175
                reportInterruptAfterWait(interruptMode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2176
            return !timedout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2177
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2178
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2179
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2180
         * Implements timed condition wait.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2181
         * <ol>
32990
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  2182
         * <li>If current thread is interrupted, throw InterruptedException.
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  2183
         * <li>Save lock state returned by {@link #getState}.
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  2184
         * <li>Invoke {@link #release} with saved state as argument,
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  2185
         *     throwing IllegalMonitorStateException if it fails.
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  2186
         * <li>Block until signalled, interrupted, or timed out.
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  2187
         * <li>Reacquire by invoking specialized version of
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  2188
         *     {@link #acquire} with saved state as argument.
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  2189
         * <li>If interrupted while blocked in step 4, throw InterruptedException.
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  2190
         * <li>If timed out while blocked in step 4, return false, else true.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2191
         * </ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2192
         */
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
  2193
        public final boolean await(long time, TimeUnit unit)
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
  2194
                throws InterruptedException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2195
            long nanosTimeout = unit.toNanos(time);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2196
            if (Thread.interrupted())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2197
                throw new InterruptedException();
32990
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  2198
            // We don't check for nanosTimeout <= 0L here, to allow
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  2199
            // await(0, unit) as a way to "yield the lock".
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  2200
            final long deadline = System.nanoTime() + nanosTimeout;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2201
            Node node = addConditionWaiter();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2202
            int savedState = fullyRelease(node);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2203
            boolean timedout = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2204
            int interruptMode = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2205
            while (!isOnSyncQueue(node)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2206
                if (nanosTimeout <= 0L) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2207
                    timedout = transferAfterCancelledWait(node);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2208
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2209
                }
34348
ba5c2f2fc9d7 8143087: Miscellaneous changes imported from jsr166 CVS 2015-11
dl
parents: 33674
diff changeset
  2210
                if (nanosTimeout > SPIN_FOR_TIMEOUT_THRESHOLD)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2211
                    LockSupport.parkNanos(this, nanosTimeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2212
                if ((interruptMode = checkInterruptWhileWaiting(node)) != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2213
                    break;
14325
622c473a21aa 8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents: 9242
diff changeset
  2214
                nanosTimeout = deadline - System.nanoTime();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2215
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2216
            if (acquireQueued(node, savedState) && interruptMode != THROW_IE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2217
                interruptMode = REINTERRUPT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2218
            if (node.nextWaiter != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2219
                unlinkCancelledWaiters();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2220
            if (interruptMode != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2221
                reportInterruptAfterWait(interruptMode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2222
            return !timedout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2223
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2224
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2225
        //  support for instrumentation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2226
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2227
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2228
         * Returns true if this condition was created by the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2229
         * synchronization object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2230
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2231
         * @return {@code true} if owned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2232
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2233
        final boolean isOwnedBy(AbstractQueuedSynchronizer sync) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2234
            return sync == AbstractQueuedSynchronizer.this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2235
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2236
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2237
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2238
         * 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
  2239
         * Implements {@link AbstractQueuedSynchronizer#hasWaiters(ConditionObject)}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2240
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2241
         * @return {@code true} if there are any waiting threads
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2242
         * @throws IllegalMonitorStateException if {@link #isHeldExclusively}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2243
         *         returns {@code false}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2244
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2245
        protected final boolean hasWaiters() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2246
            if (!isHeldExclusively())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2247
                throw new IllegalMonitorStateException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2248
            for (Node w = firstWaiter; w != null; w = w.nextWaiter) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2249
                if (w.waitStatus == Node.CONDITION)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2250
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2251
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2252
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2253
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2254
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2255
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2256
         * Returns an estimate of the number of threads waiting on
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2257
         * this condition.
18576
7a5c231327af 8019377: Sync j.u.c locks and atomic from 166 to tl
dl
parents: 14325
diff changeset
  2258
         * Implements {@link AbstractQueuedSynchronizer#getWaitQueueLength(ConditionObject)}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2259
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2260
         * @return the estimated number of waiting threads
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2261
         * @throws IllegalMonitorStateException if {@link #isHeldExclusively}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2262
         *         returns {@code false}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2263
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2264
        protected final int getWaitQueueLength() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2265
            if (!isHeldExclusively())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2266
                throw new IllegalMonitorStateException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2267
            int n = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2268
            for (Node w = firstWaiter; w != null; w = w.nextWaiter) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2269
                if (w.waitStatus == Node.CONDITION)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2270
                    ++n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2271
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2272
            return n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2273
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2274
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2275
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2276
         * Returns a collection containing those threads that may be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2277
         * waiting on this Condition.
18576
7a5c231327af 8019377: Sync j.u.c locks and atomic from 166 to tl
dl
parents: 14325
diff changeset
  2278
         * Implements {@link AbstractQueuedSynchronizer#getWaitingThreads(ConditionObject)}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2279
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2280
         * @return the collection of threads
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2281
         * @throws IllegalMonitorStateException if {@link #isHeldExclusively}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2282
         *         returns {@code false}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2283
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2284
        protected final Collection<Thread> getWaitingThreads() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2285
            if (!isHeldExclusively())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2286
                throw new IllegalMonitorStateException();
32990
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  2287
            ArrayList<Thread> list = new ArrayList<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2288
            for (Node w = firstWaiter; w != null; w = w.nextWaiter) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2289
                if (w.waitStatus == Node.CONDITION) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2290
                    Thread t = w.thread;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2291
                    if (t != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2292
                        list.add(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2293
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2294
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2295
            return list;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2296
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2297
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2298
39725
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 35247
diff changeset
  2299
    // VarHandle mechanics
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 35247
diff changeset
  2300
    private static final VarHandle STATE;
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 35247
diff changeset
  2301
    private static final VarHandle HEAD;
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 35247
diff changeset
  2302
    private static final VarHandle TAIL;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2303
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2304
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2305
        try {
39725
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 35247
diff changeset
  2306
            MethodHandles.Lookup l = MethodHandles.lookup();
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 35247
diff changeset
  2307
            STATE = l.findVarHandle(AbstractQueuedSynchronizer.class, "state", int.class);
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 35247
diff changeset
  2308
            HEAD = l.findVarHandle(AbstractQueuedSynchronizer.class, "head", Node.class);
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 35247
diff changeset
  2309
            TAIL = l.findVarHandle(AbstractQueuedSynchronizer.class, "tail", Node.class);
32990
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  2310
        } catch (ReflectiveOperationException e) {
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  2311
            throw new Error(e);
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  2312
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2313
32990
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  2314
        // Reduce the risk of rare disastrous classloading in first call to
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  2315
        // LockSupport.park: https://bugs.openjdk.java.net/browse/JDK-8074773
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  2316
        Class<?> ensureLoaded = LockSupport.class;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2317
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2318
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2319
    /**
32990
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  2320
     * Initializes head and tail fields on first contention.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2321
     */
32990
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  2322
    private final void initializeSyncQueue() {
34348
ba5c2f2fc9d7 8143087: Miscellaneous changes imported from jsr166 CVS 2015-11
dl
parents: 33674
diff changeset
  2323
        Node h;
39725
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 35247
diff changeset
  2324
        if (HEAD.compareAndSet(this, null, (h = new Node())))
34348
ba5c2f2fc9d7 8143087: Miscellaneous changes imported from jsr166 CVS 2015-11
dl
parents: 33674
diff changeset
  2325
            tail = h;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2326
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2327
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2328
    /**
32990
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  2329
     * CASes tail field.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2330
     */
32990
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  2331
    private final boolean compareAndSetTail(Node expect, Node update) {
39725
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 35247
diff changeset
  2332
        return TAIL.compareAndSet(this, expect, update);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2333
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2334
}