activemq-cpp-3.6.0
|
A reentrant mutual exclusion Lock with extended capabilities. More...
#include <src/main/decaf/util/concurrent/locks/ReentrantLock.h>
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 Condition * | newCondition () |
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::Thread * | getOwner () 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. |
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.
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.
fair | Boolean value indicating if the lock should be fair or not. |
|
virtual |
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(); } }
}
|
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.
|
protected |
Creates and returns a new Collection object that contains a best effort snapshot of the threads that are currently waiting to acquire.
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.
|
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.
NullPointerException | if the ConditionObject pointer is NULL. |
IllegalArgumentException | if the ConditionObject is not associated with this Synchronizer. |
IllegalMonitorStateException | if 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.
NullPointerException | if the ConditionObject pointer is NULL. |
IllegalArgumentException | if the ConditionObject is not associated with this Synchronizer. |
IllegalMonitorStateException | if the caller does not hold exclusive synchronization. |
bool decaf::util::concurrent::locks::ReentrantLock::hasQueuedThread | ( | decaf::lang::Thread * | thread | ) | const |
bool decaf::util::concurrent::locks::ReentrantLock::hasQueuedThreads | ( | ) | const |
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.
NullPointerException | if the ConditionObject pointer is NULL. |
IllegalArgumentException | if the ConditionObject is not associated with this Lock. |
IllegalMonitorStateException | if the caller does not hold exclusive synchronization. |
bool decaf::util::concurrent::locks::ReentrantLock::isFair | ( | ) | const |
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(); } } }
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.
|
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.
RuntimeException | if 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 |
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:
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.
RuntimeException | if an error occurs while acquiring the lock. |
InterruptedException | if 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 |
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).
RuntimeException | if an error occurs while creating the Condition. |
UnsupportedOperationException | if this Lock implementation does not support conditions |
Implements decaf::util::concurrent::locks::Lock.
Referenced by decaf::util::concurrent::LinkedBlockingQueue< Pointer< Transport > >::LinkedBlockingQueue().
|
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.
Implements decaf::util::concurrent::locks::Lock.
|
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.
RuntimeException | if an error occurs while acquiring the lock. |
Implements decaf::util::concurrent::locks::Lock.
|
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.
time | the maximum time to wait for the lock |
unit | the time unit of the time argument |
RuntimeException | if an error occurs while acquiring the lock. |
InterruptedException | if the current thread is interrupted while acquiring the lock (and interruption of lock acquisition is supported) |
Implements decaf::util::concurrent::locks::Lock.
|
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.
RuntimeException | if 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().