sdk/dotnet/src/Microsoft.Datasync.Client/Utils/Threading/AsyncLockDictionary.cs
/// <summary>
/// Acquire a lock (asynchronously) from the dictionary of locks.
/// </summary>
/// <param name="key">The key for the lock.</param>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> to observe.</param>
/// <returns>An <see cref="IDisposable"/> to release the lock when done.</returns>
Acquire a lock (asynchronously) from the dictionary of locks.
Acquire a lock ( asynchronously ) from the dictionary of locks .
public async Task<IDisposable> AcquireAsync(string key, CancellationToken cancellationToken)
        {
            LockEntry entry;
            lock (locks)
            {
                if (!locks.TryGetValue(key, out entry))
                {
                    locks[key] = entry = new LockEntry();
                }
                entry.Count++;
            }
            IDisposable releaser = await entry.Lock.AcquireAsync(cancellationToken).ConfigureAwait(false);
            return new DisposeAction(() =>
            {
                lock (locks)
                {
                    entry.Count--;
                    releaser.Dispose();
                    if (entry.Count == 0)
                    {
                        locks.Remove(key);
                        entry.Dispose();
                    }
                }
            });
        }
public async Task < IDisposable > AcquireAsync ( string key , CancellationToken cancellationToken ) { LockEntry entry ; lock ( locks ) { if ( ! locks . TryGetValue ( key , out entry ) ) { locks [ key ] = entry = new LockEntry ( ) ; } entry . Count ++ ; } IDisposable releaser = await entry . Lock . AcquireAsync ( cancellationToken ) . ConfigureAwait ( false ) ; return new DisposeAction ( ( ) => { lock ( locks ) { entry . Count -- ; releaser . Dispose ( ) ; if ( entry . Count == 0 ) { locks . Remove ( key ) ; entry . Dispose ( ) ; } } } ) ; }
Acquire a lock (asynchronously) from the dictionary of locks.
Acquire a lock ( asynchronously ) from the dictionary of locks .
An  to release the lock when done.
An to release the lock when done .
key
string
The key for the lock.
The key for the lock .
cancellationToken
CancellationToken
A  to observe.
A to observe .