Main Page | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Class Members | File Members | Related Pages

AbstractLock Class Reference

Provides a framework for implementing blocking locks and related synchronizers (semaphores, events, etc) that rely on first-in-first-out (FIFO) wait queues. More...

Inheritance diagram for AbstractLock:

CountDownLatch::Sync ReentrantLock::Sync ReentrantReadWriteLock::Sync Semaphore::Sync ReentrantLock::FairSync ReentrantLock::NonfairSync ReentrantReadWriteLock::FairSync ReentrantReadWriteLock::NonfairSync Semaphore::FairSync Semaphore::NonfairSync List of all members.

Public Member Functions

Node v_head ()
void v_head (Node val)
Node v_tail ()
void v_tail (Node val)
bool isHeldExclusively ()
void acquire (int arg)
bool tryAcquireNanos (int arg, long nanosTimeout)
bool release (int arg)
void acquireShared (int arg)
bool tryAcquireSharedNanos (int arg, long nanosTimeout)
bool releaseShared (int arg)
bool hasQueuedThreads ()
bool hasContended ()
Thread getFirstQueuedThread ()

Protected Member Functions

 this ()
final int state ()
final void state (int newState)
bool tryAcquire (int arg)
bool tryRelease (int arg)
int tryAcquireShared (int arg)
bool tryReleaseShared (int arg)

Protected Attributes

int state_

Private Member Functions

Node enq (Node node)
Node addWaiter (Node mode)
void setHead (Node node)
void unparkSuccessor (Node node)
void setHeadAndPropagate (Node node, int propagate)
void cancelAcquire (Node node)
void acquireQueued (Node node, int arg)
bool doAcquireNanos (int arg, long nanosTimeout)
void doAcquireShared (int arg)
bool doAcquireSharedNanos (int arg, long nanosTimeout)
Thread fullGetFirstQueuedThread ()
bool isQueued (Thread thread)
int getQueueLength ()
Thread[] getQueuedThreads ()
Thread[] getExclusiveQueuedThreads ()
Thread[] getSharedQueuedThreads ()
char[] toString ()
bool isOnSyncQueue (Node node)
bool findNodeFromTail (Node node)
bool transferForNotify (Node node)
bool transferAfterCancelledWait (Node node)
int fullyRelease (Node node)
bool owns (ConditionObject condition)
bool hasWaiters (ConditionObject condition)
int getWaitQueueLength (ConditionObject condition)
Thread[] getWaitingThreads (ConditionObject condition)

Static Private Member Functions

static bool shouldParkAfterFailedAcquire (Node pred, Node node)

Private Attributes

Node head
Node tail

Classes

class  ConditionObject
class  Node

Detailed Description

Provides a framework for implementing blocking locks and related synchronizers (semaphores, events, etc) that rely on first-in-first-out (FIFO) wait queues.

This class is designed to be a useful basis for most kinds of synchronizers that rely on a single atomic int value to represent state. Subclasses must define the protected methods that change this state, and which define what that state means in terms of this object being acquired or released. Given these, the other methods in this class carry out all queuing and blocking mechanics. Subclasses can maintain other state fields, but only the atomically updated int value manipulated using the getter and setter for the state property and compareAndSet32 is tracked with respect to synchronization.

Subclasses should be defined as non-public internal helper classes that are used to implement the synchronization properties of their enclosing class. Class AbstractLock does not implement any synchronization interface. Instead it defines methods such as acquire that can be invoked as appropriate by concrete locks and related synchronizers to implement their public methods.

This class supports either or both a default exclusive mode and a shared mode. When acquired in exclusive mode, attempted acquires by other threads cannot succeed. Shared mode acquires by multiple threads may (but need not) succeed. This class does not "understand" these differences except in the mechanical sense that when a shared mode acquire succeeds, the next waiting thread (if one exists) must also determine whether it can acquire as well. Threads waiting in the different modes share the same FIFO queue. Usually, implementation subclasses support only one of these modes, but both can come into play for example in a ReadWriteLock. Subclasses that support only exclusive or only shared modes need not define the methods supporting the unused mode.

This class defines a nested ConditionObject class that can be used as a Condition implementation by subclasses supporting exclusive mode for which method isHeldExclusively reports whether synchronization is exclusively held with respect to the current thread, method release invoked with the current state value fully releases this object, and acquire, given this saved state value, eventually restores this object to its previous acquired state. No AbstractLock method otherwise creates such a condition, so if this constraint cannot be met, do not use it. The behavior of ConditionObject depends of course on the semantics of its synchronizer implementation.

This class provides inspection, instrumentation, and monitoring methods for the internal queue, as well as similar methods for condition objects. These can be exported as desired into classes using an AbstractLock for their synchronization mechanics.

Usage

To use this class as the basis of a synchronizer, redefine the following methods, as applicable, by inspecting and/or modifying the synchronization state using the state getter and setter and/or compareAndSet32.

Each of these methods by default throws UnsupportedOperationException. Implementations of these methods must be internally thread-safe, and should in general be short and not block. Defining these methods is the only supported means of using this class. All other methods are declared final because they cannot be independently varied.

Even though this class is based on an internal FIFO queue, it does not automatically enforce FIFO acquisition policies. The core of exclusive synchronization takes the form:

 Acquire:
     while (!tryAcquire(arg)) {
        enqueue thread if it is not already queued;
        possibly block current thread;
     }

 Release:
     if (tryRelease(arg))
        unblock the first queued thread;
 

(Shared mode is similar but may involve cascading signals.)

Because checks in acquire are invoked before enqueuing, a newly acquiring thread may barge ahead of others that are blocked and queued. However, you can, if desired, define tryAcquire and/or tryAcquireShared to disable barging by internally invoking one or more of the inspection methods. In particular, a strict FIFO lock can define tryAcquire to immediately return false if getFirstQueuedThread does not return the current thread. A normally preferable non-strict fair version can immediately return false only if hasQueuedThreads returns true and getFirstQueuedThread is not the current thread; or equivalently, that getFirstQueuedThread is both non-null and not the current thread. Further variations are possible.

Throughput and scalability are generally highest for the default barging (also known as greedy, renouncement, and convoy-avoidance) strategy. While this is not guaranteed to be fair or starvation-free, earlier queued threads are allowed to recontend before later queued threads, and each recontention has an unbiased chance to succeed against incoming threads. Also, while acquires do not "spin" in the usual sense, they may perform multiple invocations of tryAcquire interspersed with other computations before blocking. This gives most of the benefits of spins when exclusive synchronization is only briefly held, without most of the liabilities when it isn't. If so desired, you can augment this by preceding calls to acquire methods with "fast-path" checks, possibly prechecking hasContended and/or hasQueuedThreads to only do so if the synchronizer is likely not to be contended.

Usage Examples

Here is a non-reentrant mutual exclusion lock class that uses the value zero to represent the unlocked state, and one to represent the locked state. It also supports conditions and exposes one of the instrumentation methods:

 class Mutex : Lock {

    // Our internal helper class
    private class Sync : AbstractLock {
      // Report whether in locked state
      bool isHeldExclusively() { 
        return state == 1; 
      }

      // Acquire the lock if state is zero
      bool tryAcquire(int acquires) {
        assert ( acquires == 1 ); // Otherwise unused
        return Atomic.compareAndSet32(&state, 0, 1);
      }

      // Release the lock by setting state to zero
      protected bool tryRelease(int releases) {
        assert ( releases == 1 ); // Otherwise unused
        state = 0;
        return true;
      }
       
      // Provide a Condition
      Condition newCondition() { return new ConditionObject(); }
    }

    // The sync object does all the hard work. We just forward to it.
    private Sync sync = new Sync();

    void lock()                { sync.acquire(1); }
    bool tryLock()          { return sync.tryAcquire(1); }
    void unlock()              { sync.release(1); }
    Condition newCondition()   { return sync.newCondition(); }
    bool isLocked()         { return sync.isHeldExclusively(); }
    bool hasQueuedThreads() { return sync.hasQueuedThreads(); }
    bool tryLock(long timeout, TimeUnit unit) {
      return sync.tryAcquireNanos(1, toNanos(timeout,unit));
    }
 }

Here is a latch class that is like a CountDownLatch except that it only requires a single notify to fire. Because a latch is non-exclusive, it uses the shared acquire and release methods.

 class BoolLatch {
    private class Sync : AbstractLock {
      bool isSignalled() { return state != 0; }

      protected int tryAcquireShared(int ignore) {
        return isSignalled()? 1 : -1;
      }
        
      protected bool tryReleaseShared(int ignore) {
        state = 1;
        return true;
      }
    }

    private final Sync sync = new Sync();
    bool isSignalled() { return sync.isSignalled(); }
    void notify()         { sync.releaseShared(1); }
    void wait() {
      sync.acquireShared(1);
    }
 }

Definition at line 232 of file LockImpl.d.


Member Function Documentation

this  )  [inline, protected]
 

Creates a new AbstractLock instance with initial synchronization state of zero.

Definition at line 238 of file LockImpl.d.

Node v_head  )  [inline]
 

Definition at line 448 of file LockImpl.d.

References head.

Referenced by acquireQueued(), doAcquireNanos(), doAcquireShared(), doAcquireSharedNanos(), and hasContended().

void v_head Node  val  )  [inline]
 

Definition at line 449 of file LockImpl.d.

References head.

Node v_tail  )  [inline]
 

Definition at line 456 of file LockImpl.d.

References tail.

Referenced by addWaiter(), enq(), findNodeFromTail(), getExclusiveQueuedThreads(), getQueuedThreads(), getQueueLength(), getSharedQueuedThreads(), and isQueued().

void v_tail Node  val  )  [inline]
 

Definition at line 457 of file LockImpl.d.

References tail.

final int state  )  [inline, protected]
 

Returns the current value of synchronization state. This operation has memory semantics of a volatile read.

Returns:
current state value

Definition at line 469 of file LockImpl.d.

References state_.

Referenced by fullyRelease(), and toString().

final void state int  newState  )  [inline, protected]
 

Sets the value of synchronization state. This operation has memory semantics of a volatile write.

Parameters:
newState the new state value

Definition at line 478 of file LockImpl.d.

References state_.

Node enq Node  node  )  [inline, private]
 

Insert node into queue, initializing if necessary. See picture above.

Parameters:
node the node to insert
Returns:
node's predecessor

Definition at line 489 of file LockImpl.d.

References head, AbstractLock::Node::next, Node, AbstractLock::Node::prev, tail, AbstractLock::Node::v_next(), AbstractLock::Node::v_prev(), and v_tail().

Referenced by addWaiter(), transferAfterCancelledWait(), and transferForNotify().

Node addWaiter Node  mode  )  [inline, private]
 

Create and enq node for given thread and mode

Parameters:
current the thread
mode Node.EXCLUSIVE for exclusive, Node.SHARED for shared
Returns:
the new node

Definition at line 521 of file LockImpl.d.

References enq(), Node, tail, AbstractLock::Node::v_next(), AbstractLock::Node::v_prev(), and v_tail().

Referenced by acquire(), doAcquireNanos(), doAcquireShared(), and doAcquireSharedNanos().

void setHead Node  node  )  [inline, private]
 

Set head of queue to be node, thus dequeuing. Called only by acquire methods. Also nulls out unused fields for sake of GC and to suppress unnecessary signals and traversals.

Parameters:
node the node

Definition at line 542 of file LockImpl.d.

References head, AbstractLock::Node::prev, and AbstractLock::Node::thread.

Referenced by acquireQueued(), doAcquireNanos(), and setHeadAndPropagate().

void unparkSuccessor Node  node  )  [inline, private]
 

Wake up node's successor, if one exists.

Parameters:
node the node

Definition at line 554 of file LockImpl.d.

References AbstractLock::Node::next, AbstractLock::Node::prev, AbstractLock::Node::SIGNAL, tail, AbstractLock::Node::thread, thread, and AbstractLock::Node::waitStatus.

Referenced by cancelAcquire(), release(), releaseShared(), and setHeadAndPropagate().

void setHeadAndPropagate Node  node,
int  propagate
[inline, private]
 

Set head of queue, and check if successor may be waiting in shared mode, if so propagating if propagate > 0.

Parameters:
pred the node holding waitStatus for node
node the node
propagate the return value from a tryAcquireShared

Definition at line 593 of file LockImpl.d.

References AbstractLock::Node::isShared(), setHead(), unparkSuccessor(), AbstractLock::Node::v_next(), and AbstractLock::Node::v_waitStatus().

Referenced by doAcquireShared(), and doAcquireSharedNanos().

void cancelAcquire Node  node  )  [inline, private]
 

Cancel an ongoing attempt to acquire.

Parameters:
node the node

Definition at line 612 of file LockImpl.d.

References AbstractLock::Node::CANCELLED, AbstractLock::Node::thread, unparkSuccessor(), and AbstractLock::Node::waitStatus.

Referenced by acquireQueued(), doAcquireNanos(), doAcquireShared(), and doAcquireSharedNanos().

static bool shouldParkAfterFailedAcquire Node  pred,
Node  node
[inline, static, private]
 

Checks and updates status for a node that failed to acquire. Returns true if thread should block. This is the main signal control in all acquire loops. Requires that pred == node.prev

Parameters:
pred node's predecessor holding status
node the node
Returns:
true if thread should block

Definition at line 631 of file LockImpl.d.

References AbstractLock::Node::prev, AbstractLock::Node::SIGNAL, AbstractLock::Node::v_prev(), AbstractLock::Node::v_waitStatus(), and AbstractLock::Node::waitStatus.

Referenced by acquireQueued(), doAcquireNanos(), doAcquireShared(), and doAcquireSharedNanos().

void acquireQueued Node  node,
int  arg
[inline, private]
 

Acquire in exclusive uninterruptible mode for thread already in queue. Used by condition wait methods as well as acquire.

Parameters:
node the node
arg the acquire argument

Definition at line 671 of file LockImpl.d.

References assert(), cancelAcquire(), Exception, AbstractLock::Node::predecessor(), setHead(), shouldParkAfterFailedAcquire(), tryAcquire(), v_head(), AbstractLock::Node::v_next(), and version.

Referenced by acquire(), AbstractLock::ConditionObject::wait(), and AbstractLock::ConditionObject::waitNanos().

bool doAcquireNanos int  arg,
long  nanosTimeout
[inline, private]
 

Acquire in exclusive timed mode

Parameters:
arg the acquire argument
nanosTimeout max wait time
Returns:
true if acquired

Definition at line 699 of file LockImpl.d.

References addWaiter(), assert(), cancelAcquire(), Exception, AbstractLock::Node::predecessor(), setHead(), shouldParkAfterFailedAcquire(), sleepNanos(), tryAcquire(), v_head(), and AbstractLock::Node::v_next().

Referenced by tryAcquireNanos().

void doAcquireShared int  arg  )  [inline, private]
 

Acquire in shared uninterruptible mode

Parameters:
arg the acquire argument

Definition at line 732 of file LockImpl.d.

References addWaiter(), assert(), cancelAcquire(), Exception, AbstractLock::Node::predecessor(), setHeadAndPropagate(), shouldParkAfterFailedAcquire(), tryAcquireShared(), v_head(), AbstractLock::Node::v_next(), and version.

Referenced by acquireShared().

bool doAcquireSharedNanos int  arg,
long  nanosTimeout
[inline, private]
 

Acquire in shared timed mode

Parameters:
arg the acquire argument
nanosTimeout max wait time
Returns:
true if acquired

Definition at line 764 of file LockImpl.d.

References addWaiter(), assert(), cancelAcquire(), Exception, AbstractLock::Node::predecessor(), setHeadAndPropagate(), shouldParkAfterFailedAcquire(), sleepNanos(), tryAcquireShared(), v_head(), and AbstractLock::Node::v_next().

Referenced by tryAcquireSharedNanos().

bool tryAcquire int  arg  )  [inline, protected]
 

Attempts to acquire in exclusive mode. This method should query if the state of the object permits it to be acquired in the exclusive mode, and if so to acquire it.

This method is always invoked by the thread performing acquire. If this method reports failure, the acquire method may queue the thread, if it is not already queued, until it is signalled by a release from some other thread. This can be used to implement method tryLock().

The default implementation throws UnsupportedOperationException

Parameters:
arg the acquire argument. This value is always the one passed to an acquire method, or is the value saved on entry to a condition wait. The value is otherwise uninterpreted and can represent anything you like.
Returns:
true if successful. Upon success, this object has been acquired.

Reimplemented in ReentrantReadWriteLock::NonfairSync, ReentrantReadWriteLock::FairSync, ReentrantLock::NonfairSync, and ReentrantLock::FairSync.

Definition at line 819 of file LockImpl.d.

Referenced by acquire(), acquireQueued(), doAcquireNanos(), and tryAcquireNanos().

bool tryRelease int  arg  )  [inline, protected]
 

Attempts to set the state to reflect a release in exclusive mode. This method is always invoked by the thread performing release.

The default implementation throws UnsupportedOperationException

Parameters:
arg the release argument. This value is always the one passed to a release method, or the current state value upon entry to a condition wait. The value is otherwise uninterpreted and can represent anything you like.
Returns:
true if this object is now in a fully released state, so that any waiting threads may attempt to acquire; and false otherwise.

Reimplemented in ReentrantReadWriteLock::Sync, and ReentrantLock::Sync.

Definition at line 840 of file LockImpl.d.

Referenced by release().

int tryAcquireShared int  arg  )  [inline, protected]
 

Attempts to acquire in shared mode. This method should query if the state of the object permits it to be acquired in the shared mode, and if so to acquire it.

This method is always invoked by the thread performing acquire. If this method reports failure, the acquire method may queue the thread, if it is not already queued, until it is signalled by a release from some other thread.

The default implementation throws UnsupportedOperationException

Parameters:
arg the acquire argument. This value is always the one passed to an acquire method, or is the value saved on entry to a condition wait. The value is otherwise uninterpreted and can represent anything you like.
Returns:
a negative value on failure, zero on exclusive success, and a positive value if non-exclusively successful, in which case a subsequent waiting thread must check availability. (Support for three different return values enables this method to be used in contexts where acquires only sometimes act exclusively.) Upon success, this object has been acquired.

Reimplemented in CountDownLatch::Sync, ReentrantReadWriteLock::NonfairSync, ReentrantReadWriteLock::FairSync, Semaphore::NonfairSync, and Semaphore::FairSync.

Definition at line 870 of file LockImpl.d.

Referenced by acquireShared(), doAcquireShared(), doAcquireSharedNanos(), and tryAcquireSharedNanos().

bool tryReleaseShared int  arg  )  [inline, protected]
 

Attempts to set the state to reflect a release in shared mode. This method is always invoked by the thread performing release. The default implementation throws UnsupportedOperationException.

Parameters:
arg the release argument. This value is always the one passed to a release method, or the current state value upon entry to a condition wait. The value is otherwise uninterpreted and can represent anything you like.
Returns:
true if this object is now in a fully released state, so that any waiting threads may attempt to acquire; and false otherwise.

Reimplemented in CountDownLatch::Sync, ReentrantReadWriteLock::Sync, and Semaphore::Sync.

Definition at line 890 of file LockImpl.d.

Referenced by releaseShared().

bool isHeldExclusively  )  [inline]
 

Returns true if synchronization is held exclusively with respect to the current (calling) thread. This method is invoked upon each call to a non-waiting ConditionObject method. (Waiting methods instead invoke release.)

The default implementation throws UnsupportedOperationException. This method is invoked internally only within ConditionObject methods, so need not be defined if conditions are not used.

Returns:
true if synchronization is held exclusively; else false

Reimplemented in ReentrantReadWriteLock::Sync, and ReentrantLock::Sync.

Definition at line 908 of file LockImpl.d.

Referenced by AbstractLock::ConditionObject::getWaitingThreads(), AbstractLock::ConditionObject::getWaitQueueLength(), AbstractLock::ConditionObject::hasWaiters(), AbstractLock::ConditionObject::notify(), and AbstractLock::ConditionObject::notifyAll().

void acquire int  arg  )  [inline]
 

Acquires in exclusive mode. Implemented by invoking at least once tryAcquire, returning on success. Otherwise the thread is queued, possibly repeatedly blocking and unblocking, invoking tryAcquire until success. This method can be used to implement method Lock.lock

Parameters:
arg the acquire argument. This value is conveyed to tryAcquire but is otherwise uninterpreted and can represent anything you like.

Definition at line 924 of file LockImpl.d.

References acquireQueued(), addWaiter(), and tryAcquire().

Referenced by ReentrantLock::FairSync::lock(), ReentrantLock::NonfairSync::lock(), ReentrantReadWriteLock::FairSync::wlock(), and ReentrantReadWriteLock::NonfairSync::wlock().

bool tryAcquireNanos int  arg,
long  nanosTimeout
[inline]
 

Attempts to acquire in exclusive mode and failing if the given timeout elapses. Implemented by first checking interrupt status, then invoking at least once tryAcquire, returning on success. Otherwise, the thread is queued, possibly repeatedly blocking and unblocking, invoking tryAcquire until success or the timeout elapses. This method can be used to implement method Lock.tryLock(long, TimeUnit).

Parameters:
arg the acquire argument. This value is conveyed to tryAcquire but is otherwise uninterpreted and can represent anything you like.
nanosTimeout the maximum number of nanoseconds to wait
Returns:
true if acquired; false if timed out

Definition at line 944 of file LockImpl.d.

References doAcquireNanos(), and tryAcquire().

Referenced by ReentrantLock::tryLock(), and ReentrantReadWriteLock::WriteLock::tryLock().

bool release int  arg  )  [inline]
 

Releases in exclusive mode. Implemented by unblocking one or more threads if tryRelease returns true. This method can be used to implement method Lock.unlock

Parameters:
arg the release argument. This value is conveyed to tryRelease but is otherwise uninterpreted and can represent anything you like.
Returns:
the value returned from tryRelease

Definition at line 959 of file LockImpl.d.

References head, tryRelease(), unparkSuccessor(), and AbstractLock::Node::waitStatus.

Referenced by fullyRelease(), ReentrantLock::unlock(), and ReentrantReadWriteLock::WriteLock::unlock().

void acquireShared int  arg  )  [inline]
 

Acquires in shared mode. Implemented by first invoking at least once tryAcquireShared, returning on success. Otherwise the thread is queued, possibly repeatedly blocking and unblocking, invoking tryAcquireShared until success.

Parameters:
arg the acquire argument. This value is conveyed to tryAcquireShared but is otherwise uninterpreted and can represent anything you like.

Definition at line 981 of file LockImpl.d.

References doAcquireShared(), and tryAcquireShared().

Referenced by Semaphore::acquire(), ReentrantReadWriteLock::ReadLock::lock(), and CountDownLatch::wait().

bool tryAcquireSharedNanos int  arg,
long  nanosTimeout
[inline]
 

Attempts to acquire in shared mode and failing if the given timeout elapses. Implemented by invoking at least once tryAcquireShared, returning on success. Otherwise, the thread is queued, possibly repeatedly blocking and unblocking, invoking tryAcquireShared until success or the timeout elapses.

Parameters:
arg the acquire argument. This value is conveyed to tryAcquireShared but is otherwise uninterpreted and can represent anything you like.
nanosTimeout the maximum number of nanoseconds to wait
Returns:
true if acquired; false if timed out

Definition at line 999 of file LockImpl.d.

References doAcquireSharedNanos(), and tryAcquireShared().

Referenced by Semaphore::tryAcquire(), ReentrantReadWriteLock::ReadLock::tryLock(), and CountDownLatch::wait().

bool releaseShared int  arg  )  [inline]
 

Releases in shared mode. Implemented by unblocking one or more threads if tryReleaseShared returns true.

Parameters:
arg the release argument. This value is conveyed to tryReleaseShared but is otherwise uninterpreted and can represent anything you like.
Returns:
the value returned from tryReleaseShared

Definition at line 1012 of file LockImpl.d.

References head, tryReleaseShared(), unparkSuccessor(), and AbstractLock::Node::waitStatus.

Referenced by CountDownLatch::countDown(), Semaphore::release(), and ReentrantReadWriteLock::ReadLock::unlock().

bool hasQueuedThreads  )  [inline]
 

Queries whether any threads are waiting to acquire. Note that because cancellations due to interrupts and timeouts may occur at any time, a true return does not guarantee that any other thread will ever acquire.

In this implementation, this operation returns in constant time.

Returns:
true if there may be other threads waiting to acquire the lock.

Definition at line 1038 of file LockImpl.d.

References head, and tail.

Referenced by Semaphore::hasQueuedThreads(), ReentrantLock::hasQueuedThreads(), ReentrantReadWriteLock::hasQueuedThreads(), and toString().

bool hasContended  )  [inline]
 

Queries whether any threads have ever contended to acquire this synchronizer; that is if an acquire method has ever blocked.

In this implementation, this operation returns in constant time.

Returns:
true if there has ever been contention

Definition at line 1051 of file LockImpl.d.

References v_head().

Thread getFirstQueuedThread  )  [inline]
 

Returns the first (longest-waiting) thread in the queue, or null if no threads are currently queued.

In this implementation, this operation normally returns in constant time, but may iterate upon contention if other threads are concurrently modifying the queue.

Returns:
the first (longest-waiting) thread in the queue, or null if no threads are currently queued.

Definition at line 1066 of file LockImpl.d.

References fullGetFirstQueuedThread(), head, and tail.

Referenced by ReentrantLock::FairSync::tryAcquire(), ReentrantReadWriteLock::FairSync::tryAcquire(), Semaphore::FairSync::tryAcquireShared(), and ReentrantReadWriteLock::FairSync::tryAcquireShared().

Thread fullGetFirstQueuedThread  )  [inline, private]
 

Version of getFirstQueuedThread called when fastpath fails

Definition at line 1074 of file LockImpl.d.

References head, AbstractLock::Node::next, AbstractLock::Node::prev, tail, and AbstractLock::Node::thread.

Referenced by getFirstQueuedThread().

bool isQueued Thread  thread  )  [inline, private]
 

Returns true if the given thread is currently queued.

This implementation traverses the queue to determine presence of the given thread.

Parameters:
thread the thread
Returns:
true if the given thread in on the queue

Definition at line 1129 of file LockImpl.d.

References AbstractLock::Node::prev, thread, and v_tail().

int getQueueLength  )  [inline, private]
 

Returns an estimate of the number of threads waiting to acquire. The value is only an estimate because the number of threads may change dynamically while this method traverses internal data structures. This method is designed for use in monitoring system state, not for synchronization control.

Returns:
the estimated number of threads waiting for this lock

Definition at line 1153 of file LockImpl.d.

References AbstractLock::Node::prev, and v_tail().

Thread [] getQueuedThreads  )  [inline, private]
 

Returns a collection containing threads that may be waiting to acquire. Because the actual set of threads may change dynamically while constructing this result, the returned collection is only a best-effort estimate. The elements of the returned collection are in no particular order. This method is designed to facilitate construction of subclasses that provide more extensive monitoring facilities.

Returns:
the collection of threads

Definition at line 1175 of file LockImpl.d.

References AbstractLock::Node::prev, AbstractLock::Node::thread, and v_tail().

Thread [] getExclusiveQueuedThreads  )  [inline, private]
 

Returns a collection containing threads that may be waiting to acquire in exclusive mode. This has the same properties as getQueuedThreads except that it only returns those threads waiting due to an exclusive acquire.

Returns:
the collection of threads

Definition at line 1195 of file LockImpl.d.

References AbstractLock::Node::prev, AbstractLock::Node::thread, and v_tail().

Thread [] getSharedQueuedThreads  )  [inline, private]
 

Returns a collection containing threads that may be waiting to acquire in shared mode. This has the same properties as getQueuedThreads except that it only returns those threads waiting due to a shared acquire.

Returns:
the collection of threads

Definition at line 1217 of file LockImpl.d.

References AbstractLock::Node::prev, AbstractLock::Node::thread, and v_tail().

char [] toString  )  [inline, private]
 

Returns a string identifying this synchronizer, as well as its state. The state, in brackets, includes the String "State =" followed by the current value of state(), and either "nonempty" or "empty" depending on whether the queue is empty.

Returns:
a string identifying this synchronizer, as well as its state.

Definition at line 1241 of file LockImpl.d.

References hasQueuedThreads(), and state().

bool isOnSyncQueue Node  node  )  [inline, private]
 

Returns true if a node, always one that was initially placed on a condition queue, is now waiting to reacquire on sync queue.

Parameters:
node the node
Returns:
true if is reacquiring

Definition at line 1258 of file LockImpl.d.

References AbstractLock::Node::CONDITION, findNodeFromTail(), AbstractLock::Node::next, AbstractLock::Node::prev, and AbstractLock::Node::waitStatus.

Referenced by transferAfterCancelledWait(), AbstractLock::ConditionObject::wait(), and AbstractLock::ConditionObject::waitNanos().

bool findNodeFromTail Node  node  )  [inline, private]
 

Returns true if node is on sync queue by searching backwards from tail. Called only when needed by isOnSyncQueue.

Returns:
true if present

Definition at line 1281 of file LockImpl.d.

References AbstractLock::Node::v_prev(), and v_tail().

Referenced by isOnSyncQueue().

bool transferForNotify Node  node  )  [inline, private]
 

Transfers a node from a condition queue onto sync queue. Returns true if successful.

Parameters:
node the node
Returns:
true if successfully transferred (else the node was cancelled before signal).

Definition at line 1306 of file LockImpl.d.

References AbstractLock::Node::CONDITION, enq(), AbstractLock::Node::SIGNAL, AbstractLock::Node::v_thread(), AbstractLock::Node::v_waitStatus(), and AbstractLock::Node::waitStatus.

Referenced by AbstractLock::ConditionObject::doNotify(), and AbstractLock::ConditionObject::doNotifyAll().

bool transferAfterCancelledWait Node  node  )  [inline, private]
 

Transfers node, if necessary, to sync queue after a cancelled wait. Returns true if thread was cancelled before being signalled.

Parameters:
current the waiting thread
node its node
Returns:
true if cancelled before the node was signalled.

Definition at line 1336 of file LockImpl.d.

References AbstractLock::Node::CONDITION, enq(), isOnSyncQueue(), and AbstractLock::Node::waitStatus.

Referenced by AbstractLock::ConditionObject::wait(), and AbstractLock::ConditionObject::waitNanos().

int fullyRelease Node  node  )  [inline, private]
 

Invoke release with current state value; return saved state. Cancel node and throw exception on failure.

Parameters:
node the condition node for this wait
Returns:
previous sync state

Definition at line 1358 of file LockImpl.d.

References AbstractLock::Node::CANCELLED, Exception, release(), state(), and AbstractLock::Node::v_waitStatus().

Referenced by AbstractLock::ConditionObject::wait(), and AbstractLock::ConditionObject::waitNanos().

bool owns ConditionObject  condition  )  [inline, private]
 

Queries whether the given ConditionObject uses this synchronizer as its lock.

Parameters:
condition the condition
Returns:
true if owned

Definition at line 1380 of file LockImpl.d.

References AbstractLock::ConditionObject::isOwnedBy().

Referenced by getWaitingThreads(), getWaitQueueLength(), and hasWaiters().

bool hasWaiters ConditionObject  condition  )  [inline, private]
 

Queries whether any threads are waiting on the given condition associated with this synchronizer. Note that because timeouts and interrupts may occur at any time, a true return does not guarantee that a future signal will awaken any threads. This method is designed primarily for use in monitoring of the system state.

Parameters:
condition the condition
Returns:
true if there are any waiting threads.

Definition at line 1396 of file LockImpl.d.

References AbstractLock::ConditionObject::hasWaiters(), and owns().

int getWaitQueueLength ConditionObject  condition  )  [inline, private]
 

Returns an estimate of the number of threads waiting on the given condition associated with this synchronizer. Note that because timeouts may occur at any time, the estimate serves only as an upper bound on the actual number of waiters. This method is designed for use in monitoring of the system state, not for synchronization control.

Parameters:
condition the condition
Returns:
the estimated number of waiting threads.

Definition at line 1412 of file LockImpl.d.

References AbstractLock::ConditionObject::getWaitQueueLength(), and owns().

Thread [] getWaitingThreads ConditionObject  condition  )  [inline, private]
 

Returns a collection containing those threads that may be waiting on the given condition associated with this synchronizer. Because the actual set of threads may change dynamically while constructing this result, the returned collection is only a best-effort estimate. The elements of the returned collection are in no particular order.

Parameters:
condition the condition
Returns:
the collection of threads

Definition at line 1428 of file LockImpl.d.

References AbstractLock::ConditionObject::getWaitingThreads(), and owns().


Member Data Documentation

Node head [private]
 

Head of the wait queue, lazily initialized. Except for initialization, it is modified only via method setHead. Note: If head exists, its waitStatus is guaranteed not to be CANCELLED.

Definition at line 447 of file LockImpl.d.

Referenced by enq(), fullGetFirstQueuedThread(), getFirstQueuedThread(), hasQueuedThreads(), release(), releaseShared(), setHead(), and v_head().

Node tail [private]
 

Tail of the wait queue, lazily initialized. Modified only via method enq to add new wait node.

Definition at line 455 of file LockImpl.d.

Referenced by addWaiter(), enq(), fullGetFirstQueuedThread(), getFirstQueuedThread(), hasQueuedThreads(), unparkSuccessor(), and v_tail().

int state_ [protected]
 

The synchronization state.

Definition at line 462 of file LockImpl.d.

Referenced by state().


The documentation for this class was generated from the following file:
Generated on Fri Nov 11 18:44:30 2005 for Mango by  doxygen 1.4.0