| | 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 |
| 0 | 19 | | => new WithCancellationTaskAwaitable<T>(task, cancellationToken); |
| | 20 | |
|
| | 21 | | public static WithCancellationValueTaskAwaitable<T> AwaitWithCancellation<T>(this ValueTask<T> task, Cancellatio |
| 0 | 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 |
| 200 | 29 | | if (HasSynchronizationContext()) |
| | 30 | | { |
| 0 | 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 |
| 200 | 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 |
| 0 | 44 | | if (HasSynchronizationContext()) |
| | 45 | | { |
| 0 | 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 |
| 0 | 50 | | task.GetAwaiter().GetResult(); |
| | 51 | | #pragma warning restore AZC0102 // Do not use GetAwaiter().GetResult(). Use the TaskExtensions.EnsureCompleted() extensi |
| 0 | 52 | | } |
| | 53 | |
|
| | 54 | | public static T EnsureCompleted<T>(this ValueTask<T> task) |
| | 55 | | { |
| 0 | 56 | | if (!task.IsCompleted) |
| | 57 | | { |
| | 58 | | #pragma warning disable AZC0107 // public asynchronous method shouldn't be called in synchronous scope. Use synchronous |
| 0 | 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 |
| 0 | 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 | | { |
| 0 | 69 | | if (!task.IsCompleted) |
| | 70 | | { |
| | 71 | | #pragma warning disable AZC0107 // public asynchronous method shouldn't be called in synchronous scope. Use synchronous |
| 0 | 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 |
| 0 | 78 | | task.GetAwaiter().GetResult(); |
| | 79 | | #pragma warning restore AZC0102 // Do not use GetAwaiter().GetResult(). Use the TaskExtensions.EnsureCompleted() extensi |
| | 80 | | } |
| 0 | 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 | | { |
| 0 | 87 | | if (!async) |
| | 88 | | { |
| | 89 | | #if DEBUG |
| | 90 | | VerifyTaskCompleted(awaitable.GetAwaiter().IsCompleted); |
| | 91 | | #endif |
| | 92 | | } |
| 0 | 93 | | return awaitable; |
| | 94 | | } |
| | 95 | |
|
| | 96 | | public static ConfiguredValueTaskAwaitable EnsureCompleted(this ConfiguredValueTaskAwaitable awaitable, bool asy |
| | 97 | | { |
| | 98 | |
|
| 0 | 99 | | if (!async) |
| | 100 | | { |
| | 101 | | #if DEBUG |
| | 102 | | VerifyTaskCompleted(awaitable.GetAwaiter().IsCompleted); |
| | 103 | | #endif |
| | 104 | | } |
| 0 | 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() |
| 200 | 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 | | { |
| 0 | 173 | | _awaitable = task.ConfigureAwait(false); |
| 0 | 174 | | _cancellationToken = cancellationToken; |
| 0 | 175 | | } |
| | 176 | |
|
| 0 | 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 | | { |
| 0 | 187 | | _awaitable = task.ConfigureAwait(false); |
| 0 | 188 | | _cancellationToken = cancellationToken; |
| 0 | 189 | | } |
| | 190 | |
|
| 0 | 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 | | { |
| 0 | 201 | | _taskAwaiter = awaiter; |
| 0 | 202 | | _cancellationToken = cancellationToken; |
| 0 | 203 | | } |
| | 204 | |
|
| 0 | 205 | | public bool IsCompleted => _taskAwaiter.IsCompleted || _cancellationToken.IsCancellationRequested; |
| | 206 | |
|
| 0 | 207 | | public void OnCompleted(Action continuation) => _taskAwaiter.OnCompleted(WrapContinuation(continuation)); |
| | 208 | |
|
| 0 | 209 | | public void UnsafeOnCompleted(Action continuation) => _taskAwaiter.UnsafeOnCompleted(WrapContinuation(contin |
| | 210 | |
|
| | 211 | | public T GetResult() |
| | 212 | | { |
| | 213 | | Debug.Assert(IsCompleted); |
| 0 | 214 | | if (!_taskAwaiter.IsCompleted) |
| | 215 | | { |
| 0 | 216 | | _cancellationToken.ThrowIfCancellationRequested(); |
| | 217 | | } |
| 0 | 218 | | return _taskAwaiter.GetResult(); |
| | 219 | | } |
| | 220 | |
|
| | 221 | | private Action WrapContinuation(in Action originalContinuation) |
| 0 | 222 | | => _cancellationToken.CanBeCanceled |
| 0 | 223 | | ? new WithCancellationContinuationWrapper(originalContinuation, _cancellationToken).Continuation |
| 0 | 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 | | { |
| 0 | 234 | | _taskAwaiter = awaiter; |
| 0 | 235 | | _cancellationToken = cancellationToken; |
| 0 | 236 | | } |
| | 237 | |
|
| 0 | 238 | | public bool IsCompleted => _taskAwaiter.IsCompleted || _cancellationToken.IsCancellationRequested; |
| | 239 | |
|
| 0 | 240 | | public void OnCompleted(Action continuation) => _taskAwaiter.OnCompleted(WrapContinuation(continuation)); |
| | 241 | |
|
| 0 | 242 | | public void UnsafeOnCompleted(Action continuation) => _taskAwaiter.UnsafeOnCompleted(WrapContinuation(contin |
| | 243 | |
|
| | 244 | | public T GetResult() |
| | 245 | | { |
| | 246 | | Debug.Assert(IsCompleted); |
| 0 | 247 | | if (!_taskAwaiter.IsCompleted) |
| | 248 | | { |
| 0 | 249 | | _cancellationToken.ThrowIfCancellationRequested(); |
| | 250 | | } |
| 0 | 251 | | return _taskAwaiter.GetResult(); |
| | 252 | | } |
| | 253 | |
|
| | 254 | | private Action WrapContinuation(in Action originalContinuation) |
| 0 | 255 | | => _cancellationToken.CanBeCanceled |
| 0 | 256 | | ? new WithCancellationContinuationWrapper(originalContinuation, _cancellationToken).Continuation |
| 0 | 257 | | : originalContinuation; |
| | 258 | | } |
| | 259 | |
|
| | 260 | | private class WithCancellationContinuationWrapper |
| | 261 | | { |
| | 262 | | private Action _originalContinuation; |
| | 263 | | private readonly CancellationTokenRegistration _registration; |
| | 264 | |
|
| 0 | 265 | | public WithCancellationContinuationWrapper(Action originalContinuation, CancellationToken cancellationToken) |
| | 266 | | { |
| 0 | 267 | | Action continuation = ContinuationImplementation; |
| 0 | 268 | | _originalContinuation = originalContinuation; |
| 0 | 269 | | _registration = cancellationToken.Register(continuation); |
| 0 | 270 | | Continuation = continuation; |
| 0 | 271 | | } |
| | 272 | |
|
| 0 | 273 | | public Action Continuation { get; } |
| | 274 | |
|
| | 275 | | private void ContinuationImplementation() |
| | 276 | | { |
| 0 | 277 | | Action originalContinuation = Interlocked.Exchange(ref _originalContinuation, null); |
| 0 | 278 | | if (originalContinuation != null) |
| | 279 | | { |
| 0 | 280 | | _registration.Dispose(); |
| 0 | 281 | | originalContinuation(); |
| | 282 | | } |
| 0 | 283 | | } |
| | 284 | | } |
| | 285 | | } |
| | 286 | | } |