Show / Hide Table of Contents

Struct Lock

Unified representation of monitor lock, semaphore lock, read lock, write lock or upgradeable read lock.

Implements
IDisposable
IEquatable<Lock>
Inherited Members
Object.Equals(Object, Object)
Object.GetType()
Object.ReferenceEquals(Object, Object)
Namespace: DotNext.Threading
Assembly: DotNext.dll
Syntax
public struct Lock : IDisposable, IEquatable<Lock>
Remarks

The lock acquisition may block the caller thread.

Methods

| Improve this Doc View Source

Acquire()

Acquires lock.

Declaration
public Lock.Holder Acquire()
Returns
Type Description
Lock.Holder

The holder of the acquired lock.

| Improve this Doc View Source

Acquire(TimeSpan)

Acquires lock.

Declaration
public Lock.Holder Acquire(TimeSpan timeout)
Parameters
Type Name Description
TimeSpan timeout

The amount of time to wait for the lock.

Returns
Type Description
Lock.Holder

The holder of the acquired lock.

Exceptions
Type Condition
TimeoutException

The lock has not been acquired during the specified timeout.

| Improve this Doc View Source

Dispose()

Destroy this lock and dispose underlying lock object if it is owned by the given lock.

Declaration
public void Dispose()
Remarks

If the given lock is an owner of the underlying lock object then this method will call Dispose() on it; otherwise, the underlying lock object will not be destroyed. As a result, this lock is not usable after calling of this method.

| Improve this Doc View Source

Equals(Lock)

Determines whether this lock object is the same as other lock.

Declaration
public bool Equals(Lock other)
Parameters
Type Name Description
Lock other

Other lock to compare.

Returns
Type Description
Boolean

true if this lock is the same as the specified lock; otherwise, false.

| Improve this Doc View Source

Equals(Object)

Determines whether this lock object is the same as other lock.

Declaration
public override bool Equals(object other)
Parameters
Type Name Description
Object other

Other lock to compare.

Returns
Type Description
Boolean

true if this lock is the same as the specified lock; otherwise, false.

Overrides
ValueType.Equals(Object)
| Improve this Doc View Source

GetHashCode()

Computes hash code of this lock.

Declaration
public override int GetHashCode()
Returns
Type Description
Int32

The hash code of this lock.

Overrides
ValueType.GetHashCode()
| Improve this Doc View Source

Monitor()

Creates exclusive lock.

Declaration
public static Lock Monitor()
Returns
Type Description
Lock

The exclusive lock.

Remarks

Constructed lock owns the object instance used as a monitor.

| Improve this Doc View Source

Monitor(Object)

Creates monitor-based lock control object but doesn't acquire the lock.

Declaration
public static Lock Monitor(object obj)
Parameters
Type Name Description
Object obj

Monitor lock target.

Returns
Type Description
Lock

The lock representing monitor.

| Improve this Doc View Source

ReadLock(ReaderWriterLockSlim, Boolean)

Creates read lock but doesn't acquire it.

Declaration
public static Lock ReadLock(ReaderWriterLockSlim rwLock, bool upgradeable)
Parameters
Type Name Description
ReaderWriterLockSlim rwLock

Read/write lock source.

Boolean upgradeable

true to create upgradeable read lock wrapper.

Returns
Type Description
Lock

Reader lock.

| Improve this Doc View Source

Semaphore(Int32, Int32)

Creates semaphore-based lock but doesn't acquire the lock.

Declaration
public static Lock Semaphore(int initialCount, int maxCount)
Parameters
Type Name Description
Int32 initialCount

The initial number of requests for the semaphore that can be granted concurrently.

Int32 maxCount

The maximum number of requests for the semaphore that can be granted concurrently.

Returns
Type Description
Lock

The lock representing semaphore.

Remarks

Constructed lock owns the semaphore instance.

| Improve this Doc View Source

Semaphore(SemaphoreSlim)

Wraps semaphore instance into the unified representation of the lock.

Declaration
public static Lock Semaphore(SemaphoreSlim semaphore)
Parameters
Type Name Description
SemaphoreSlim semaphore

The semaphore to wrap into lock object.

Returns
Type Description
Lock

The lock representing semaphore.

| Improve this Doc View Source

ToString()

Returns actual type of this lock in the form of the string.

Declaration
public override string ToString()
Returns
Type Description
String

The actual type of this lock.

Overrides
ValueType.ToString()
| Improve this Doc View Source

TryAcquire(out Lock.Holder)

Attempts to acquire lock.

Declaration
public bool TryAcquire(out Lock.Holder holder)
Parameters
Type Name Description
Lock.Holder holder

The lock holder that can be used to release acquired lock.

Returns
Type Description
Boolean

true, if lock is acquired successfully; otherwise, false.

| Improve this Doc View Source

TryAcquire(TimeSpan, out Lock.Holder)

Attempts to acquire lock.

Declaration
public bool TryAcquire(TimeSpan timeout, out Lock.Holder holder)
Parameters
Type Name Description
TimeSpan timeout

The amount of time to wait for the lock.

Lock.Holder holder

The lock holder that can be used to release acquired lock.

Returns
Type Description
Boolean

true, if lock is acquired successfully; otherwise, false.

| Improve this Doc View Source

WriteLock(ReaderWriterLockSlim)

Creates write lock but doesn't acquire it.

Declaration
public static Lock WriteLock(ReaderWriterLockSlim rwLock)
Parameters
Type Name Description
ReaderWriterLockSlim rwLock

Read/write lock source.

Returns
Type Description
Lock

Write-only lock.

Operators

| Improve this Doc View Source

Equality(Lock, Lock)

Determines whether two locks are the same.

Declaration
public static bool operator ==(in Lock first, in Lock second)
Parameters
Type Name Description
Lock first

The first lock to compare.

Lock second

The second lock to compare.

Returns
Type Description
Boolean

true, if both are the same; otherwise, false.

| Improve this Doc View Source

Inequality(Lock, Lock)

Determines whether two locks are not the same.

Declaration
public static bool operator !=(in Lock first, in Lock second)
Parameters
Type Name Description
Lock first

The first lock to compare.

Lock second

The second lock to compare.

Returns
Type Description
Boolean

true, if both are not the same; otherwise, false.

Implements

System.IDisposable
System.IEquatable<T>

Extension Methods

ValueTypeExtensions.IsOneOf<T>(T, IEnumerable<T>)
ValueTypeExtensions.IsOneOf<T>(T, T[])
Sequence.Skip<TEnumerator, T>(ref TEnumerator, Int32)
ExpressionBuilder.Const<T>(T)
  • Improve this Doc
  • View Source
In This Article
Back to top Generated by DocFX