| | | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | | 2 | | // Licensed under the MIT License. |
| | | 3 | | |
| | | 4 | | #nullable disable |
| | | 5 | | |
| | | 6 | | using System; |
| | | 7 | | using System.Collections; |
| | | 8 | | using System.Collections.Generic; |
| | | 9 | | using System.Diagnostics; |
| | | 10 | | using System.Runtime.CompilerServices; |
| | | 11 | | using System.Threading; |
| | | 12 | | using System.Threading.Tasks; |
| | | 13 | | |
| | | 14 | | namespace Azure.Core.Pipeline |
| | | 15 | | { |
| | | 16 | | internal static class TaskExtensions |
| | | 17 | | { |
| | | 18 | | public static WithCancellationTaskAwaitable<T> AwaitWithCancellation<T>(this Task<T> task, CancellationToken can |
| | 414 | 19 | | => new WithCancellationTaskAwaitable<T>(task, cancellationToken); |
| | | 20 | | |
| | | 21 | | public static WithCancellationValueTaskAwaitable<T> AwaitWithCancellation<T>(this ValueTask<T> task, Cancellatio |
| | 10 | 22 | | => new WithCancellationValueTaskAwaitable<T>(task, cancellationToken); |
| | | 23 | | |
| | | 24 | | public static T EnsureCompleted<T>(this Task<T> task) |
| | | 25 | | { |
| | | 26 | | #if DEBUG |
| | | 27 | | VerifyTaskCompleted(task.IsCompleted); |
| | | 28 | | #else |
| | 706 | 29 | | if (HasSynchronizationContext()) |
| | | 30 | | { |
| | 4 | 31 | | throw new InvalidOperationException("Synchronously waiting on non-completed task isn't allowed."); |
| | | 32 | | } |
| | | 33 | | #endif |
| | | 34 | | #pragma warning disable AZC0102 // Do not use GetAwaiter().GetResult(). Use the TaskExtensions.EnsureCompleted() extensi |
| | 702 | 35 | | return task.GetAwaiter().GetResult(); |
| | | 36 | | #pragma warning restore AZC0102 // Do not use GetAwaiter().GetResult(). Use the TaskExtensions.EnsureCompleted() extensi |
| | | 37 | | } |
| | | 38 | | |
| | | 39 | | public static void EnsureCompleted(this Task task) |
| | | 40 | | { |
| | | 41 | | #if DEBUG |
| | | 42 | | VerifyTaskCompleted(task.IsCompleted); |
| | | 43 | | #else |
| | 26 | 44 | | if (HasSynchronizationContext()) |
| | | 45 | | { |
| | 4 | 46 | | throw new InvalidOperationException("Synchronously waiting on non-completed task isn't allowed."); |
| | | 47 | | } |
| | | 48 | | #endif |
| | | 49 | | #pragma warning disable AZC0102 // Do not use GetAwaiter().GetResult(). Use the TaskExtensions.EnsureCompleted() extensi |
| | 22 | 50 | | task.GetAwaiter().GetResult(); |
| | | 51 | | #pragma warning restore AZC0102 // Do not use GetAwaiter().GetResult(). Use the TaskExtensions.EnsureCompleted() extensi |
| | 18 | 52 | | } |
| | | 53 | | |
| | | 54 | | public static T EnsureCompleted<T>(this ValueTask<T> task) |
| | | 55 | | { |
| | 6 | 56 | | if (!task.IsCompleted) |
| | | 57 | | { |
| | | 58 | | #pragma warning disable AZC0107 // public asynchronous method shouldn't be called in synchronous scope. Use synchronous |
| | 2 | 59 | | return EnsureCompleted(task.AsTask()); |
| | | 60 | | #pragma warning restore AZC0107 // public asynchronous method shouldn't be called in synchronous scope. Use synchronous |
| | | 61 | | } |
| | | 62 | | #pragma warning disable AZC0102 // Do not use GetAwaiter().GetResult(). Use the TaskExtensions.EnsureCompleted() extensi |
| | 4 | 63 | | return task.GetAwaiter().GetResult(); |
| | | 64 | | #pragma warning restore AZC0102 // Do not use GetAwaiter().GetResult(). Use the TaskExtensions.EnsureCompleted() extensi |
| | | 65 | | } |
| | | 66 | | |
| | | 67 | | public static void EnsureCompleted(this ValueTask task) |
| | | 68 | | { |
| | 4502 | 69 | | if (!task.IsCompleted) |
| | | 70 | | { |
| | | 71 | | #pragma warning disable AZC0107 // public asynchronous method shouldn't be called in synchronous scope. Use synchronous |
| | 2 | 72 | | EnsureCompleted(task.AsTask()); |
| | | 73 | | #pragma warning restore AZC0107 // public asynchronous method shouldn't be called in synchronous scope. Use synchronous |
| | | 74 | | } |
| | | 75 | | else |
| | | 76 | | { |
| | | 77 | | #pragma warning disable AZC0102 // Do not use GetAwaiter().GetResult(). Use the TaskExtensions.EnsureCompleted() extensi |
| | 4500 | 78 | | task.GetAwaiter().GetResult(); |
| | | 79 | | #pragma warning restore AZC0102 // Do not use GetAwaiter().GetResult(). Use the TaskExtensions.EnsureCompleted() extensi |
| | | 80 | | } |
| | 4236 | 81 | | } |
| | | 82 | | |
| | 0 | 83 | | public static Enumerable<T> EnsureSyncEnumerable<T>(this IAsyncEnumerable<T> asyncEnumerable) => new Enumerable< |
| | | 84 | | |
| | | 85 | | public static ConfiguredValueTaskAwaitable<T> EnsureCompleted<T>(this ConfiguredValueTaskAwaitable<T> awaitable, |
| | | 86 | | { |
| | 36 | 87 | | if (!async) |
| | | 88 | | { |
| | | 89 | | #if DEBUG |
| | | 90 | | VerifyTaskCompleted(awaitable.GetAwaiter().IsCompleted); |
| | | 91 | | #endif |
| | | 92 | | } |
| | 36 | 93 | | return awaitable; |
| | | 94 | | } |
| | | 95 | | |
| | | 96 | | public static ConfiguredValueTaskAwaitable EnsureCompleted(this ConfiguredValueTaskAwaitable awaitable, bool asy |
| | | 97 | | { |
| | | 98 | | |
| | 4220 | 99 | | if (!async) |
| | | 100 | | { |
| | | 101 | | #if DEBUG |
| | | 102 | | VerifyTaskCompleted(awaitable.GetAwaiter().IsCompleted); |
| | | 103 | | #endif |
| | | 104 | | } |
| | 4220 | 105 | | return awaitable; |
| | | 106 | | } |
| | | 107 | | |
| | | 108 | | [Conditional("DEBUG")] |
| | | 109 | | private static void VerifyTaskCompleted(bool isCompleted) |
| | | 110 | | { |
| | 0 | 111 | | if (!isCompleted) |
| | | 112 | | { |
| | 0 | 113 | | if (Debugger.IsAttached) |
| | | 114 | | { |
| | 0 | 115 | | Debugger.Break(); |
| | | 116 | | } |
| | | 117 | | // Throw an InvalidOperationException instead of using |
| | | 118 | | // Debug.Assert because that brings down nUnit immediately |
| | 0 | 119 | | throw new InvalidOperationException("Task is not completed"); |
| | | 120 | | } |
| | 0 | 121 | | } |
| | | 122 | | |
| | | 123 | | private static bool HasSynchronizationContext() |
| | 732 | 124 | | => SynchronizationContext.Current != null && SynchronizationContext.Current.GetType() != typeof(Synchronizat |
| | | 125 | | |
| | | 126 | | /// <summary> |
| | | 127 | | /// Both <see cref="Enumerable{T}"/> and <see cref="Enumerator{T}"/> are defined as public structs so that forea |
| | | 128 | | /// to call <see cref="Enumerable{T}.GetEnumerator"/> and avoid heap memory allocation. |
| | | 129 | | /// Please don't delete this method and don't make these types private. |
| | | 130 | | /// </summary> |
| | | 131 | | /// <typeparam name="T"></typeparam> |
| | | 132 | | public readonly struct Enumerable<T> : IEnumerable<T> |
| | | 133 | | { |
| | | 134 | | private readonly IAsyncEnumerable<T> _asyncEnumerable; |
| | | 135 | | |
| | 0 | 136 | | public Enumerable(IAsyncEnumerable<T> asyncEnumerable) => _asyncEnumerable = asyncEnumerable; |
| | | 137 | | |
| | 0 | 138 | | public Enumerator<T> GetEnumerator() => new Enumerator<T>(_asyncEnumerable.GetAsyncEnumerator()); |
| | | 139 | | |
| | 0 | 140 | | IEnumerator<T> IEnumerable<T>.GetEnumerator() => new Enumerator<T>(_asyncEnumerable.GetAsyncEnumerator()); |
| | | 141 | | |
| | 0 | 142 | | IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); |
| | | 143 | | } |
| | | 144 | | |
| | | 145 | | public readonly struct Enumerator<T> : IEnumerator<T> |
| | | 146 | | { |
| | | 147 | | private readonly IAsyncEnumerator<T> _asyncEnumerator; |
| | | 148 | | |
| | 0 | 149 | | public Enumerator(IAsyncEnumerator<T> asyncEnumerator) => _asyncEnumerator = asyncEnumerator; |
| | | 150 | | |
| | | 151 | | #pragma warning disable AZC0107 // Do not call public asynchronous method in synchronous scope. |
| | 0 | 152 | | public bool MoveNext() => _asyncEnumerator.MoveNextAsync().EnsureCompleted(); |
| | | 153 | | #pragma warning restore AZC0107 // Do not call public asynchronous method in synchronous scope. |
| | | 154 | | |
| | 0 | 155 | | public void Reset() => throw new NotSupportedException($"{GetType()} is a synchronous wrapper for {_asyncEnu |
| | | 156 | | |
| | 0 | 157 | | public T Current => _asyncEnumerator.Current; |
| | | 158 | | |
| | 0 | 159 | | object IEnumerator.Current => Current; |
| | | 160 | | |
| | | 161 | | #pragma warning disable AZC0107 // Do not call public asynchronous method in synchronous scope. |
| | 0 | 162 | | public void Dispose() => _asyncEnumerator.DisposeAsync().EnsureCompleted(); |
| | | 163 | | #pragma warning restore AZC0107 // Do not call public asynchronous method in synchronous scope. |
| | | 164 | | } |
| | | 165 | | |
| | | 166 | | public readonly struct WithCancellationTaskAwaitable<T> |
| | | 167 | | { |
| | | 168 | | private readonly CancellationToken _cancellationToken; |
| | | 169 | | private readonly ConfiguredTaskAwaitable<T> _awaitable; |
| | | 170 | | |
| | | 171 | | public WithCancellationTaskAwaitable(Task<T> task, CancellationToken cancellationToken) |
| | | 172 | | { |
| | 414 | 173 | | _awaitable = task.ConfigureAwait(false); |
| | 414 | 174 | | _cancellationToken = cancellationToken; |
| | 414 | 175 | | } |
| | | 176 | | |
| | 414 | 177 | | public WithCancellationTaskAwaiter<T> GetAwaiter() => new WithCancellationTaskAwaiter<T>(_awaitable.GetAwait |
| | | 178 | | } |
| | | 179 | | |
| | | 180 | | public readonly struct WithCancellationValueTaskAwaitable<T> |
| | | 181 | | { |
| | | 182 | | private readonly CancellationToken _cancellationToken; |
| | | 183 | | private readonly ConfiguredValueTaskAwaitable<T> _awaitable; |
| | | 184 | | |
| | | 185 | | public WithCancellationValueTaskAwaitable(ValueTask<T> task, CancellationToken cancellationToken) |
| | | 186 | | { |
| | 10 | 187 | | _awaitable = task.ConfigureAwait(false); |
| | 10 | 188 | | _cancellationToken = cancellationToken; |
| | 10 | 189 | | } |
| | | 190 | | |
| | 10 | 191 | | public WithCancellationValueTaskAwaiter<T> GetAwaiter() => new WithCancellationValueTaskAwaiter<T>(_awaitabl |
| | | 192 | | } |
| | | 193 | | |
| | | 194 | | public readonly struct WithCancellationTaskAwaiter<T> : ICriticalNotifyCompletion |
| | | 195 | | { |
| | | 196 | | private readonly CancellationToken _cancellationToken; |
| | | 197 | | private readonly ConfiguredTaskAwaitable<T>.ConfiguredTaskAwaiter _taskAwaiter; |
| | | 198 | | |
| | | 199 | | public WithCancellationTaskAwaiter(ConfiguredTaskAwaitable<T>.ConfiguredTaskAwaiter awaiter, CancellationTok |
| | | 200 | | { |
| | 414 | 201 | | _taskAwaiter = awaiter; |
| | 414 | 202 | | _cancellationToken = cancellationToken; |
| | 414 | 203 | | } |
| | | 204 | | |
| | 426 | 205 | | public bool IsCompleted => _taskAwaiter.IsCompleted || _cancellationToken.IsCancellationRequested; |
| | | 206 | | |
| | 0 | 207 | | public void OnCompleted(Action continuation) => _taskAwaiter.OnCompleted(WrapContinuation(continuation)); |
| | | 208 | | |
| | 408 | 209 | | public void UnsafeOnCompleted(Action continuation) => _taskAwaiter.UnsafeOnCompleted(WrapContinuation(contin |
| | | 210 | | |
| | | 211 | | public T GetResult() |
| | | 212 | | { |
| | | 213 | | Debug.Assert(IsCompleted); |
| | 414 | 214 | | if (!_taskAwaiter.IsCompleted) |
| | | 215 | | { |
| | 6 | 216 | | _cancellationToken.ThrowIfCancellationRequested(); |
| | | 217 | | } |
| | 408 | 218 | | return _taskAwaiter.GetResult(); |
| | | 219 | | } |
| | | 220 | | |
| | | 221 | | private Action WrapContinuation(in Action originalContinuation) |
| | 408 | 222 | | => _cancellationToken.CanBeCanceled |
| | 408 | 223 | | ? new WithCancellationContinuationWrapper(originalContinuation, _cancellationToken).Continuation |
| | 408 | 224 | | : originalContinuation; |
| | | 225 | | } |
| | | 226 | | |
| | | 227 | | public readonly struct WithCancellationValueTaskAwaiter<T> : ICriticalNotifyCompletion |
| | | 228 | | { |
| | | 229 | | private readonly CancellationToken _cancellationToken; |
| | | 230 | | private readonly ConfiguredValueTaskAwaitable<T>.ConfiguredValueTaskAwaiter _taskAwaiter; |
| | | 231 | | |
| | | 232 | | public WithCancellationValueTaskAwaiter(ConfiguredValueTaskAwaitable<T>.ConfiguredValueTaskAwaiter awaiter, |
| | | 233 | | { |
| | 10 | 234 | | _taskAwaiter = awaiter; |
| | 10 | 235 | | _cancellationToken = cancellationToken; |
| | 10 | 236 | | } |
| | | 237 | | |
| | 22 | 238 | | public bool IsCompleted => _taskAwaiter.IsCompleted || _cancellationToken.IsCancellationRequested; |
| | | 239 | | |
| | 0 | 240 | | public void OnCompleted(Action continuation) => _taskAwaiter.OnCompleted(WrapContinuation(continuation)); |
| | | 241 | | |
| | 6 | 242 | | public void UnsafeOnCompleted(Action continuation) => _taskAwaiter.UnsafeOnCompleted(WrapContinuation(contin |
| | | 243 | | |
| | | 244 | | public T GetResult() |
| | | 245 | | { |
| | | 246 | | Debug.Assert(IsCompleted); |
| | 10 | 247 | | if (!_taskAwaiter.IsCompleted) |
| | | 248 | | { |
| | 4 | 249 | | _cancellationToken.ThrowIfCancellationRequested(); |
| | | 250 | | } |
| | 6 | 251 | | return _taskAwaiter.GetResult(); |
| | | 252 | | } |
| | | 253 | | |
| | | 254 | | private Action WrapContinuation(in Action originalContinuation) |
| | 6 | 255 | | => _cancellationToken.CanBeCanceled |
| | 6 | 256 | | ? new WithCancellationContinuationWrapper(originalContinuation, _cancellationToken).Continuation |
| | 6 | 257 | | : originalContinuation; |
| | | 258 | | } |
| | | 259 | | |
| | | 260 | | private class WithCancellationContinuationWrapper |
| | | 261 | | { |
| | | 262 | | private Action _originalContinuation; |
| | | 263 | | private readonly CancellationTokenRegistration _registration; |
| | | 264 | | |
| | 12 | 265 | | public WithCancellationContinuationWrapper(Action originalContinuation, CancellationToken cancellationToken) |
| | | 266 | | { |
| | 12 | 267 | | Action continuation = ContinuationImplementation; |
| | 12 | 268 | | _originalContinuation = originalContinuation; |
| | 12 | 269 | | _registration = cancellationToken.Register(continuation); |
| | 12 | 270 | | Continuation = continuation; |
| | 12 | 271 | | } |
| | | 272 | | |
| | 12 | 273 | | public Action Continuation { get; } |
| | | 274 | | |
| | | 275 | | private void ContinuationImplementation() |
| | | 276 | | { |
| | 16 | 277 | | Action originalContinuation = Interlocked.Exchange(ref _originalContinuation, null); |
| | 16 | 278 | | if (originalContinuation != null) |
| | | 279 | | { |
| | 12 | 280 | | _registration.Dispose(); |
| | 12 | 281 | | originalContinuation(); |
| | | 282 | | } |
| | 16 | 283 | | } |
| | | 284 | | } |
| | | 285 | | } |
| | | 286 | | } |