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