16232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson/* 26232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * Written by Doug Lea with assistance from members of JCP JSR-166 36232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * Expert Group and released to the public domain, as explained at 4a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson * http://creativecommons.org/publicdomain/zero/1.0/ 56232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson */ 66232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson 76232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilsonpackage java.util.concurrent.locks; 8edf43d27e240d82106f39ae91404963c23987234Narayan Kamath 991770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravleimport java.util.concurrent.TimeUnit; 1091770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravleimport java.util.ArrayList; 1191770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravleimport java.util.Collection; 1291770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravleimport java.util.Date; 136232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilsonimport sun.misc.Unsafe; 146232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson 156232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson/** 166232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * A version of {@link AbstractQueuedSynchronizer} in 1791770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle * which synchronization state is maintained as a {@code long}. 186232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * This class has exactly the same structure, properties, and methods 1991770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle * as {@code AbstractQueuedSynchronizer} with the exception 206232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * that all state-related parameters and results are defined 2191770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle * as {@code long} rather than {@code int}. This class 226232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * may be useful when creating synchronizers such as 236232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * multilevel locks and barriers that require 246232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * 64 bits of state. 256232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * 266232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * <p>See {@link AbstractQueuedSynchronizer} for usage 276232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * notes and examples. 286232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * 296232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * @since 1.6 306232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * @author Doug Lea 316232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson */ 326232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilsonpublic abstract class AbstractQueuedLongSynchronizer 336232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson extends AbstractOwnableSynchronizer 346232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson implements java.io.Serializable { 356232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson 366232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson private static final long serialVersionUID = 7373984972572414692L; 376232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson 386232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson /* 396232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson To keep sources in sync, the remainder of this source file is 406232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson exactly cloned from AbstractQueuedSynchronizer, replacing class 416232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson name and changing ints related with sync state to longs. Please 426232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson keep it that way. 436232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson */ 446232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson 456232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson /** 4691770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle * Creates a new {@code AbstractQueuedLongSynchronizer} instance 476232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * with initial synchronization state of zero. 486232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson */ 496232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson protected AbstractQueuedLongSynchronizer() { } 506232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson 516232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson /** 526232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * Wait queue node class. 536232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * 546232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * <p>The wait queue is a variant of a "CLH" (Craig, Landin, and 556232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * Hagersten) lock queue. CLH locks are normally used for 566232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * spinlocks. We instead use them for blocking synchronizers, but 576232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * use the same basic tactic of holding some of the control 586232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * information about a thread in the predecessor of its node. A 596232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * "status" field in each node keeps track of whether a thread 606232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * should block. A node is signalled when its predecessor 616232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * releases. Each node of the queue otherwise serves as a 626232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * specific-notification-style monitor holding a single waiting 636232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * thread. The status field does NOT control whether threads are 646232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * granted locks etc though. A thread may try to acquire if it is 656232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * first in the queue. But being first does not guarantee success; 666232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * it only gives the right to contend. So the currently released 676232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * contender thread may need to rewait. 686232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * 696232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * <p>To enqueue into a CLH lock, you atomically splice it in as new 706232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * tail. To dequeue, you just set the head field. 716232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * <pre> 726232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * +------+ prev +-----+ +-----+ 736232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * head | | <---- | | <---- | | tail 746232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * +------+ +-----+ +-----+ 756232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * </pre> 766232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * 776232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * <p>Insertion into a CLH queue requires only a single atomic 786232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * operation on "tail", so there is a simple atomic point of 7991770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle * demarcation from unqueued to queued. Similarly, dequeuing 806232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * involves only updating the "head". However, it takes a bit 816232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * more work for nodes to determine who their successors are, 826232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * in part to deal with possible cancellation due to timeouts 836232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * and interrupts. 846232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * 856232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * <p>The "prev" links (not used in original CLH locks), are mainly 866232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * needed to handle cancellation. If a node is cancelled, its 876232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * successor is (normally) relinked to a non-cancelled 886232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * predecessor. For explanation of similar mechanics in the case 896232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * of spin locks, see the papers by Scott and Scherer at 906232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * http://www.cs.rochester.edu/u/scott/synchronization/ 916232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * 926232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * <p>We also use "next" links to implement blocking mechanics. 936232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * The thread id for each node is kept in its own node, so a 946232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * predecessor signals the next node to wake up by traversing 956232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * next link to determine which thread it is. Determination of 966232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * successor must avoid races with newly queued nodes to set 976232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * the "next" fields of their predecessors. This is solved 986232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * when necessary by checking backwards from the atomically 996232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * updated "tail" when a node's successor appears to be null. 1006232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * (Or, said differently, the next-links are an optimization 1016232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * so that we don't usually need a backward scan.) 1026232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * 1036232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * <p>Cancellation introduces some conservatism to the basic 1046232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * algorithms. Since we must poll for cancellation of other 1056232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * nodes, we can miss noticing whether a cancelled node is 1066232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * ahead or behind us. This is dealt with by always unparking 1076232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * successors upon cancellation, allowing them to stabilize on 1086232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * a new predecessor, unless we can identify an uncancelled 1096232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * predecessor who will carry this responsibility. 1106232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * 1116232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * <p>CLH queues need a dummy header node to get started. But 1126232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * we don't create them on construction, because it would be wasted 1136232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * effort if there is never contention. Instead, the node 1146232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * is constructed and head and tail pointers are set upon first 1156232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * contention. 1166232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * 1176232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * <p>Threads waiting on Conditions use the same nodes, but 1186232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * use an additional link. Conditions only need to link nodes 1196232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * in simple (non-concurrent) linked queues because they are 1206232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * only accessed when exclusively held. Upon await, a node is 1216232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * inserted into a condition queue. Upon signal, the node is 1226232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * transferred to the main queue. A special value of status 1236232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * field is used to mark which queue a node is on. 1246232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * 1256232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * <p>Thanks go to Dave Dice, Mark Moir, Victor Luchangco, Bill 1266232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * Scherer and Michael Scott, along with members of JSR-166 1276232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * expert group, for helpful ideas, discussions, and critiques 1286232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * on the design of this class. 1296232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson */ 1306232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson static final class Node { 1316232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson /** Marker to indicate a node is waiting in shared mode */ 1326232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson static final Node SHARED = new Node(); 1336232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson /** Marker to indicate a node is waiting in exclusive mode */ 1346232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson static final Node EXCLUSIVE = null; 1356232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson 1366232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson /** waitStatus value to indicate thread has cancelled */ 1376232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson static final int CANCELLED = 1; 1386232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson /** waitStatus value to indicate successor's thread needs unparking */ 1396232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson static final int SIGNAL = -1; 1406232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson /** waitStatus value to indicate thread is waiting on condition */ 1416232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson static final int CONDITION = -2; 1426232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson /** 1436232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * waitStatus value to indicate the next acquireShared should 1446232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * unconditionally propagate 1456232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson */ 1466232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson static final int PROPAGATE = -3; 1476232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson 1486232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson /** 1496232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * Status field, taking on only the values: 1506232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * SIGNAL: The successor of this node is (or will soon be) 1516232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * blocked (via park), so the current node must 1526232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * unpark its successor when it releases or 1536232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * cancels. To avoid races, acquire methods must 1546232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * first indicate they need a signal, 1556232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * then retry the atomic acquire, and then, 1566232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * on failure, block. 1576232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * CANCELLED: This node is cancelled due to timeout or interrupt. 1586232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * Nodes never leave this state. In particular, 1596232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * a thread with cancelled node never again blocks. 1606232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * CONDITION: This node is currently on a condition queue. 1616232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * It will not be used as a sync queue node 1626232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * until transferred, at which time the status 1636232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * will be set to 0. (Use of this value here has 1646232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * nothing to do with the other uses of the 1656232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * field, but simplifies mechanics.) 1666232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * PROPAGATE: A releaseShared should be propagated to other 1676232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * nodes. This is set (for head node only) in 1686232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * doReleaseShared to ensure propagation 1696232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * continues, even if other operations have 1706232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * since intervened. 1716232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * 0: None of the above 1726232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * 1736232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * The values are arranged numerically to simplify use. 1746232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * Non-negative values mean that a node doesn't need to 1756232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * signal. So, most code doesn't need to check for particular 1766232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * values, just for sign. 1776232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * 1786232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * The field is initialized to 0 for normal sync nodes, and 1796232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * CONDITION for condition nodes. It is modified using CAS 1806232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * (or when possible, unconditional volatile writes). 1816232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson */ 1826232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson volatile int waitStatus; 1836232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson 1846232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson /** 1856232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * Link to predecessor node that current node/thread relies on 18691770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle * for checking waitStatus. Assigned during enqueuing, and nulled 1876232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * out (for sake of GC) only upon dequeuing. Also, upon 1886232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * cancellation of a predecessor, we short-circuit while 1896232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * finding a non-cancelled one, which will always exist 1906232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * because the head node is never cancelled: A node becomes 1916232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * head only as a result of successful acquire. A 1926232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * cancelled thread never succeeds in acquiring, and a thread only 1936232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * cancels itself, not any other node. 1946232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson */ 1956232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson volatile Node prev; 1966232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson 1976232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson /** 1986232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * Link to the successor node that the current node/thread 1996232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * unparks upon release. Assigned during enqueuing, adjusted 2006232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * when bypassing cancelled predecessors, and nulled out (for 2016232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * sake of GC) when dequeued. The enq operation does not 2026232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * assign next field of a predecessor until after attachment, 2036232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * so seeing a null next field does not necessarily mean that 2046232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * node is at end of queue. However, if a next field appears 2056232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * to be null, we can scan prev's from the tail to 2066232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * double-check. The next field of cancelled nodes is set to 2076232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * point to the node itself instead of null, to make life 2086232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * easier for isOnSyncQueue. 2096232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson */ 2106232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson volatile Node next; 2116232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson 2126232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson /** 2136232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * The thread that enqueued this node. Initialized on 2146232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * construction and nulled out after use. 2156232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson */ 2166232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson volatile Thread thread; 2176232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson 2186232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson /** 2196232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * Link to next node waiting on condition, or the special 2206232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * value SHARED. Because condition queues are accessed only 2216232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * when holding in exclusive mode, we just need a simple 2226232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * linked queue to hold nodes while they are waiting on 2236232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * conditions. They are then transferred to the queue to 2246232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * re-acquire. And because conditions can only be exclusive, 2256232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * we save a field by using special value to indicate shared 2266232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * mode. 2276232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson */ 2286232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson Node nextWaiter; 2296232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson 2306232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson /** 231edf43d27e240d82106f39ae91404963c23987234Narayan Kamath * Returns true if node is waiting in shared mode. 2326232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson */ 2336232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson final boolean isShared() { 2346232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson return nextWaiter == SHARED; 2356232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson } 2366232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson 2376232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson /** 2386232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * Returns previous node, or throws NullPointerException if null. 2396232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * Use when predecessor cannot be null. The null check could 2406232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * be elided, but is present to help the VM. 2416232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * 2426232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * @return the predecessor of this node 2436232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson */ 2446232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson final Node predecessor() throws NullPointerException { 2456232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson Node p = prev; 2466232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson if (p == null) 2476232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson throw new NullPointerException(); 2486232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson else 2496232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson return p; 2506232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson } 2516232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson 2526232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson Node() { // Used to establish initial head or SHARED marker 2536232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson } 2546232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson 2556232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson Node(Thread thread, Node mode) { // Used by addWaiter 2566232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson this.nextWaiter = mode; 2576232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson this.thread = thread; 2586232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson } 2596232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson 2606232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson Node(Thread thread, int waitStatus) { // Used by Condition 2616232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson this.waitStatus = waitStatus; 2626232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson this.thread = thread; 2636232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson } 2646232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson } 2656232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson 2666232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson /** 2676232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * Head of the wait queue, lazily initialized. Except for 2686232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * initialization, it is modified only via method setHead. Note: 2696232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * If head exists, its waitStatus is guaranteed not to be 2706232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * CANCELLED. 2716232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson */ 2726232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson private transient volatile Node head; 2736232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson 2746232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson /** 2756232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * Tail of the wait queue, lazily initialized. Modified only via 2766232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * method enq to add new wait node. 2776232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson */ 2786232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson private transient volatile Node tail; 2796232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson 2806232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson /** 2816232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * The synchronization state. 2826232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson */ 2836232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson private volatile long state; 2846232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson 2856232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson /** 2866232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * Returns the current value of synchronization state. 28791770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle * This operation has memory semantics of a {@code volatile} read. 2886232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * @return current state value 2896232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson */ 2906232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson protected final long getState() { 2916232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson return state; 2926232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson } 2936232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson 2946232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson /** 2956232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * Sets the value of synchronization state. 29691770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle * This operation has memory semantics of a {@code volatile} write. 2976232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * @param newState the new state value 2986232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson */ 2996232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson protected final void setState(long newState) { 3006232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson state = newState; 3016232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson } 3026232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson 3036232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson /** 3046232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * Atomically sets synchronization state to the given updated 3056232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * value if the current state value equals the expected value. 30691770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle * This operation has memory semantics of a {@code volatile} read 3076232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * and write. 3086232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * 3096232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * @param expect the expected value 3106232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * @param update the new value 3116232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * @return true if successful. False return indicates that the actual 3126232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * value was not equal to the expected value. 3136232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson */ 3146232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson protected final boolean compareAndSetState(long expect, long update) { 3156232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson // See below for intrinsics setup to support this 3166232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson return unsafe.compareAndSwapLong(this, stateOffset, expect, update); 3176232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson } 3186232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson 3196232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson // Queuing utilities 3206232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson 3216232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson /** 3226232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * The number of nanoseconds for which it is faster to spin 3236232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * rather than to use timed park. A rough estimate suffices 3246232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * to improve responsiveness with very short timeouts. 3256232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson */ 3266232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson static final long spinForTimeoutThreshold = 1000L; 3276232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson 3286232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson /** 3296232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * Inserts node into queue, initializing if necessary. See picture above. 3306232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * @param node the node to insert 3316232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * @return node's predecessor 3326232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson */ 3336232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson private Node enq(final Node node) { 3346232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson for (;;) { 3356232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson Node t = tail; 3366232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson if (t == null) { // Must initialize 3376232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson if (compareAndSetHead(new Node())) 3386232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson tail = head; 3396232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson } else { 3406232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson node.prev = t; 3416232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson if (compareAndSetTail(t, node)) { 3426232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson t.next = node; 3436232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson return t; 3446232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson } 3456232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson } 3466232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson } 3476232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson } 3486232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson 3496232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson /** 3506232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * Creates and enqueues node for current thread and given mode. 3516232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * 3526232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * @param mode Node.EXCLUSIVE for exclusive, Node.SHARED for shared 3536232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * @return the new node 3546232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson */ 3556232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson private Node addWaiter(Node mode) { 3566232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson Node node = new Node(Thread.currentThread(), mode); 3576232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson // Try the fast path of enq; backup to full enq on failure 3586232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson Node pred = tail; 3596232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson if (pred != null) { 3606232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson node.prev = pred; 3616232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson if (compareAndSetTail(pred, node)) { 3626232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson pred.next = node; 3636232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson return node; 3646232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson } 3656232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson } 3666232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson enq(node); 3676232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson return node; 3686232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson } 3696232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson 3706232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson /** 3716232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * Sets head of queue to be node, thus dequeuing. Called only by 3726232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * acquire methods. Also nulls out unused fields for sake of GC 3736232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * and to suppress unnecessary signals and traversals. 3746232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * 3756232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * @param node the node 3766232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson */ 3776232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson private void setHead(Node node) { 3786232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson head = node; 3796232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson node.thread = null; 3806232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson node.prev = null; 3816232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson } 3826232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson 3836232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson /** 3846232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * Wakes up node's successor, if one exists. 3856232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * 3866232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * @param node the node 3876232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson */ 3886232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson private void unparkSuccessor(Node node) { 3896232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson /* 3906232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * If status is negative (i.e., possibly needing signal) try 3916232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * to clear in anticipation of signalling. It is OK if this 3926232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * fails or if status is changed by waiting thread. 3936232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson */ 3946232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson int ws = node.waitStatus; 3956232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson if (ws < 0) 3966232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson compareAndSetWaitStatus(node, ws, 0); 3976232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson 3986232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson /* 3996232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * Thread to unpark is held in successor, which is normally 4006232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * just the next node. But if cancelled or apparently null, 4016232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * traverse backwards from tail to find the actual 4026232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * non-cancelled successor. 4036232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson */ 4046232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson Node s = node.next; 4056232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson if (s == null || s.waitStatus > 0) { 4066232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson s = null; 407edf43d27e240d82106f39ae91404963c23987234Narayan Kamath for (Node p = tail; p != null && p != node; p = p.prev) 408edf43d27e240d82106f39ae91404963c23987234Narayan Kamath if (p.waitStatus <= 0) 409edf43d27e240d82106f39ae91404963c23987234Narayan Kamath s = p; 4106232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson } 4116232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson if (s != null) 4126232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson LockSupport.unpark(s.thread); 4136232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson } 4146232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson 4156232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson /** 41691770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle * Release action for shared mode -- signals successor and ensures 4176232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * propagation. (Note: For exclusive mode, release just amounts 4186232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * to calling unparkSuccessor of head if it needs signal.) 4196232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson */ 4206232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson private void doReleaseShared() { 4216232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson /* 4226232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * Ensure that a release propagates, even if there are other 4236232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * in-progress acquires/releases. This proceeds in the usual 4246232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * way of trying to unparkSuccessor of head if it needs 4256232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * signal. But if it does not, status is set to PROPAGATE to 4266232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * ensure that upon release, propagation continues. 4276232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * Additionally, we must loop in case a new node is added 4286232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * while we are doing this. Also, unlike other uses of 4296232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * unparkSuccessor, we need to know if CAS to reset status 4306232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * fails, if so rechecking. 4316232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson */ 4326232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson for (;;) { 4336232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson Node h = head; 4346232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson if (h != null && h != tail) { 4356232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson int ws = h.waitStatus; 4366232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson if (ws == Node.SIGNAL) { 4376232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson if (!compareAndSetWaitStatus(h, Node.SIGNAL, 0)) 4386232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson continue; // loop to recheck cases 4396232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson unparkSuccessor(h); 4406232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson } 4416232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson else if (ws == 0 && 4426232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson !compareAndSetWaitStatus(h, 0, Node.PROPAGATE)) 4436232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson continue; // loop on failed CAS 4446232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson } 4456232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson if (h == head) // loop if head changed 4466232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson break; 4476232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson } 4486232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson } 4496232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson 4506232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson /** 4516232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * Sets head of queue, and checks if successor may be waiting 4526232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * in shared mode, if so propagating if either propagate > 0 or 4536232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * PROPAGATE status was set. 4546232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * 4556232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * @param node the node 4566232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * @param propagate the return value from a tryAcquireShared 4576232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson */ 4586232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson private void setHeadAndPropagate(Node node, long propagate) { 4596232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson Node h = head; // Record old head for check below 4606232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson setHead(node); 4616232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson /* 4626232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * Try to signal next queued node if: 4636232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * Propagation was indicated by caller, 4646232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * or was recorded (as h.waitStatus) by a previous operation 4656232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * (note: this uses sign-check of waitStatus because 4666232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * PROPAGATE status may transition to SIGNAL.) 4676232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * and 4686232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * The next node is waiting in shared mode, 4696232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * or we don't know, because it appears null 4706232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * 4716232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * The conservatism in both of these checks may cause 4726232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * unnecessary wake-ups, but only when there are multiple 4736232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * racing acquires/releases, so most need signals now or soon 4746232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * anyway. 4756232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson */ 4766232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson if (propagate > 0 || h == null || h.waitStatus < 0) { 4776232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson Node s = node.next; 4786232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson if (s == null || s.isShared()) 4796232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson doReleaseShared(); 4806232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson } 4816232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson } 4826232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson 4836232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson // Utilities for various versions of acquire 4846232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson 4856232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson /** 4866232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * Cancels an ongoing attempt to acquire. 4876232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * 4886232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * @param node the node 4896232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson */ 4906232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson private void cancelAcquire(Node node) { 4916232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson // Ignore if node doesn't exist 4926232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson if (node == null) 4936232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson return; 4946232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson 4956232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson node.thread = null; 4966232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson 4976232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson // Skip cancelled predecessors 4986232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson Node pred = node.prev; 4996232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson while (pred.waitStatus > 0) 5006232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson node.prev = pred = pred.prev; 5016232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson 5026232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson // predNext is the apparent node to unsplice. CASes below will 5036232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson // fail if not, in which case, we lost race vs another cancel 5046232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson // or signal, so no further action is necessary. 5056232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson Node predNext = pred.next; 5066232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson 5076232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson // Can use unconditional write instead of CAS here. 5086232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson // After this atomic step, other Nodes can skip past us. 5096232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson // Before, we are free of interference from other threads. 5106232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson node.waitStatus = Node.CANCELLED; 5116232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson 5126232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson // If we are the tail, remove ourselves. 5136232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson if (node == tail && compareAndSetTail(node, pred)) { 5146232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson compareAndSetNext(pred, predNext, null); 5156232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson } else { 5166232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson // If successor needs signal, try to set pred's next-link 5176232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson // so it will get one. Otherwise wake it up to propagate. 5186232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson int ws; 5196232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson if (pred != head && 5206232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson ((ws = pred.waitStatus) == Node.SIGNAL || 5216232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson (ws <= 0 && compareAndSetWaitStatus(pred, ws, Node.SIGNAL))) && 5226232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson pred.thread != null) { 5236232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson Node next = node.next; 5246232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson if (next != null && next.waitStatus <= 0) 5256232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson compareAndSetNext(pred, predNext, next); 5266232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson } else { 5276232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson unparkSuccessor(node); 5286232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson } 5296232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson 5306232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson node.next = node; // help GC 5316232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson } 5326232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson } 5336232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson 5346232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson /** 5356232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * Checks and updates status for a node that failed to acquire. 5366232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * Returns true if thread should block. This is the main signal 53791770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle * control in all acquire loops. Requires that pred == node.prev. 5386232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * 5396232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * @param pred node's predecessor holding status 5406232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * @param node the node 5416232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * @return {@code true} if thread should block 5426232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson */ 5436232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson private static boolean shouldParkAfterFailedAcquire(Node pred, Node node) { 5446232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson int ws = pred.waitStatus; 5456232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson if (ws == Node.SIGNAL) 5466232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson /* 5476232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * This node has already set status asking a release 5486232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * to signal it, so it can safely park. 5496232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson */ 5506232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson return true; 5516232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson if (ws > 0) { 5526232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson /* 5536232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * Predecessor was cancelled. Skip over predecessors and 5546232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * indicate retry. 5556232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson */ 5566232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson do { 5576232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson node.prev = pred = pred.prev; 5586232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson } while (pred.waitStatus > 0); 5596232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson pred.next = node; 5606232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson } else { 5616232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson /* 5626232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * waitStatus must be 0 or PROPAGATE. Indicate that we 5636232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * need a signal, but don't park yet. Caller will need to 5646232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * retry to make sure it cannot acquire before parking. 5656232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson */ 5666232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson compareAndSetWaitStatus(pred, ws, Node.SIGNAL); 5676232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson } 5686232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson return false; 5696232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson } 5706232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson 5716232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson /** 5726232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * Convenience method to interrupt current thread. 5736232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson */ 574a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson static void selfInterrupt() { 5756232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson Thread.currentThread().interrupt(); 5766232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson } 5776232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson 5786232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson /** 5796232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * Convenience method to park and then check if interrupted 5806232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * 5816232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * @return {@code true} if interrupted 5826232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson */ 5836232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson private final boolean parkAndCheckInterrupt() { 5846232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson LockSupport.park(this); 5856232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson return Thread.interrupted(); 5866232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson } 5876232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson 5886232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson /* 5896232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * Various flavors of acquire, varying in exclusive/shared and 5906232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * control modes. Each is mostly the same, but annoyingly 5916232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * different. Only a little bit of factoring is possible due to 5926232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * interactions of exception mechanics (including ensuring that we 5936232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * cancel if tryAcquire throws exception) and other control, at 5946232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * least not without hurting performance too much. 5956232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson */ 5966232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson 5976232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson /** 5986232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * Acquires in exclusive uninterruptible mode for thread already in 5996232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * queue. Used by condition wait methods as well as acquire. 6006232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * 6016232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * @param node the node 6026232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * @param arg the acquire argument 6036232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * @return {@code true} if interrupted while waiting 6046232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson */ 6056232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson final boolean acquireQueued(final Node node, long arg) { 6066232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson boolean failed = true; 6076232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson try { 6086232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson boolean interrupted = false; 6096232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson for (;;) { 6106232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson final Node p = node.predecessor(); 6116232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson if (p == head && tryAcquire(arg)) { 6126232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson setHead(node); 6136232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson p.next = null; // help GC 6146232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson failed = false; 6156232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson return interrupted; 6166232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson } 6176232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson if (shouldParkAfterFailedAcquire(p, node) && 6186232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson parkAndCheckInterrupt()) 6196232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson interrupted = true; 6206232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson } 6216232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson } finally { 6226232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson if (failed) 6236232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson cancelAcquire(node); 6246232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson } 6256232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson } 6266232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson 6276232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson /** 6286232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * Acquires in exclusive interruptible mode. 6296232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * @param arg the acquire argument 6306232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson */ 6316232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson private void doAcquireInterruptibly(long arg) 6326232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson throws InterruptedException { 6336232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson final Node node = addWaiter(Node.EXCLUSIVE); 6346232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson boolean failed = true; 6356232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson try { 6366232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson for (;;) { 6376232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson final Node p = node.predecessor(); 6386232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson if (p == head && tryAcquire(arg)) { 6396232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson setHead(node); 6406232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson p.next = null; // help GC 6416232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson failed = false; 6426232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson return; 6436232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson } 6446232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson if (shouldParkAfterFailedAcquire(p, node) && 6456232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson parkAndCheckInterrupt()) 6466232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson throw new InterruptedException(); 6476232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson } 6486232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson } finally { 6496232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson if (failed) 6506232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson cancelAcquire(node); 6516232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson } 6526232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson } 6536232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson 6546232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson /** 6556232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * Acquires in exclusive timed mode. 6566232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * 6576232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * @param arg the acquire argument 6586232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * @param nanosTimeout max wait time 6596232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * @return {@code true} if acquired 6606232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson */ 6616232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson private boolean doAcquireNanos(long arg, long nanosTimeout) 66291770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle throws InterruptedException { 66391770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle if (nanosTimeout <= 0L) 66491770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle return false; 66591770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle final long deadline = System.nanoTime() + nanosTimeout; 6666232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson final Node node = addWaiter(Node.EXCLUSIVE); 6676232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson boolean failed = true; 6686232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson try { 6696232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson for (;;) { 6706232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson final Node p = node.predecessor(); 6716232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson if (p == head && tryAcquire(arg)) { 6726232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson setHead(node); 6736232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson p.next = null; // help GC 6746232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson failed = false; 6756232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson return true; 6766232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson } 67791770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle nanosTimeout = deadline - System.nanoTime(); 67891770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle if (nanosTimeout <= 0L) 6796232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson return false; 6806232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson if (shouldParkAfterFailedAcquire(p, node) && 6816232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson nanosTimeout > spinForTimeoutThreshold) 6826232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson LockSupport.parkNanos(this, nanosTimeout); 6836232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson if (Thread.interrupted()) 6846232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson throw new InterruptedException(); 6856232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson } 6866232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson } finally { 6876232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson if (failed) 6886232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson cancelAcquire(node); 6896232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson } 6906232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson } 6916232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson 6926232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson /** 6936232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * Acquires in shared uninterruptible mode. 6946232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * @param arg the acquire argument 6956232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson */ 6966232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson private void doAcquireShared(long arg) { 6976232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson final Node node = addWaiter(Node.SHARED); 6986232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson boolean failed = true; 6996232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson try { 7006232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson boolean interrupted = false; 7016232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson for (;;) { 7026232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson final Node p = node.predecessor(); 7036232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson if (p == head) { 7046232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson long r = tryAcquireShared(arg); 7056232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson if (r >= 0) { 7066232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson setHeadAndPropagate(node, r); 7076232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson p.next = null; // help GC 7086232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson if (interrupted) 7096232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson selfInterrupt(); 7106232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson failed = false; 7116232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson return; 7126232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson } 7136232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson } 7146232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson if (shouldParkAfterFailedAcquire(p, node) && 7156232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson parkAndCheckInterrupt()) 7166232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson interrupted = true; 7176232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson } 7186232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson } finally { 7196232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson if (failed) 7206232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson cancelAcquire(node); 7216232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson } 7226232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson } 7236232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson 7246232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson /** 7256232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * Acquires in shared interruptible mode. 7266232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * @param arg the acquire argument 7276232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson */ 7286232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson private void doAcquireSharedInterruptibly(long arg) 7296232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson throws InterruptedException { 7306232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson final Node node = addWaiter(Node.SHARED); 7316232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson boolean failed = true; 7326232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson try { 7336232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson for (;;) { 7346232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson final Node p = node.predecessor(); 7356232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson if (p == head) { 7366232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson long r = tryAcquireShared(arg); 7376232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson if (r >= 0) { 7386232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson setHeadAndPropagate(node, r); 7396232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson p.next = null; // help GC 7406232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson failed = false; 7416232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson return; 7426232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson } 7436232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson } 7446232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson if (shouldParkAfterFailedAcquire(p, node) && 7456232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson parkAndCheckInterrupt()) 7466232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson throw new InterruptedException(); 7476232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson } 7486232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson } finally { 7496232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson if (failed) 7506232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson cancelAcquire(node); 7516232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson } 7526232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson } 7536232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson 7546232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson /** 7556232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * Acquires in shared timed mode. 7566232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * 7576232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * @param arg the acquire argument 7586232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * @param nanosTimeout max wait time 7596232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * @return {@code true} if acquired 7606232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson */ 7616232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson private boolean doAcquireSharedNanos(long arg, long nanosTimeout) 76291770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle throws InterruptedException { 76391770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle if (nanosTimeout <= 0L) 76491770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle return false; 76591770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle final long deadline = System.nanoTime() + nanosTimeout; 7666232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson final Node node = addWaiter(Node.SHARED); 7676232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson boolean failed = true; 7686232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson try { 7696232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson for (;;) { 7706232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson final Node p = node.predecessor(); 7716232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson if (p == head) { 7726232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson long r = tryAcquireShared(arg); 7736232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson if (r >= 0) { 7746232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson setHeadAndPropagate(node, r); 7756232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson p.next = null; // help GC 7766232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson failed = false; 7776232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson return true; 7786232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson } 7796232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson } 78091770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle nanosTimeout = deadline - System.nanoTime(); 78191770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle if (nanosTimeout <= 0L) 7826232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson return false; 7836232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson if (shouldParkAfterFailedAcquire(p, node) && 7846232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson nanosTimeout > spinForTimeoutThreshold) 7856232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson LockSupport.parkNanos(this, nanosTimeout); 7866232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson if (Thread.interrupted()) 7876232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson throw new InterruptedException(); 7886232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson } 7896232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson } finally { 7906232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson if (failed) 7916232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson cancelAcquire(node); 7926232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson } 7936232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson } 7946232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson 7956232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson // Main exported methods 7966232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson 7976232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson /** 7986232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * Attempts to acquire in exclusive mode. This method should query 7996232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * if the state of the object permits it to be acquired in the 8006232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * exclusive mode, and if so to acquire it. 8016232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * 8026232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * <p>This method is always invoked by the thread performing 8036232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * acquire. If this method reports failure, the acquire method 8046232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * may queue the thread, if it is not already queued, until it is 8056232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * signalled by a release from some other thread. This can be used 8066232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * to implement method {@link Lock#tryLock()}. 8076232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * 8086232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * <p>The default 8096232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * implementation throws {@link UnsupportedOperationException}. 8106232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * 8116232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * @param arg the acquire argument. This value is always the one 8126232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * passed to an acquire method, or is the value saved on entry 8136232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * to a condition wait. The value is otherwise uninterpreted 8146232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * and can represent anything you like. 8156232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * @return {@code true} if successful. Upon success, this object has 8166232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * been acquired. 8176232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * @throws IllegalMonitorStateException if acquiring would place this 8186232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * synchronizer in an illegal state. This exception must be 8196232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * thrown in a consistent fashion for synchronization to work 8206232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * correctly. 8216232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * @throws UnsupportedOperationException if exclusive mode is not supported 8226232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson */ 8236232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson protected boolean tryAcquire(long arg) { 8246232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson throw new UnsupportedOperationException(); 8256232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson } 8266232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson 8276232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson /** 8286232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * Attempts to set the state to reflect a release in exclusive 8296232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * mode. 8306232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * 8316232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * <p>This method is always invoked by the thread performing release. 8326232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * 8336232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * <p>The default implementation throws 8346232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * {@link UnsupportedOperationException}. 8356232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * 8366232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * @param arg the release argument. This value is always the one 8376232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * passed to a release method, or the current state value upon 8386232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * entry to a condition wait. The value is otherwise 8396232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * uninterpreted and can represent anything you like. 8406232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * @return {@code true} if this object is now in a fully released 8416232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * state, so that any waiting threads may attempt to acquire; 8426232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * and {@code false} otherwise. 8436232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * @throws IllegalMonitorStateException if releasing would place this 8446232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * synchronizer in an illegal state. This exception must be 8456232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * thrown in a consistent fashion for synchronization to work 8466232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * correctly. 8476232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * @throws UnsupportedOperationException if exclusive mode is not supported 8486232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson */ 8496232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson protected boolean tryRelease(long arg) { 8506232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson throw new UnsupportedOperationException(); 8516232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson } 8526232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson 8536232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson /** 8546232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * Attempts to acquire in shared mode. This method should query if 8556232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * the state of the object permits it to be acquired in the shared 8566232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * mode, and if so to acquire it. 8576232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * 8586232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * <p>This method is always invoked by the thread performing 8596232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * acquire. If this method reports failure, the acquire method 8606232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * may queue the thread, if it is not already queued, until it is 8616232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * signalled by a release from some other thread. 8626232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * 8636232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * <p>The default implementation throws {@link 8646232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * UnsupportedOperationException}. 8656232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * 8666232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * @param arg the acquire argument. This value is always the one 8676232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * passed to an acquire method, or is the value saved on entry 8686232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * to a condition wait. The value is otherwise uninterpreted 8696232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * and can represent anything you like. 8706232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * @return a negative value on failure; zero if acquisition in shared 8716232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * mode succeeded but no subsequent shared-mode acquire can 8726232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * succeed; and a positive value if acquisition in shared 8736232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * mode succeeded and subsequent shared-mode acquires might 8746232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * also succeed, in which case a subsequent waiting thread 8756232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * must check availability. (Support for three different 8766232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * return values enables this method to be used in contexts 8776232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * where acquires only sometimes act exclusively.) Upon 8786232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * success, this object has been acquired. 8796232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * @throws IllegalMonitorStateException if acquiring would place this 8806232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * synchronizer in an illegal state. This exception must be 8816232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * thrown in a consistent fashion for synchronization to work 8826232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * correctly. 8836232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * @throws UnsupportedOperationException if shared mode is not supported 8846232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson */ 8856232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson protected long tryAcquireShared(long arg) { 8866232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson throw new UnsupportedOperationException(); 8876232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson } 8886232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson 8896232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson /** 8906232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * Attempts to set the state to reflect a release in shared mode. 8916232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * 8926232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * <p>This method is always invoked by the thread performing release. 8936232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * 8946232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * <p>The default implementation throws 8956232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * {@link UnsupportedOperationException}. 8966232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * 8976232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * @param arg the release argument. This value is always the one 8986232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * passed to a release method, or the current state value upon 8996232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * entry to a condition wait. The value is otherwise 9006232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * uninterpreted and can represent anything you like. 9016232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * @return {@code true} if this release of shared mode may permit a 9026232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * waiting acquire (shared or exclusive) to succeed; and 9036232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * {@code false} otherwise 9046232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * @throws IllegalMonitorStateException if releasing would place this 9056232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * synchronizer in an illegal state. This exception must be 9066232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * thrown in a consistent fashion for synchronization to work 9076232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * correctly. 9086232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * @throws UnsupportedOperationException if shared mode is not supported 9096232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson */ 9106232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson protected boolean tryReleaseShared(long arg) { 9116232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson throw new UnsupportedOperationException(); 9126232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson } 9136232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson 9146232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson /** 9156232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * Returns {@code true} if synchronization is held exclusively with 9166232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * respect to the current (calling) thread. This method is invoked 9176232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * upon each call to a non-waiting {@link ConditionObject} method. 9186232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * (Waiting methods instead invoke {@link #release}.) 9196232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * 9206232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * <p>The default implementation throws {@link 9216232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * UnsupportedOperationException}. This method is invoked 9226232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * internally only within {@link ConditionObject} methods, so need 9236232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * not be defined if conditions are not used. 9246232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * 9256232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * @return {@code true} if synchronization is held exclusively; 9266232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * {@code false} otherwise 9276232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * @throws UnsupportedOperationException if conditions are not supported 9286232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson */ 9296232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson protected boolean isHeldExclusively() { 9306232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson throw new UnsupportedOperationException(); 9316232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson } 9326232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson 9336232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson /** 9346232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * Acquires in exclusive mode, ignoring interrupts. Implemented 9356232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * by invoking at least once {@link #tryAcquire}, 9366232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * returning on success. Otherwise the thread is queued, possibly 9376232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * repeatedly blocking and unblocking, invoking {@link 9386232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * #tryAcquire} until success. This method can be used 9396232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * to implement method {@link Lock#lock}. 9406232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * 9416232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * @param arg the acquire argument. This value is conveyed to 9426232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * {@link #tryAcquire} but is otherwise uninterpreted and 9436232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * can represent anything you like. 9446232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson */ 9456232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson public final void acquire(long arg) { 9466232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson if (!tryAcquire(arg) && 9476232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson acquireQueued(addWaiter(Node.EXCLUSIVE), arg)) 9486232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson selfInterrupt(); 9496232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson } 9506232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson 9516232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson /** 9526232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * Acquires in exclusive mode, aborting if interrupted. 9536232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * Implemented by first checking interrupt status, then invoking 9546232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * at least once {@link #tryAcquire}, returning on 9556232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * success. Otherwise the thread is queued, possibly repeatedly 9566232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * blocking and unblocking, invoking {@link #tryAcquire} 9576232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * until success or the thread is interrupted. This method can be 9586232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * used to implement method {@link Lock#lockInterruptibly}. 9596232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * 9606232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * @param arg the acquire argument. This value is conveyed to 9616232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * {@link #tryAcquire} but is otherwise uninterpreted and 9626232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * can represent anything you like. 9636232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * @throws InterruptedException if the current thread is interrupted 9646232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson */ 9658eb35c835be1345d3873a82cc9e42f944d698afdJesse Wilson public final void acquireInterruptibly(long arg) 9668eb35c835be1345d3873a82cc9e42f944d698afdJesse Wilson throws InterruptedException { 9676232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson if (Thread.interrupted()) 9686232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson throw new InterruptedException(); 9696232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson if (!tryAcquire(arg)) 9706232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson doAcquireInterruptibly(arg); 9716232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson } 9726232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson 9736232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson /** 9746232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * Attempts to acquire in exclusive mode, aborting if interrupted, 9756232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * and failing if the given timeout elapses. Implemented by first 9766232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * checking interrupt status, then invoking at least once {@link 9776232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * #tryAcquire}, returning on success. Otherwise, the thread is 9786232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * queued, possibly repeatedly blocking and unblocking, invoking 9796232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * {@link #tryAcquire} until success or the thread is interrupted 9806232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * or the timeout elapses. This method can be used to implement 9816232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * method {@link Lock#tryLock(long, TimeUnit)}. 9826232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * 9836232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * @param arg the acquire argument. This value is conveyed to 9846232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * {@link #tryAcquire} but is otherwise uninterpreted and 9856232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * can represent anything you like. 9866232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * @param nanosTimeout the maximum number of nanoseconds to wait 9876232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * @return {@code true} if acquired; {@code false} if timed out 9886232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * @throws InterruptedException if the current thread is interrupted 9896232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson */ 9908eb35c835be1345d3873a82cc9e42f944d698afdJesse Wilson public final boolean tryAcquireNanos(long arg, long nanosTimeout) 9918eb35c835be1345d3873a82cc9e42f944d698afdJesse Wilson throws InterruptedException { 9926232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson if (Thread.interrupted()) 9936232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson throw new InterruptedException(); 9946232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson return tryAcquire(arg) || 9956232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson doAcquireNanos(arg, nanosTimeout); 9966232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson } 9976232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson 9986232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson /** 9996232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * Releases in exclusive mode. Implemented by unblocking one or 10006232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * more threads if {@link #tryRelease} returns true. 10016232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * This method can be used to implement method {@link Lock#unlock}. 10026232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * 10036232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * @param arg the release argument. This value is conveyed to 10046232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * {@link #tryRelease} but is otherwise uninterpreted and 10056232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * can represent anything you like. 10066232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * @return the value returned from {@link #tryRelease} 10076232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson */ 10086232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson public final boolean release(long arg) { 10096232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson if (tryRelease(arg)) { 10106232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson Node h = head; 10116232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson if (h != null && h.waitStatus != 0) 10126232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson unparkSuccessor(h); 10136232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson return true; 10146232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson } 10156232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson return false; 10166232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson } 10176232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson 10186232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson /** 10196232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * Acquires in shared mode, ignoring interrupts. Implemented by 10206232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * first invoking at least once {@link #tryAcquireShared}, 10216232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * returning on success. Otherwise the thread is queued, possibly 10226232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * repeatedly blocking and unblocking, invoking {@link 10236232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * #tryAcquireShared} until success. 10246232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * 10256232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * @param arg the acquire argument. This value is conveyed to 10266232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * {@link #tryAcquireShared} but is otherwise uninterpreted 10276232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * and can represent anything you like. 10286232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson */ 10296232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson public final void acquireShared(long arg) { 10306232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson if (tryAcquireShared(arg) < 0) 10316232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson doAcquireShared(arg); 10326232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson } 10336232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson 10346232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson /** 10356232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * Acquires in shared mode, aborting if interrupted. Implemented 10366232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * by first checking interrupt status, then invoking at least once 10376232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * {@link #tryAcquireShared}, returning on success. Otherwise the 10386232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * thread is queued, possibly repeatedly blocking and unblocking, 10396232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * invoking {@link #tryAcquireShared} until success or the thread 10406232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * is interrupted. 104191770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle * @param arg the acquire argument. 10426232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * This value is conveyed to {@link #tryAcquireShared} but is 10436232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * otherwise uninterpreted and can represent anything 10446232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * you like. 10456232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * @throws InterruptedException if the current thread is interrupted 10466232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson */ 10478eb35c835be1345d3873a82cc9e42f944d698afdJesse Wilson public final void acquireSharedInterruptibly(long arg) 10488eb35c835be1345d3873a82cc9e42f944d698afdJesse Wilson throws InterruptedException { 10496232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson if (Thread.interrupted()) 10506232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson throw new InterruptedException(); 10516232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson if (tryAcquireShared(arg) < 0) 10526232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson doAcquireSharedInterruptibly(arg); 10536232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson } 10546232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson 10556232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson /** 10566232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * Attempts to acquire in shared mode, aborting if interrupted, and 10576232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * failing if the given timeout elapses. Implemented by first 10586232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * checking interrupt status, then invoking at least once {@link 10596232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * #tryAcquireShared}, returning on success. Otherwise, the 10606232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * thread is queued, possibly repeatedly blocking and unblocking, 10616232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * invoking {@link #tryAcquireShared} until success or the thread 10626232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * is interrupted or the timeout elapses. 10636232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * 10646232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * @param arg the acquire argument. This value is conveyed to 10656232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * {@link #tryAcquireShared} but is otherwise uninterpreted 10666232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * and can represent anything you like. 10676232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * @param nanosTimeout the maximum number of nanoseconds to wait 10686232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * @return {@code true} if acquired; {@code false} if timed out 10696232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * @throws InterruptedException if the current thread is interrupted 10706232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson */ 10718eb35c835be1345d3873a82cc9e42f944d698afdJesse Wilson public final boolean tryAcquireSharedNanos(long arg, long nanosTimeout) 10728eb35c835be1345d3873a82cc9e42f944d698afdJesse Wilson throws InterruptedException { 10736232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson if (Thread.interrupted()) 10746232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson throw new InterruptedException(); 10756232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson return tryAcquireShared(arg) >= 0 || 10766232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson doAcquireSharedNanos(arg, nanosTimeout); 10776232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson } 10786232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson 10796232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson /** 10806232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * Releases in shared mode. Implemented by unblocking one or more 10816232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * threads if {@link #tryReleaseShared} returns true. 10826232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * 10836232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * @param arg the release argument. This value is conveyed to 10846232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * {@link #tryReleaseShared} but is otherwise uninterpreted 10856232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * and can represent anything you like. 10866232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * @return the value returned from {@link #tryReleaseShared} 10876232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson */ 10886232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson public final boolean releaseShared(long arg) { 10896232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson if (tryReleaseShared(arg)) { 10906232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson doReleaseShared(); 10916232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson return true; 10926232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson } 10936232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson return false; 10946232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson } 10956232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson 10966232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson // Queue inspection methods 10976232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson 10986232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson /** 10996232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * Queries whether any threads are waiting to acquire. Note that 11006232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * because cancellations due to interrupts and timeouts may occur 11016232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * at any time, a {@code true} return does not guarantee that any 11026232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * other thread will ever acquire. 11036232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * 11046232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * <p>In this implementation, this operation returns in 11056232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * constant time. 11066232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * 11076232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * @return {@code true} if there may be other threads waiting to acquire 11086232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson */ 11096232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson public final boolean hasQueuedThreads() { 11106232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson return head != tail; 11116232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson } 11126232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson 11136232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson /** 11146232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * Queries whether any threads have ever contended to acquire this 11156232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * synchronizer; that is if an acquire method has ever blocked. 11166232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * 11176232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * <p>In this implementation, this operation returns in 11186232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * constant time. 11196232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * 11206232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * @return {@code true} if there has ever been contention 11216232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson */ 11226232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson public final boolean hasContended() { 11236232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson return head != null; 11246232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson } 11256232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson 11266232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson /** 11276232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * Returns the first (longest-waiting) thread in the queue, or 11286232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * {@code null} if no threads are currently queued. 11296232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * 11306232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * <p>In this implementation, this operation normally returns in 11316232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * constant time, but may iterate upon contention if other threads are 11326232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * concurrently modifying the queue. 11336232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * 11346232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * @return the first (longest-waiting) thread in the queue, or 11356232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * {@code null} if no threads are currently queued 11366232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson */ 11376232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson public final Thread getFirstQueuedThread() { 11386232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson // handle only fast path, else relay 11396232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson return (head == tail) ? null : fullGetFirstQueuedThread(); 11406232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson } 11416232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson 11426232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson /** 11436232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * Version of getFirstQueuedThread called when fastpath fails 11446232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson */ 11456232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson private Thread fullGetFirstQueuedThread() { 11466232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson /* 11476232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * The first node is normally head.next. Try to get its 11486232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * thread field, ensuring consistent reads: If thread 11496232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * field is nulled out or s.prev is no longer head, then 11506232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * some other thread(s) concurrently performed setHead in 11516232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * between some of our reads. We try this twice before 11526232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * resorting to traversal. 11536232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson */ 11546232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson Node h, s; 11556232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson Thread st; 11566232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson if (((h = head) != null && (s = h.next) != null && 11576232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson s.prev == head && (st = s.thread) != null) || 11586232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson ((h = head) != null && (s = h.next) != null && 11596232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson s.prev == head && (st = s.thread) != null)) 11606232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson return st; 11616232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson 11626232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson /* 11636232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * Head's next field might not have been set yet, or may have 11646232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * been unset after setHead. So we must check to see if tail 11656232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * is actually first node. If not, we continue on, safely 11666232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * traversing from tail back to head to find first, 11676232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * guaranteeing termination. 11686232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson */ 11696232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson 11706232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson Node t = tail; 11716232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson Thread firstThread = null; 11726232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson while (t != null && t != head) { 11736232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson Thread tt = t.thread; 11746232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson if (tt != null) 11756232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson firstThread = tt; 11766232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson t = t.prev; 11776232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson } 11786232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson return firstThread; 11796232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson } 11806232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson 11816232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson /** 11826232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * Returns true if the given thread is currently queued. 11836232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * 11846232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * <p>This implementation traverses the queue to determine 11856232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * presence of the given thread. 11866232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * 11876232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * @param thread the thread 11886232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * @return {@code true} if the given thread is on the queue 11896232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * @throws NullPointerException if the thread is null 11906232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson */ 11916232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson public final boolean isQueued(Thread thread) { 11926232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson if (thread == null) 11936232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson throw new NullPointerException(); 11946232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson for (Node p = tail; p != null; p = p.prev) 11956232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson if (p.thread == thread) 11966232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson return true; 11976232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson return false; 11986232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson } 11996232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson 12006232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson /** 12016232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * Returns {@code true} if the apparent first queued thread, if one 12026232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * exists, is waiting in exclusive mode. If this method returns 12036232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * {@code true}, and the current thread is attempting to acquire in 12046232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * shared mode (that is, this method is invoked from {@link 12056232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * #tryAcquireShared}) then it is guaranteed that the current thread 12066232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * is not the first queued thread. Used only as a heuristic in 12076232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * ReentrantReadWriteLock. 12086232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson */ 12096232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson final boolean apparentlyFirstQueuedIsExclusive() { 12106232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson Node h, s; 12116232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson return (h = head) != null && 12126232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson (s = h.next) != null && 12136232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson !s.isShared() && 12146232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson s.thread != null; 12156232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson } 12166232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson 12176232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson /** 12186232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * Queries whether any threads have been waiting to acquire longer 12196232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * than the current thread. 12206232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * 12216232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * <p>An invocation of this method is equivalent to (but may be 12226232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * more efficient than): 12236232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * <pre> {@code 12246232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * getFirstQueuedThread() != Thread.currentThread() && 12256232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * hasQueuedThreads()}</pre> 12266232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * 12276232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * <p>Note that because cancellations due to interrupts and 12286232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * timeouts may occur at any time, a {@code true} return does not 12296232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * guarantee that some other thread will acquire before the current 12306232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * thread. Likewise, it is possible for another thread to win a 12316232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * race to enqueue after this method has returned {@code false}, 12326232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * due to the queue being empty. 12336232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * 12346232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * <p>This method is designed to be used by a fair synchronizer to 1235a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson * avoid <a href="AbstractQueuedSynchronizer.html#barging">barging</a>. 12366232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * Such a synchronizer's {@link #tryAcquire} method should return 12376232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * {@code false}, and its {@link #tryAcquireShared} method should 12386232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * return a negative value, if this method returns {@code true} 12396232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * (unless this is a reentrant acquire). For example, the {@code 12406232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * tryAcquire} method for a fair, reentrant, exclusive mode 12416232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * synchronizer might look like this: 12426232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * 12436232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * <pre> {@code 12446232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * protected boolean tryAcquire(int arg) { 12456232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * if (isHeldExclusively()) { 12466232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * // A reentrant acquire; increment hold count 12476232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * return true; 12486232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * } else if (hasQueuedPredecessors()) { 12496232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * return false; 12506232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * } else { 12516232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * // try to acquire normally 12526232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * } 12536232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * }}</pre> 12546232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * 12556232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * @return {@code true} if there is a queued thread preceding the 12566232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * current thread, and {@code false} if the current thread 12576232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * is at the head of the queue or the queue is empty 12586232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * @since 1.7 12596232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson */ 126091770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle public final boolean hasQueuedPredecessors() { 12616232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson // The correctness of this depends on head being initialized 12626232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson // before tail and on head.next being accurate if the current 12636232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson // thread is first in queue. 12646232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson Node t = tail; // Read fields in reverse initialization order 12656232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson Node h = head; 12666232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson Node s; 12676232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson return h != t && 12686232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson ((s = h.next) == null || s.thread != Thread.currentThread()); 12696232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson } 12706232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson 12716232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson 12726232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson // Instrumentation and monitoring methods 12736232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson 12746232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson /** 12756232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * Returns an estimate of the number of threads waiting to 12766232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * acquire. The value is only an estimate because the number of 12776232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * threads may change dynamically while this method traverses 12786232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * internal data structures. This method is designed for use in 12796232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * monitoring system state, not for synchronization 12806232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * control. 12816232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * 12826232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * @return the estimated number of threads waiting to acquire 12836232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson */ 12846232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson public final int getQueueLength() { 12856232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson int n = 0; 12866232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson for (Node p = tail; p != null; p = p.prev) { 12876232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson if (p.thread != null) 12886232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson ++n; 12896232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson } 12906232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson return n; 12916232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson } 12926232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson 12936232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson /** 12946232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * Returns a collection containing threads that may be waiting to 12956232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * acquire. Because the actual set of threads may change 12966232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * dynamically while constructing this result, the returned 12976232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * collection is only a best-effort estimate. The elements of the 12986232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * returned collection are in no particular order. This method is 12996232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * designed to facilitate construction of subclasses that provide 13006232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * more extensive monitoring facilities. 13016232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * 13026232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * @return the collection of threads 13036232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson */ 13046232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson public final Collection<Thread> getQueuedThreads() { 13056232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson ArrayList<Thread> list = new ArrayList<Thread>(); 13066232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson for (Node p = tail; p != null; p = p.prev) { 13076232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson Thread t = p.thread; 13086232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson if (t != null) 13096232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson list.add(t); 13106232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson } 13116232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson return list; 13126232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson } 13136232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson 13146232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson /** 13156232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * Returns a collection containing threads that may be waiting to 13166232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * acquire in exclusive mode. This has the same properties 13176232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * as {@link #getQueuedThreads} except that it only returns 13186232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * those threads waiting due to an exclusive acquire. 13196232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * 13206232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * @return the collection of threads 13216232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson */ 13226232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson public final Collection<Thread> getExclusiveQueuedThreads() { 13236232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson ArrayList<Thread> list = new ArrayList<Thread>(); 13246232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson for (Node p = tail; p != null; p = p.prev) { 13256232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson if (!p.isShared()) { 13266232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson Thread t = p.thread; 13276232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson if (t != null) 13286232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson list.add(t); 13296232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson } 13306232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson } 13316232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson return list; 13326232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson } 13336232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson 13346232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson /** 13356232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * Returns a collection containing threads that may be waiting to 13366232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * acquire in shared mode. This has the same properties 13376232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * as {@link #getQueuedThreads} except that it only returns 13386232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * those threads waiting due to a shared acquire. 13396232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * 13406232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * @return the collection of threads 13416232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson */ 13426232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson public final Collection<Thread> getSharedQueuedThreads() { 13436232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson ArrayList<Thread> list = new ArrayList<Thread>(); 13446232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson for (Node p = tail; p != null; p = p.prev) { 13456232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson if (p.isShared()) { 13466232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson Thread t = p.thread; 13476232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson if (t != null) 13486232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson list.add(t); 13496232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson } 13506232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson } 13516232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson return list; 13526232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson } 13536232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson 13546232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson /** 13556232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * Returns a string identifying this synchronizer, as well as its state. 13566232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * The state, in brackets, includes the String {@code "State ="} 13576232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * followed by the current value of {@link #getState}, and either 13586232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * {@code "nonempty"} or {@code "empty"} depending on whether the 13596232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * queue is empty. 13606232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * 13616232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * @return a string identifying this synchronizer, as well as its state 13626232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson */ 13636232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson public String toString() { 13646232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson long s = getState(); 13656232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson String q = hasQueuedThreads() ? "non" : ""; 13666232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson return super.toString() + 13676232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson "[State = " + s + ", " + q + "empty queue]"; 13686232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson } 13696232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson 13706232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson 13716232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson // Internal support methods for Conditions 13726232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson 13736232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson /** 13746232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * Returns true if a node, always one that was initially placed on 13756232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * a condition queue, is now waiting to reacquire on sync queue. 13766232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * @param node the node 13776232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * @return true if is reacquiring 13786232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson */ 13796232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson final boolean isOnSyncQueue(Node node) { 13806232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson if (node.waitStatus == Node.CONDITION || node.prev == null) 13816232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson return false; 13826232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson if (node.next != null) // If has successor, it must be on queue 13836232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson return true; 13846232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson /* 13856232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * node.prev can be non-null, but not yet on queue because 13866232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * the CAS to place it on queue can fail. So we have to 13876232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * traverse from tail to make sure it actually made it. It 13886232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * will always be near the tail in calls to this method, and 13896232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * unless the CAS failed (which is unlikely), it will be 13906232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * there, so we hardly ever traverse much. 13916232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson */ 13926232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson return findNodeFromTail(node); 13936232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson } 13946232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson 13956232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson /** 13966232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * Returns true if node is on sync queue by searching backwards from tail. 13976232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * Called only when needed by isOnSyncQueue. 13986232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * @return true if present 13996232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson */ 14006232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson private boolean findNodeFromTail(Node node) { 1401edf43d27e240d82106f39ae91404963c23987234Narayan Kamath Node p = tail; 14026232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson for (;;) { 1403edf43d27e240d82106f39ae91404963c23987234Narayan Kamath if (p == node) 14046232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson return true; 1405edf43d27e240d82106f39ae91404963c23987234Narayan Kamath if (p == null) 14066232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson return false; 1407edf43d27e240d82106f39ae91404963c23987234Narayan Kamath p = p.prev; 14086232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson } 14096232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson } 14106232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson 14116232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson /** 14126232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * Transfers a node from a condition queue onto sync queue. 14136232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * Returns true if successful. 14146232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * @param node the node 14156232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * @return true if successfully transferred (else the node was 141691770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle * cancelled before signal) 14176232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson */ 14186232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson final boolean transferForSignal(Node node) { 14196232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson /* 14206232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * If cannot change waitStatus, the node has been cancelled. 14216232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson */ 14226232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson if (!compareAndSetWaitStatus(node, Node.CONDITION, 0)) 14236232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson return false; 14246232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson 14256232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson /* 14266232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * Splice onto queue and try to set waitStatus of predecessor to 14276232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * indicate that thread is (probably) waiting. If cancelled or 14286232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * attempt to set waitStatus fails, wake up to resync (in which 14296232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * case the waitStatus can be transiently and harmlessly wrong). 14306232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson */ 14316232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson Node p = enq(node); 14326232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson int ws = p.waitStatus; 14336232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson if (ws > 0 || !compareAndSetWaitStatus(p, ws, Node.SIGNAL)) 14346232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson LockSupport.unpark(node.thread); 14356232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson return true; 14366232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson } 14376232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson 14386232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson /** 143991770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle * Transfers node, if necessary, to sync queue after a cancelled wait. 144091770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle * Returns true if thread was cancelled before being signalled. 144191770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle * 144291770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle * @param node the node 14436232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * @return true if cancelled before the node was signalled 14446232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson */ 14456232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson final boolean transferAfterCancelledWait(Node node) { 14466232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson if (compareAndSetWaitStatus(node, Node.CONDITION, 0)) { 14476232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson enq(node); 14486232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson return true; 14496232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson } 14506232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson /* 14516232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * If we lost out to a signal(), then we can't proceed 14526232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * until it finishes its enq(). Cancelling during an 14536232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * incomplete transfer is both rare and transient, so just 14546232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * spin. 14556232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson */ 14566232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson while (!isOnSyncQueue(node)) 14576232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson Thread.yield(); 14586232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson return false; 14596232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson } 14606232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson 14616232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson /** 14626232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * Invokes release with current state value; returns saved state. 14636232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * Cancels node and throws exception on failure. 14646232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * @param node the condition node for this wait 14656232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * @return previous sync state 14666232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson */ 14676232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson final long fullyRelease(Node node) { 14686232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson boolean failed = true; 14696232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson try { 14706232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson long savedState = getState(); 14716232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson if (release(savedState)) { 14726232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson failed = false; 14736232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson return savedState; 14746232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson } else { 14756232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson throw new IllegalMonitorStateException(); 14766232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson } 14776232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson } finally { 14786232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson if (failed) 14796232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson node.waitStatus = Node.CANCELLED; 14806232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson } 14816232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson } 14826232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson 14836232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson // Instrumentation methods for conditions 14846232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson 14856232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson /** 14866232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * Queries whether the given ConditionObject 14876232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * uses this synchronizer as its lock. 14886232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * 14896232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * @param condition the condition 149091770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle * @return {@code true} if owned 14916232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * @throws NullPointerException if the condition is null 14926232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson */ 14936232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson public final boolean owns(ConditionObject condition) { 14946232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson return condition.isOwnedBy(this); 14956232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson } 14966232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson 14976232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson /** 14986232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * Queries whether any threads are waiting on the given condition 14996232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * associated with this synchronizer. Note that because timeouts 150091770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle * and interrupts may occur at any time, a {@code true} return 150191770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle * does not guarantee that a future {@code signal} will awaken 15026232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * any threads. This method is designed primarily for use in 15036232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * monitoring of the system state. 15046232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * 15056232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * @param condition the condition 150691770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle * @return {@code true} if there are any waiting threads 15076232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * @throws IllegalMonitorStateException if exclusive synchronization 15086232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * is not held 15096232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * @throws IllegalArgumentException if the given condition is 15106232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * not associated with this synchronizer 15116232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * @throws NullPointerException if the condition is null 15126232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson */ 15136232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson public final boolean hasWaiters(ConditionObject condition) { 15146232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson if (!owns(condition)) 15156232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson throw new IllegalArgumentException("Not owner"); 15166232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson return condition.hasWaiters(); 15176232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson } 15186232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson 15196232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson /** 15206232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * Returns an estimate of the number of threads waiting on the 15216232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * given condition associated with this synchronizer. Note that 15226232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * because timeouts and interrupts may occur at any time, the 15236232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * estimate serves only as an upper bound on the actual number of 15246232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * waiters. This method is designed for use in monitoring of the 15256232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * system state, not for synchronization control. 15266232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * 15276232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * @param condition the condition 15286232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * @return the estimated number of waiting threads 15296232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * @throws IllegalMonitorStateException if exclusive synchronization 15306232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * is not held 15316232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * @throws IllegalArgumentException if the given condition is 15326232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * not associated with this synchronizer 15336232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * @throws NullPointerException if the condition is null 15346232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson */ 15356232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson public final int getWaitQueueLength(ConditionObject condition) { 15366232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson if (!owns(condition)) 15376232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson throw new IllegalArgumentException("Not owner"); 15386232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson return condition.getWaitQueueLength(); 15396232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson } 15406232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson 15416232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson /** 15426232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * Returns a collection containing those threads that may be 15436232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * waiting on the given condition associated with this 15446232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * synchronizer. Because the actual set of threads may change 15456232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * dynamically while constructing this result, the returned 15466232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * collection is only a best-effort estimate. The elements of the 15476232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * returned collection are in no particular order. 15486232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * 15496232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * @param condition the condition 15506232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * @return the collection of threads 15516232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * @throws IllegalMonitorStateException if exclusive synchronization 15526232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * is not held 15536232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * @throws IllegalArgumentException if the given condition is 15546232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * not associated with this synchronizer 15556232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * @throws NullPointerException if the condition is null 15566232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson */ 15576232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson public final Collection<Thread> getWaitingThreads(ConditionObject condition) { 15586232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson if (!owns(condition)) 15596232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson throw new IllegalArgumentException("Not owner"); 15606232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson return condition.getWaitingThreads(); 15616232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson } 15626232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson 15636232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson /** 15646232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * Condition implementation for a {@link 15656232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * AbstractQueuedLongSynchronizer} serving as the basis of a {@link 15666232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * Lock} implementation. 15676232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * 15686232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * <p>Method documentation for this class describes mechanics, 15696232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * not behavioral specifications from the point of view of Lock 15706232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * and Condition users. Exported versions of this class will in 15716232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * general need to be accompanied by documentation describing 15726232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * condition semantics that rely on those of the associated 157391770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle * {@code AbstractQueuedLongSynchronizer}. 15746232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * 15756232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * <p>This class is Serializable, but all fields are transient, 15766232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * so deserialized conditions have no waiters. 15776232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * 15786232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * @since 1.6 15796232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson */ 15806232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson public class ConditionObject implements Condition, java.io.Serializable { 15816232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson private static final long serialVersionUID = 1173984872572414699L; 15826232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson /** First node of condition queue. */ 15836232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson private transient Node firstWaiter; 15846232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson /** Last node of condition queue. */ 15856232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson private transient Node lastWaiter; 15866232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson 15876232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson /** 158891770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle * Creates a new {@code ConditionObject} instance. 15896232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson */ 15906232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson public ConditionObject() { } 15916232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson 15926232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson // Internal methods 15936232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson 15946232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson /** 15956232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * Adds a new waiter to wait queue. 15966232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * @return its new wait node 15976232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson */ 15986232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson private Node addConditionWaiter() { 15996232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson Node t = lastWaiter; 16006232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson // If lastWaiter is cancelled, clean out. 16016232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson if (t != null && t.waitStatus != Node.CONDITION) { 16026232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson unlinkCancelledWaiters(); 16036232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson t = lastWaiter; 16046232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson } 16056232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson Node node = new Node(Thread.currentThread(), Node.CONDITION); 16066232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson if (t == null) 16076232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson firstWaiter = node; 16086232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson else 16096232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson t.nextWaiter = node; 16106232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson lastWaiter = node; 16116232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson return node; 16126232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson } 16136232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson 16146232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson /** 16156232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * Removes and transfers nodes until hit non-cancelled one or 16166232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * null. Split out from signal in part to encourage compilers 16176232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * to inline the case of no waiters. 16186232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * @param first (non-null) the first node on condition queue 16196232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson */ 16206232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson private void doSignal(Node first) { 16216232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson do { 16226232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson if ( (firstWaiter = first.nextWaiter) == null) 16236232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson lastWaiter = null; 16246232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson first.nextWaiter = null; 16256232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson } while (!transferForSignal(first) && 16266232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson (first = firstWaiter) != null); 16276232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson } 16286232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson 16296232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson /** 16306232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * Removes and transfers all nodes. 16316232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * @param first (non-null) the first node on condition queue 16326232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson */ 16336232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson private void doSignalAll(Node first) { 16346232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson lastWaiter = firstWaiter = null; 16356232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson do { 16366232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson Node next = first.nextWaiter; 16376232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson first.nextWaiter = null; 16386232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson transferForSignal(first); 16396232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson first = next; 16406232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson } while (first != null); 16416232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson } 16426232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson 16436232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson /** 16446232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * Unlinks cancelled waiter nodes from condition queue. 16456232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * Called only while holding lock. This is called when 16466232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * cancellation occurred during condition wait, and upon 16476232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * insertion of a new waiter when lastWaiter is seen to have 16486232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * been cancelled. This method is needed to avoid garbage 16496232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * retention in the absence of signals. So even though it may 16506232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * require a full traversal, it comes into play only when 16516232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * timeouts or cancellations occur in the absence of 16526232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * signals. It traverses all nodes rather than stopping at a 16536232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * particular target to unlink all pointers to garbage nodes 16546232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * without requiring many re-traversals during cancellation 16556232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * storms. 16566232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson */ 16576232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson private void unlinkCancelledWaiters() { 16586232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson Node t = firstWaiter; 16596232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson Node trail = null; 16606232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson while (t != null) { 16616232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson Node next = t.nextWaiter; 16626232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson if (t.waitStatus != Node.CONDITION) { 16636232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson t.nextWaiter = null; 16646232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson if (trail == null) 16656232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson firstWaiter = next; 16666232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson else 16676232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson trail.nextWaiter = next; 16686232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson if (next == null) 16696232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson lastWaiter = trail; 16706232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson } 16716232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson else 16726232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson trail = t; 16736232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson t = next; 16746232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson } 16756232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson } 16766232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson 16776232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson // public methods 16786232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson 16796232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson /** 16806232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * Moves the longest-waiting thread, if one exists, from the 16816232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * wait queue for this condition to the wait queue for the 16826232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * owning lock. 16836232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * 16846232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * @throws IllegalMonitorStateException if {@link #isHeldExclusively} 16856232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * returns {@code false} 16866232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson */ 16876232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson public final void signal() { 16886232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson if (!isHeldExclusively()) 16896232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson throw new IllegalMonitorStateException(); 16906232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson Node first = firstWaiter; 16916232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson if (first != null) 16926232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson doSignal(first); 16936232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson } 16946232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson 16956232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson /** 16966232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * Moves all threads from the wait queue for this condition to 16976232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * the wait queue for the owning lock. 16986232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * 16996232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * @throws IllegalMonitorStateException if {@link #isHeldExclusively} 17006232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * returns {@code false} 17016232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson */ 17026232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson public final void signalAll() { 17036232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson if (!isHeldExclusively()) 17046232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson throw new IllegalMonitorStateException(); 17056232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson Node first = firstWaiter; 17066232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson if (first != null) 17076232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson doSignalAll(first); 17086232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson } 17096232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson 17106232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson /** 17116232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * Implements uninterruptible condition wait. 17126232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * <ol> 17136232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * <li> Save lock state returned by {@link #getState}. 171491770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle * <li> Invoke {@link #release} with saved state as argument, 171591770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle * throwing IllegalMonitorStateException if it fails. 17166232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * <li> Block until signalled. 17176232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * <li> Reacquire by invoking specialized version of 17186232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * {@link #acquire} with saved state as argument. 17196232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * </ol> 17206232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson */ 17216232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson public final void awaitUninterruptibly() { 17226232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson Node node = addConditionWaiter(); 17236232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson long savedState = fullyRelease(node); 17246232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson boolean interrupted = false; 17256232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson while (!isOnSyncQueue(node)) { 17266232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson LockSupport.park(this); 17276232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson if (Thread.interrupted()) 17286232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson interrupted = true; 17296232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson } 17306232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson if (acquireQueued(node, savedState) || interrupted) 17316232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson selfInterrupt(); 17326232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson } 17336232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson 17346232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson /* 17356232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * For interruptible waits, we need to track whether to throw 17366232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * InterruptedException, if interrupted while blocked on 17376232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * condition, versus reinterrupt current thread, if 17386232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * interrupted while blocked waiting to re-acquire. 17396232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson */ 17406232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson 17416232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson /** Mode meaning to reinterrupt on exit from wait */ 17426232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson private static final int REINTERRUPT = 1; 17436232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson /** Mode meaning to throw InterruptedException on exit from wait */ 17446232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson private static final int THROW_IE = -1; 17456232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson 17466232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson /** 17476232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * Checks for interrupt, returning THROW_IE if interrupted 17486232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * before signalled, REINTERRUPT if after signalled, or 17496232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * 0 if not interrupted. 17506232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson */ 17516232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson private int checkInterruptWhileWaiting(Node node) { 17526232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson return Thread.interrupted() ? 17536232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson (transferAfterCancelledWait(node) ? THROW_IE : REINTERRUPT) : 17546232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson 0; 17556232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson } 17566232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson 17576232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson /** 17586232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * Throws InterruptedException, reinterrupts current thread, or 17596232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * does nothing, depending on mode. 17606232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson */ 17616232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson private void reportInterruptAfterWait(int interruptMode) 17626232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson throws InterruptedException { 17636232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson if (interruptMode == THROW_IE) 17646232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson throw new InterruptedException(); 17656232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson else if (interruptMode == REINTERRUPT) 17666232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson selfInterrupt(); 17676232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson } 17686232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson 17696232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson /** 17706232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * Implements interruptible condition wait. 17716232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * <ol> 17726232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * <li> If current thread is interrupted, throw InterruptedException. 17736232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * <li> Save lock state returned by {@link #getState}. 177491770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle * <li> Invoke {@link #release} with saved state as argument, 177591770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle * throwing IllegalMonitorStateException if it fails. 17766232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * <li> Block until signalled or interrupted. 17776232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * <li> Reacquire by invoking specialized version of 17786232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * {@link #acquire} with saved state as argument. 17796232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * <li> If interrupted while blocked in step 4, throw InterruptedException. 17806232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * </ol> 17816232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson */ 17826232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson public final void await() throws InterruptedException { 17836232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson if (Thread.interrupted()) 17846232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson throw new InterruptedException(); 17856232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson Node node = addConditionWaiter(); 17866232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson long savedState = fullyRelease(node); 17876232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson int interruptMode = 0; 17886232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson while (!isOnSyncQueue(node)) { 17896232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson LockSupport.park(this); 17906232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson if ((interruptMode = checkInterruptWhileWaiting(node)) != 0) 17916232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson break; 17926232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson } 17936232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson if (acquireQueued(node, savedState) && interruptMode != THROW_IE) 17946232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson interruptMode = REINTERRUPT; 17956232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson if (node.nextWaiter != null) // clean up if cancelled 17966232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson unlinkCancelledWaiters(); 17976232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson if (interruptMode != 0) 17986232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson reportInterruptAfterWait(interruptMode); 17996232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson } 18006232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson 18016232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson /** 18026232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * Implements timed condition wait. 18036232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * <ol> 18046232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * <li> If current thread is interrupted, throw InterruptedException. 18056232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * <li> Save lock state returned by {@link #getState}. 180691770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle * <li> Invoke {@link #release} with saved state as argument, 180791770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle * throwing IllegalMonitorStateException if it fails. 18086232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * <li> Block until signalled, interrupted, or timed out. 18096232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * <li> Reacquire by invoking specialized version of 18106232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * {@link #acquire} with saved state as argument. 18116232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * <li> If interrupted while blocked in step 4, throw InterruptedException. 18126232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * </ol> 18136232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson */ 18148eb35c835be1345d3873a82cc9e42f944d698afdJesse Wilson public final long awaitNanos(long nanosTimeout) 18158eb35c835be1345d3873a82cc9e42f944d698afdJesse Wilson throws InterruptedException { 18166232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson if (Thread.interrupted()) 18176232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson throw new InterruptedException(); 1818edf43d27e240d82106f39ae91404963c23987234Narayan Kamath long initialNanos = nanosTimeout; 18196232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson Node node = addConditionWaiter(); 18206232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson long savedState = fullyRelease(node); 182191770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle final long deadline = System.nanoTime() + nanosTimeout; 18226232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson int interruptMode = 0; 18236232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson while (!isOnSyncQueue(node)) { 18246232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson if (nanosTimeout <= 0L) { 18256232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson transferAfterCancelledWait(node); 18266232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson break; 18276232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson } 182891770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle if (nanosTimeout >= spinForTimeoutThreshold) 182991770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle LockSupport.parkNanos(this, nanosTimeout); 18306232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson if ((interruptMode = checkInterruptWhileWaiting(node)) != 0) 18316232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson break; 183291770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle nanosTimeout = deadline - System.nanoTime(); 18336232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson } 18346232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson if (acquireQueued(node, savedState) && interruptMode != THROW_IE) 18356232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson interruptMode = REINTERRUPT; 18366232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson if (node.nextWaiter != null) 18376232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson unlinkCancelledWaiters(); 18386232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson if (interruptMode != 0) 18396232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson reportInterruptAfterWait(interruptMode); 1840edf43d27e240d82106f39ae91404963c23987234Narayan Kamath long remaining = deadline - System.nanoTime(); // avoid overflow 1841edf43d27e240d82106f39ae91404963c23987234Narayan Kamath return (remaining < initialNanos) ? remaining : Long.MIN_VALUE; 18426232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson } 18436232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson 18446232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson /** 18456232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * Implements absolute timed condition wait. 18466232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * <ol> 18476232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * <li> If current thread is interrupted, throw InterruptedException. 18486232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * <li> Save lock state returned by {@link #getState}. 184991770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle * <li> Invoke {@link #release} with saved state as argument, 185091770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle * throwing IllegalMonitorStateException if it fails. 18516232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * <li> Block until signalled, interrupted, or timed out. 18526232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * <li> Reacquire by invoking specialized version of 18536232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * {@link #acquire} with saved state as argument. 18546232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * <li> If interrupted while blocked in step 4, throw InterruptedException. 18556232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * <li> If timed out while blocked in step 4, return false, else true. 18566232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * </ol> 18576232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson */ 18588eb35c835be1345d3873a82cc9e42f944d698afdJesse Wilson public final boolean awaitUntil(Date deadline) 18598eb35c835be1345d3873a82cc9e42f944d698afdJesse Wilson throws InterruptedException { 18606232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson long abstime = deadline.getTime(); 18616232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson if (Thread.interrupted()) 18626232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson throw new InterruptedException(); 18636232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson Node node = addConditionWaiter(); 18646232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson long savedState = fullyRelease(node); 18656232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson boolean timedout = false; 18666232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson int interruptMode = 0; 18676232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson while (!isOnSyncQueue(node)) { 18686232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson if (System.currentTimeMillis() > abstime) { 18696232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson timedout = transferAfterCancelledWait(node); 18706232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson break; 18716232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson } 18726232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson LockSupport.parkUntil(this, abstime); 18736232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson if ((interruptMode = checkInterruptWhileWaiting(node)) != 0) 18746232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson break; 18756232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson } 18766232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson if (acquireQueued(node, savedState) && interruptMode != THROW_IE) 18776232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson interruptMode = REINTERRUPT; 18786232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson if (node.nextWaiter != null) 18796232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson unlinkCancelledWaiters(); 18806232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson if (interruptMode != 0) 18816232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson reportInterruptAfterWait(interruptMode); 18826232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson return !timedout; 18836232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson } 18846232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson 18856232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson /** 18866232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * Implements timed condition wait. 18876232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * <ol> 18886232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * <li> If current thread is interrupted, throw InterruptedException. 18896232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * <li> Save lock state returned by {@link #getState}. 189091770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle * <li> Invoke {@link #release} with saved state as argument, 189191770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle * throwing IllegalMonitorStateException if it fails. 18926232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * <li> Block until signalled, interrupted, or timed out. 18936232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * <li> Reacquire by invoking specialized version of 18946232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * {@link #acquire} with saved state as argument. 18956232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * <li> If interrupted while blocked in step 4, throw InterruptedException. 18966232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * <li> If timed out while blocked in step 4, return false, else true. 18976232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * </ol> 18986232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson */ 18998eb35c835be1345d3873a82cc9e42f944d698afdJesse Wilson public final boolean await(long time, TimeUnit unit) 19008eb35c835be1345d3873a82cc9e42f944d698afdJesse Wilson throws InterruptedException { 19016232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson long nanosTimeout = unit.toNanos(time); 19026232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson if (Thread.interrupted()) 19036232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson throw new InterruptedException(); 19046232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson Node node = addConditionWaiter(); 19056232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson long savedState = fullyRelease(node); 190691770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle final long deadline = System.nanoTime() + nanosTimeout; 19076232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson boolean timedout = false; 19086232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson int interruptMode = 0; 19096232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson while (!isOnSyncQueue(node)) { 19106232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson if (nanosTimeout <= 0L) { 19116232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson timedout = transferAfterCancelledWait(node); 19126232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson break; 19136232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson } 19146232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson if (nanosTimeout >= spinForTimeoutThreshold) 19156232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson LockSupport.parkNanos(this, nanosTimeout); 19166232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson if ((interruptMode = checkInterruptWhileWaiting(node)) != 0) 19176232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson break; 191891770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle nanosTimeout = deadline - System.nanoTime(); 19196232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson } 19206232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson if (acquireQueued(node, savedState) && interruptMode != THROW_IE) 19216232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson interruptMode = REINTERRUPT; 19226232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson if (node.nextWaiter != null) 19236232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson unlinkCancelledWaiters(); 19246232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson if (interruptMode != 0) 19256232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson reportInterruptAfterWait(interruptMode); 19266232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson return !timedout; 19276232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson } 19286232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson 19296232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson // support for instrumentation 19306232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson 19316232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson /** 19326232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * Returns true if this condition was created by the given 19336232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * synchronization object. 19346232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * 19356232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * @return {@code true} if owned 19366232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson */ 19376232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson final boolean isOwnedBy(AbstractQueuedLongSynchronizer sync) { 19386232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson return sync == AbstractQueuedLongSynchronizer.this; 19396232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson } 19406232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson 19416232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson /** 19426232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * Queries whether any threads are waiting on this condition. 19436232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * Implements {@link AbstractQueuedLongSynchronizer#hasWaiters}. 19446232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * 19456232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * @return {@code true} if there are any waiting threads 19466232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * @throws IllegalMonitorStateException if {@link #isHeldExclusively} 19476232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * returns {@code false} 19486232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson */ 19496232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson protected final boolean hasWaiters() { 19506232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson if (!isHeldExclusively()) 19516232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson throw new IllegalMonitorStateException(); 19526232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson for (Node w = firstWaiter; w != null; w = w.nextWaiter) { 19536232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson if (w.waitStatus == Node.CONDITION) 19546232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson return true; 19556232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson } 19566232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson return false; 19576232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson } 19586232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson 19596232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson /** 19606232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * Returns an estimate of the number of threads waiting on 19616232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * this condition. 19626232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * Implements {@link AbstractQueuedLongSynchronizer#getWaitQueueLength}. 19636232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * 19646232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * @return the estimated number of waiting threads 19656232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * @throws IllegalMonitorStateException if {@link #isHeldExclusively} 19666232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * returns {@code false} 19676232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson */ 19686232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson protected final int getWaitQueueLength() { 19696232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson if (!isHeldExclusively()) 19706232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson throw new IllegalMonitorStateException(); 19716232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson int n = 0; 19726232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson for (Node w = firstWaiter; w != null; w = w.nextWaiter) { 19736232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson if (w.waitStatus == Node.CONDITION) 19746232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson ++n; 19756232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson } 19766232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson return n; 19776232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson } 19786232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson 19796232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson /** 19806232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * Returns a collection containing those threads that may be 19816232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * waiting on this Condition. 19826232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * Implements {@link AbstractQueuedLongSynchronizer#getWaitingThreads}. 19836232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * 19846232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * @return the collection of threads 19856232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * @throws IllegalMonitorStateException if {@link #isHeldExclusively} 19866232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * returns {@code false} 19876232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson */ 19886232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson protected final Collection<Thread> getWaitingThreads() { 19896232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson if (!isHeldExclusively()) 19906232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson throw new IllegalMonitorStateException(); 19916232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson ArrayList<Thread> list = new ArrayList<Thread>(); 19926232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson for (Node w = firstWaiter; w != null; w = w.nextWaiter) { 19936232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson if (w.waitStatus == Node.CONDITION) { 19946232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson Thread t = w.thread; 19956232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson if (t != null) 19966232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson list.add(t); 19976232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson } 19986232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson } 19996232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson return list; 20006232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson } 20016232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson } 20026232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson 20036232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson /** 20046232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * Setup to support compareAndSet. We need to natively implement 20056232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * this here: For the sake of permitting future enhancements, we 20066232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * cannot explicitly subclass AtomicLong, which would be 20076232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * efficient and useful otherwise. So, as the lesser of evils, we 20086232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * natively implement using hotspot intrinsics API. And while we 20096232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * are at it, we do the same for other CASable fields (which could 20106232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * otherwise be done with atomic field updaters). 20116232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson */ 20126232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson private static final Unsafe unsafe = Unsafe.getUnsafe(); 20136232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson private static final long stateOffset; 20146232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson private static final long headOffset; 20156232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson private static final long tailOffset; 20166232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson private static final long waitStatusOffset; 20176232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson private static final long nextOffset; 20186232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson 20196232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson static { 20206232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson try { 20216232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson stateOffset = unsafe.objectFieldOffset 20226232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson (AbstractQueuedLongSynchronizer.class.getDeclaredField("state")); 20236232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson headOffset = unsafe.objectFieldOffset 20246232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson (AbstractQueuedLongSynchronizer.class.getDeclaredField("head")); 20256232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson tailOffset = unsafe.objectFieldOffset 20266232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson (AbstractQueuedLongSynchronizer.class.getDeclaredField("tail")); 20276232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson waitStatusOffset = unsafe.objectFieldOffset 20286232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson (Node.class.getDeclaredField("waitStatus")); 20296232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson nextOffset = unsafe.objectFieldOffset 20306232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson (Node.class.getDeclaredField("next")); 20316232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson 20326232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson } catch (Exception ex) { throw new Error(ex); } 2033edf43d27e240d82106f39ae91404963c23987234Narayan Kamath 2034edf43d27e240d82106f39ae91404963c23987234Narayan Kamath // Reduce the risk of rare disastrous classloading in first call to 2035edf43d27e240d82106f39ae91404963c23987234Narayan Kamath // LockSupport.park: https://bugs.openjdk.java.net/browse/JDK-8074773 2036edf43d27e240d82106f39ae91404963c23987234Narayan Kamath Class<?> ensureLoaded = LockSupport.class; 20376232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson } 20386232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson 20396232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson /** 20406232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * CAS head field. Used only by enq. 20416232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson */ 20426232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson private final boolean compareAndSetHead(Node update) { 20436232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson return unsafe.compareAndSwapObject(this, headOffset, null, update); 20446232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson } 20456232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson 20466232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson /** 20476232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * CAS tail field. Used only by enq. 20486232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson */ 20496232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson private final boolean compareAndSetTail(Node expect, Node update) { 20506232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson return unsafe.compareAndSwapObject(this, tailOffset, expect, update); 20516232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson } 20526232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson 20536232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson /** 20546232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * CAS waitStatus field of a node. 20556232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson */ 20568eb35c835be1345d3873a82cc9e42f944d698afdJesse Wilson private static final boolean compareAndSetWaitStatus(Node node, 20576232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson int expect, 20586232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson int update) { 20596232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson return unsafe.compareAndSwapInt(node, waitStatusOffset, 20606232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson expect, update); 20616232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson } 20626232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson 20636232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson /** 20646232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson * CAS next field of a node. 20656232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson */ 20668eb35c835be1345d3873a82cc9e42f944d698afdJesse Wilson private static final boolean compareAndSetNext(Node node, 20676232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson Node expect, 20686232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson Node update) { 20696232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson return unsafe.compareAndSwapObject(node, nextOffset, expect, update); 20706232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson } 20716232a5efed0ea103e35aec73206e5e03a8b82e0cJesse Wilson} 2072