Struct ReaderWriterSpinLock
Represents lightweight reader-writer lock based on spin loop.
Inherited Members
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 SourceCurrentReadCount
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 |
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 |
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 SourceDowngradeFromWriteLock()
Downgrades a writer lock back to the reader lock.
Declaration
public void DowngradeFromWriteLock()
EnterReadLock()
Enters the lock in read mode.
Declaration
public void EnterReadLock()
EnterWriteLock()
Enters the lock in write mode.
Declaration
public void EnterWriteLock()
ExitReadLock()
Exits read mode.
Declaration
public void ExitReadLock()
ExitWriteLock()
Exits the writer lock.
Declaration
public void ExitWriteLock()
TryEnterReadLock()
Attempts to enter reader lock without blocking the caller thread.
Declaration
public bool TryEnterReadLock()
Returns
| Type | Description |
|---|---|
| bool |
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. |
TryEnterWriteLock()
Attempts to enter writer lock without blocking the caller thread.
Declaration
public bool TryEnterWriteLock()
Returns
| Type | Description |
|---|---|
| bool |
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. |
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. |
TryUpgradeToWriteLock()
Attempts to upgrade a reader lock to the writer lock.
Declaration
public bool TryUpgradeToWriteLock()
Returns
| Type | Description |
|---|---|
| bool |
Remarks
The method releases the reader lock even if it returns false, so the caller is responsible to reacquire the reader lock.
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 |
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. |
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.
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. |