src/java.base/share/classes/java/util/concurrent/locks/AbstractQueuedSynchronizer.java
author chegar
Thu, 17 Oct 2019 20:54:25 +0100
branchdatagramsocketimpl-branch
changeset 58679 9c3209ff7550
parent 58678 9cf78a70fa4f
parent 58385 489532b89775
permissions -rw-r--r--
datagramsocketimpl-branch: merge with default
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
14325
622c473a21aa 8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents: 9242
diff changeset
    38
import java.util.ArrayList;
622c473a21aa 8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents: 9242
diff changeset
    39
import java.util.Collection;
622c473a21aa 8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents: 9242
diff changeset
    40
import java.util.Date;
32990
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
    41
import java.util.concurrent.TimeUnit;
58134
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
    42
import java.util.concurrent.ForkJoinPool;
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
    43
import jdk.internal.misc.Unsafe;
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
 *
54685
e1bec7613945 8220248: fix headings in java.util.concurrent
dl
parents: 49565
diff changeset
   107
 * <h2>Usage</h2>
2
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
 *
54685
e1bec7613945 8220248: fix headings in java.util.concurrent
dl
parents: 49565
diff changeset
   190
 * <h2>Usage Examples</h2>
2
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
58134
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   315
    /*
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   316
     * Overview.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     *
58134
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   318
     * The wait queue is a variant of a "CLH" (Craig, Landin, and
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * Hagersten) lock queue. CLH locks are normally used for
58134
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   320
     * spinlocks.  We instead use them for blocking synchronizers by
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   321
     * including explicit ("prev" and "next") links plus a "status"
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   322
     * field that allow nodes to signal successors when releasing
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   323
     * locks, and handle cancellation due to interrupts and timeouts.
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   324
     * The status field includes bits that track whether a thread
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   325
     * needs a signal (using LockSupport.unpark). Despite these
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   326
     * additions, we maintain most CLH locality properties.
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   327
     *
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   328
     * To enqueue into a CLH lock, you atomically splice it in as new
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   329
     * tail. To dequeue, you set the head field, so the next eligible
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   330
     * waiter becomes first.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     *
58134
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   332
     *  +------+  prev +-------+       +------+
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   333
     *  | head | <---- | first | <---- | tail |
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   334
     *  +------+       +-------+       +------+
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   335
     *
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   336
     * Insertion into a CLH queue requires only a single atomic
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   337
     * operation on "tail", so there is a simple point of demarcation
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   338
     * from unqueued to queued. The "next" link of the predecessor is
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   339
     * set by the enqueuing thread after successful CAS. Even though
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   340
     * non-atomic, this suffices to ensure that any blocked thread is
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   341
     * signalled by a predecessor when eligible (although in the case
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   342
     * of cancellation, possibly with the assistance of a signal in
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   343
     * method cleanQueue). Signalling is based in part on a
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   344
     * Dekker-like scheme in which the to-be waiting thread indicates
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   345
     * WAITING status, then retries acquiring, and then rechecks
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   346
     * status before blocking. The signaller atomically clears WAITING
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   347
     * status when unparking.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     *
58134
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   349
     * Dequeuing on acquire involves detaching (nulling) a node's
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   350
     * "prev" node and then updating the "head". Other threads check
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   351
     * if a node is or was dequeued by checking "prev" rather than
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   352
     * head. We enforce the nulling then setting order by spin-waiting
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   353
     * if necessary. Because of this, the lock algorithm is not itself
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   354
     * strictly "lock-free" because an acquiring thread may need to
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   355
     * wait for a previous acquire to make progress. When used with
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   356
     * exclusive locks, such progress is required anyway. However
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   357
     * Shared mode may (uncommonly) require a spin-wait before
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   358
     * setting head field to ensure proper propagation. (Historical
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   359
     * note: This allows some simplifications and efficiencies
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   360
     * compared to previous versions of this class.)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     *
58134
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   362
     * A node's predecessor can change due to cancellation while it is
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   363
     * waiting, until the node is first in queue, at which point it
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   364
     * cannot change. The acquire methods cope with this by rechecking
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   365
     * "prev" before waiting. The prev and next fields are modified
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   366
     * only via CAS by cancelled nodes in method cleanQueue. The
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   367
     * unsplice strategy is reminiscent of Michael-Scott queues in
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   368
     * that after a successful CAS to prev field, other threads help
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   369
     * fix next fields.  Because cancellation often occurs in bunches
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   370
     * that complicate decisions about necessary signals, each call to
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   371
     * cleanQueue traverses the queue until a clean sweep. Nodes that
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   372
     * become relinked as first are unconditionally unparked
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   373
     * (sometimes unnecessarily, but those cases are not worth
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   374
     * avoiding).
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     *
58134
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   376
     * A thread may try to acquire if it is first (frontmost) in the
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   377
     * queue, and sometimes before.  Being first does not guarantee
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   378
     * success; it only gives the right to contend. We balance
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   379
     * throughput, overhead, and fairness by allowing incoming threads
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   380
     * to "barge" and acquire the synchronizer while in the process of
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   381
     * enqueuing, in which case an awakened first thread may need to
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   382
     * rewait.  To counteract possible repeated unlucky rewaits, we
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   383
     * exponentially increase retries (up to 256) to acquire each time
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   384
     * a thread is unparked. Except in this case, AQS locks do not
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   385
     * spin; they instead interleave attempts to acquire with
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   386
     * bookkeeping steps. (Users who want spinlocks can use
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   387
     * tryAcquire.)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     *
58134
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   389
     * To improve garbage collectibility, fields of nodes not yet on
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   390
     * list are null. (It is not rare to create and then throw away a
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   391
     * node without using it.) Fields of nodes coming off the list are
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   392
     * nulled out as soon as possible. This accentuates the challenge
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   393
     * of externally determining the first waiting thread (as in
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   394
     * method getFirstQueuedThread). This sometimes requires the
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   395
     * fallback of traversing backwards from the atomically updated
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   396
     * "tail" when fields appear null. (This is never needed in the
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   397
     * process of signalling though.)
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   398
     *
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   399
     * CLH queues need a dummy header node to get started. But
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     * we don't create them on construction, because it would be wasted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     * effort if there is never contention. Instead, the node
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     * is constructed and head and tail pointers are set upon first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     * contention.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     *
58134
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   405
     * Shared mode operations differ from Exclusive in that an acquire
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   406
     * signals the next waiter to try to acquire if it is also
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   407
     * Shared. The tryAcquireShared API allows users to indicate the
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   408
     * degree of propagation, but in most applications, it is more
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   409
     * efficient to ignore this, allowing the successor to try
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   410
     * acquiring in any case.
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   411
     *
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   412
     * Threads waiting on Conditions use nodes with an additional
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   413
     * link to maintain the (FIFO) list of conditions. Conditions only
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   414
     * need to link nodes in simple (non-concurrent) linked queues
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   415
     * because they are only accessed when exclusively held.  Upon
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   416
     * await, a node is inserted into a condition queue.  Upon signal,
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   417
     * the node is enqueued on the main queue.  A special status field
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   418
     * value is used to track and atomically trigger this.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     *
58134
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   420
     * Accesses to fields head, tail, and state use full Volatile
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   421
     * mode, along with CAS. Node fields status, prev and next also do
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   422
     * so while threads may be signallable, but sometimes use weaker
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   423
     * modes otherwise. Accesses to field "waiter" (the thread to be
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   424
     * signalled) are always sandwiched between other atomic accesses
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   425
     * so are used in Plain mode. We use jdk.internal Unsafe versions
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   426
     * of atomic access methods rather than VarHandles to avoid
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   427
     * potential VM bootstrap issues.
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   428
     *
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   429
     * Most of the above is performed by primary internal method
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   430
     * acquire, that is invoked in some way by all exported acquire
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   431
     * methods.  (It is usually easy for compilers to optimize
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   432
     * call-site specializations when heavily used.)
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   433
     *
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   434
     * There are several arbitrary decisions about when and how to
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   435
     * check interrupts in both acquire and await before and/or after
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   436
     * blocking. The decisions are less arbitrary in implementation
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   437
     * updates because some users appear to rely on original behaviors
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   438
     * in ways that are racy and so (rarely) wrong in general but hard
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   439
     * to justify changing.
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   440
     *
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   441
     * Thanks go to Dave Dice, Mark Moir, Victor Luchangco, Bill
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
     * Scherer and Michael Scott, along with members of JSR-166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
     * expert group, for helpful ideas, discussions, and critiques
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
     * on the design of this class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
     */
58134
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   446
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   447
    // Node status bits, also used as argument and return values
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   448
    static final int WAITING   = 1;          // must be 1
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   449
    static final int CANCELLED = 0x80000000; // must be negative
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   450
    static final int COND      = 2;          // in a condition wait
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
58134
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   452
    /** CLH Nodes */
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   453
    abstract static class Node {
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   454
        volatile Node prev;       // initially attached via casTail
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   455
        volatile Node next;       // visibly nonnull when signallable
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   456
        Thread waiter;            // visibly nonnull when enqueued
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   457
        volatile int status;      // written by owner, atomic bit ops by others
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
58134
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   459
        // methods for atomic operations
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   460
        final boolean casPrev(Node c, Node v) {  // for cleanQueue
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   461
            return U.weakCompareAndSetReference(this, PREV, c, v);
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   462
        }
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   463
        final boolean casNext(Node c, Node v) {  // for cleanQueue
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   464
            return U.weakCompareAndSetReference(this, NEXT, c, v);
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   465
        }
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   466
        final int getAndUnsetStatus(int v) {     // for signalling
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   467
            return U.getAndBitwiseAndInt(this, STATUS, ~v);
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   468
        }
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   469
        final void setPrevRelaxed(Node p) {      // for off-queue assignment
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   470
            U.putReference(this, PREV, p);
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   471
        }
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   472
        final void setStatusRelaxed(int s) {     // for off-queue assignment
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   473
            U.putInt(this, STATUS, s);
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   474
        }
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   475
        final void clearStatus() {               // for reducing unneeded signals
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   476
            U.putIntOpaque(this, STATUS, 0);
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   477
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
58134
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   479
        private static final long STATUS
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   480
            = U.objectFieldOffset(Node.class, "status");
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   481
        private static final long NEXT
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   482
            = U.objectFieldOffset(Node.class, "next");
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   483
        private static final long PREV
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   484
            = U.objectFieldOffset(Node.class, "prev");
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   485
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
58134
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   487
    // Concrete classes tagged by type
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   488
    static final class ExclusiveNode extends Node { }
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   489
    static final class SharedNode extends Node { }
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   490
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   491
    static final class ConditionNode extends Node
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   492
        implements ForkJoinPool.ManagedBlocker {
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   493
        ConditionNode nextWaiter;            // link to next waiting node
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
        /**
58134
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   496
         * Allows Conditions to be used in ForkJoinPools without
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   497
         * risking fixed pool exhaustion. This is usable only for
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   498
         * untimed Condition waits, not timed versions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
         */
58134
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   500
        public final boolean isReleasable() {
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   501
            return status <= 1 || Thread.currentThread().isInterrupted();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
58134
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   504
        public final boolean block() {
58385
489532b89775 8231032: ThreadMXBean locking tests fail after JSR 166 refresh
dl
parents: 58134
diff changeset
   505
            while (!isReleasable()) LockSupport.park();
58134
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   506
            return true;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
    /**
58134
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   511
     * Head of the wait queue, lazily initialized.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
    private transient volatile Node head;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
    /**
58134
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   516
     * Tail of the wait queue. After initialization, modified only via casTail.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
    private transient volatile Node tail;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
     * The synchronization state.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
    private volatile int state;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
     * 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
   527
     * This operation has memory semantics of a {@code volatile} read.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
     * @return current state value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
    protected final int getState() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
        return state;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
     * 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
   536
     * This operation has memory semantics of a {@code volatile} write.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
     * @param newState the new state value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
    protected final void setState(int newState) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
        state = newState;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
     * Atomically sets synchronization state to the given updated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
     * 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
   546
     * This operation has memory semantics of a {@code volatile} read
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
     * and write.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
     * @param expect the expected value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
     * @param update the new value
18576
7a5c231327af 8019377: Sync j.u.c locks and atomic from 166 to tl
dl
parents: 14325
diff changeset
   551
     * @return {@code true} if successful. False return indicates that the actual
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
     *         value was not equal to the expected value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
    protected final boolean compareAndSetState(int expect, int update) {
58134
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   555
        return U.compareAndSetInt(this, STATE, expect, update);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
    // Queuing utilities
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
58134
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   560
    private boolean casTail(Node c, Node v) {
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   561
        return U.compareAndSetReference(this, TAIL, c, v);
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   562
    }
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   563
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   564
    /** tries once to CAS a new dummy node for head */
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   565
    private void tryInitializeHead() {
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   566
        Node h = new ExclusiveNode();
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   567
        if (U.compareAndSetReference(this, HEAD, null, h))
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   568
            tail = h;
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   569
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
    /**
58134
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   572
     * Enqueues the node unless null. (Currently used only for
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   573
     * ConditionNodes; other cases are interleaved with acquires.)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
     */
58134
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   575
    final void enqueue(Node node) {
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   576
        if (node != null) {
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   577
            for (;;) {
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   578
                Node t = tail;
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   579
                node.setPrevRelaxed(t);        // avoid unnecessary fence
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   580
                if (t == null)                 // initialize
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   581
                    tryInitializeHead();
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   582
                else if (casTail(t, node)) {
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   583
                    t.next = node;
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   584
                    if (t.status < 0)          // wake up to clean link
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   585
                        LockSupport.unpark(node.waiter);
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   586
                    break;
32990
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
   587
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
58134
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   592
    /** Returns true if node is found in traversal from tail */
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   593
    final boolean isEnqueued(Node node) {
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   594
        for (Node t = tail; t != null; t = t.prev)
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   595
            if (t == node)
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   596
                return true;
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   597
        return false;
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   598
    }
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   599
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
    /**
58134
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   601
     * Wakes up the successor of given node, if one exists, and unsets its
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   602
     * WAITING status to avoid park race. This may fail to wake up an
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   603
     * eligible thread when one or more have been cancelled, but
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   604
     * cancelAcquire ensures liveness.
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   605
     */
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   606
    private static void signalNext(Node h) {
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   607
        Node s;
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   608
        if (h != null && (s = h.next) != null && s.status != 0) {
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   609
            s.getAndUnsetStatus(WAITING);
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   610
            LockSupport.unpark(s.waiter);
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   611
        }
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   612
    }
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   613
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   614
    /** Wakes up the given node if in shared mode */
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   615
    private static void signalNextIfShared(Node h) {
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   616
        Node s;
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   617
        if (h != null && (s = h.next) != null &&
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   618
            (s instanceof SharedNode) && s.status != 0) {
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   619
            s.getAndUnsetStatus(WAITING);
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   620
            LockSupport.unpark(s.waiter);
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   621
        }
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   622
    }
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   623
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   624
    /**
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   625
     * Main acquire method, invoked by all exported acquire methods.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
     *
58134
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   627
     * @param node null unless a reacquiring Condition
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   628
     * @param arg the acquire argument
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   629
     * @param shared true if shared mode else exclusive
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   630
     * @param interruptible if abort and return negative on interrupt
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   631
     * @param timed if true use timed waits
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   632
     * @param time if timed, the System.nanoTime value to timeout
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   633
     * @return positive if acquired, 0 if timed out, negative if interrupted
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
     */
58134
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   635
    final int acquire(Node node, int arg, boolean shared,
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   636
                      boolean interruptible, boolean timed, long time) {
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   637
        Thread current = Thread.currentThread();
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   638
        byte spins = 0, postSpins = 0;   // retries upon unpark of first thread
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   639
        boolean interrupted = false, first = false;
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   640
        Node pred = null;                // predecessor of node when enqueued
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   641
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   642
        /*
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   643
         * Repeatedly:
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   644
         *  Check if node now first
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   645
         *    if so, ensure head stable, else ensure valid predecessor
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   646
         *  if node is first or not yet enqueued, try acquiring
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   647
         *  else if node not yet created, create it
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   648
         *  else if not yet enqueued, try once to enqueue
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   649
         *  else if woken from park, retry (up to postSpins times)
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   650
         *  else if WAITING status not set, set and retry
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   651
         *  else park and clear WAITING status, and check cancellation
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   652
         */
32990
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
   653
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
   654
        for (;;) {
58134
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   655
            if (!first && (pred = (node == null) ? null : node.prev) != null &&
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   656
                !(first = (head == pred))) {
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   657
                if (pred.status < 0) {
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   658
                    cleanQueue();           // predecessor cancelled
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   659
                    continue;
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   660
                } else if (pred.prev == null) {
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   661
                    Thread.onSpinWait();    // ensure serialization
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   662
                    continue;
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   663
                }
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   664
            }
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   665
            if (first || pred == null) {
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   666
                boolean acquired;
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   667
                try {
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   668
                    if (shared)
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   669
                        acquired = (tryAcquireShared(arg) >= 0);
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   670
                    else
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   671
                        acquired = tryAcquire(arg);
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   672
                } catch (Throwable ex) {
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   673
                    cancelAcquire(node, interrupted, false);
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   674
                    throw ex;
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   675
                }
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   676
                if (acquired) {
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   677
                    if (first) {
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   678
                        node.prev = null;
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   679
                        head = node;
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   680
                        pred.next = null;
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   681
                        node.waiter = null;
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   682
                        if (shared)
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   683
                            signalNextIfShared(node);
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   684
                        if (interrupted)
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   685
                            current.interrupt();
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   686
                    }
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   687
                    return 1;
32990
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
   688
                }
58134
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   689
            }
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   690
            if (node == null) {                 // allocate; retry before enqueue
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   691
                if (shared)
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   692
                    node = new SharedNode();
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   693
                else
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   694
                    node = new ExclusiveNode();
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   695
            } else if (pred == null) {          // try to enqueue
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   696
                node.waiter = current;
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   697
                Node t = tail;
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   698
                node.setPrevRelaxed(t);         // avoid unnecessary fence
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   699
                if (t == null)
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   700
                    tryInitializeHead();
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   701
                else if (!casTail(t, node))
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   702
                    node.setPrevRelaxed(null);  // back out
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   703
                else
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   704
                    t.next = node;
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   705
            } else if (first && spins != 0) {
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   706
                --spins;                        // reduce unfairness on rewaits
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   707
                Thread.onSpinWait();
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   708
            } else if (node.status == 0) {
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   709
                node.status = WAITING;          // enable signal and recheck
32990
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
   710
            } else {
58134
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   711
                long nanos;
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   712
                spins = postSpins = (byte)((postSpins << 1) | 1);
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   713
                if (!timed)
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   714
                    LockSupport.park(this);
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   715
                else if ((nanos = time - System.nanoTime()) > 0L)
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   716
                    LockSupport.parkNanos(this, nanos);
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   717
                else
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   718
                    break;
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   719
                node.clearStatus();
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   720
                if ((interrupted |= Thread.interrupted()) && interruptible)
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   721
                    break;
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   722
            }
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   723
        }
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   724
        return cancelAcquire(node, interrupted, interruptible);
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   725
    }
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   726
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   727
    /**
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   728
     * Possibly repeatedly traverses from tail, unsplicing cancelled
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   729
     * nodes until none are found. Unparks nodes that may have been
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   730
     * relinked to be next eligible acquirer.
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   731
     */
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   732
    private void cleanQueue() {
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   733
        for (;;) {                               // restart point
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   734
            for (Node q = tail, s = null, p, n;;) { // (p, q, s) triples
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   735
                if (q == null || (p = q.prev) == null)
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   736
                    return;                      // end of list
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   737
                if (s == null ? tail != q : (s.prev != q || s.status < 0))
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   738
                    break;                       // inconsistent
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   739
                if (q.status < 0) {              // cancelled
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   740
                    if ((s == null ? casTail(q, p) : s.casPrev(q, p)) &&
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   741
                        q.prev == p) {
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   742
                        p.casNext(q, s);         // OK if fails
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   743
                        if (p.prev == null)
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   744
                            signalNext(p);
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   745
                    }
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   746
                    break;
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   747
                }
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   748
                if ((n = p.next) != q) {         // help finish
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   749
                    if (n != null && q.prev == p) {
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   750
                        p.casNext(n, q);
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   751
                        if (p.prev == null)
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   752
                            signalNext(p);
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   753
                    }
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   754
                    break;
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   755
                }
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   756
                s = q;
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   757
                q = q.prev;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
     * Cancels an ongoing attempt to acquire.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
     *
58134
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   765
     * @param node the node (may be null if cancelled before enqueuing)
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   766
     * @param interrupted true if thread interrupted
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   767
     * @param interruptible if should report interruption vs reset
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
     */
58134
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   769
    private int cancelAcquire(Node node, boolean interrupted,
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   770
                              boolean interruptible) {
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   771
        if (node != null) {
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   772
            node.waiter = null;
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   773
            node.status = CANCELLED;
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   774
            if (node.prev != null)
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   775
                cleanQueue();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
        }
58134
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   777
        if (interrupted) {
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   778
            if (interruptible)
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   779
                return CANCELLED;
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   780
            else
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   781
                Thread.currentThread().interrupt();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
        }
58134
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   783
        return 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
    // Main exported methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
     * Attempts to acquire in exclusive mode. This method should query
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
     * if the state of the object permits it to be acquired in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
     * exclusive mode, and if so to acquire it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
     * <p>This method is always invoked by the thread performing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
     * acquire.  If this method reports failure, the acquire method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
     * may queue the thread, if it is not already queued, until it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
     * signalled by a release from some other thread. This can be used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
     * to implement method {@link Lock#tryLock()}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
     * <p>The default
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
     * implementation throws {@link UnsupportedOperationException}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
     * @param arg the acquire argument. This value is always the one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
     *        passed to an acquire method, or is the value saved on entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
     *        to a condition wait.  The value is otherwise uninterpreted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
     *        and can represent anything you like.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
     * @return {@code true} if successful. Upon success, this object has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
     *         been acquired.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
     * @throws IllegalMonitorStateException if acquiring would place this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
     *         synchronizer in an illegal state. This exception must be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
     *         thrown in a consistent fashion for synchronization to work
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
     *         correctly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
     * @throws UnsupportedOperationException if exclusive mode is not supported
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
    protected boolean tryAcquire(int arg) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
        throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
     * Attempts to set the state to reflect a release in exclusive
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
     * mode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
     * <p>This method is always invoked by the thread performing release.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
     * <p>The default implementation throws
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
     * {@link UnsupportedOperationException}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
     * @param arg the release argument. This value is always the one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
     *        passed to a release method, or the current state value upon
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
     *        entry to a condition wait.  The value is otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
     *        uninterpreted and can represent anything you like.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
     * @return {@code true} if this object is now in a fully released
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
     *         state, so that any waiting threads may attempt to acquire;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
     *         and {@code false} otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
     * @throws IllegalMonitorStateException if releasing would place this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
     *         synchronizer in an illegal state. This exception must be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
     *         thrown in a consistent fashion for synchronization to work
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
     *         correctly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
     * @throws UnsupportedOperationException if exclusive mode is not supported
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
    protected boolean tryRelease(int arg) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
        throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
     * Attempts to acquire in shared mode. This method should query if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
     * the state of the object permits it to be acquired in the shared
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
     * mode, and if so to acquire it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
     * <p>This method is always invoked by the thread performing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
     * acquire.  If this method reports failure, the acquire method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
     * may queue the thread, if it is not already queued, until it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
     * signalled by a release from some other thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
     * <p>The default implementation throws {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
     * UnsupportedOperationException}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
     * @param arg the acquire argument. This value is always the one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
     *        passed to an acquire method, or is the value saved on entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
     *        to a condition wait.  The value is otherwise uninterpreted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
     *        and can represent anything you like.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
     * @return a negative value on failure; zero if acquisition in shared
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
     *         mode succeeded but no subsequent shared-mode acquire can
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
     *         succeed; and a positive value if acquisition in shared
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
     *         mode succeeded and subsequent shared-mode acquires might
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
     *         also succeed, in which case a subsequent waiting thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
     *         must check availability. (Support for three different
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
     *         return values enables this method to be used in contexts
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
     *         where acquires only sometimes act exclusively.)  Upon
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
     *         success, this object has been acquired.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
     * @throws IllegalMonitorStateException if acquiring would place this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
     *         synchronizer in an illegal state. This exception must be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
     *         thrown in a consistent fashion for synchronization to work
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
     *         correctly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
     * @throws UnsupportedOperationException if shared mode is not supported
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
    protected int tryAcquireShared(int arg) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
        throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
     * Attempts to set the state to reflect a release in shared mode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
     * <p>This method is always invoked by the thread performing release.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
     * <p>The default implementation throws
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
     * {@link UnsupportedOperationException}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
     * @param arg the release argument. This value is always the one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
     *        passed to a release method, or the current state value upon
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
     *        entry to a condition wait.  The value is otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
     *        uninterpreted and can represent anything you like.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
     * @return {@code true} if this release of shared mode may permit a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
     *         waiting acquire (shared or exclusive) to succeed; and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
     *         {@code false} otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
     * @throws IllegalMonitorStateException if releasing would place this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
     *         synchronizer in an illegal state. This exception must be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
     *         thrown in a consistent fashion for synchronization to work
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
     *         correctly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
     * @throws UnsupportedOperationException if shared mode is not supported
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
    protected boolean tryReleaseShared(int arg) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
        throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
     * Returns {@code true} if synchronization is held exclusively with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
     * 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
   908
     * upon each call to a {@link ConditionObject} method.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
     * <p>The default implementation throws {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
     * UnsupportedOperationException}. This method is invoked
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
     * internally only within {@link ConditionObject} methods, so need
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
     * not be defined if conditions are not used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
     * @return {@code true} if synchronization is held exclusively;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
     *         {@code false} otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
     * @throws UnsupportedOperationException if conditions are not supported
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
    protected boolean isHeldExclusively() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
        throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
     * Acquires in exclusive mode, ignoring interrupts.  Implemented
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
     * by invoking at least once {@link #tryAcquire},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
     * returning on success.  Otherwise the thread is queued, possibly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
     * repeatedly blocking and unblocking, invoking {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
     * #tryAcquire} until success.  This method can be used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
     * to implement method {@link Lock#lock}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
     * @param arg the acquire argument.  This value is conveyed to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
     *        {@link #tryAcquire} but is otherwise uninterpreted and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
     *        can represent anything you like.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
    public final void acquire(int arg) {
58134
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   936
        if (!tryAcquire(arg))
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   937
            acquire(null, arg, false, false, false, 0L);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
     * Acquires in exclusive mode, aborting if interrupted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
     * Implemented by first checking interrupt status, then invoking
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
     * at least once {@link #tryAcquire}, returning on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
     * success.  Otherwise the thread is queued, possibly repeatedly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
     * blocking and unblocking, invoking {@link #tryAcquire}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
     * until success or the thread is interrupted.  This method can be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
     * used to implement method {@link Lock#lockInterruptibly}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
     * @param arg the acquire argument.  This value is conveyed to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
     *        {@link #tryAcquire} but is otherwise uninterpreted and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
     *        can represent anything you like.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
     * @throws InterruptedException if the current thread is interrupted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
     */
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   954
    public final void acquireInterruptibly(int arg)
58134
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   955
        throws InterruptedException {
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   956
        if (Thread.interrupted() ||
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   957
            (!tryAcquire(arg) && acquire(null, arg, false, true, false, 0L) < 0))
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
            throw new InterruptedException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
     * Attempts to acquire in exclusive mode, aborting if interrupted,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
     * and failing if the given timeout elapses.  Implemented by first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
     * checking interrupt status, then invoking at least once {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
     * #tryAcquire}, returning on success.  Otherwise, the thread is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
     * queued, possibly repeatedly blocking and unblocking, invoking
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
     * {@link #tryAcquire} until success or the thread is interrupted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
     * or the timeout elapses.  This method can be used to implement
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
     * method {@link Lock#tryLock(long, TimeUnit)}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
     * @param arg the acquire argument.  This value is conveyed to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
     *        {@link #tryAcquire} but is otherwise uninterpreted and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
     *        can represent anything you like.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
     * @param nanosTimeout the maximum number of nanoseconds to wait
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
     * @return {@code true} if acquired; {@code false} if timed out
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
     * @throws InterruptedException if the current thread is interrupted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
     */
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   978
    public final boolean tryAcquireNanos(int arg, long nanosTimeout)
58134
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   979
        throws InterruptedException {
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   980
        if (!Thread.interrupted()) {
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   981
            if (tryAcquire(arg))
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   982
                return true;
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   983
            if (nanosTimeout <= 0L)
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   984
                return false;
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   985
            int stat = acquire(null, arg, false, true, true,
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   986
                               System.nanoTime() + nanosTimeout);
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   987
            if (stat > 0)
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   988
                return true;
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   989
            if (stat == 0)
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   990
                return false;
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   991
        }
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
   992
        throw new InterruptedException();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
     * Releases in exclusive mode.  Implemented by unblocking one or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
     * more threads if {@link #tryRelease} returns true.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
     * This method can be used to implement method {@link Lock#unlock}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
     * @param arg the release argument.  This value is conveyed to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
     *        {@link #tryRelease} but is otherwise uninterpreted and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
     *        can represent anything you like.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
     * @return the value returned from {@link #tryRelease}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
    public final boolean release(int arg) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
        if (tryRelease(arg)) {
58134
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1007
            signalNext(head);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
     * Acquires in shared mode, ignoring interrupts.  Implemented by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
     * first invoking at least once {@link #tryAcquireShared},
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
     * returning on success.  Otherwise the thread is queued, possibly
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
     * repeatedly blocking and unblocking, invoking {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
     * #tryAcquireShared} until success.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
     * @param arg the acquire argument.  This value is conveyed to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
     *        {@link #tryAcquireShared} but is otherwise uninterpreted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
     *        and can represent anything you like.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
    public final void acquireShared(int arg) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
        if (tryAcquireShared(arg) < 0)
58134
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1026
            acquire(null, arg, true, false, false, 0L);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
     * Acquires in shared mode, aborting if interrupted.  Implemented
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
     * by first checking interrupt status, then invoking at least once
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
     * {@link #tryAcquireShared}, returning on success.  Otherwise the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
     * thread is queued, possibly repeatedly blocking and unblocking,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
     * invoking {@link #tryAcquireShared} until success or the thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
     * is interrupted.
18576
7a5c231327af 8019377: Sync j.u.c locks and atomic from 166 to tl
dl
parents: 14325
diff changeset
  1036
     * @param arg the acquire argument.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
     * This value is conveyed to {@link #tryAcquireShared} but is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
     * otherwise uninterpreted and can represent anything
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
     * you like.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
     * @throws InterruptedException if the current thread is interrupted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
     */
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
  1042
    public final void acquireSharedInterruptibly(int arg)
58134
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1043
        throws InterruptedException {
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1044
        if (Thread.interrupted() ||
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1045
            (tryAcquireShared(arg) < 0 &&
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1046
             acquire(null, arg, true, true, false, 0L) < 0))
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
            throw new InterruptedException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
     * Attempts to acquire in shared mode, aborting if interrupted, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
     * failing if the given timeout elapses.  Implemented by first
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
     * checking interrupt status, then invoking at least once {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
     * #tryAcquireShared}, returning on success.  Otherwise, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
     * thread is queued, possibly repeatedly blocking and unblocking,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
     * invoking {@link #tryAcquireShared} until success or the thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
     * is interrupted or the timeout elapses.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
     * @param arg the acquire argument.  This value is conveyed to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
     *        {@link #tryAcquireShared} but is otherwise uninterpreted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
     *        and can represent anything you like.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
     * @param nanosTimeout the maximum number of nanoseconds to wait
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
     * @return {@code true} if acquired; {@code false} if timed out
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
     * @throws InterruptedException if the current thread is interrupted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
     */
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
  1066
    public final boolean tryAcquireSharedNanos(int arg, long nanosTimeout)
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
  1067
            throws InterruptedException {
58134
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1068
        if (!Thread.interrupted()) {
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1069
            if (tryAcquireShared(arg) >= 0)
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1070
                return true;
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1071
            if (nanosTimeout <= 0L)
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1072
                return false;
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1073
            int stat = acquire(null, arg, true, true, true,
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1074
                               System.nanoTime() + nanosTimeout);
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1075
            if (stat > 0)
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1076
                return true;
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1077
            if (stat == 0)
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1078
                return false;
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1079
        }
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1080
        throw new InterruptedException();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
     * Releases in shared mode.  Implemented by unblocking one or more
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
     * threads if {@link #tryReleaseShared} returns true.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
     * @param arg the release argument.  This value is conveyed to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
     *        {@link #tryReleaseShared} but is otherwise uninterpreted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
     *        and can represent anything you like.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
     * @return the value returned from {@link #tryReleaseShared}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
    public final boolean releaseShared(int arg) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
        if (tryReleaseShared(arg)) {
58134
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1094
            signalNext(head);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
    // Queue inspection methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
     * Queries whether any threads are waiting to acquire. Note that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
     * because cancellations due to interrupts and timeouts may occur
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
     * at any time, a {@code true} return does not guarantee that any
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
     * other thread will ever acquire.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
     * @return {@code true} if there may be other threads waiting to acquire
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
    public final boolean hasQueuedThreads() {
48540
221cf8307606 8191483: AbstractQueuedSynchronizer cancel/cancel race
dl
parents: 48046
diff changeset
  1111
        for (Node p = tail, h = head; p != h && p != null; p = p.prev)
58134
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1112
            if (p.status >= 0)
48540
221cf8307606 8191483: AbstractQueuedSynchronizer cancel/cancel race
dl
parents: 48046
diff changeset
  1113
                return true;
221cf8307606 8191483: AbstractQueuedSynchronizer cancel/cancel race
dl
parents: 48046
diff changeset
  1114
        return false;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
     * 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
  1119
     * synchronizer; that is, if an acquire method has ever blocked.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
     * <p>In this implementation, this operation returns in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
     * constant time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
     * @return {@code true} if there has ever been contention
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
    public final boolean hasContended() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
        return head != null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
     * Returns the first (longest-waiting) thread in the queue, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
     * {@code null} if no threads are currently queued.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
     * <p>In this implementation, this operation normally returns in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
     * constant time, but may iterate upon contention if other threads are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
     * concurrently modifying the queue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
     * @return the first (longest-waiting) thread in the queue, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
     *         {@code null} if no threads are currently queued
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
    public final Thread getFirstQueuedThread() {
58134
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1142
        Thread first = null, w; Node h, s;
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1143
        if ((h = head) != null && ((s = h.next) == null ||
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1144
                                   (first = s.waiter) == null ||
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1145
                                   s.prev == null)) {
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1146
            // traverse from tail on stale reads
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1147
            for (Node p = tail, q; p != null && (q = p.prev) != null; p = q)
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1148
                if ((w = p.waiter) != null)
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1149
                    first = w;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
        }
58134
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1151
        return first;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
     * Returns true if the given thread is currently queued.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
     * <p>This implementation traverses the queue to determine
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
     * presence of the given thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
     * @param thread the thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
     * @return {@code true} if the given thread is on the queue
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
     * @throws NullPointerException if the thread is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
    public final boolean isQueued(Thread thread) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
        if (thread == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
        for (Node p = tail; p != null; p = p.prev)
58134
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1168
            if (p.waiter == thread)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
     * Returns {@code true} if the apparent first queued thread, if one
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
     * exists, is waiting in exclusive mode.  If this method returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
     * {@code true}, and the current thread is attempting to acquire in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
     * shared mode (that is, this method is invoked from {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
     * #tryAcquireShared}) then it is guaranteed that the current thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
     * is not the first queued thread.  Used only as a heuristic in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
     * ReentrantReadWriteLock.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
    final boolean apparentlyFirstQueuedIsExclusive() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
        Node h, s;
58134
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1184
        return (h = head) != null && (s = h.next)  != null &&
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1185
            !(s instanceof SharedNode) && s.waiter != null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
     * Queries whether any threads have been waiting to acquire longer
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
     * than the current thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
     * <p>An invocation of this method is equivalent to (but may be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
     * more efficient than):
32990
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  1194
     * <pre> {@code
34348
ba5c2f2fc9d7 8143087: Miscellaneous changes imported from jsr166 CVS 2015-11
dl
parents: 33674
diff changeset
  1195
     * getFirstQueuedThread() != Thread.currentThread()
ba5c2f2fc9d7 8143087: Miscellaneous changes imported from jsr166 CVS 2015-11
dl
parents: 33674
diff changeset
  1196
     *   && hasQueuedThreads()}</pre>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
     * <p>Note that because cancellations due to interrupts and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
     * timeouts may occur at any time, a {@code true} return does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
     * guarantee that some other thread will acquire before the current
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
     * thread.  Likewise, it is possible for another thread to win a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
     * race to enqueue after this method has returned {@code false},
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
     * due to the queue being empty.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
     * <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
  1206
     * avoid <a href="AbstractQueuedSynchronizer.html#barging">barging</a>.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
     * Such a synchronizer's {@link #tryAcquire} method should return
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
     * {@code false}, and its {@link #tryAcquireShared} method should
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
     * return a negative value, if this method returns {@code true}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
     * (unless this is a reentrant acquire).  For example, the {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
     * tryAcquire} method for a fair, reentrant, exclusive mode
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
     * synchronizer might look like this:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
     *
32990
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  1214
     * <pre> {@code
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
     * protected boolean tryAcquire(int arg) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
     *   if (isHeldExclusively()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
     *     // A reentrant acquire; increment hold count
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
     *     return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
     *   } else if (hasQueuedPredecessors()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
     *     return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
     *   } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
     *     // try to acquire normally
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
     *   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
     * }}</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
     * @return {@code true} if there is a queued thread preceding the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
     *         current thread, and {@code false} if the current thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
     *         is at the head of the queue or the queue is empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
     * @since 1.7
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
    public final boolean hasQueuedPredecessors() {
58134
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1232
        Thread first = null; Node h, s;
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1233
        if ((h = head) != null && ((s = h.next) == null ||
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1234
                                   (first = s.waiter) == null ||
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1235
                                   s.prev == null))
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1236
            first = getFirstQueuedThread(); // retry via getFirstQueuedThread
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1237
        return first != null && first != Thread.currentThread();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
    // Instrumentation and monitoring methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
     * Returns an estimate of the number of threads waiting to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
     * acquire.  The value is only an estimate because the number of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
     * threads may change dynamically while this method traverses
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
     * 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
  1247
     * monitoring system state, not for synchronization control.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
     * @return the estimated number of threads waiting to acquire
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
    public final int getQueueLength() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
        int n = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
        for (Node p = tail; p != null; p = p.prev) {
58134
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1254
            if (p.waiter != null)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
                ++n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
        return n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
     * Returns a collection containing threads that may be waiting to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
     * acquire.  Because the actual set of threads may change
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
     * dynamically while constructing this result, the returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
     * collection is only a best-effort estimate.  The elements of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
     * returned collection are in no particular order.  This method is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
     * designed to facilitate construction of subclasses that provide
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
     * more extensive monitoring facilities.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
     * @return the collection of threads
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
    public final Collection<Thread> getQueuedThreads() {
32990
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  1272
        ArrayList<Thread> list = new ArrayList<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
        for (Node p = tail; p != null; p = p.prev) {
58134
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1274
            Thread t = p.waiter;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
            if (t != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
                list.add(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
        return list;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
     * Returns a collection containing threads that may be waiting to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
     * acquire in exclusive mode. This has the same properties
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
     * as {@link #getQueuedThreads} except that it only returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
     * those threads waiting due to an exclusive acquire.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
     * @return the collection of threads
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
    public final Collection<Thread> getExclusiveQueuedThreads() {
32990
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  1290
        ArrayList<Thread> list = new ArrayList<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
        for (Node p = tail; p != null; p = p.prev) {
58134
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1292
            if (!(p instanceof SharedNode)) {
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1293
                Thread t = p.waiter;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
                if (t != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
                    list.add(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
        return list;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
     * Returns a collection containing threads that may be waiting to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
     * acquire in shared mode. This has the same properties
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
     * as {@link #getQueuedThreads} except that it only returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
     * those threads waiting due to a shared acquire.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
     * @return the collection of threads
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
    public final Collection<Thread> getSharedQueuedThreads() {
32990
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  1310
        ArrayList<Thread> list = new ArrayList<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
        for (Node p = tail; p != null; p = p.prev) {
58134
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1312
            if (p instanceof SharedNode) {
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1313
                Thread t = p.waiter;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
                if (t != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
                    list.add(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
        return list;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
     * Returns a string identifying this synchronizer, as well as its state.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
     * The state, in brackets, includes the String {@code "State ="}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
     * followed by the current value of {@link #getState}, and either
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
     * {@code "nonempty"} or {@code "empty"} depending on whether the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
     * queue is empty.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
     * @return a string identifying this synchronizer, as well as its state
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
    public String toString() {
32990
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  1331
        return super.toString()
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  1332
            + "[State = " + getState() + ", "
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  1333
            + (hasQueuedThreads() ? "non" : "") + "empty queue]";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
    // Instrumentation methods for conditions
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
     * Queries whether the given ConditionObject
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
     * uses this synchronizer as its lock.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
     * @param condition the condition
18576
7a5c231327af 8019377: Sync j.u.c locks and atomic from 166 to tl
dl
parents: 14325
diff changeset
  1343
     * @return {@code true} if owned
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
     * @throws NullPointerException if the condition is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
    public final boolean owns(ConditionObject condition) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
        return condition.isOwnedBy(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
     * Queries whether any threads are waiting on the given condition
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
     * 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
  1353
     * 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
  1354
     * does not guarantee that a future {@code signal} will awaken
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
     * any threads.  This method is designed primarily for use in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
     * monitoring of the system state.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
     * @param condition the condition
18576
7a5c231327af 8019377: Sync j.u.c locks and atomic from 166 to tl
dl
parents: 14325
diff changeset
  1359
     * @return {@code true} if there are any waiting threads
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
     * @throws IllegalMonitorStateException if exclusive synchronization
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
     *         is not held
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
     * @throws IllegalArgumentException if the given condition is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
     *         not associated with this synchronizer
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
     * @throws NullPointerException if the condition is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
    public final boolean hasWaiters(ConditionObject condition) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
        if (!owns(condition))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
            throw new IllegalArgumentException("Not owner");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
        return condition.hasWaiters();
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
     * Returns an estimate of the number of threads waiting on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
     * given condition associated with this synchronizer. Note that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
     * because timeouts and interrupts may occur at any time, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
     * 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
  1377
     * 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
  1378
     * state, not for synchronization control.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
     * @param condition the condition
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
     * @return the estimated number of waiting threads
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
     * @throws IllegalMonitorStateException if exclusive synchronization
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
     *         is not held
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
     * @throws IllegalArgumentException if the given condition is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
     *         not associated with this synchronizer
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
     * @throws NullPointerException if the condition is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
    public final int getWaitQueueLength(ConditionObject condition) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
        if (!owns(condition))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
            throw new IllegalArgumentException("Not owner");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
        return condition.getWaitQueueLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
     * Returns a collection containing those threads that may be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
     * waiting on the given condition associated with this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
     * synchronizer.  Because the actual set of threads may change
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
     * dynamically while constructing this result, the returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
     * collection is only a best-effort estimate. The elements of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
     * returned collection are in no particular order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
     * @param condition the condition
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
     * @return the collection of threads
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
     * @throws IllegalMonitorStateException if exclusive synchronization
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
     *         is not held
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1406
     * @throws IllegalArgumentException if the given condition is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1407
     *         not associated with this synchronizer
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1408
     * @throws NullPointerException if the condition is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1410
    public final Collection<Thread> getWaitingThreads(ConditionObject condition) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
        if (!owns(condition))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
            throw new IllegalArgumentException("Not owner");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1413
        return condition.getWaitingThreads();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1414
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
    /**
47302
f517fa4f4dc6 8187408: AbstractQueuedSynchronizer wait queue corrupted when thread awaits without holding the lock
dl
parents: 47216
diff changeset
  1417
     * Condition implementation for a {@link AbstractQueuedSynchronizer}
f517fa4f4dc6 8187408: AbstractQueuedSynchronizer wait queue corrupted when thread awaits without holding the lock
dl
parents: 47216
diff changeset
  1418
     * serving as the basis of a {@link Lock} implementation.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
     * <p>Method documentation for this class describes mechanics,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1421
     * not behavioral specifications from the point of view of Lock
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
     * and Condition users. Exported versions of this class will in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
     * general need to be accompanied by documentation describing
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
     * 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
  1425
     * {@code AbstractQueuedSynchronizer}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1427
     * <p>This class is Serializable, but all fields are transient,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1428
     * so deserialized conditions have no waiters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1429
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1430
    public class ConditionObject implements Condition, java.io.Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1431
        private static final long serialVersionUID = 1173984872572414699L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1432
        /** First node of condition queue. */
58134
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1433
        private transient ConditionNode firstWaiter;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1434
        /** Last node of condition queue. */
58134
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1435
        private transient ConditionNode lastWaiter;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1436
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1437
        /**
18576
7a5c231327af 8019377: Sync j.u.c locks and atomic from 166 to tl
dl
parents: 14325
diff changeset
  1438
         * Creates a new {@code ConditionObject} instance.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1439
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1440
        public ConditionObject() { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1441
58134
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1442
        // Signalling methods
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1443
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1444
        /**
58134
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1445
         * Removes and transfers one or all waiters to sync queue.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1446
         */
58134
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1447
        private void doSignal(ConditionNode first, boolean all) {
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1448
            while (first != null) {
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1449
                ConditionNode next = first.nextWaiter;
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1450
                if ((firstWaiter = next) == null)
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1451
                    lastWaiter = null;
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1452
                if ((first.getAndUnsetStatus(COND) & COND) != 0) {
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1453
                    enqueue(first);
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1454
                    if (!all)
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1455
                        break;
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1456
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1457
                first = next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1458
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1459
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1460
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1461
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1462
         * Moves the longest-waiting thread, if one exists, from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1463
         * wait queue for this condition to the wait queue for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1464
         * owning lock.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1465
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1466
         * @throws IllegalMonitorStateException if {@link #isHeldExclusively}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1467
         *         returns {@code false}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1468
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1469
        public final void signal() {
58134
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1470
            ConditionNode first = firstWaiter;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1471
            if (!isHeldExclusively())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1472
                throw new IllegalMonitorStateException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1473
            if (first != null)
58134
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1474
                doSignal(first, false);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1475
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1476
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1477
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1478
         * Moves all threads from the wait queue for this condition to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1479
         * the wait queue for the owning lock.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1480
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1481
         * @throws IllegalMonitorStateException if {@link #isHeldExclusively}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1482
         *         returns {@code false}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1483
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1484
        public final void signalAll() {
58134
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1485
            ConditionNode first = firstWaiter;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1486
            if (!isHeldExclusively())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1487
                throw new IllegalMonitorStateException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1488
            if (first != null)
58134
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1489
                doSignal(first, true);
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1490
        }
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1491
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1492
        // Waiting methods
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1493
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1494
        /**
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1495
         * Adds node to condition list and releases lock.
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1496
         *
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1497
         * @param node the node
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1498
         * @return savedState to reacquire after wait
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1499
         */
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1500
        private int enableWait(ConditionNode node) {
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1501
            if (isHeldExclusively()) {
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1502
                node.waiter = Thread.currentThread();
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1503
                node.setStatusRelaxed(COND | WAITING);
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1504
                ConditionNode last = lastWaiter;
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1505
                if (last == null)
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1506
                    firstWaiter = node;
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1507
                else
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1508
                    last.nextWaiter = node;
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1509
                lastWaiter = node;
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1510
                int savedState = getState();
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1511
                if (release(savedState))
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1512
                    return savedState;
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1513
            }
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1514
            node.status = CANCELLED; // lock not held or inconsistent
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1515
            throw new IllegalMonitorStateException();
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1516
        }
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1517
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1518
        /**
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1519
         * Returns true if a node that was initially placed on a condition
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1520
         * queue is now ready to reacquire on sync queue.
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1521
         * @param node the node
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1522
         * @return true if is reacquiring
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1523
         */
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1524
        private boolean canReacquire(ConditionNode node) {
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1525
            // check links, not status to avoid enqueue race
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1526
            return node != null && node.prev != null && isEnqueued(node);
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1527
        }
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1528
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1529
        /**
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1530
         * Unlinks the given node and other non-waiting nodes from
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1531
         * condition queue unless already unlinked.
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1532
         */
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1533
        private void unlinkCancelledWaiters(ConditionNode node) {
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1534
            if (node == null || node.nextWaiter != null || node == lastWaiter) {
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1535
                ConditionNode w = firstWaiter, trail = null;
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1536
                while (w != null) {
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1537
                    ConditionNode next = w.nextWaiter;
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1538
                    if ((w.status & COND) == 0) {
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1539
                        w.nextWaiter = null;
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1540
                        if (trail == null)
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1541
                            firstWaiter = next;
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1542
                        else
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1543
                            trail.nextWaiter = next;
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1544
                        if (next == null)
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1545
                            lastWaiter = trail;
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1546
                    } else
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1547
                        trail = w;
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1548
                    w = next;
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1549
                }
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1550
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1551
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1552
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1553
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1554
         * Implements uninterruptible condition wait.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1555
         * <ol>
32990
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  1556
         * <li>Save lock state returned by {@link #getState}.
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  1557
         * <li>Invoke {@link #release} with saved state as argument,
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  1558
         *     throwing IllegalMonitorStateException if it fails.
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  1559
         * <li>Block until signalled.
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  1560
         * <li>Reacquire by invoking specialized version of
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  1561
         *     {@link #acquire} with saved state as argument.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1562
         * </ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1563
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1564
        public final void awaitUninterruptibly() {
58134
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1565
            ConditionNode node = new ConditionNode();
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1566
            int savedState = enableWait(node);
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1567
            LockSupport.setCurrentBlocker(this); // for back-compatibility
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1568
            boolean interrupted = false;
58134
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1569
            while (!canReacquire(node)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1570
                if (Thread.interrupted())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1571
                    interrupted = true;
58134
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1572
                else if ((node.status & COND) != 0) {
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1573
                    try {
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1574
                        ForkJoinPool.managedBlock(node);
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1575
                    } catch (InterruptedException ie) {
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1576
                        interrupted = true;
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1577
                    }
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1578
                } else
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1579
                    Thread.onSpinWait();    // awoke while enqueuing
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1580
            }
58134
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1581
            LockSupport.setCurrentBlocker(null);
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1582
            node.clearStatus();
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1583
            acquire(node, savedState, false, false, false, 0L);
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1584
            if (interrupted)
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1585
                Thread.currentThread().interrupt();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1586
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1587
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1588
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1589
         * Implements interruptible condition wait.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1590
         * <ol>
32990
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  1591
         * <li>If current thread is interrupted, throw InterruptedException.
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  1592
         * <li>Save lock state returned by {@link #getState}.
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  1593
         * <li>Invoke {@link #release} with saved state as argument,
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  1594
         *     throwing IllegalMonitorStateException if it fails.
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  1595
         * <li>Block until signalled or interrupted.
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  1596
         * <li>Reacquire by invoking specialized version of
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  1597
         *     {@link #acquire} with saved state as argument.
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  1598
         * <li>If interrupted while blocked in step 4, throw InterruptedException.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1599
         * </ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1600
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1601
        public final void await() throws InterruptedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1602
            if (Thread.interrupted())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1603
                throw new InterruptedException();
58134
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1604
            ConditionNode node = new ConditionNode();
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1605
            int savedState = enableWait(node);
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1606
            LockSupport.setCurrentBlocker(this); // for back-compatibility
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1607
            boolean interrupted = false, cancelled = false;
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1608
            while (!canReacquire(node)) {
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1609
                if (interrupted |= Thread.interrupted()) {
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1610
                    if (cancelled = (node.getAndUnsetStatus(COND) & COND) != 0)
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1611
                        break;              // else interrupted after signal
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1612
                } else if ((node.status & COND) != 0) {
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1613
                    try {
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1614
                        ForkJoinPool.managedBlock(node);
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1615
                    } catch (InterruptedException ie) {
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1616
                        interrupted = true;
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1617
                    }
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1618
                } else
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1619
                    Thread.onSpinWait();    // awoke while enqueuing
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1620
            }
58134
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1621
            LockSupport.setCurrentBlocker(null);
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1622
            node.clearStatus();
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1623
            acquire(node, savedState, false, false, false, 0L);
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1624
            if (interrupted) {
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1625
                if (cancelled) {
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1626
                    unlinkCancelledWaiters(node);
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1627
                    throw new InterruptedException();
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1628
                }
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1629
                Thread.currentThread().interrupt();
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1630
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1631
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1632
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1633
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1634
         * Implements timed condition wait.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1635
         * <ol>
32990
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  1636
         * <li>If current thread is interrupted, throw InterruptedException.
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  1637
         * <li>Save lock state returned by {@link #getState}.
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  1638
         * <li>Invoke {@link #release} with saved state as argument,
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  1639
         *     throwing IllegalMonitorStateException if it fails.
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  1640
         * <li>Block until signalled, interrupted, or timed out.
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  1641
         * <li>Reacquire by invoking specialized version of
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  1642
         *     {@link #acquire} with saved state as argument.
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  1643
         * <li>If interrupted while blocked in step 4, throw InterruptedException.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1644
         * </ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1645
         */
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
  1646
        public final long awaitNanos(long nanosTimeout)
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
  1647
                throws InterruptedException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1648
            if (Thread.interrupted())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1649
                throw new InterruptedException();
58134
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1650
            ConditionNode node = new ConditionNode();
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1651
            int savedState = enableWait(node);
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1652
            long nanos = (nanosTimeout < 0L) ? 0L : nanosTimeout;
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1653
            long deadline = System.nanoTime() + nanos;
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1654
            boolean cancelled = false, interrupted = false;
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1655
            while (!canReacquire(node)) {
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1656
                if ((interrupted |= Thread.interrupted()) ||
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1657
                    (nanos = deadline - System.nanoTime()) <= 0L) {
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1658
                    if (cancelled = (node.getAndUnsetStatus(COND) & COND) != 0)
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1659
                        break;
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1660
                } else
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1661
                    LockSupport.parkNanos(this, nanos);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1662
            }
58134
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1663
            node.clearStatus();
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1664
            acquire(node, savedState, false, false, false, 0L);
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1665
            if (cancelled) {
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1666
                unlinkCancelledWaiters(node);
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1667
                if (interrupted)
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1668
                    throw new InterruptedException();
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1669
            } else if (interrupted)
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1670
                Thread.currentThread().interrupt();
32990
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  1671
            long remaining = deadline - System.nanoTime(); // avoid overflow
58134
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1672
            return (remaining <= nanosTimeout) ? remaining : Long.MIN_VALUE;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1673
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1674
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1675
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1676
         * Implements absolute timed condition wait.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1677
         * <ol>
32990
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  1678
         * <li>If current thread is interrupted, throw InterruptedException.
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  1679
         * <li>Save lock state returned by {@link #getState}.
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  1680
         * <li>Invoke {@link #release} with saved state as argument,
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  1681
         *     throwing IllegalMonitorStateException if it fails.
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  1682
         * <li>Block until signalled, interrupted, or timed out.
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  1683
         * <li>Reacquire by invoking specialized version of
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  1684
         *     {@link #acquire} with saved state as argument.
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  1685
         * <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
  1686
         * <li>If timed out while blocked in step 4, return false, else true.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1687
         * </ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1688
         */
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
  1689
        public final boolean awaitUntil(Date deadline)
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
  1690
                throws InterruptedException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1691
            long abstime = deadline.getTime();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1692
            if (Thread.interrupted())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1693
                throw new InterruptedException();
58134
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1694
            ConditionNode node = new ConditionNode();
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1695
            int savedState = enableWait(node);
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1696
            boolean cancelled = false, interrupted = false;
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1697
            while (!canReacquire(node)) {
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1698
                if ((interrupted |= Thread.interrupted()) ||
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1699
                    System.currentTimeMillis() >= abstime) {
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1700
                    if (cancelled = (node.getAndUnsetStatus(COND) & COND) != 0)
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1701
                        break;
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1702
                } else
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1703
                    LockSupport.parkUntil(this, abstime);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1704
            }
58134
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1705
            node.clearStatus();
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1706
            acquire(node, savedState, false, false, false, 0L);
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1707
            if (cancelled) {
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1708
                unlinkCancelledWaiters(node);
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1709
                if (interrupted)
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1710
                    throw new InterruptedException();
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1711
            } else if (interrupted)
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1712
                Thread.currentThread().interrupt();
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1713
            return !cancelled;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1714
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1715
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1716
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1717
         * Implements timed condition wait.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1718
         * <ol>
32990
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  1719
         * <li>If current thread is interrupted, throw InterruptedException.
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  1720
         * <li>Save lock state returned by {@link #getState}.
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  1721
         * <li>Invoke {@link #release} with saved state as argument,
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  1722
         *     throwing IllegalMonitorStateException if it fails.
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  1723
         * <li>Block until signalled, interrupted, or timed out.
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  1724
         * <li>Reacquire by invoking specialized version of
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  1725
         *     {@link #acquire} with saved state as argument.
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  1726
         * <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
  1727
         * <li>If timed out while blocked in step 4, return false, else true.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1728
         * </ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1729
         */
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
  1730
        public final boolean await(long time, TimeUnit unit)
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
  1731
                throws InterruptedException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1732
            long nanosTimeout = unit.toNanos(time);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1733
            if (Thread.interrupted())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1734
                throw new InterruptedException();
58134
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1735
            ConditionNode node = new ConditionNode();
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1736
            int savedState = enableWait(node);
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1737
            long nanos = (nanosTimeout < 0L) ? 0L : nanosTimeout;
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1738
            long deadline = System.nanoTime() + nanos;
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1739
            boolean cancelled = false, interrupted = false;
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1740
            while (!canReacquire(node)) {
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1741
                if ((interrupted |= Thread.interrupted()) ||
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1742
                    (nanos = deadline - System.nanoTime()) <= 0L) {
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1743
                    if (cancelled = (node.getAndUnsetStatus(COND) & COND) != 0)
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1744
                        break;
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1745
                } else
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1746
                    LockSupport.parkNanos(this, nanos);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1747
            }
58134
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1748
            node.clearStatus();
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1749
            acquire(node, savedState, false, false, false, 0L);
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1750
            if (cancelled) {
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1751
                unlinkCancelledWaiters(node);
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1752
                if (interrupted)
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1753
                    throw new InterruptedException();
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1754
            } else if (interrupted)
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1755
                Thread.currentThread().interrupt();
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1756
            return !cancelled;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1757
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1758
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1759
        //  support for instrumentation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1760
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1761
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1762
         * Returns true if this condition was created by the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1763
         * synchronization object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1764
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1765
         * @return {@code true} if owned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1766
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1767
        final boolean isOwnedBy(AbstractQueuedSynchronizer sync) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1768
            return sync == AbstractQueuedSynchronizer.this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1769
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1770
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1771
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1772
         * 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
  1773
         * Implements {@link AbstractQueuedSynchronizer#hasWaiters(ConditionObject)}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1774
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1775
         * @return {@code true} if there are any waiting threads
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1776
         * @throws IllegalMonitorStateException if {@link #isHeldExclusively}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1777
         *         returns {@code false}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1778
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1779
        protected final boolean hasWaiters() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1780
            if (!isHeldExclusively())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1781
                throw new IllegalMonitorStateException();
58134
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1782
            for (ConditionNode w = firstWaiter; w != null; w = w.nextWaiter) {
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1783
                if ((w.status & COND) != 0)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1784
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1785
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1786
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1787
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1788
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1789
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1790
         * Returns an estimate of the number of threads waiting on
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1791
         * this condition.
18576
7a5c231327af 8019377: Sync j.u.c locks and atomic from 166 to tl
dl
parents: 14325
diff changeset
  1792
         * Implements {@link AbstractQueuedSynchronizer#getWaitQueueLength(ConditionObject)}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1793
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1794
         * @return the estimated number of waiting threads
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1795
         * @throws IllegalMonitorStateException if {@link #isHeldExclusively}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1796
         *         returns {@code false}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1797
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1798
        protected final int getWaitQueueLength() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1799
            if (!isHeldExclusively())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1800
                throw new IllegalMonitorStateException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1801
            int n = 0;
58134
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1802
            for (ConditionNode w = firstWaiter; w != null; w = w.nextWaiter) {
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1803
                if ((w.status & COND) != 0)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1804
                    ++n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1805
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1806
            return n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1807
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1808
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1809
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1810
         * Returns a collection containing those threads that may be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1811
         * waiting on this Condition.
18576
7a5c231327af 8019377: Sync j.u.c locks and atomic from 166 to tl
dl
parents: 14325
diff changeset
  1812
         * Implements {@link AbstractQueuedSynchronizer#getWaitingThreads(ConditionObject)}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1813
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1814
         * @return the collection of threads
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1815
         * @throws IllegalMonitorStateException if {@link #isHeldExclusively}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1816
         *         returns {@code false}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1817
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1818
        protected final Collection<Thread> getWaitingThreads() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1819
            if (!isHeldExclusively())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1820
                throw new IllegalMonitorStateException();
32990
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  1821
            ArrayList<Thread> list = new ArrayList<>();
58134
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1822
            for (ConditionNode w = firstWaiter; w != null; w = w.nextWaiter) {
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1823
                if ((w.status & COND) != 0) {
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1824
                    Thread t = w.waiter;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1825
                    if (t != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1826
                        list.add(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1827
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1828
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1829
            return list;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1830
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1831
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1832
58134
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1833
    // Unsafe
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1834
    private static final Unsafe U = Unsafe.getUnsafe();
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1835
    private static final long STATE
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1836
        = U.objectFieldOffset(AbstractQueuedSynchronizer.class, "state");
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1837
    private static final long HEAD
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1838
        = U.objectFieldOffset(AbstractQueuedSynchronizer.class, "head");
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1839
    private static final long TAIL
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 54685
diff changeset
  1840
        = U.objectFieldOffset(AbstractQueuedSynchronizer.class, "tail");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1841
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1842
    static {
32990
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  1843
        Class<?> ensureLoaded = LockSupport.class;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1844
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1845
}