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.GetType()
object.Equals(object, object)
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

View Source

Acquire()

Acquires lock.

Declaration
public readonly Lock.Scope Acquire()
Returns
Type Description
Lock.Scope

The holder of the acquired lock.

View Source

Acquire(TimeSpan)

Acquires lock.

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

The amount of time to wait for the lock.

Returns
Type Description
Lock.Scope

The holder of the acquired lock.

Exceptions
Type Condition
TimeoutException

The lock has not been acquired during the specified timeout.

View Source

AcquireReadLock(object)

Acquires read lock for the specified object.

Declaration
public static Lock.Scope AcquireReadLock(object obj)
Parameters
Type Name Description
object obj

The object to be locked.

Returns
Type Description
Lock.Scope

The acquired read lock.

View Source

AcquireReadLock(object, TimeSpan)

Acquires read lock for the specified object.

Declaration
public static Lock.Scope AcquireReadLock(object obj, TimeSpan timeout)
Parameters
Type Name Description
object obj

The object to be locked.

TimeSpan timeout

The amount of time to wait for the lock.

Returns
Type Description
Lock.Scope

The acquired read lock.

Exceptions
Type Condition
TimeoutException

The lock cannot be acquired during the specified amount of time.

View Source

AcquireUpgradeableReadLock(object)

Acquires upgradeable read lock for the specified object.

Declaration
public static Lock.Scope AcquireUpgradeableReadLock(object obj)
Parameters
Type Name Description
object obj

The object to be locked.

Returns
Type Description
Lock.Scope

The acquired upgradeable read lock.

View Source

AcquireUpgradeableReadLock(object, TimeSpan)

Acquires upgradeable read lock for the specified object.

Declaration
public static Lock.Scope AcquireUpgradeableReadLock(object obj, TimeSpan timeout)
Parameters
Type Name Description
object obj

The object to be locked.

TimeSpan timeout

The amount of time to wait for the lock.

Returns
Type Description
Lock.Scope

The acquired upgradeable read lock.

Exceptions
Type Condition
TimeoutException

The lock cannot be acquired during the specified amount of time.

View Source

AcquireWriteLock(object)

Acquires write lock for the specified object.

Declaration
public static Lock.Scope AcquireWriteLock(object obj)
Parameters
Type Name Description
object obj

The object to be locked.

Returns
Type Description
Lock.Scope

The acquired write lock.

View Source

AcquireWriteLock(object, TimeSpan)

Acquires write lock for the specified object.

Declaration
public static Lock.Scope AcquireWriteLock(object obj, TimeSpan timeout)
Parameters
Type Name Description
object obj

The object to be locked.

TimeSpan timeout

The amount of time to wait for the lock.

Returns
Type Description
Lock.Scope

The acquired write lock.

Exceptions
Type Condition
TimeoutException

The lock cannot be acquired during the specified amount of time.

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.

View Source

Equals(Lock)

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

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

Other lock to compare.

Returns
Type Description
bool

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

View Source

Equals(object?)

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

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

Other lock to compare.

Returns
Type Description
bool

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

Overrides
ValueType.Equals(object)
View Source

ExclusiveLock()

Creates exclusive lock.

Declaration
public static Lock ExclusiveLock()
Returns
Type Description
Lock

The exclusive lock.

Remarks

Constructed lock owns the Lock instance.

View Source

ExclusiveLock(Lock)

Creates exclusive lock.

Declaration
public static Lock ExclusiveLock(Lock locker)
Parameters
Type Name Description
Lock locker

The locker object.

Returns
Type Description
Lock

The exclusive lock.

View Source

GetHashCode()

Computes hash code of this lock.

Declaration
public override readonly int GetHashCode()
Returns
Type Description
int

The hash code of this lock.

Overrides
ValueType.GetHashCode()
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.

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 the monitor.

View Source

ReadLock(ReaderWriterLockSlim, bool)

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.

bool upgradeable

true to create upgradeable read lock wrapper.

Returns
Type Description
Lock

Reader lock.

View Source

Semaphore(int, int)

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

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

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

int 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.

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.

View Source

ToString()

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

Declaration
public override readonly string ToString()
Returns
Type Description
string

The actual type of this lock.

Overrides
ValueType.ToString()
View Source

TryAcquire(out Scope)

Attempts to acquire lock.

Declaration
public readonly bool TryAcquire(out Lock.Scope scope)
Parameters
Type Name Description
Lock.Scope scope

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

Returns
Type Description
bool

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

View Source

TryAcquire(TimeSpan, out Scope)

Attempts to acquire lock.

Declaration
public readonly bool TryAcquire(TimeSpan timeout, out Lock.Scope scope)
Parameters
Type Name Description
TimeSpan timeout

The amount of time to wait for the lock.

Lock.Scope scope

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

Returns
Type Description
bool

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

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

View Source

operator ==(in Lock, in 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
bool

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

View Source

operator !=(in Lock, in 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
bool

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

Implements

IDisposable
IEquatable<T>

Extension Methods

BasicExtensions.IsBetween<T, TLowerBound, TUpperBound>(T, TLowerBound, TUpperBound)
BasicExtensions.IsOneOf<T>(T, params ReadOnlySpan<T>)
Enumerator.Skip<TEnumerator, T>(ref TEnumerator, int)
  • View Source
☀
☾
In this article
Back to top
Supported by the .NET Foundation
☀
☾