C# Class Microsoft.Common.Core.Threading.BinaryAsyncLock

BinaryAsyncLock is a helper primitive that can be used instead of SemaphoreSlim.WaitAsync + double-checked locking
After BinaryAsyncLock is created or reset, the first caller of WaitAsync will immediately get IBinaryAsyncLockToken that is not set. All other callers will either wait until IBinaryAsyncLockToken.Set is called and then will get , or until until IBinaryAsyncLockToken.Reset is called and next awaiting caller will get ,
ファイルを表示 Open project: Microsoft/RTVS Class Usage Examples

Public Methods

Method Description
BinaryAsyncLock ( bool isSet = false ) : System.Threading
EnqueueReset ( ) : void
ResetAsync ( CancellationToken cancellationToken = default(CancellationToken) ) : Task

Creates a task that is completed when lock is in Unset state and no reset waiters are in front of current one and all tokens that were issued prior this one are released

Method tries to replace current tail with the new Reset TokenSource (when its task is completed, lock will be in Unset state), and if it fails, it means that another thread has updated the tail, so method tries again with that new tail. If there is no tail, it is considered that lock is in the Unset state already, but no one has requested a token yet If there is a tail, method tries to set its TokenSource.Next to the new TokenSource, and if it fails, it means that another thread has updated the property or the tail, so method tries again with that new tail. If replacing tail succeeded, method stops adding Set tokens. If there are no unreleased set tokens, new tail task is set to completed.

WaitAsync ( CancellationToken cancellationToken = default(CancellationToken) ) : Task

Private Methods

Method Description
TokenReset ( TokenSource tokenSource, bool setIfLast ) : void
TokenSet ( TokenSource tokenSource ) : void

Method Details

BinaryAsyncLock() public method

public BinaryAsyncLock ( bool isSet = false ) : System.Threading
isSet bool
return System.Threading

EnqueueReset() public method

public EnqueueReset ( ) : void
return void

ResetAsync() public method

Creates a task that is completed when lock is in Unset state and no reset waiters are in front of current one and all tokens that were issued prior this one are released
Method tries to replace current tail with the new Reset TokenSource (when its task is completed, lock will be in Unset state), and if it fails, it means that another thread has updated the tail, so method tries again with that new tail. If there is no tail, it is considered that lock is in the Unset state already, but no one has requested a token yet If there is a tail, method tries to set its TokenSource.Next to the new TokenSource, and if it fails, it means that another thread has updated the property or the tail, so method tries again with that new tail. If replacing tail succeeded, method stops adding Set tokens. If there are no unreleased set tokens, new tail task is set to completed.
public ResetAsync ( CancellationToken cancellationToken = default(CancellationToken) ) : Task
cancellationToken System.Threading.CancellationToken
return Task

WaitAsync() public method

public WaitAsync ( CancellationToken cancellationToken = default(CancellationToken) ) : Task
cancellationToken System.Threading.CancellationToken
return Task