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