|
| 1 | +#if !BCL_HAS_CONFIGUREAWAIT |
| 2 | + |
| 3 | +// Licensed to the .NET Foundation under one or more agreements. |
| 4 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 5 | +// See the LICENSE file in the project root for more information. |
| 6 | + |
| 7 | +using System.Diagnostics; |
| 8 | +using System.Reflection; |
| 9 | +using System.Runtime.InteropServices; |
| 10 | +using System.Text; |
| 11 | +using System.Threading; |
| 12 | +using System.Threading.Tasks; |
| 13 | + |
| 14 | +namespace System.Runtime.CompilerServices |
| 15 | +{ |
| 16 | + /// <summary>Represents a builder for asynchronous iterators.</summary> |
| 17 | + [StructLayout(LayoutKind.Auto)] |
| 18 | + public struct AsyncIteratorMethodBuilder |
| 19 | + { |
| 20 | + // AsyncIteratorMethodBuilder is used by the language compiler as part of generating |
| 21 | + // async iterators. For now, the implementation just wraps AsyncTaskMethodBuilder, as |
| 22 | + // most of the logic is shared. However, in the future this could be changed and |
| 23 | + // optimized. For example, we do need to allocate an object (once) to flow state like |
| 24 | + // ExecutionContext, which AsyncTaskMethodBuilder handles, but it handles it by |
| 25 | + // allocating a Task-derived object. We could optimize this further by removing |
| 26 | + // the Task from the hierarchy, but in doing so we'd also lose a variety of optimizations |
| 27 | + // related to it, so we'd need to replicate all of those optimizations (e.g. storing |
| 28 | + // that box object directly into a Task's continuation field). |
| 29 | + |
| 30 | +#pragma warning disable IDE0044 // Add readonly modifier |
| 31 | + private AsyncTaskMethodBuilder _methodBuilder; // mutable struct; do not make it readonly |
| 32 | +#pragma warning restore IDE0044 // Add readonly modifier |
| 33 | + |
| 34 | + /// <summary>Creates an instance of the <see cref="AsyncIteratorMethodBuilder"/> struct.</summary> |
| 35 | + /// <returns>The initialized instance.</returns> |
| 36 | + public static AsyncIteratorMethodBuilder Create() => |
| 37 | +#if PROJECTN |
| 38 | + // ProjectN's AsyncTaskMethodBuilder.Create() currently does additional debugger-related |
| 39 | + // work, so we need to delegate to it. |
| 40 | + new AsyncIteratorMethodBuilder() { _methodBuilder = AsyncTaskMethodBuilder.Create() }; |
| 41 | +#else |
| 42 | + // _methodBuilder should be initialized to AsyncTaskMethodBuilder.Create(), but on coreclr |
| 43 | + // that Create() is a nop, so we can just return the default here. |
| 44 | + default; |
| 45 | +#endif |
| 46 | + |
| 47 | + /// <summary>Invokes <see cref="IAsyncStateMachine.MoveNext"/> on the state machine while guarding the <see cref="ExecutionContext"/>.</summary> |
| 48 | + /// <typeparam name="TStateMachine">The type of the state machine.</typeparam> |
| 49 | + /// <param name="stateMachine">The state machine instance, passed by reference.</param> |
| 50 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 51 | + public void MoveNext<TStateMachine>(ref TStateMachine stateMachine) where TStateMachine : IAsyncStateMachine => |
| 52 | +#if CORERT || !BCL_HAS_CONFIGUREAWAIT |
| 53 | + _methodBuilder.Start(ref stateMachine); |
| 54 | +#else |
| 55 | + AsyncMethodBuilderCore.Start(ref stateMachine); |
| 56 | +#endif |
| 57 | + |
| 58 | + /// <summary>Schedules the state machine to proceed to the next action when the specified awaiter completes.</summary> |
| 59 | + /// <typeparam name="TAwaiter">The type of the awaiter.</typeparam> |
| 60 | + /// <typeparam name="TStateMachine">The type of the state machine.</typeparam> |
| 61 | + /// <param name="awaiter">The awaiter.</param> |
| 62 | + /// <param name="stateMachine">The state machine.</param> |
| 63 | + public void AwaitOnCompleted<TAwaiter, TStateMachine>(ref TAwaiter awaiter, ref TStateMachine stateMachine) |
| 64 | + where TAwaiter : INotifyCompletion |
| 65 | + where TStateMachine : IAsyncStateMachine => |
| 66 | + _methodBuilder.AwaitOnCompleted(ref awaiter, ref stateMachine); |
| 67 | + |
| 68 | + /// <summary>Schedules the state machine to proceed to the next action when the specified awaiter completes.</summary> |
| 69 | + /// <typeparam name="TAwaiter">The type of the awaiter.</typeparam> |
| 70 | + /// <typeparam name="TStateMachine">The type of the state machine.</typeparam> |
| 71 | + /// <param name="awaiter">The awaiter.</param> |
| 72 | + /// <param name="stateMachine">The state machine.</param> |
| 73 | + public void AwaitUnsafeOnCompleted<TAwaiter, TStateMachine>(ref TAwaiter awaiter, ref TStateMachine stateMachine) |
| 74 | + where TAwaiter : ICriticalNotifyCompletion |
| 75 | + where TStateMachine : IAsyncStateMachine => |
| 76 | + _methodBuilder.AwaitUnsafeOnCompleted(ref awaiter, ref stateMachine); |
| 77 | + |
| 78 | + /// <summary>Marks iteration as being completed, whether successfully or otherwise.</summary> |
| 79 | + public void Complete() => _methodBuilder.SetResult(); |
| 80 | + |
| 81 | + /// <summary>Gets an object that may be used to uniquely identify this builder to the debugger.</summary> |
| 82 | + internal object ObjectIdForDebugger => typeof(AsyncTaskMethodBuilder) |
| 83 | + .GetProperty("ObjectIdForDebugger", BindingFlags.Instance | BindingFlags.NonPublic) |
| 84 | + .GetMethod.Invoke(_methodBuilder, null); |
| 85 | + |
| 86 | + |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +#else |
| 91 | +using System.Runtime.CompilerServices; |
| 92 | + |
| 93 | +[assembly: TypeForwardedTo(typeof(AsyncIteratorMethodBuilder))] |
| 94 | + |
| 95 | +#endif |
0 commit comments