src/java.base/share/classes/java/util/concurrent/locks/AbstractQueuedLongSynchronizer.java
author chegar
Thu, 17 Oct 2019 20:54:25 +0100
branchdatagramsocketimpl-branch
changeset 58679 9c3209ff7550
parent 58678 9cf78a70fa4f
parent 58385 489532b89775
permissions -rw-r--r--
datagramsocketimpl-branch: merge with default
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2430
diff changeset
     6
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2430
diff changeset
     8
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2430
diff changeset
    20
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2430
diff changeset
    21
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2430
diff changeset
    22
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * This file is available under and governed by the GNU General Public
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * License version 2 only, as published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * However, the following notice accompanied the original version of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * file:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * Written by Doug Lea with assistance from members of JCP JSR-166
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * Expert Group and released to the public domain, as explained at
9242
ef138d47df58 7034657: Update Creative Commons license URL in legal notices
dl
parents: 7518
diff changeset
    33
 * http://creativecommons.org/publicdomain/zero/1.0/
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
package java.util.concurrent.locks;
32990
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
    37
14325
622c473a21aa 8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents: 9242
diff changeset
    38
import java.util.ArrayList;
622c473a21aa 8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents: 9242
diff changeset
    39
import java.util.Collection;
622c473a21aa 8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents: 9242
diff changeset
    40
import java.util.Date;
32990
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
    41
import java.util.concurrent.TimeUnit;
58134
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
    42
import java.util.concurrent.ForkJoinPool;
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
    43
import jdk.internal.misc.Unsafe;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * 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
    47
 * which synchronization state is maintained as a {@code long}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * 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
    49
 * as {@code AbstractQueuedSynchronizer} with the exception
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * 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
    51
 * as {@code long} rather than {@code int}. This class
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * may be useful when creating synchronizers such as
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * multilevel locks and barriers that require
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * 64 bits of state.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * <p>See {@link AbstractQueuedSynchronizer} for usage
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * notes and examples.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * @author Doug Lea
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
public abstract class AbstractQueuedLongSynchronizer
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    extends AbstractOwnableSynchronizer
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    implements java.io.Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    private static final long serialVersionUID = 7373984972572414692L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    /*
47302
f517fa4f4dc6 8187408: AbstractQueuedSynchronizer wait queue corrupted when thread awaits without holding the lock
dl
parents: 47216
diff changeset
    69
     * 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
    70
     * exactly cloned from AbstractQueuedSynchronizer, replacing class
f517fa4f4dc6 8187408: AbstractQueuedSynchronizer wait queue corrupted when thread awaits without holding the lock
dl
parents: 47216
diff changeset
    71
     * 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
    72
     * keep it that way.
f517fa4f4dc6 8187408: AbstractQueuedSynchronizer wait queue corrupted when thread awaits without holding the lock
dl
parents: 47216
diff changeset
    73
     */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
58134
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
    75
    // Node status bits, also used as argument and return values
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
    76
    static final int WAITING   = 1;          // must be 1
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
    77
    static final int CANCELLED = 0x80000000; // must be negative
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
    78
    static final int COND      = 2;          // in a condition wait
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
    79
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
    80
    /** CLH Nodes */
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
    81
    abstract static class Node {
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
    82
        volatile Node prev;       // initially attached via casTail
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
    83
        volatile Node next;       // visibly nonnull when signallable
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
    84
        Thread waiter;            // visibly nonnull when enqueued
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
    85
        volatile int status;      // written by owner, atomic bit ops by others
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
    86
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
    87
        // methods for atomic operations
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
    88
        final boolean casPrev(Node c, Node v) {  // for cleanQueue
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
    89
            return U.weakCompareAndSetReference(this, PREV, c, v);
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
    90
        }
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
    91
        final boolean casNext(Node c, Node v) {  // for cleanQueue
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
    92
            return U.weakCompareAndSetReference(this, NEXT, c, v);
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
    93
        }
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
    94
        final int getAndUnsetStatus(int v) {     // for signalling
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
    95
            return U.getAndBitwiseAndInt(this, STATUS, ~v);
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
    96
        }
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
    97
        final void setPrevRelaxed(Node p) {      // for off-queue assignment
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
    98
            U.putReference(this, PREV, p);
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
    99
        }
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   100
        final void setStatusRelaxed(int s) {     // for off-queue assignment
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   101
            U.putInt(this, STATUS, s);
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   102
        }
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   103
        final void clearStatus() {               // for reducing unneeded signals
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   104
            U.putIntOpaque(this, STATUS, 0);
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   105
        }
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   106
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   107
        private static final long STATUS
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   108
            = U.objectFieldOffset(Node.class, "status");
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   109
        private static final long NEXT
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   110
            = U.objectFieldOffset(Node.class, "next");
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   111
        private static final long PREV
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   112
            = U.objectFieldOffset(Node.class, "prev");
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   113
    }
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   114
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   115
    // Concrete classes tagged by type
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   116
    static final class ExclusiveNode extends Node { }
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   117
    static final class SharedNode extends Node { }
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   118
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   119
    static final class ConditionNode extends Node
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   120
        implements ForkJoinPool.ManagedBlocker {
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   121
        ConditionNode nextWaiter;            // link to next waiting node
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   122
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   123
        /**
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   124
         * Allows Conditions to be used in ForkJoinPools without
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   125
         * risking fixed pool exhaustion. This is usable only for
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   126
         * untimed Condition waits, not timed versions.
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   127
         */
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   128
        public final boolean isReleasable() {
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   129
            return status <= 1 || Thread.currentThread().isInterrupted();
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   130
        }
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   131
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   132
        public final boolean block() {
58385
489532b89775 8231032: ThreadMXBean locking tests fail after JSR 166 refresh
dl
parents: 58134
diff changeset
   133
            while (!isReleasable()) LockSupport.park();
58134
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   134
            return true;
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   135
        }
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   136
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    /**
58134
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   139
     * Head of the wait queue, lazily initialized.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    private transient volatile Node head;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    /**
58134
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   144
     * Tail of the wait queue. After initialization, modified only via casTail.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    private transient volatile Node tail;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * The synchronization state.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    private volatile long state;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * 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
   155
     * This operation has memory semantics of a {@code volatile} read.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * @return current state value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    protected final long getState() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        return state;
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
     * 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
   164
     * This operation has memory semantics of a {@code volatile} write.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * @param newState the new state value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    protected final void setState(long newState) {
58134
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   168
        state = newState;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * Atomically sets synchronization state to the given updated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * 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
   174
     * This operation has memory semantics of a {@code volatile} read
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * and write.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * @param expect the expected value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * @param update the new value
18576
7a5c231327af 8019377: Sync j.u.c locks and atomic from 166 to tl
dl
parents: 14325
diff changeset
   179
     * @return {@code true} if successful. False return indicates that the actual
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     *         value was not equal to the expected value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    protected final boolean compareAndSetState(long expect, long update) {
58134
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   183
        return U.compareAndSetLong(this, STATE, expect, update);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    // Queuing utilities
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
58134
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   188
    private boolean casTail(Node c, Node v) {
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   189
        return U.compareAndSetReference(this, TAIL, c, v);
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   190
    }
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   191
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   192
    /** tries once to CAS a new dummy node for head */
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   193
    private void tryInitializeHead() {
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   194
        Node h = new ExclusiveNode();
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   195
        if (U.compareAndSetReference(this, HEAD, null, h))
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   196
            tail = h;
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   197
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    /**
58134
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   200
     * Enqueues the node unless null. (Currently used only for
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   201
     * ConditionNodes; other cases are interleaved with acquires.)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     */
58134
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   203
    final void enqueue(Node node) {
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   204
        if (node != null) {
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   205
            for (;;) {
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   206
                Node t = tail;
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   207
                node.setPrevRelaxed(t);        // avoid unnecessary fence
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   208
                if (t == null)                 // initialize
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   209
                    tryInitializeHead();
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   210
                else if (casTail(t, node)) {
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   211
                    t.next = node;
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   212
                    if (t.status < 0)          // wake up to clean link
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   213
                        LockSupport.unpark(node.waiter);
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   214
                    break;
32990
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
   215
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
58134
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   220
    /** Returns true if node is found in traversal from tail */
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   221
    final boolean isEnqueued(Node node) {
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   222
        for (Node t = tail; t != null; t = t.prev)
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   223
            if (t == node)
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   224
                return true;
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   225
        return false;
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   226
    }
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   227
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    /**
58134
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   229
     * Wakes up the successor of given node, if one exists, and unsets its
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   230
     * WAITING status to avoid park race. This may fail to wake up an
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   231
     * eligible thread when one or more have been cancelled, but
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   232
     * cancelAcquire ensures liveness.
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   233
     */
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   234
    private static void signalNext(Node h) {
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   235
        Node s;
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   236
        if (h != null && (s = h.next) != null && s.status != 0) {
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   237
            s.getAndUnsetStatus(WAITING);
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   238
            LockSupport.unpark(s.waiter);
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   239
        }
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   240
    }
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   241
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   242
    /** Wakes up the given node if in shared mode */
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   243
    private static void signalNextIfShared(Node h) {
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   244
        Node s;
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   245
        if (h != null && (s = h.next) != null &&
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   246
            (s instanceof SharedNode) && s.status != 0) {
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   247
            s.getAndUnsetStatus(WAITING);
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   248
            LockSupport.unpark(s.waiter);
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   249
        }
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   250
    }
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   251
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   252
    /**
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   253
     * Main acquire method, invoked by all exported acquire methods.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     *
58134
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   255
     * @param node null unless a reacquiring Condition
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   256
     * @param arg the acquire argument
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   257
     * @param shared true if shared mode else exclusive
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   258
     * @param interruptible if abort and return negative on interrupt
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   259
     * @param timed if true use timed waits
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   260
     * @param time if timed, the System.nanoTime value to timeout
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   261
     * @return positive if acquired, 0 if timed out, negative if interrupted
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     */
58134
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   263
    final int acquire(Node node, long arg, boolean shared,
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   264
                      boolean interruptible, boolean timed, long time) {
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   265
        Thread current = Thread.currentThread();
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   266
        byte spins = 0, postSpins = 0;   // retries upon unpark of first thread
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   267
        boolean interrupted = false, first = false;
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   268
        Node pred = null;                // predecessor of node when enqueued
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   269
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   270
        /*
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   271
         * Repeatedly:
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   272
         *  Check if node now first
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   273
         *    if so, ensure head stable, else ensure valid predecessor
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   274
         *  if node is first or not yet enqueued, try acquiring
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   275
         *  else if node not yet created, create it
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   276
         *  else if not yet enqueued, try once to enqueue
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   277
         *  else if woken from park, retry (up to postSpins times)
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   278
         *  else if WAITING status not set, set and retry
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   279
         *  else park and clear WAITING status, and check cancellation
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   280
         */
32990
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
   281
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
   282
        for (;;) {
58134
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   283
            if (!first && (pred = (node == null) ? null : node.prev) != null &&
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   284
                !(first = (head == pred))) {
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   285
                if (pred.status < 0) {
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   286
                    cleanQueue();           // predecessor cancelled
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   287
                    continue;
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   288
                } else if (pred.prev == null) {
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   289
                    Thread.onSpinWait();    // ensure serialization
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   290
                    continue;
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   291
                }
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   292
            }
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   293
            if (first || pred == null) {
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   294
                boolean acquired;
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   295
                try {
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   296
                    if (shared)
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   297
                        acquired = (tryAcquireShared(arg) >= 0);
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   298
                    else
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   299
                        acquired = tryAcquire(arg);
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   300
                } catch (Throwable ex) {
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   301
                    cancelAcquire(node, interrupted, false);
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   302
                    throw ex;
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   303
                }
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   304
                if (acquired) {
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   305
                    if (first) {
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   306
                        node.prev = null;
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   307
                        head = node;
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   308
                        pred.next = null;
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   309
                        node.waiter = null;
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   310
                        if (shared)
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   311
                            signalNextIfShared(node);
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   312
                        if (interrupted)
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   313
                            current.interrupt();
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   314
                    }
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   315
                    return 1;
32990
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
   316
                }
58134
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   317
            }
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   318
            if (node == null) {                 // allocate; retry before enqueue
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   319
                if (shared)
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   320
                    node = new SharedNode();
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   321
                else
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   322
                    node = new ExclusiveNode();
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   323
            } else if (pred == null) {          // try to enqueue
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   324
                node.waiter = current;
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   325
                Node t = tail;
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   326
                node.setPrevRelaxed(t);         // avoid unnecessary fence
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   327
                if (t == null)
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   328
                    tryInitializeHead();
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   329
                else if (!casTail(t, node))
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   330
                    node.setPrevRelaxed(null);  // back out
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   331
                else
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   332
                    t.next = node;
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   333
            } else if (first && spins != 0) {
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   334
                --spins;                        // reduce unfairness on rewaits
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   335
                Thread.onSpinWait();
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   336
            } else if (node.status == 0) {
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   337
                node.status = WAITING;          // enable signal and recheck
32990
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
   338
            } else {
58134
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   339
                long nanos;
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   340
                spins = postSpins = (byte)((postSpins << 1) | 1);
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   341
                if (!timed)
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   342
                    LockSupport.park(this);
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   343
                else if ((nanos = time - System.nanoTime()) > 0L)
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   344
                    LockSupport.parkNanos(this, nanos);
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   345
                else
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   346
                    break;
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   347
                node.clearStatus();
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   348
                if ((interrupted |= Thread.interrupted()) && interruptible)
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   349
                    break;
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   350
            }
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   351
        }
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   352
        return cancelAcquire(node, interrupted, interruptible);
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   353
    }
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   354
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   355
    /**
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   356
     * Possibly repeatedly traverses from tail, unsplicing cancelled
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   357
     * nodes until none are found.
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   358
     */
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   359
    private void cleanQueue() {
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   360
        for (;;) {                               // restart point
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   361
            for (Node q = tail, s = null, p, n;;) { // (p, q, s) triples
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   362
                if (q == null || (p = q.prev) == null)
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   363
                    return;                      // end of list
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   364
                if (s == null ? tail != q : (s.prev != q || s.status < 0))
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   365
                    break;                       // inconsistent
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   366
                if (q.status < 0) {              // cancelled
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   367
                    if ((s == null ? casTail(q, p) : s.casPrev(q, p)) &&
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   368
                        q.prev == p) {
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   369
                        p.casNext(q, s);         // OK if fails
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   370
                        if (p.prev == null)
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   371
                            signalNext(p);
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   372
                    }
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   373
                    break;
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   374
                }
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   375
                if ((n = p.next) != q) {         // help finish
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   376
                    if (n != null && q.prev == p) {
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   377
                        p.casNext(n, q);
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   378
                        if (p.prev == null)
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   379
                            signalNext(p);
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   380
                    }
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   381
                    break;
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   382
                }
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   383
                s = q;
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   384
                q = q.prev;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     * Cancels an ongoing attempt to acquire.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     *
58134
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   392
     * @param node the node (may be null if cancelled before enqueuing)
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   393
     * @param interrupted true if thread interrupted
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   394
     * @param interruptible if should report interruption vs reset
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     */
58134
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   396
    private int cancelAcquire(Node node, boolean interrupted,
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   397
                              boolean interruptible) {
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   398
        if (node != null) {
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   399
            node.waiter = null;
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   400
            node.status = CANCELLED;
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   401
            if (node.prev != null)
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   402
                cleanQueue();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        }
58134
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   404
        if (interrupted) {
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   405
            if (interruptible)
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   406
                return CANCELLED;
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   407
            else
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   408
                Thread.currentThread().interrupt();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        }
58134
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   410
        return 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
    // Main exported methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     * Attempts to acquire in exclusive mode. This method should query
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     * if the state of the object permits it to be acquired in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     * exclusive mode, and if so to acquire it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     * <p>This method is always invoked by the thread performing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     * acquire.  If this method reports failure, the acquire method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     * may queue the thread, if it is not already queued, until it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     * signalled by a release from some other thread. This can be used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     * to implement method {@link Lock#tryLock()}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     * <p>The default
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     * implementation throws {@link UnsupportedOperationException}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     * @param arg the acquire argument. This value is always the one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
     *        passed to an acquire method, or is the value saved on entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     *        to a condition wait.  The value is otherwise uninterpreted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     *        and can represent anything you like.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     * @return {@code true} if successful. Upon success, this object has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
     *         been acquired.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
     * @throws IllegalMonitorStateException if acquiring would place this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     *         synchronizer in an illegal state. This exception must be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     *         thrown in a consistent fashion for synchronization to work
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     *         correctly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
     * @throws UnsupportedOperationException if exclusive mode is not supported
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
    protected boolean tryAcquire(long arg) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
        throw new UnsupportedOperationException();
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
     * Attempts to set the state to reflect a release in exclusive
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
     * mode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
     * <p>This method is always invoked by the thread performing release.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
     * <p>The default implementation throws
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
     * {@link UnsupportedOperationException}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
     * @param arg the release argument. This value is always the one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
     *        passed to a release method, or the current state value upon
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
     *        entry to a condition wait.  The value is otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
     *        uninterpreted and can represent anything you like.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
     * @return {@code true} if this object is now in a fully released
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
     *         state, so that any waiting threads may attempt to acquire;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
     *         and {@code false} otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
     * @throws IllegalMonitorStateException if releasing would place this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
     *         synchronizer in an illegal state. This exception must be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
     *         thrown in a consistent fashion for synchronization to work
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
     *         correctly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
     * @throws UnsupportedOperationException if exclusive mode is not supported
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
    protected boolean tryRelease(long arg) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
        throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
     * Attempts to acquire in shared mode. This method should query if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
     * the state of the object permits it to be acquired in the shared
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
     * mode, and if so to acquire it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
     * <p>This method is always invoked by the thread performing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
     * acquire.  If this method reports failure, the acquire method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
     * may queue the thread, if it is not already queued, until it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
     * signalled by a release from some other thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
     * <p>The default implementation throws {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
     * UnsupportedOperationException}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
     * @param arg the acquire argument. This value is always the one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
     *        passed to an acquire method, or is the value saved on entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
     *        to a condition wait.  The value is otherwise uninterpreted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
     *        and can represent anything you like.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
     * @return a negative value on failure; zero if acquisition in shared
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
     *         mode succeeded but no subsequent shared-mode acquire can
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
     *         succeed; and a positive value if acquisition in shared
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
     *         mode succeeded and subsequent shared-mode acquires might
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
     *         also succeed, in which case a subsequent waiting thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
     *         must check availability. (Support for three different
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
     *         return values enables this method to be used in contexts
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
     *         where acquires only sometimes act exclusively.)  Upon
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
     *         success, this object has been acquired.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
     * @throws IllegalMonitorStateException if acquiring would place this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
     *         synchronizer in an illegal state. This exception must be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
     *         thrown in a consistent fashion for synchronization to work
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
     *         correctly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
     * @throws UnsupportedOperationException if shared mode is not supported
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
    protected long tryAcquireShared(long arg) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
        throw new UnsupportedOperationException();
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
     * Attempts to set the state to reflect a release in shared mode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
     * <p>This method is always invoked by the thread performing release.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
     * <p>The default implementation throws
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
     * {@link UnsupportedOperationException}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
     * @param arg the release argument. This value is always the one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
     *        passed to a release method, or the current state value upon
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
     *        entry to a condition wait.  The value is otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
     *        uninterpreted and can represent anything you like.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
     * @return {@code true} if this release of shared mode may permit a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
     *         waiting acquire (shared or exclusive) to succeed; and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
     *         {@code false} otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
     * @throws IllegalMonitorStateException if releasing would place this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
     *         synchronizer in an illegal state. This exception must be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
     *         thrown in a consistent fashion for synchronization to work
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
     *         correctly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
     * @throws UnsupportedOperationException if shared mode is not supported
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
    protected boolean tryReleaseShared(long arg) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
        throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
     * Returns {@code true} if synchronization is held exclusively with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
     * 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
   535
     * upon each call to a {@link ConditionObject} method.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
     * <p>The default implementation throws {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
     * UnsupportedOperationException}. This method is invoked
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
     * internally only within {@link ConditionObject} methods, so need
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
     * not be defined if conditions are not used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
     * @return {@code true} if synchronization is held exclusively;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
     *         {@code false} otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
     * @throws UnsupportedOperationException if conditions are not supported
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
    protected boolean isHeldExclusively() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
        throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
     * Acquires in exclusive mode, ignoring interrupts.  Implemented
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
     * by invoking at least once {@link #tryAcquire},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
     * returning on success.  Otherwise the thread is queued, possibly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
     * repeatedly blocking and unblocking, invoking {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
     * #tryAcquire} until success.  This method can be used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
     * to implement method {@link Lock#lock}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
     * @param arg the acquire argument.  This value is conveyed to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
     *        {@link #tryAcquire} but is otherwise uninterpreted and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
     *        can represent anything you like.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
    public final void acquire(long arg) {
58134
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   563
        if (!tryAcquire(arg))
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   564
            acquire(null, arg, false, false, false, 0L);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
     * Acquires in exclusive mode, aborting if interrupted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
     * Implemented by first checking interrupt status, then invoking
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
     * at least once {@link #tryAcquire}, returning on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
     * success.  Otherwise the thread is queued, possibly repeatedly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
     * blocking and unblocking, invoking {@link #tryAcquire}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
     * until success or the thread is interrupted.  This method can be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
     * used to implement method {@link Lock#lockInterruptibly}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
     * @param arg the acquire argument.  This value is conveyed to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
     *        {@link #tryAcquire} but is otherwise uninterpreted and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
     *        can represent anything you like.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
     * @throws InterruptedException if the current thread is interrupted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
     */
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   581
    public final void acquireInterruptibly(long arg)
58134
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   582
        throws InterruptedException {
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   583
        if (Thread.interrupted() ||
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   584
            (!tryAcquire(arg) && acquire(null, arg, false, true, false, 0L) < 0))
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
            throw new InterruptedException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
     * Attempts to acquire in exclusive mode, aborting if interrupted,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
     * and failing if the given timeout elapses.  Implemented by first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
     * checking interrupt status, then invoking at least once {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
     * #tryAcquire}, returning on success.  Otherwise, the thread is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
     * queued, possibly repeatedly blocking and unblocking, invoking
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
     * {@link #tryAcquire} until success or the thread is interrupted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
     * or the timeout elapses.  This method can be used to implement
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
     * method {@link Lock#tryLock(long, TimeUnit)}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
     * @param arg the acquire argument.  This value is conveyed to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
     *        {@link #tryAcquire} but is otherwise uninterpreted and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
     *        can represent anything you like.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
     * @param nanosTimeout the maximum number of nanoseconds to wait
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
     * @return {@code true} if acquired; {@code false} if timed out
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
     * @throws InterruptedException if the current thread is interrupted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
     */
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   605
    public final boolean tryAcquireNanos(long arg, long nanosTimeout)
58134
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   606
        throws InterruptedException {
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   607
        if (!Thread.interrupted()) {
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   608
            if (tryAcquire(arg))
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   609
                return true;
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   610
            if (nanosTimeout <= 0L)
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   611
                return false;
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   612
            int stat = acquire(null, arg, false, true, true,
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   613
                               System.nanoTime() + nanosTimeout);
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   614
            if (stat > 0)
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   615
                return true;
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   616
            if (stat == 0)
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   617
                return false;
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   618
        }
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   619
        throw new InterruptedException();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
     * Releases in exclusive mode.  Implemented by unblocking one or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
     * more threads if {@link #tryRelease} returns true.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
     * This method can be used to implement method {@link Lock#unlock}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
     * @param arg the release argument.  This value is conveyed to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
     *        {@link #tryRelease} but is otherwise uninterpreted and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
     *        can represent anything you like.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
     * @return the value returned from {@link #tryRelease}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
    public final boolean release(long arg) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
        if (tryRelease(arg)) {
58134
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   634
            signalNext(head);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
     * Acquires in shared mode, ignoring interrupts.  Implemented by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
     * first invoking at least once {@link #tryAcquireShared},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
     * returning on success.  Otherwise the thread is queued, possibly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
     * repeatedly blocking and unblocking, invoking {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
     * #tryAcquireShared} until success.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
     * @param arg the acquire argument.  This value is conveyed to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
     *        {@link #tryAcquireShared} but is otherwise uninterpreted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
     *        and can represent anything you like.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
    public final void acquireShared(long arg) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
        if (tryAcquireShared(arg) < 0)
58134
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   653
            acquire(null, arg, true, false, false, 0L);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
     * Acquires in shared mode, aborting if interrupted.  Implemented
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
     * by first checking interrupt status, then invoking at least once
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
     * {@link #tryAcquireShared}, returning on success.  Otherwise the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
     * thread is queued, possibly repeatedly blocking and unblocking,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
     * invoking {@link #tryAcquireShared} until success or the thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
     * is interrupted.
18576
7a5c231327af 8019377: Sync j.u.c locks and atomic from 166 to tl
dl
parents: 14325
diff changeset
   663
     * @param arg the acquire argument.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
     * This value is conveyed to {@link #tryAcquireShared} but is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
     * otherwise uninterpreted and can represent anything
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
     * you like.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
     * @throws InterruptedException if the current thread is interrupted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
     */
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   669
    public final void acquireSharedInterruptibly(long arg)
58134
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   670
        throws InterruptedException {
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   671
        if (Thread.interrupted() ||
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   672
            (tryAcquireShared(arg) < 0 &&
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   673
             acquire(null, arg, true, true, false, 0L) < 0))
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
            throw new InterruptedException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
     * Attempts to acquire in shared mode, aborting if interrupted, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
     * failing if the given timeout elapses.  Implemented by first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
     * checking interrupt status, then invoking at least once {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
     * #tryAcquireShared}, returning on success.  Otherwise, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
     * thread is queued, possibly repeatedly blocking and unblocking,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
     * invoking {@link #tryAcquireShared} until success or the thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
     * is interrupted or the timeout elapses.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
     * @param arg the acquire argument.  This value is conveyed to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
     *        {@link #tryAcquireShared} but is otherwise uninterpreted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
     *        and can represent anything you like.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
     * @param nanosTimeout the maximum number of nanoseconds to wait
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
     * @return {@code true} if acquired; {@code false} if timed out
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
     * @throws InterruptedException if the current thread is interrupted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
     */
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   693
    public final boolean tryAcquireSharedNanos(long arg, long nanosTimeout)
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   694
            throws InterruptedException {
58134
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   695
        if (!Thread.interrupted()) {
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   696
            if (tryAcquireShared(arg) >= 0)
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   697
                return true;
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   698
            if (nanosTimeout <= 0L)
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   699
                return false;
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   700
            int stat = acquire(null, arg, true, true, true,
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   701
                               System.nanoTime() + nanosTimeout);
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   702
            if (stat > 0)
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   703
                return true;
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   704
            if (stat == 0)
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   705
                return false;
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   706
        }
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   707
        throw new InterruptedException();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
     * Releases in shared mode.  Implemented by unblocking one or more
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
     * threads if {@link #tryReleaseShared} returns true.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
     * @param arg the release argument.  This value is conveyed to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
     *        {@link #tryReleaseShared} but is otherwise uninterpreted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
     *        and can represent anything you like.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
     * @return the value returned from {@link #tryReleaseShared}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
    public final boolean releaseShared(long arg) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
        if (tryReleaseShared(arg)) {
58134
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   721
            signalNext(head);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
    // Queue inspection methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
     * Queries whether any threads are waiting to acquire. Note that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
     * because cancellations due to interrupts and timeouts may occur
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
     * at any time, a {@code true} return does not guarantee that any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
     * other thread will ever acquire.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
     * @return {@code true} if there may be other threads waiting to acquire
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
    public final boolean hasQueuedThreads() {
48540
221cf8307606 8191483: AbstractQueuedSynchronizer cancel/cancel race
dl
parents: 48046
diff changeset
   738
        for (Node p = tail, h = head; p != h && p != null; p = p.prev)
58134
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   739
            if (p.status >= 0)
48540
221cf8307606 8191483: AbstractQueuedSynchronizer cancel/cancel race
dl
parents: 48046
diff changeset
   740
                return true;
221cf8307606 8191483: AbstractQueuedSynchronizer cancel/cancel race
dl
parents: 48046
diff changeset
   741
        return false;
2
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
     * 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
   746
     * synchronizer; that is, if an acquire method has ever blocked.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
     * <p>In this implementation, this operation returns in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
     * constant time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
     * @return {@code true} if there has ever been contention
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
    public final boolean hasContended() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
        return head != null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
     * Returns the first (longest-waiting) thread in the queue, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
     * {@code null} if no threads are currently queued.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
     * <p>In this implementation, this operation normally returns in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
     * constant time, but may iterate upon contention if other threads are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
     * concurrently modifying the queue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
     * @return the first (longest-waiting) thread in the queue, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
     *         {@code null} if no threads are currently queued
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
    public final Thread getFirstQueuedThread() {
58134
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   769
        Thread first = null, w; Node h, s;
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   770
        if ((h = head) != null && ((s = h.next) == null ||
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   771
                                   (first = s.waiter) == null ||
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   772
                                   s.prev == null)) {
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   773
            // traverse from tail on stale reads
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   774
            for (Node p = tail, q; p != null && (q = p.prev) != null; p = q)
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   775
                if ((w = p.waiter) != null)
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   776
                    first = w;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
        }
58134
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   778
        return first;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
     * Returns true if the given thread is currently queued.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
     * <p>This implementation traverses the queue to determine
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
     * presence of the given thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
     * @param thread the thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
     * @return {@code true} if the given thread is on the queue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
     * @throws NullPointerException if the thread is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
    public final boolean isQueued(Thread thread) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
        if (thread == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
        for (Node p = tail; p != null; p = p.prev)
58134
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   795
            if (p.waiter == thread)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
     * Returns {@code true} if the apparent first queued thread, if one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
     * exists, is waiting in exclusive mode.  If this method returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
     * {@code true}, and the current thread is attempting to acquire in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
     * shared mode (that is, this method is invoked from {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
     * #tryAcquireShared}) then it is guaranteed that the current thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
     * is not the first queued thread.  Used only as a heuristic in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
     * ReentrantReadWriteLock.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
    final boolean apparentlyFirstQueuedIsExclusive() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
        Node h, s;
58134
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   811
        return (h = head) != null && (s = h.next)  != null &&
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   812
            !(s instanceof SharedNode) && s.waiter != null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
     * Queries whether any threads have been waiting to acquire longer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
     * than the current thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
     * <p>An invocation of this method is equivalent to (but may be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
     * more efficient than):
32990
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
   821
     * <pre> {@code
34348
ba5c2f2fc9d7 8143087: Miscellaneous changes imported from jsr166 CVS 2015-11
dl
parents: 33674
diff changeset
   822
     * getFirstQueuedThread() != Thread.currentThread()
ba5c2f2fc9d7 8143087: Miscellaneous changes imported from jsr166 CVS 2015-11
dl
parents: 33674
diff changeset
   823
     *   && hasQueuedThreads()}</pre>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
     * <p>Note that because cancellations due to interrupts and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
     * timeouts may occur at any time, a {@code true} return does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
     * guarantee that some other thread will acquire before the current
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
     * thread.  Likewise, it is possible for another thread to win a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
     * race to enqueue after this method has returned {@code false},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
     * due to the queue being empty.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
     * <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
   833
     * avoid <a href="AbstractQueuedSynchronizer.html#barging">barging</a>.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
     * Such a synchronizer's {@link #tryAcquire} method should return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
     * {@code false}, and its {@link #tryAcquireShared} method should
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
     * return a negative value, if this method returns {@code true}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
     * (unless this is a reentrant acquire).  For example, the {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
     * tryAcquire} method for a fair, reentrant, exclusive mode
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
     * synchronizer might look like this:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
     *
32990
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
   841
     * <pre> {@code
58134
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   842
     * protected boolean tryAcquire(long arg) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
     *   if (isHeldExclusively()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
     *     // A reentrant acquire; increment hold count
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
     *     return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
     *   } else if (hasQueuedPredecessors()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
     *     return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
     *   } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
     *     // try to acquire normally
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
     *   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
     * }}</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
     * @return {@code true} if there is a queued thread preceding the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
     *         current thread, and {@code false} if the current thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
     *         is at the head of the queue or the queue is empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
     * @since 1.7
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
    public final boolean hasQueuedPredecessors() {
58134
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   859
        Thread first = null; Node h, s;
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   860
        if ((h = head) != null && ((s = h.next) == null ||
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   861
                                   (first = s.waiter) == null ||
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   862
                                   s.prev == null))
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   863
            first = getFirstQueuedThread(); // retry via getFirstQueuedThread
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   864
        return first != null && first != Thread.currentThread();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
    // Instrumentation and monitoring methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
     * Returns an estimate of the number of threads waiting to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
     * acquire.  The value is only an estimate because the number of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
     * threads may change dynamically while this method traverses
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
     * 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
   874
     * monitoring system state, not for synchronization control.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
     * @return the estimated number of threads waiting to acquire
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
    public final int getQueueLength() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
        int n = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
        for (Node p = tail; p != null; p = p.prev) {
58134
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   881
            if (p.waiter != null)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
                ++n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
        return n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
     * Returns a collection containing threads that may be waiting to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
     * acquire.  Because the actual set of threads may change
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
     * dynamically while constructing this result, the returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
     * collection is only a best-effort estimate.  The elements of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
     * returned collection are in no particular order.  This method is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
     * designed to facilitate construction of subclasses that provide
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
     * more extensive monitoring facilities.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
     * @return the collection of threads
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
    public final Collection<Thread> getQueuedThreads() {
32990
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
   899
        ArrayList<Thread> list = new ArrayList<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
        for (Node p = tail; p != null; p = p.prev) {
58134
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   901
            Thread t = p.waiter;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
            if (t != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
                list.add(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
        return list;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
     * Returns a collection containing threads that may be waiting to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
     * acquire in exclusive mode. This has the same properties
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
     * as {@link #getQueuedThreads} except that it only returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
     * those threads waiting due to an exclusive acquire.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
     * @return the collection of threads
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
    public final Collection<Thread> getExclusiveQueuedThreads() {
32990
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
   917
        ArrayList<Thread> list = new ArrayList<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
        for (Node p = tail; p != null; p = p.prev) {
58134
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   919
            if (!(p instanceof SharedNode)) {
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   920
                Thread t = p.waiter;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
                if (t != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
                    list.add(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
        return list;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
     * Returns a collection containing threads that may be waiting to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
     * acquire in shared mode. This has the same properties
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
     * as {@link #getQueuedThreads} except that it only returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
     * those threads waiting due to a shared acquire.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
     * @return the collection of threads
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
    public final Collection<Thread> getSharedQueuedThreads() {
32990
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
   937
        ArrayList<Thread> list = new ArrayList<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
        for (Node p = tail; p != null; p = p.prev) {
58134
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   939
            if (p instanceof SharedNode) {
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
   940
                Thread t = p.waiter;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
                if (t != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
                    list.add(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
        return list;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
     * Returns a string identifying this synchronizer, as well as its state.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
     * The state, in brackets, includes the String {@code "State ="}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
     * followed by the current value of {@link #getState}, and either
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
     * {@code "nonempty"} or {@code "empty"} depending on whether the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
     * queue is empty.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
     * @return a string identifying this synchronizer, as well as its state
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
    public String toString() {
32990
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
   958
        return super.toString()
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
   959
            + "[State = " + getState() + ", "
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
   960
            + (hasQueuedThreads() ? "non" : "") + "empty queue]";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
    // Instrumentation methods for conditions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
     * Queries whether the given ConditionObject
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
     * uses this synchronizer as its lock.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
     * @param condition the condition
18576
7a5c231327af 8019377: Sync j.u.c locks and atomic from 166 to tl
dl
parents: 14325
diff changeset
   970
     * @return {@code true} if owned
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
     * @throws NullPointerException if the condition is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
    public final boolean owns(ConditionObject condition) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
        return condition.isOwnedBy(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
     * Queries whether any threads are waiting on the given condition
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
     * 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
   980
     * 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
   981
     * does not guarantee that a future {@code signal} will awaken
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
     * any threads.  This method is designed primarily for use in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
     * monitoring of the system state.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
     * @param condition the condition
18576
7a5c231327af 8019377: Sync j.u.c locks and atomic from 166 to tl
dl
parents: 14325
diff changeset
   986
     * @return {@code true} if there are any waiting threads
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
     * @throws IllegalMonitorStateException if exclusive synchronization
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
     *         is not held
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
     * @throws IllegalArgumentException if the given condition is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
     *         not associated with this synchronizer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
     * @throws NullPointerException if the condition is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
    public final boolean hasWaiters(ConditionObject condition) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
        if (!owns(condition))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
            throw new IllegalArgumentException("Not owner");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
        return condition.hasWaiters();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
     * Returns an estimate of the number of threads waiting on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
     * given condition associated with this synchronizer. Note that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
     * because timeouts and interrupts may occur at any time, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
     * 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
  1004
     * 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
  1005
     * state, not for synchronization control.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
     * @param condition the condition
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
     * @return the estimated number of waiting threads
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
     * @throws IllegalMonitorStateException if exclusive synchronization
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
     *         is not held
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
     * @throws IllegalArgumentException if the given condition is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
     *         not associated with this synchronizer
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
     * @throws NullPointerException if the condition is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
    public final int getWaitQueueLength(ConditionObject condition) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
        if (!owns(condition))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
            throw new IllegalArgumentException("Not owner");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
        return condition.getWaitQueueLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
     * Returns a collection containing those threads that may be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
     * waiting on the given condition associated with this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
     * synchronizer.  Because the actual set of threads may change
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
     * dynamically while constructing this result, the returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
     * collection is only a best-effort estimate. The elements of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
     * returned collection are in no particular order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
     * @param condition the condition
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
     * @return the collection of threads
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
     * @throws IllegalMonitorStateException if exclusive synchronization
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
     *         is not held
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
     * @throws IllegalArgumentException if the given condition is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
     *         not associated with this synchronizer
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
     * @throws NullPointerException if the condition is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
    public final Collection<Thread> getWaitingThreads(ConditionObject condition) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
        if (!owns(condition))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
            throw new IllegalArgumentException("Not owner");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
        return condition.getWaitingThreads();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
    /**
47302
f517fa4f4dc6 8187408: AbstractQueuedSynchronizer wait queue corrupted when thread awaits without holding the lock
dl
parents: 47216
diff changeset
  1044
     * Condition implementation for a {@link AbstractQueuedLongSynchronizer}
f517fa4f4dc6 8187408: AbstractQueuedSynchronizer wait queue corrupted when thread awaits without holding the lock
dl
parents: 47216
diff changeset
  1045
     * serving as the basis of a {@link Lock} implementation.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
     * <p>Method documentation for this class describes mechanics,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
     * not behavioral specifications from the point of view of Lock
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
     * and Condition users. Exported versions of this class will in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
     * general need to be accompanied by documentation describing
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
     * 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
  1052
     * {@code AbstractQueuedLongSynchronizer}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
     * <p>This class is Serializable, but all fields are transient,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
     * so deserialized conditions have no waiters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
    public class ConditionObject implements Condition, java.io.Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
        private static final long serialVersionUID = 1173984872572414699L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
        /** First node of condition queue. */
58134
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1060
        private transient ConditionNode firstWaiter;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
        /** Last node of condition queue. */
58134
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1062
        private transient ConditionNode lastWaiter;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
        /**
18576
7a5c231327af 8019377: Sync j.u.c locks and atomic from 166 to tl
dl
parents: 14325
diff changeset
  1065
         * Creates a new {@code ConditionObject} instance.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
        public ConditionObject() { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
58134
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1069
        // Signalling methods
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
        /**
58134
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1072
         * Removes and transfers one or all waiters to sync queue.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
         */
58134
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1074
        private void doSignal(ConditionNode first, boolean all) {
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1075
            while (first != null) {
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1076
                ConditionNode next = first.nextWaiter;
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1077
                if ((firstWaiter = next) == null)
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1078
                    lastWaiter = null;
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1079
                if ((first.getAndUnsetStatus(COND) & COND) != 0) {
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1080
                    enqueue(first);
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1081
                    if (!all)
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1082
                        break;
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1083
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
                first = next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
         * Moves the longest-waiting thread, if one exists, from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
         * wait queue for this condition to the wait queue for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
         * owning lock.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
         * @throws IllegalMonitorStateException if {@link #isHeldExclusively}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
         *         returns {@code false}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
        public final void signal() {
58134
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1097
            ConditionNode first = firstWaiter;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
            if (!isHeldExclusively())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
                throw new IllegalMonitorStateException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
            if (first != null)
58134
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1101
                doSignal(first, false);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
         * Moves all threads from the wait queue for this condition to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
         * the wait queue for the owning lock.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
         * @throws IllegalMonitorStateException if {@link #isHeldExclusively}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
         *         returns {@code false}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
        public final void signalAll() {
58134
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1112
            ConditionNode first = firstWaiter;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
            if (!isHeldExclusively())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
                throw new IllegalMonitorStateException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
            if (first != null)
58134
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1116
                doSignal(first, true);
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1117
        }
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1118
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1119
        // Waiting methods
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1120
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1121
        /**
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1122
         * Adds node to condition list and releases lock.
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1123
         *
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1124
         * @param node the node
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1125
         * @return savedState to reacquire after wait
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1126
         */
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1127
        private long enableWait(ConditionNode node) {
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1128
            if (isHeldExclusively()) {
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1129
                node.waiter = Thread.currentThread();
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1130
                node.setStatusRelaxed(COND | WAITING);
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1131
                ConditionNode last = lastWaiter;
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1132
                if (last == null)
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1133
                    firstWaiter = node;
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1134
                else
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1135
                    last.nextWaiter = node;
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1136
                lastWaiter = node;
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1137
                long savedState = getState();
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1138
                if (release(savedState))
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1139
                    return savedState;
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1140
            }
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1141
            node.status = CANCELLED; // lock not held or inconsistent
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1142
            throw new IllegalMonitorStateException();
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1143
        }
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1144
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1145
        /**
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1146
         * Returns true if a node that was initially placed on a condition
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1147
         * queue is now ready to reacquire on sync queue.
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1148
         * @param node the node
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1149
         * @return true if is reacquiring
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1150
         */
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1151
        private boolean canReacquire(ConditionNode node) {
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1152
            // check links, not status to avoid enqueue race
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1153
            return node != null && node.prev != null && isEnqueued(node);
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1154
        }
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1155
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1156
        /**
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1157
         * Unlinks the given node and other non-waiting nodes from
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1158
         * condition queue unless already unlinked.
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1159
         */
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1160
        private void unlinkCancelledWaiters(ConditionNode node) {
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1161
            if (node == null || node.nextWaiter != null || node == lastWaiter) {
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1162
                ConditionNode w = firstWaiter, trail = null;
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1163
                while (w != null) {
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1164
                    ConditionNode next = w.nextWaiter;
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1165
                    if ((w.status & COND) == 0) {
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1166
                        w.nextWaiter = null;
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1167
                        if (trail == null)
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1168
                            firstWaiter = next;
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1169
                        else
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1170
                            trail.nextWaiter = next;
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1171
                        if (next == null)
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1172
                            lastWaiter = trail;
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1173
                    } else
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1174
                        trail = w;
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1175
                    w = next;
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1176
                }
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1177
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
         * Implements uninterruptible condition wait.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
         * <ol>
32990
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  1183
         * <li>Save lock state returned by {@link #getState}.
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  1184
         * <li>Invoke {@link #release} with saved state as argument,
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  1185
         *     throwing IllegalMonitorStateException if it fails.
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  1186
         * <li>Block until signalled.
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  1187
         * <li>Reacquire by invoking specialized version of
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  1188
         *     {@link #acquire} with saved state as argument.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
         * </ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
        public final void awaitUninterruptibly() {
58134
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1192
            ConditionNode node = new ConditionNode();
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1193
            long savedState = enableWait(node);
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1194
            LockSupport.setCurrentBlocker(this); // for back-compatibility
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
            boolean interrupted = false;
58134
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1196
            while (!canReacquire(node)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
                if (Thread.interrupted())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
                    interrupted = true;
58134
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1199
                else if ((node.status & COND) != 0) {
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1200
                    try {
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1201
                        ForkJoinPool.managedBlock(node);
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1202
                    } catch (InterruptedException ie) {
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1203
                        interrupted = true;
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1204
                    }
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1205
                } else
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1206
                    Thread.onSpinWait();    // awoke while enqueuing
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
            }
58134
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1208
            LockSupport.setCurrentBlocker(null);
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1209
            node.clearStatus();
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1210
            acquire(node, savedState, false, false, false, 0L);
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1211
            if (interrupted)
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1212
                Thread.currentThread().interrupt();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
         * Implements interruptible condition wait.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
         * <ol>
32990
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  1218
         * <li>If current thread is interrupted, throw InterruptedException.
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  1219
         * <li>Save lock state returned by {@link #getState}.
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  1220
         * <li>Invoke {@link #release} with saved state as argument,
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  1221
         *     throwing IllegalMonitorStateException if it fails.
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  1222
         * <li>Block until signalled or interrupted.
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  1223
         * <li>Reacquire by invoking specialized version of
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  1224
         *     {@link #acquire} with saved state as argument.
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  1225
         * <li>If interrupted while blocked in step 4, throw InterruptedException.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
         * </ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
        public final void await() throws InterruptedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
            if (Thread.interrupted())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
                throw new InterruptedException();
58134
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1231
            ConditionNode node = new ConditionNode();
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1232
            long savedState = enableWait(node);
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1233
            LockSupport.setCurrentBlocker(this); // for back-compatibility
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1234
            boolean interrupted = false, cancelled = false;
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1235
            while (!canReacquire(node)) {
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1236
                if (interrupted |= Thread.interrupted()) {
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1237
                    if (cancelled = (node.getAndUnsetStatus(COND) & COND) != 0)
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1238
                        break;              // else interrupted after signal
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1239
                } else if ((node.status & COND) != 0) {
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1240
                    try {
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1241
                        ForkJoinPool.managedBlock(node);
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1242
                    } catch (InterruptedException ie) {
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1243
                        interrupted = true;
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1244
                    }
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1245
                } else
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1246
                    Thread.onSpinWait();    // awoke while enqueuing
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
            }
58134
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1248
            LockSupport.setCurrentBlocker(null);
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1249
            node.clearStatus();
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1250
            acquire(node, savedState, false, false, false, 0L);
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1251
            if (interrupted) {
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1252
                if (cancelled) {
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1253
                    unlinkCancelledWaiters(node);
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1254
                    throw new InterruptedException();
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1255
                }
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1256
                Thread.currentThread().interrupt();
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1257
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
         * Implements timed condition wait.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
         * <ol>
32990
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  1263
         * <li>If current thread is interrupted, throw InterruptedException.
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  1264
         * <li>Save lock state returned by {@link #getState}.
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  1265
         * <li>Invoke {@link #release} with saved state as argument,
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  1266
         *     throwing IllegalMonitorStateException if it fails.
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  1267
         * <li>Block until signalled, interrupted, or timed out.
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  1268
         * <li>Reacquire by invoking specialized version of
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  1269
         *     {@link #acquire} with saved state as argument.
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  1270
         * <li>If interrupted while blocked in step 4, throw InterruptedException.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
         * </ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
         */
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
  1273
        public final long awaitNanos(long nanosTimeout)
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
  1274
                throws InterruptedException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
            if (Thread.interrupted())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
                throw new InterruptedException();
58134
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1277
            ConditionNode node = new ConditionNode();
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1278
            long savedState = enableWait(node);
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1279
            long nanos = (nanosTimeout < 0L) ? 0L : nanosTimeout;
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1280
            long deadline = System.nanoTime() + nanos;
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1281
            boolean cancelled = false, interrupted = false;
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1282
            while (!canReacquire(node)) {
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1283
                if ((interrupted |= Thread.interrupted()) ||
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1284
                    (nanos = deadline - System.nanoTime()) <= 0L) {
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1285
                    if (cancelled = (node.getAndUnsetStatus(COND) & COND) != 0)
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1286
                        break;
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1287
                } else
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1288
                    LockSupport.parkNanos(this, nanos);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
            }
58134
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1290
            node.clearStatus();
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1291
            acquire(node, savedState, false, false, false, 0L);
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1292
            if (cancelled) {
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1293
                unlinkCancelledWaiters(node);
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1294
                if (interrupted)
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1295
                    throw new InterruptedException();
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1296
            } else if (interrupted)
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1297
                Thread.currentThread().interrupt();
32990
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  1298
            long remaining = deadline - System.nanoTime(); // avoid overflow
58134
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1299
            return (remaining <= nanosTimeout) ? remaining : Long.MIN_VALUE;
2
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
         * Implements absolute timed condition wait.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
         * <ol>
32990
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  1305
         * <li>If current thread is interrupted, throw InterruptedException.
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  1306
         * <li>Save lock state returned by {@link #getState}.
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  1307
         * <li>Invoke {@link #release} with saved state as argument,
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  1308
         *     throwing IllegalMonitorStateException if it fails.
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  1309
         * <li>Block until signalled, interrupted, or timed out.
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  1310
         * <li>Reacquire by invoking specialized version of
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  1311
         *     {@link #acquire} with saved state as argument.
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  1312
         * <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
  1313
         * <li>If timed out while blocked in step 4, return false, else true.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
         * </ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
         */
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
  1316
        public final boolean awaitUntil(Date deadline)
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
  1317
                throws InterruptedException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
            long abstime = deadline.getTime();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
            if (Thread.interrupted())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
                throw new InterruptedException();
58134
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1321
            ConditionNode node = new ConditionNode();
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1322
            long savedState = enableWait(node);
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1323
            boolean cancelled = false, interrupted = false;
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1324
            while (!canReacquire(node)) {
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1325
                if ((interrupted |= Thread.interrupted()) ||
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1326
                    System.currentTimeMillis() >= abstime) {
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1327
                    if (cancelled = (node.getAndUnsetStatus(COND) & COND) != 0)
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1328
                        break;
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1329
                } else
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1330
                    LockSupport.parkUntil(this, abstime);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
            }
58134
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1332
            node.clearStatus();
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1333
            acquire(node, savedState, false, false, false, 0L);
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1334
            if (cancelled) {
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1335
                unlinkCancelledWaiters(node);
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1336
                if (interrupted)
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1337
                    throw new InterruptedException();
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1338
            } else if (interrupted)
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1339
                Thread.currentThread().interrupt();
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1340
            return !cancelled;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
         * Implements timed condition wait.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
         * <ol>
32990
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  1346
         * <li>If current thread is interrupted, throw InterruptedException.
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  1347
         * <li>Save lock state returned by {@link #getState}.
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  1348
         * <li>Invoke {@link #release} with saved state as argument,
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  1349
         *     throwing IllegalMonitorStateException if it fails.
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  1350
         * <li>Block until signalled, interrupted, or timed out.
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  1351
         * <li>Reacquire by invoking specialized version of
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  1352
         *     {@link #acquire} with saved state as argument.
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  1353
         * <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
  1354
         * <li>If timed out while blocked in step 4, return false, else true.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
         * </ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
         */
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
  1357
        public final boolean await(long time, TimeUnit unit)
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
  1358
                throws InterruptedException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
            long nanosTimeout = unit.toNanos(time);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
            if (Thread.interrupted())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
                throw new InterruptedException();
58134
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1362
            ConditionNode node = new ConditionNode();
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1363
            long savedState = enableWait(node);
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1364
            long nanos = (nanosTimeout < 0L) ? 0L : nanosTimeout;
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1365
            long deadline = System.nanoTime() + nanos;
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1366
            boolean cancelled = false, interrupted = false;
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1367
            while (!canReacquire(node)) {
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1368
                if ((interrupted |= Thread.interrupted()) ||
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1369
                    (nanos = deadline - System.nanoTime()) <= 0L) {
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1370
                    if (cancelled = (node.getAndUnsetStatus(COND) & COND) != 0)
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1371
                        break;
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1372
                } else
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1373
                    LockSupport.parkNanos(this, nanos);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
            }
58134
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1375
            node.clearStatus();
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1376
            acquire(node, savedState, false, false, false, 0L);
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1377
            if (cancelled) {
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1378
                unlinkCancelledWaiters(node);
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1379
                if (interrupted)
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1380
                    throw new InterruptedException();
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1381
            } else if (interrupted)
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1382
                Thread.currentThread().interrupt();
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1383
            return !cancelled;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
        //  support for instrumentation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
         * Returns true if this condition was created by the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
         * synchronization object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
         * @return {@code true} if owned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
        final boolean isOwnedBy(AbstractQueuedLongSynchronizer sync) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
            return sync == AbstractQueuedLongSynchronizer.this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
         * 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
  1400
         * Implements {@link AbstractQueuedLongSynchronizer#hasWaiters(ConditionObject)}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
         * @return {@code true} if there are any waiting threads
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
         * @throws IllegalMonitorStateException if {@link #isHeldExclusively}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
         *         returns {@code false}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1406
        protected final boolean hasWaiters() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1407
            if (!isHeldExclusively())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1408
                throw new IllegalMonitorStateException();
58134
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1409
            for (ConditionNode w = firstWaiter; w != null; w = w.nextWaiter) {
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1410
                if ((w.status & COND) != 0)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1413
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1414
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
         * Returns an estimate of the number of threads waiting on
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1418
         * this condition.
18576
7a5c231327af 8019377: Sync j.u.c locks and atomic from 166 to tl
dl
parents: 14325
diff changeset
  1419
         * Implements {@link AbstractQueuedLongSynchronizer#getWaitQueueLength(ConditionObject)}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1421
         * @return the estimated number of waiting threads
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
         * @throws IllegalMonitorStateException if {@link #isHeldExclusively}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
         *         returns {@code false}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1425
        protected final int getWaitQueueLength() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
            if (!isHeldExclusively())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1427
                throw new IllegalMonitorStateException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1428
            int n = 0;
58134
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1429
            for (ConditionNode w = firstWaiter; w != null; w = w.nextWaiter) {
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1430
                if ((w.status & COND) != 0)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1431
                    ++n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1432
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1433
            return n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1434
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1435
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1436
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1437
         * Returns a collection containing those threads that may be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1438
         * waiting on this Condition.
18576
7a5c231327af 8019377: Sync j.u.c locks and atomic from 166 to tl
dl
parents: 14325
diff changeset
  1439
         * Implements {@link AbstractQueuedLongSynchronizer#getWaitingThreads(ConditionObject)}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1440
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1441
         * @return the collection of threads
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1442
         * @throws IllegalMonitorStateException if {@link #isHeldExclusively}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1443
         *         returns {@code false}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1444
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1445
        protected final Collection<Thread> getWaitingThreads() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1446
            if (!isHeldExclusively())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1447
                throw new IllegalMonitorStateException();
32990
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  1448
            ArrayList<Thread> list = new ArrayList<>();
58134
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1449
            for (ConditionNode w = firstWaiter; w != null; w = w.nextWaiter) {
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1450
                if ((w.status & COND) != 0) {
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1451
                    Thread t = w.waiter;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1452
                    if (t != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1453
                        list.add(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1454
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1455
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1456
            return list;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1457
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1458
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1459
58134
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1460
    // Unsafe
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1461
    private static final Unsafe U = Unsafe.getUnsafe();
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1462
    private static final long STATE
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1463
        = U.objectFieldOffset(AbstractQueuedLongSynchronizer.class, "state");
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1464
    private static final long HEAD
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1465
        = U.objectFieldOffset(AbstractQueuedLongSynchronizer.class, "head");
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1466
    private static final long TAIL
51cd29502ea9 8229442: AQS and lock classes refresh
dl
parents: 49565
diff changeset
  1467
        = U.objectFieldOffset(AbstractQueuedLongSynchronizer.class, "tail");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1468
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1469
    static {
32990
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
  1470
        Class<?> ensureLoaded = LockSupport.class;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1471
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1472
}