Class BlockingLock
An IDisposable which can be used as a replacement for the lock keyword which honors the calling thread's ThreadTimeout
Implements
Inherited Members
Namespace: Tangosol.Util
Assembly: Coherence.dll
Syntax
public class BlockingLock : IDisposable
Remarks
Sample usage:
try
{
using (BlockingLock l = BlockingLock.Lock(o))
{
// critical section
}
}
catch(ThreadInterruptedException)
{
// handle timeout or thread interrupt
}
Constructors
BlockingLock(object)
Construct a BlockingLock.
Declaration
protected BlockingLock(object o)
Parameters
Type | Name | Description |
---|---|---|
object | o | The object to be locked. |
Fields
f_lockObject
The object being locked.
Declaration
protected readonly object f_lockObject
Field Value
Type | Description |
---|---|
object |
m_lockObtained
Whether the object was successfully locked.
Declaration
protected bool m_lockObtained
Field Value
Type | Description |
---|---|
bool |
Properties
IsLockObtained
Whether the object was successfully locked.
Declaration
public bool IsLockObtained { get; }
Property Value
Type | Description |
---|---|
bool |
Methods
Dispose()
Release the lock if it was obtained.
Declaration
public void Dispose()
Enter()
Lock the object while still respecting the calling thread's ThreadTimeout.
Declaration
protected void Enter()
Remarks
This method will only return when the object is locked.
Exceptions
Type | Condition |
---|---|
ThreadInterruptedException | If the thread is interrupted or the timeout is reached. |
Lock(object)
Construct a new BlockingLock for the specified lockObject and wait for the lockObject to be locked while still respecting the calling thread's ThreadTimeout.
Declaration
public static BlockingLock Lock(object lockObject)
Parameters
Type | Name | Description |
---|---|---|
object | lockObject | The object to be locked. |
Returns
Type | Description |
---|---|
BlockingLock | The BlockingLock |
Remarks
This method will only return when the object is locked.
Exceptions
Type | Condition |
---|---|
ThreadInterruptedException | If the thread is interrupted or the timeout is reached. |
TryEnter(int)
Attempt to lock the object, while still respecting the calling thread's ThreadTimeout..
Declaration
protected void TryEnter(int millis)
Parameters
Type | Name | Description |
---|---|---|
int | millis | The maximum number of milliseconds to wait for the lock. |
Exceptions
Type | Condition |
---|---|
ThreadInterruptedException | If the thread is interrupted. |
TryEnter(TimeSpan)
Attempt to lock the object, while still respecting the calling thread's ThreadTimeout..
Declaration
protected void TryEnter(TimeSpan span)
Parameters
Type | Name | Description |
---|---|---|
TimeSpan | span | The maximum TimeSpan to wait for the lock. |
Exceptions
Type | Condition |
---|---|
ThreadInterruptedException | If the thread is interrupted. |
TryLock(object, int)
Construct a new BlockingLock for the specified lockObject and wait for the lockObject to be locked while still respecting the calling thread's ThreadTimeout.
Declaration
public static BlockingLock TryLock(object lockObject, int millis)
Parameters
Type | Name | Description |
---|---|---|
object | lockObject | The object to be locked. |
int | millis | The maximum number of milliseconds to wait for the lock. |
Returns
Type | Description |
---|---|
BlockingLock | The BlockingLock |
Remarks
The BlockingLock.IsObjectLocked property can be checked on return to see if the object has been locked.
Exceptions
Type | Condition |
---|---|
ThreadInterruptedException | If the thread is interrupted. |
TryLock(object, TimeSpan)
Construct a new BlockingLock for the specified lockObject and wait for the lockObject to be locked while still respecting the calling thread's ThreadTimeout.
Declaration
public static BlockingLock TryLock(object lockObject, TimeSpan span)
Parameters
Type | Name | Description |
---|---|---|
object | lockObject | The object to be locked. |
TimeSpan | span | The maximum TimeSpan to wait for the lock.. |
Returns
Type | Description |
---|---|
BlockingLock | The BlockingLock |
Remarks
The BlockingLock.IsObjectLocked property can be checked on return to see if the object has been locked.
Exceptions
Type | Condition |
---|---|
ThreadInterruptedException | If the thread is interrupted. |