C# Class BitSharper.Threading.Locks.ReentrantLock

Inheritance: IDisposable, ConditionVariable.IExclusiveLock
Datei anzeigen Open project: TangibleCryptography/BitSharper

Public Methods

Method Description
GetWaitQueueLength ( ICondition condition ) : int

Returns an estimate of the number of threads waiting on the condition associated with this lock. Note that because timeouts and interrupts 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.

HasWaiters ( ICondition condition ) : bool

Queries whether any threads are waiting on the condition associated with this lock. 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.

IsQueuedThread ( Thread thread ) : bool

Queries whether the thread is waiting to acquire this lock. Note that because cancellations may occur at any time, a true return does not guarantee that this thread will ever acquire this lock. This method is designed primarily for use in monitoring of the system state.

Lock ( ) : IDisposable

Acquires the lock and returns an IDisposable that can be used to unlock when disposed.

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.

Below is a typical use of Lock ReentrantLock reentrantLock = ...; using(reentrantLock.Lock()) { // locked } // unlocked. it is equvilant to ReentrantLock reentrantLock = ...; reentrantLock.Lock(); try { // locked } finally { reentrantLock.Unlock(); } // unlocked
LockInterruptibly ( ) : IDisposable

Acquires the lock unless System.Threading.Thread.Interrupt() is called on the current thread

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 Some other thread calls System.Threading.Thread.Interrupt() on 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 System.Threading.Thread.Interrupt() is called while acquiring the lock then System.Threading.ThreadInterruptedException 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.

Acquires the lock unless Thread.Interrupt() is called on the current thread. Returns an IDisposable that can be used to unlock if the lock is sucessfully obtained.

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. Some other thread calls Thread.Interrupt() on 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 Thread.Interrupt() is called while acquiring the lock

then ThreadInterruptedException 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.

Below is a typical use of LockInterruptibly ReentrantLock reentrantLock = ...; using(reentrantLock.LockInterruptibly()) { // locked } // unlocked. it is equvilant to ReentrantLock reentrantLock = ...; reentrantLock.LockInterruptibly(); try { // locked } finally { reentrantLock.Unlock(); } // unlocked
NewCondition ( ) : ICondition

Returns a BitSharper.Threading.Locks.ICondition instance for use with this BitSharper.Threading.Locks.ILock instance.

The returned BitSharper.Threading.Locks.ICondition instance supports the same usages as do the System.Threading.Monitor methods System.Threading.Monitor.Wait(object), System.Threading.Monitor.Pulse(object), and System.Threading.Monitor.PulseAll(object)) when used with the built-in monitor lock. If this lock is not held when either BitSharper.Threading.Locks.ICondition.Await() or BitSharper.Threading.Locks.ICondition.Signal() methods are called, then an System.InvalidOperationException is thrown. When the condition BitSharper.Threading.Locks.IConditionawait() 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 System.Threading.Thread.Interrupt() is called while waiting then the wait will terminate, an System.Threading.ThreadInterruptedException and the thread's interrupted status will be cleared. Waiting threads are signalled 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.

ReentrantLock ( ) : System

Creates an instance of BitSharper.Threading.Locks.ReentrantLock. This is equivalent to using ReentrantLock(false).

ReentrantLock ( bool fair ) : System

Creates an instance of BitSharper.Threading.Locks.ReentrantLock with the given fairness policy.

ToString ( ) : string

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 System.Threading.Thread.Name of the owning thread.

TryLock ( ) : bool

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 BitSharper.Threading.Locks.ReentrantLock.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 BitSharper.Threading.Locks.ReentrantLock.TryLock(TimeSpan) 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.

TryLock ( System.TimeSpan timeSpan ) : bool

Acquires the lock if it is not held by another thread within the given waiting time and System.Threading.Thread.Interrupt() hasn't been called on the current thread

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 BitSharper.Threading.Locks.ReentrantLock.TryLock() method. If you want a timed BitSharper.Threading.Locks.ReentrantLock.TryLock() that does permit barging on a fair lock then combine the timed and un-timed forms together: if (lock.TryLock() || lock.TryLock(timeSpan) ) { ... }

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 Some other thread calls System.Threading.Thread.Interrupt() the current thread 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 has System.Threading.Thread.Interrupt() called on it while acquiring the lock then System.Threading.ThreadInterruptedException 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.

Unlock ( ) : void

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 System.InvalidOperationException is thrown.

Protected Methods

Method Description
GetWaitingThreads ( ICondition condition ) : ICollection

Returns a collection containing those threads that may be waiting on the condition associated with this lock. 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 condition monitoring facilities.

Private Methods

Method Description
AsConditionVariable ( ICondition condition ) : ConditionVariable
IDisposable ( ) : void

Method Details

GetWaitQueueLength() public method

Returns an estimate of the number of threads waiting on the condition associated with this lock. Note that because timeouts and interrupts 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.
if the is null if the is not associated with this lock
public GetWaitQueueLength ( ICondition condition ) : int
condition ICondition the condition
return int

GetWaitingThreads() protected method

Returns a collection containing those threads that may be waiting on the condition associated with this lock. 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 condition monitoring facilities.
if the is null if the is not associated with this lock
protected GetWaitingThreads ( ICondition condition ) : ICollection
condition ICondition the condition
return ICollection

HasWaiters() public method

Queries whether any threads are waiting on the condition associated with this lock. 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.
if the is null if the is not associated with this lock
public HasWaiters ( ICondition condition ) : bool
condition ICondition the condition
return bool

IsQueuedThread() public method

Queries whether the thread is waiting to acquire this lock. Note that because cancellations may occur at any time, a true return does not guarantee that this thread will ever acquire this lock. This method is designed primarily for use in monitoring of the system state.
if is null.
public IsQueuedThread ( Thread thread ) : bool
thread Thread the instance. ///
return bool

Lock() public method

Acquires the lock and returns an IDisposable that can be used to unlock when disposed.

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.

Below is a typical use of Lock ReentrantLock reentrantLock = ...; using(reentrantLock.Lock()) { // locked } // unlocked. it is equvilant to ReentrantLock reentrantLock = ...; reentrantLock.Lock(); try { // locked } finally { reentrantLock.Unlock(); } // unlocked
public Lock ( ) : IDisposable
return IDisposable

LockInterruptibly() public method

Acquires the lock unless System.Threading.Thread.Interrupt() is called on the current thread

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 Some other thread calls System.Threading.Thread.Interrupt() on 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 System.Threading.Thread.Interrupt() is called while acquiring the lock then System.Threading.ThreadInterruptedException 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. Acquires the lock unless Thread.Interrupt() is called on the current thread. Returns an IDisposable that can be used to unlock if the lock is sucessfully obtained.

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. Some other thread calls Thread.Interrupt() on 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 Thread.Interrupt() is called while acquiring the lock

then ThreadInterruptedException 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.

Below is a typical use of LockInterruptibly ReentrantLock reentrantLock = ...; using(reentrantLock.LockInterruptibly()) { // locked } // unlocked. it is equvilant to ReentrantLock reentrantLock = ...; reentrantLock.LockInterruptibly(); try { // locked } finally { reentrantLock.Unlock(); } // unlocked
if the current thread is interrupted /// If the current thread is interrupted. ///
public LockInterruptibly ( ) : IDisposable
return IDisposable

NewCondition() public method

Returns a BitSharper.Threading.Locks.ICondition instance for use with this BitSharper.Threading.Locks.ILock instance.

The returned BitSharper.Threading.Locks.ICondition instance supports the same usages as do the System.Threading.Monitor methods System.Threading.Monitor.Wait(object), System.Threading.Monitor.Pulse(object), and System.Threading.Monitor.PulseAll(object)) when used with the built-in monitor lock. If this lock is not held when either BitSharper.Threading.Locks.ICondition.Await() or BitSharper.Threading.Locks.ICondition.Signal() methods are called, then an System.InvalidOperationException is thrown. When the condition BitSharper.Threading.Locks.IConditionawait() 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 System.Threading.Thread.Interrupt() is called while waiting then the wait will terminate, an System.Threading.ThreadInterruptedException and the thread's interrupted status will be cleared. Waiting threads are signalled 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.

public NewCondition ( ) : ICondition
return ICondition

ReentrantLock() public method

Creates an instance of BitSharper.Threading.Locks.ReentrantLock. This is equivalent to using ReentrantLock(false).
public ReentrantLock ( ) : System
return System

ReentrantLock() public method

Creates an instance of BitSharper.Threading.Locks.ReentrantLock with the given fairness policy.
public ReentrantLock ( bool fair ) : System
fair bool true if this lock will be fair, else false ///
return System

ToString() public method

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 System.Threading.Thread.Name of the owning thread.
public ToString ( ) : string
return string

TryLock() public method

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 BitSharper.Threading.Locks.ReentrantLock.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 BitSharper.Threading.Locks.ReentrantLock.TryLock(TimeSpan) 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.

public TryLock ( ) : bool
return bool

TryLock() public method

Acquires the lock if it is not held by another thread within the given waiting time and System.Threading.Thread.Interrupt() hasn't been called on the current thread

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 BitSharper.Threading.Locks.ReentrantLock.TryLock() method. If you want a timed BitSharper.Threading.Locks.ReentrantLock.TryLock() that does permit barging on a fair lock then combine the timed and un-timed forms together: if (lock.TryLock() || lock.TryLock(timeSpan) ) { ... }

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 Some other thread calls System.Threading.Thread.Interrupt() the current thread 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 has System.Threading.Thread.Interrupt() called on it while acquiring the lock then System.Threading.ThreadInterruptedException 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.

If is null If the current thread is interrupted
public TryLock ( System.TimeSpan timeSpan ) : bool
timeSpan System.TimeSpan the to wait for the lock
return bool

Unlock() public method

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 System.InvalidOperationException is thrown.

if the current thread does not hold this lock.
public Unlock ( ) : void
return void