| | | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | | 2 | | // Licensed under the MIT License. |
| | | 3 | | |
| | | 4 | | using System; |
| | | 5 | | using System.Collections.Generic; |
| | | 6 | | using System.Threading; |
| | | 7 | | using System.Threading.Tasks; |
| | | 8 | | using Azure.Core.Pipeline; |
| | | 9 | | |
| | | 10 | | namespace Azure.Identity |
| | | 11 | | { |
| | | 12 | | /// <summary> |
| | | 13 | | /// Primitive that combines async lock and value cache |
| | | 14 | | /// </summary> |
| | | 15 | | /// <typeparam name="T"></typeparam> |
| | | 16 | | internal sealed class AsyncLockWithValue<T> |
| | | 17 | | { |
| | 1204 | 18 | | private readonly object _syncObj = new object(); |
| | | 19 | | private Queue<TaskCompletionSource<Lock>> _waiters; |
| | | 20 | | private bool _isLocked; |
| | | 21 | | private bool _hasValue; |
| | | 22 | | private T _value; |
| | | 23 | | |
| | | 24 | | /// <summary> |
| | | 25 | | /// Method that either returns cached value or acquire a lock. |
| | | 26 | | /// If one caller has acquired a lock, other callers will be waiting for the lock to be released. |
| | | 27 | | /// If value is set, lock is released and all waiters get that value. |
| | | 28 | | /// If value isn't set, the next waiter in the queue will get the lock. |
| | | 29 | | /// </summary> |
| | | 30 | | /// <param name="async"></param> |
| | | 31 | | /// <param name="cancellationToken"></param> |
| | | 32 | | /// <returns></returns> |
| | | 33 | | public async ValueTask<Lock> GetLockOrValueAsync(bool async, CancellationToken cancellationToken = default) |
| | | 34 | | { |
| | | 35 | | TaskCompletionSource<Lock> valueTcs; |
| | 1948 | 36 | | lock (_syncObj) |
| | | 37 | | { |
| | | 38 | | // If there is a value, just return it |
| | 1948 | 39 | | if (_hasValue) |
| | | 40 | | { |
| | 440 | 41 | | return new Lock(_value); |
| | | 42 | | } |
| | | 43 | | |
| | | 44 | | // If lock isn't acquire yet, acquire it and return to the caller |
| | 1508 | 45 | | if (!_isLocked) |
| | | 46 | | { |
| | 830 | 47 | | _isLocked = true; |
| | 830 | 48 | | return new Lock(this); |
| | | 49 | | } |
| | | 50 | | |
| | | 51 | | // Check cancellationToken before instantiating waiter |
| | 678 | 52 | | cancellationToken.ThrowIfCancellationRequested(); |
| | | 53 | | |
| | | 54 | | // If lock is already taken, create a waiter and wait either until value is set or lock can be acquired |
| | 490 | 55 | | _waiters ??= new Queue<TaskCompletionSource<Lock>>(); |
| | | 56 | | // if async == false, valueTcs will be waited only in this thread and only synchronously, so RunContinua |
| | 490 | 57 | | valueTcs = new TaskCompletionSource<Lock>(async ? TaskCreationOptions.RunContinuationsAsynchronously : T |
| | 490 | 58 | | _waiters.Enqueue(valueTcs); |
| | 490 | 59 | | } |
| | | 60 | | |
| | | 61 | | try |
| | | 62 | | { |
| | 490 | 63 | | if (async) |
| | | 64 | | { |
| | 342 | 65 | | return await valueTcs.Task.AwaitWithCancellation(cancellationToken); |
| | | 66 | | } |
| | | 67 | | |
| | | 68 | | #pragma warning disable AZC0104 // Use EnsureCompleted() directly on asynchronous method return value. |
| | | 69 | | #pragma warning disable AZC0111 // DO NOT use EnsureCompleted in possibly asynchronous scope. |
| | 148 | 70 | | valueTcs.Task.Wait(cancellationToken); |
| | 140 | 71 | | return valueTcs.Task.EnsureCompleted(); |
| | | 72 | | #pragma warning restore AZC0111 // DO NOT use EnsureCompleted in possibly asynchronous scope. |
| | | 73 | | #pragma warning restore AZC0104 // Use EnsureCompleted() directly on asynchronous method return value. |
| | | 74 | | } |
| | 18 | 75 | | catch (OperationCanceledException) |
| | | 76 | | { |
| | | 77 | | // Throw OperationCanceledException only if another thread hasn't set a value to this waiter |
| | | 78 | | // by calling either Reset or SetValue |
| | 18 | 79 | | if (valueTcs.TrySetCanceled()) |
| | | 80 | | { |
| | 18 | 81 | | throw; |
| | | 82 | | } |
| | | 83 | | |
| | 0 | 84 | | return valueTcs.Task.Result; |
| | | 85 | | } |
| | 1742 | 86 | | } |
| | | 87 | | |
| | | 88 | | /// <summary> |
| | | 89 | | /// Set value to the cache and to all the waiters |
| | | 90 | | /// </summary> |
| | | 91 | | /// <param name="value"></param> |
| | | 92 | | private void SetValue(T value) |
| | | 93 | | { |
| | | 94 | | Queue<TaskCompletionSource<Lock>> waiters; |
| | 68 | 95 | | lock (_syncObj) |
| | | 96 | | { |
| | 68 | 97 | | _value = value; |
| | 68 | 98 | | _hasValue = true; |
| | 68 | 99 | | _isLocked = false; |
| | 68 | 100 | | if (_waiters == default) |
| | | 101 | | { |
| | 56 | 102 | | return; |
| | | 103 | | } |
| | | 104 | | |
| | 12 | 105 | | waiters = _waiters; |
| | 12 | 106 | | _waiters = default; |
| | 12 | 107 | | } |
| | | 108 | | |
| | 82 | 109 | | while (waiters.Count > 0) |
| | | 110 | | { |
| | 70 | 111 | | waiters.Dequeue().TrySetResult(new Lock(value)); |
| | | 112 | | } |
| | 68 | 113 | | } |
| | | 114 | | |
| | | 115 | | /// <summary> |
| | | 116 | | /// Release the lock and allow next waiter acquire it |
| | | 117 | | /// </summary> |
| | | 118 | | private void Reset() |
| | | 119 | | { |
| | 1224 | 120 | | TaskCompletionSource<Lock> nextWaiter = UnlockOrGetNextWaiter(); |
| | 1224 | 121 | | while (nextWaiter != default && !nextWaiter.TrySetResult(new Lock(this))) |
| | | 122 | | { |
| | 0 | 123 | | nextWaiter = UnlockOrGetNextWaiter(); |
| | | 124 | | } |
| | 1224 | 125 | | } |
| | | 126 | | |
| | | 127 | | private TaskCompletionSource<Lock> UnlockOrGetNextWaiter() |
| | | 128 | | { |
| | 1224 | 129 | | lock (_syncObj) |
| | | 130 | | { |
| | 1224 | 131 | | if (!_isLocked) |
| | | 132 | | { |
| | 60 | 133 | | return default; |
| | | 134 | | } |
| | | 135 | | |
| | 1164 | 136 | | if (_waiters == default) |
| | | 137 | | { |
| | 756 | 138 | | _isLocked = false; |
| | 756 | 139 | | return default; |
| | | 140 | | } |
| | | 141 | | |
| | 426 | 142 | | while (_waiters.Count > 0) |
| | | 143 | | { |
| | 420 | 144 | | var nextWaiter = _waiters.Dequeue(); |
| | 420 | 145 | | if (!nextWaiter.Task.IsCompleted) |
| | | 146 | | { |
| | | 147 | | // Return the waiter only if it wasn't canceled already |
| | 402 | 148 | | return nextWaiter; |
| | | 149 | | } |
| | | 150 | | } |
| | | 151 | | |
| | 6 | 152 | | _isLocked = false; |
| | 6 | 153 | | return default; |
| | | 154 | | } |
| | 1224 | 155 | | } |
| | | 156 | | |
| | | 157 | | public readonly struct Lock : IDisposable |
| | | 158 | | { |
| | | 159 | | private readonly AsyncLockWithValue<T> _owner; |
| | 1710 | 160 | | public bool HasValue => _owner == default; |
| | 510 | 161 | | public T Value { get; } |
| | | 162 | | |
| | | 163 | | public Lock(T value) |
| | | 164 | | { |
| | 510 | 165 | | _owner = default; |
| | 510 | 166 | | Value = value; |
| | 510 | 167 | | } |
| | | 168 | | |
| | | 169 | | public Lock(AsyncLockWithValue<T> owner) |
| | | 170 | | { |
| | 1232 | 171 | | _owner = owner; |
| | 1232 | 172 | | Value = default; |
| | 1232 | 173 | | } |
| | | 174 | | |
| | 68 | 175 | | public void SetValue(T value) => _owner.SetValue(value); |
| | | 176 | | |
| | 1730 | 177 | | public void Dispose() => _owner?.Reset(); |
| | | 178 | | } |
| | | 179 | | } |
| | | 180 | | } |