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

| Edit this page 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
| Edit this page 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
| Edit this page 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

| Edit this page View Source

DowngradeFromWriteLock()

Downgrades a writer lock back to the reader lock.

Declaration
public void DowngradeFromWriteLock()
| Edit this page View Source

EnterReadLock()

Enters the lock in read mode.

Declaration
public void EnterReadLock()
| Edit this page View Source

EnterWriteLock()

Enters the lock in write mode.

Declaration
public void EnterWriteLock()
| Edit this page View Source

ExitReadLock()

Exits read mode.

Declaration
public void ExitReadLock()
| Edit this page View Source

ExitWriteLock()

Exits the write lock.

Declaration
public void ExitWriteLock()
| Edit this page 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.

| Edit this page 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.

| Edit this page 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.

| Edit this page 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.

| Edit this page View Source

TryOptimisticRead()

Returns a stamp that can be validated later.

Declaration
public readonly ReaderWriterSpinLock.LockStamp TryOptimisticRead()
Returns
Type Description
ReaderWriterSpinLock.LockStamp

Optimistic read stamp. May be invalid.

| Edit this page 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.

| Edit this page 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.

Exceptions
Type Condition
OperationCanceledException

The operation has been canceled.

| Edit this page View Source

UpgradeToWriteLock()

Upgrades a reader lock to the writer lock.

Declaration
public void UpgradeToWriteLock()
Remarks

The caller must have acquired read lock. Otherwise, the behavior is unspecified.

| Edit this page View Source

Validate(in LockStamp)

Returns true if the lock has not been exclusively acquired since issuance of the given stamp.

Declaration
public readonly bool Validate(in ReaderWriterSpinLock.LockStamp stamp)
Parameters
Type Name Description
ReaderWriterSpinLock.LockStamp stamp

A stamp to check.

Returns
Type Description
bool

true if the lock has not been exclusively acquired since issuance of the given stamp; else false.

Extension Methods

BasicExtensions.IsBetween<T, TLowerBound, TUpperBound>(T, TLowerBound, TUpperBound)
BasicExtensions.IsOneOf<T>(T, ReadOnlySpan<T>)
Collection.ToAsyncEnumerator<TEnumerator, T>(TEnumerator, CancellationToken)
Collection.ToClassicEnumerator<TEnumerator, T>(TEnumerator)
Enumerator.Skip<TEnumerator, T>(ref TEnumerator, int)
ExpressionBuilder.Const<T>(T)
  • Edit this page
  • View Source
☀
☾
In this article
Back to top
Supported by the .NET Foundation
☀
☾