activemq-cpp-3.6.0
decaf::util::concurrent::locks::ReentrantLock Class Reference

A reentrant mutual exclusion Lock with extended capabilities. More...

#include <src/main/decaf/util/concurrent/locks/ReentrantLock.h>

Inheritance diagram for decaf::util::concurrent::locks::ReentrantLock:

Public Member Functions

 ReentrantLock ()
 Create a new ReentrantLock instance with unfair locking semantics.
 ReentrantLock (bool fair)
 Create a new ReentrantLock instance with the specified locking semantics.
virtual ~ReentrantLock ()
virtual void lock ()
 Acquires the lock.
virtual void lockInterruptibly ()
 Acquires the lock unless the current thread is interrupted.
virtual bool tryLock ()
 Acquires the lock only if it is not held by another thread at the time of invocation.
virtual bool tryLock (long long time, const TimeUnit &unit)
 Acquires the lock if it is not held by another thread within the given waiting time and the current thread has not been interrupted.
virtual void unlock ()
 Attempts to release this lock.
virtual ConditionnewCondition ()
 Returns a Condition instance for use with this Lock instance.
int getHoldCount () const
 Queries the number of holds on this lock by the current thread.
bool isHeldByCurrentThread () const
 Queries if this lock is held by the current thread.
bool isLocked () const
 Queries if this lock is held by any thread.
bool isFair () const
 Returns true if this lock has fairness set true.
std::string toString () const
 Returns a string identifying this lock, as well as its lock state.
int getQueueLength () const
 Gets an estimated count of the number of threads that are currently waiting to acquire, this value changes dynamically so the result of this method can be invalid immediately after it is called.
int getWaitQueueLength (Condition *condition) const
 Gets an estimated count of the number of threads that are currently waiting on the given Condition object, this value changes dynamically so the result of this method can be invalid immediately after it is called.
bool hasWaiters (Condition *condition) const
 Returns true if there are any threads that are currently waiting on the given Condition object, the condition must be associated with this Lock instance.
bool hasQueuedThreads () const
bool hasQueuedThread (decaf::lang::Thread *thread) const
- Public Member Functions inherited from decaf::util::concurrent::locks::Lock
virtual ~Lock ()

Protected Member Functions

decaf::util::Collection
< decaf::lang::Thread * > * 
getWaitingThreads (Condition *condition) const
 Creates and returns a new Collection object that contains all the threads that may be waiting on the given Condition object instance at the time this method is called.
decaf::lang::ThreadgetOwner () const
 Returns the thread that currently owns this lock, or NULL if not owned.
decaf::util::Collection
< decaf::lang::Thread * > * 
getQueuedThreads () const
 Creates and returns a new Collection object that contains a best effort snapshot of the threads that are currently waiting to acquire.

Detailed Description

A reentrant mutual exclusion Lock with extended capabilities.

A ReentrantLock is owned by the thread last successfully locking, but not yet unlocking it. A thread invoking lock will return, successfully acquiring the lock, when the lock is not owned by another thread. The method will return immediately if the current thread already owns the lock. This can be checked using methods isHeldByCurrentThread(), and getHoldCount().

The constructor for this class accepts an optional fairness parameter. When set true, under contention, locks favor granting access to the longest-waiting thread. Otherwise this lock does not guarantee any particular access order. Programs using fair locks accessed by many threads may display lower overall throughput (i.e., are slower; often much slower) than those using the default setting, but have smaller variances in times to obtain locks and guarantee lack of starvation. Note however, that fairness of locks does not guarantee fairness of thread scheduling. Thus, one of many threads using a fair lock may obtain it multiple times in succession while other active threads are not progressing and not currently holding the lock. Also note that the untimed tryLock method does not honor the fairness setting. It will succeed if the lock is available even if other threads are waiting.

It is recommended practice to always immediately follow a call to lock with a try block, most typically in a before/after construction such as:

class X { private:

ReentrantLock lock;
// ...

public:

void m() {
    lock.lock();  // block until condition holds

    try {
        // ... method body
    } finally {
        lock.unlock()
    }
}

}

In addition to implementing the Lock interface, this class defines methods isLocked and getLockQueueLength, as well as some associated protected access methods that may be useful for instrumentation and monitoring.

Since
1.0

Constructor & Destructor Documentation

decaf::util::concurrent::locks::ReentrantLock::ReentrantLock ( )

Create a new ReentrantLock instance with unfair locking semantics.

decaf::util::concurrent::locks::ReentrantLock::ReentrantLock ( bool  fair)

Create a new ReentrantLock instance with the specified locking semantics.

Parameters
fairBoolean value indicating if the lock should be fair or not.
virtual decaf::util::concurrent::locks::ReentrantLock::~ReentrantLock ( )
virtual

Member Function Documentation

int decaf::util::concurrent::locks::ReentrantLock::getHoldCount ( ) const

Queries the number of holds on this lock by the current thread.

A thread has a hold on a lock for each lock action that is not matched by an unlock action.

The hold count information is typically only used for testing and debugging purposes. For example, if a certain section of code should not be entered with the lock already held then we can assert that fact:

class X { private:

ReentrantLock lock;
// ...

public:

void m() {
    assert( lock.getHoldCount() == 0 );
    lock.lock();
    try {
        // ... method body
    } catch(...) {
       lock.unlock();
   }
}

}

Returns
the number of holds on this lock by the current thread, or zero if this lock is not held by the current thread
decaf::lang::Thread* decaf::util::concurrent::locks::ReentrantLock::getOwner ( ) const
protected

Returns the thread that currently owns this lock, or NULL if not owned.

When this method is called by a thread that is not the owner, the return value reflects a best-effort approximation of current lock status. For example, the owner may be momentarily NULL even if there are threads trying to acquire the lock but have not yet done so. This method is designed to facilitate construction of subclasses that provide more extensive lock monitoring facilities.

Returns
pointer to the Thread that owns this lock, or NULL if not owned.
decaf::util::Collection<decaf::lang::Thread*>* decaf::util::concurrent::locks::ReentrantLock::getQueuedThreads ( ) const
protected

Creates and returns a new Collection object that contains a best effort snapshot of the threads that are currently waiting to acquire.

Returns
a Collection pointer that contains waiting threads for lock acquisition. The caller owns the returned pointer.
int decaf::util::concurrent::locks::ReentrantLock::getQueueLength ( ) const

Gets an estimated count of the number of threads that are currently waiting to acquire, this value changes dynamically so the result of this method can be invalid immediately after it is called.

Returns
an estimate of the number of waiting threads.
decaf::util::Collection<decaf::lang::Thread*>* decaf::util::concurrent::locks::ReentrantLock::getWaitingThreads ( Condition condition) const
protected

Creates and returns a new Collection object that contains all the threads that may be waiting on the given Condition object instance at the time this method is called.

Returns
a Collection pointer that contains waiting threads on given Condition object. The caller owns the returned pointer.
Exceptions
NullPointerExceptionif the ConditionObject pointer is NULL.
IllegalArgumentExceptionif the ConditionObject is not associated with this Synchronizer.
IllegalMonitorStateExceptionif the caller does not hold exclusive synchronization.
int decaf::util::concurrent::locks::ReentrantLock::getWaitQueueLength ( Condition condition) const

Gets an estimated count of the number of threads that are currently waiting on the given Condition object, this value changes dynamically so the result of this method can be invalid immediately after it is called.

The Condition object must be associated with this Lock or an exception will be thrown.

Returns
an estimate of the number of waiting threads.
Exceptions
NullPointerExceptionif the ConditionObject pointer is NULL.
IllegalArgumentExceptionif the ConditionObject is not associated with this Synchronizer.
IllegalMonitorStateExceptionif the caller does not hold exclusive synchronization.
bool decaf::util::concurrent::locks::ReentrantLock::hasQueuedThread ( decaf::lang::Thread thread) const
Returns
true if the given Thread is waiting to acquire this Lock object. Because of cancellations this method can return true but the given Thread is not in the Queue afterwards.
Exceptions
NullPointerExceptionif the given thread is NULL.
bool decaf::util::concurrent::locks::ReentrantLock::hasQueuedThreads ( ) const
Returns
true if there are threads that are currently waiting to acquire this Lock.
bool decaf::util::concurrent::locks::ReentrantLock::hasWaiters ( Condition condition) const

Returns true if there are any threads that are currently waiting on the given Condition object, the condition must be associated with this Lock instance.

Returns
true if the condition object has waiting threads.
Exceptions
NullPointerExceptionif the ConditionObject pointer is NULL.
IllegalArgumentExceptionif the ConditionObject is not associated with this Lock.
IllegalMonitorStateExceptionif the caller does not hold exclusive synchronization.
bool decaf::util::concurrent::locks::ReentrantLock::isFair ( ) const

Returns true if this lock has fairness set true.

Returns
true if this lock has fairness set true
bool decaf::util::concurrent::locks::ReentrantLock::isHeldByCurrentThread ( ) const

Queries if this lock is held by the current thread.

This method is typically used for debugging and testing. For example, a method that should only be called while a lock is held can assert that this is the case:

class X { private: ReentrantLock lock; // ...

public: void m() { assert( lock.isHeldByCurrentThread() ); // ... method body } }

It can also be used to ensure that a reentrant lock is used in a non-reentrant manner, for example:

class X { private: ReentrantLock lock; // ...

public: void m() { assert !lock.isHeldByCurrentThread(); lock.lock(); try { // ... method body } finally { lock.unlock(); } } }

Returns
true if current thread holds this lock and false otherwise
bool decaf::util::concurrent::locks::ReentrantLock::isLocked ( ) const

Queries if this lock is held by any thread.

This method is designed for use in monitoring of the system state, not for synchronization control.

Returns
true if any thread holds this lock and false otherwise
virtual void decaf::util::concurrent::locks::ReentrantLock::lock ( )
virtual

Acquires the lock.

Acquires the lock if it is not held by another thread and returns immediately, setting the lock hold count to one.

If the current thread already holds the lock then the hold count is incremented by one and the method returns immediately.

If the lock is held by another thread then the current thread becomes disabled for thread scheduling purposes and lies dormant until the lock has been acquired, at which time the lock hold count is set to one.

Exceptions
RuntimeExceptionif an error occurs while acquiring the lock.

Implements decaf::util::concurrent::locks::Lock.

Referenced by decaf::util::concurrent::LinkedBlockingQueue< Pointer< Transport > >::drainTo(), decaf::util::concurrent::LinkedBlockingQueue< Pointer< Transport > >::peek(), and decaf::util::concurrent::LinkedBlockingQueue< Pointer< Transport > >::poll().

virtual void decaf::util::concurrent::locks::ReentrantLock::lockInterruptibly ( )
virtual

Acquires the lock unless the current thread is interrupted.

Acquires the lock if it is not held by another thread and returns immediately, setting the lock hold count to one.

If the current thread already holds this lock then the hold count is incremented by one and the method returns immediately.

If the lock is held by another thread then the current thread becomes disabled for thread scheduling purposes and lies dormant until one of two things happens:

  • The lock is acquired by the current thread; or
  • Some other thread interrupts the current thread.

If the lock is acquired by the current thread then the lock hold count is set to one.

If the current thread:

* has its interrupted status set on entry to this method; or
* is interrupted while acquiring the lock,

then InterruptedException is thrown and the current thread's interrupted status is cleared.

In this implementation, as this method is an explicit interruption point, preference is given to responding to the interrupt over normal or reentrant acquisition of the lock.

Exceptions
RuntimeExceptionif an error occurs while acquiring the lock.
InterruptedExceptionif the current thread is interrupted while acquiring the lock (and interruption of lock acquisition is supported).

Implements decaf::util::concurrent::locks::Lock.

Referenced by decaf::util::concurrent::LinkedBlockingQueue< Pointer< Transport > >::offer(), decaf::util::concurrent::LinkedBlockingQueue< Pointer< Transport > >::poll(), decaf::util::concurrent::LinkedBlockingQueue< Pointer< Transport > >::put(), and decaf::util::concurrent::LinkedBlockingQueue< Pointer< Transport > >::take().

virtual Condition* decaf::util::concurrent::locks::ReentrantLock::newCondition ( )
virtual

Returns a Condition instance for use with this Lock instance.

The returned Condition instance supports the same usages as do the Mutex Class' methods (wait, notify, and notifyAll).

  • If this lock is not held when any of the Condition waiting or signalling methods are called, then an IllegalMonitorStateException is thrown.
  • When the condition waiting methods are called the lock is released and, before they return, the lock is reacquired and the lock hold count restored to what it was when the method was called.
  • If a thread is interrupted while waiting then the wait will terminate, an InterruptedException will be thrown, and the thread's interrupted status will be cleared.
  • Waiting threads are signaled in FIFO order.
  • The ordering of lock reacquisition for threads returning from waiting methods is the same as for threads initially acquiring the lock, which is in the default case not specified, but for fair locks favors those threads that have been waiting the longest.
Exceptions
RuntimeExceptionif an error occurs while creating the Condition.
UnsupportedOperationExceptionif this Lock implementation does not support conditions

Implements decaf::util::concurrent::locks::Lock.

Referenced by decaf::util::concurrent::LinkedBlockingQueue< Pointer< Transport > >::LinkedBlockingQueue().

std::string decaf::util::concurrent::locks::ReentrantLock::toString ( ) const
virtual

Returns a string identifying this lock, as well as its lock state.

The state, in brackets, includes either the String "Unlocked" or the String "Locked by" followed by the name of the owning thread.

Returns
a string identifying this lock, as well as its lock state

Implements decaf::util::concurrent::locks::Lock.

virtual bool decaf::util::concurrent::locks::ReentrantLock::tryLock ( )
virtual

Acquires the lock only if it is not held by another thread at the time of invocation.

Acquires the lock if it is not held by another thread and returns immediately with the value true, setting the lock hold count to one. Even when this lock has been set to use a fair ordering policy, a call to tryLock() will immediately acquire the lock if it is available, whether or not other threads are currently waiting for the lock. This "barging" behavior can be useful in certain circumstances, even though it breaks fairness. If you want to honor the fairness setting for this lock, then use tryLock(0, TimeUnit.SECONDS) which is almost equivalent (it also detects interruption).

If the current thread already holds this lock then the hold count is incremented by one and the method returns true.

If the lock is held by another thread then this method will return immediately with the value false.

Returns
true if the lock was acquired and false otherwise
Exceptions
RuntimeExceptionif an error occurs while acquiring the lock.

Implements decaf::util::concurrent::locks::Lock.

virtual bool decaf::util::concurrent::locks::ReentrantLock::tryLock ( long long  time,
const TimeUnit unit 
)
virtual

Acquires the lock if it is not held by another thread within the given waiting time and the current thread has not been interrupted.

Acquires the lock if it is not held by another thread and returns immediately with the value true, setting the lock hold count to one. If this lock has been set to use a fair ordering policy then an available lock will not be acquired if any other threads are waiting for the lock. This is in contrast to the tryLock() method. If you want a timed tryLock that does permit barging on a fair lock then combine the timed and un-timed forms together:

if (lock.tryLock() || lock.tryLock(timeout, unit) ) { ... }

If the current thread already holds this lock then the hold count is incremented by one and the method returns true.

If the lock is held by another thread then the current thread becomes disabled for thread scheduling purposes and lies dormant until one of three things happens:

* The lock is acquired by the current thread; or
* Some other thread interrupts the current thread; or
* The specified waiting time elapses

If the lock is acquired then the value true is returned and the lock hold count is set to one.

If the current thread:

* has its interrupted status set on entry to this method; or
* is interrupted while acquiring the lock,

then InterruptedException is thrown and the current thread's interrupted status is cleared.

If the specified waiting time elapses then the value false is returned. If the time is less than or equal to zero, the method will not wait at all.

In this implementation, as this method is an explicit interruption point, preference is given to responding to the interrupt over normal or reentrant acquisition of the lock, and over reporting the elapse of the waiting time.

Parameters
timethe maximum time to wait for the lock
unitthe time unit of the time argument
Returns
true if the lock was acquired and false if the waiting time elapsed before the lock was acquired
Exceptions
RuntimeExceptionif an error occurs while acquiring the lock.
InterruptedExceptionif the current thread is interrupted while acquiring the lock (and interruption of lock acquisition is supported)

Implements decaf::util::concurrent::locks::Lock.

virtual void decaf::util::concurrent::locks::ReentrantLock::unlock ( )
virtual

Attempts to release this lock.

If the current thread is the holder of this lock then the hold count is decremented. If the hold count is now zero then the lock is released. If the current thread is not the holder of this lock then IllegalMonitorStateException is thrown.

Exceptions
RuntimeExceptionif an error occurs while acquiring the lock.

Implements decaf::util::concurrent::locks::Lock.

Referenced by decaf::util::concurrent::LinkedBlockingQueue< Pointer< Transport > >::drainTo(), decaf::util::concurrent::LinkedBlockingQueue< Pointer< Transport > >::offer(), decaf::util::concurrent::LinkedBlockingQueue< Pointer< Transport > >::peek(), decaf::util::concurrent::LinkedBlockingQueue< Pointer< Transport > >::poll(), decaf::util::concurrent::LinkedBlockingQueue< Pointer< Transport > >::put(), and decaf::util::concurrent::LinkedBlockingQueue< Pointer< Transport > >::take().


The documentation for this class was generated from the following file: