Show / Hide Table of Contents

Struct ReaderWriterSpinLock

Represents lightweight reader-writer lock based on spin loop.

Inherited Members
ValueType.Equals(object)
ValueType.GetHashCode()
ValueType.ToString()
object.GetType()
object.Equals(object, object)
object.ReferenceEquals(object, object)
Namespace: DotNext.Threading
Assembly: DotNext.dll
Syntax
public struct ReaderWriterSpinLock
Remarks

This type should not be used to synchronize access to the I/O intensive resources.

Properties

View Source

CurrentReadCount

Gets the total number of unique threads that have entered the lock in read mode.

Declaration
public readonly int CurrentReadCount { get; }
Property Value
Type Description
int
View Source

IsReadLockHeld

Gets a value that indicates whether the current thread has entered the lock in read mode.

Declaration
public readonly bool IsReadLockHeld { get; }
Property Value
Type Description
bool
View Source

IsWriteLockHeld

Gets a value that indicates whether the current thread has entered the lock in write mode.

Declaration
public readonly bool IsWriteLockHeld { get; }
Property Value
Type Description
bool

Methods

View Source

DowngradeFromWriteLock()

Downgrades a writer lock back to the reader lock.

Declaration
public void DowngradeFromWriteLock()
View Source

EnterReadLock()

Enters the lock in read mode.

Declaration
public void EnterReadLock()
View Source

EnterWriteLock()

Enters the lock in write mode.

Declaration
public void EnterWriteLock()
View Source

ExitReadLock()

Exits read mode.

Declaration
public void ExitReadLock()
View Source

ExitWriteLock()

Exits the writer lock.

Declaration
public void ExitWriteLock()
View Source

TryEnterReadLock()

Attempts to enter reader lock without blocking the caller thread.

Declaration
public bool TryEnterReadLock()
Returns
Type Description
bool

true if reader lock is acquired; otherwise, false.

View Source

TryEnterReadLock(TimeSpan, CancellationToken)

Tries to enter the lock in read mode.

Declaration
public bool TryEnterReadLock(TimeSpan timeout, CancellationToken token = default)
Parameters
Type Name Description
TimeSpan timeout

The interval to wait.

CancellationToken token

The token that can be used to cancel the operation.

Returns
Type Description
bool

true if the calling thread entered read mode, otherwise, false.

Exceptions
Type Condition
OperationCanceledException

The operation has been canceled.

View Source

TryEnterReadLock(TimeSpan, TimeProvider, CancellationToken)

Tries to enter the lock in read mode.

Declaration
public bool TryEnterReadLock(TimeSpan timeout, TimeProvider provider, CancellationToken token = default)
Parameters
Type Name Description
TimeSpan timeout

The interval to wait.

TimeProvider provider

The time provider.

CancellationToken token

The token that can be used to cancel the operation.

Returns
Type Description
bool

true if the calling thread entered read mode, otherwise, false.

Exceptions
Type Condition
OperationCanceledException

The operation has been canceled.

View Source

TryEnterWriteLock()

Attempts to enter writer lock without blocking the caller thread.

Declaration
public bool TryEnterWriteLock()
Returns
Type Description
bool

true if writer lock is acquired; otherwise, false.

View Source

TryEnterWriteLock(TimeSpan, CancellationToken)

Tries to enter the lock in write mode.

Declaration
public bool TryEnterWriteLock(TimeSpan timeout, CancellationToken token = default)
Parameters
Type Name Description
TimeSpan timeout

The interval to wait.

CancellationToken token

The token that can be used to cancel the operation.

Returns
Type Description
bool

true if the calling thread entered read mode, otherwise, false.

Exceptions
Type Condition
OperationCanceledException

The operation has been canceled.

View Source

TryEnterWriteLock(TimeSpan, TimeProvider, CancellationToken)

Tries to enter the lock in write mode.

Declaration
public bool TryEnterWriteLock(TimeSpan timeout, TimeProvider provider, CancellationToken token = default)
Parameters
Type Name Description
TimeSpan timeout

The interval to wait.

TimeProvider provider

The time provider.

CancellationToken token

The token that can be used to cancel the operation.

Returns
Type Description
bool

true if the calling thread entered read mode, otherwise, false.

Exceptions
Type Condition
OperationCanceledException

The operation has been canceled.

View Source

TryUpgradeToWriteLock()

Attempts to upgrade a reader lock to the writer lock.

Declaration
public bool TryUpgradeToWriteLock()
Returns
Type Description
bool

true if the caller upgraded successfully; otherwise, false.

Remarks

The method releases the reader lock even if it returns false, so the caller is responsible to reacquire the reader lock.

View Source

TryUpgradeToWriteLock(TimeSpan, CancellationToken)

Attempts to upgrade a reader lock to the writer lock.

Declaration
public bool TryUpgradeToWriteLock(TimeSpan timeout, CancellationToken token = default)
Parameters
Type Name Description
TimeSpan timeout

The time to wait for the lock.

CancellationToken token

The token that can be used to cancel the operation.

Returns
Type Description
bool

true if the caller upgraded successfully; otherwise, false.

Remarks

The method releases the reader lock even if it returns false, so the caller is responsible to reacquire the reader lock.

Exceptions
Type Condition
OperationCanceledException

The operation has been canceled.

View Source

TryUpgradeToWriteLock(TimeSpan, TimeProvider, CancellationToken)

Attempts to upgrade a reader lock to the writer lock.

Declaration
public bool TryUpgradeToWriteLock(TimeSpan timeout, TimeProvider provider, CancellationToken token = default)
Parameters
Type Name Description
TimeSpan timeout

The time to wait for the lock.

TimeProvider provider

The time provider.

CancellationToken token

The token that can be used to cancel the operation.

Returns
Type Description
bool

true if the caller upgraded successfully; otherwise, false.

Remarks

The method releases the reader lock even if it returns false, so the caller is responsible to reacquire the reader lock.

Exceptions
Type Condition
OperationCanceledException

The operation has been canceled.

View Source

UpgradeToWriteLock()

Upgrades a reader lock to the writer lock.

Declaration
public void UpgradeToWriteLock()
Remarks

The caller must have acquired reader lock. Otherwise, the behavior is unspecified. The method releases the reader lock and tries to obtain the writer lock.

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
☀
☾