AsyncTaskMethodBuilder.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. using System;
  2. using System.Diagnostics;
  3. using System.Runtime.CompilerServices;
  4. using System.Security;
  5. namespace Fort23.Core
  6. {
  7. /// <summary>
  8. /// Represents a builder for asynchronous methods that return a task.
  9. /// </summary>
  10. public struct AsyncTaskMethodBuilder
  11. {
  12. private CTask _tcs;
  13. // 1. Static Create method.
  14. [DebuggerHidden]
  15. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  16. public static AsyncTaskMethodBuilder Create()
  17. {
  18. AsyncTaskMethodBuilder builder = new AsyncTaskMethodBuilder() {_tcs = CTask.Create()};
  19. return builder;
  20. }
  21. // 2. TaskLike Task property.
  22. [DebuggerHidden] public CTask Task => this._tcs;
  23. // 3. SetException
  24. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  25. [DebuggerHidden]
  26. public void SetException(Exception exception)
  27. {
  28. this._tcs.SetException(exception);
  29. }
  30. // 4. SetResult
  31. [DebuggerHidden]
  32. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  33. public void SetResult()
  34. {
  35. this._tcs.SetResult();
  36. }
  37. // 5. AwaitOnCompleted
  38. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  39. [DebuggerHidden]
  40. public void AwaitOnCompleted<TAwaiter, TStateMachine>(ref TAwaiter awaiter, ref TStateMachine stateMachine) where TAwaiter : INotifyCompletion where TStateMachine : IAsyncStateMachine
  41. {
  42. awaiter.OnCompleted(stateMachine.MoveNext);
  43. }
  44. // 6. AwaitUnsafeOnCompleted
  45. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  46. [DebuggerHidden]
  47. [SecuritySafeCritical]
  48. public void AwaitUnsafeOnCompleted<TAwaiter, TStateMachine>(ref TAwaiter awaiter, ref TStateMachine stateMachine) where TAwaiter : ICriticalNotifyCompletion where TStateMachine : IAsyncStateMachine
  49. {
  50. awaiter.OnCompleted(stateMachine.MoveNext);
  51. }
  52. // 7. Start
  53. [DebuggerHidden]
  54. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  55. public void Start<TStateMachine>(ref TStateMachine stateMachine) where TStateMachine : IAsyncStateMachine
  56. {
  57. stateMachine.MoveNext();
  58. }
  59. // 8. SetStateMachine
  60. [DebuggerHidden]
  61. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  62. public void SetStateMachine(IAsyncStateMachine stateMachine)
  63. {
  64. }
  65. }
  66. /// <summary>
  67. /// Represents a builder for asynchronous methods that return a task.
  68. /// </summary>
  69. /// <typeparam name="T"></typeparam>
  70. public struct AsyncTaskMethodBuilder<T>
  71. {
  72. private CTask<T> _task;
  73. // 1. Static Create method.
  74. [DebuggerHidden]
  75. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  76. public static AsyncTaskMethodBuilder<T> Create()
  77. {
  78. AsyncTaskMethodBuilder<T> builder = new AsyncTaskMethodBuilder<T>() {_task = CTask<T>.Create()};
  79. return builder;
  80. }
  81. // 2. TaskLike Task property.
  82. [DebuggerHidden] public CTask<T> Task => _task;
  83. // 3. SetException
  84. [DebuggerHidden]
  85. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  86. public void SetException(Exception exception)
  87. {
  88. _task.SetException(exception);
  89. }
  90. // 4. SetResult
  91. [DebuggerHidden]
  92. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  93. public void SetResult(T ret)
  94. {
  95. _task.SetResult(ret);
  96. }
  97. // 5. AwaitOnCompleted
  98. [DebuggerHidden]
  99. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  100. public void AwaitOnCompleted<TAwaiter, TStateMachine>(ref TAwaiter awaiter, ref TStateMachine stateMachine) where TAwaiter : INotifyCompletion where TStateMachine : IAsyncStateMachine
  101. {
  102. awaiter.OnCompleted(stateMachine.MoveNext);
  103. }
  104. // 6. AwaitUnsafeOnCompleted
  105. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  106. [DebuggerHidden]
  107. [SecuritySafeCritical]
  108. public void AwaitUnsafeOnCompleted<TAwaiter, TStateMachine>(ref TAwaiter awaiter, ref TStateMachine stateMachine) where TAwaiter : ICriticalNotifyCompletion where TStateMachine : IAsyncStateMachine
  109. {
  110. awaiter.OnCompleted(stateMachine.MoveNext);
  111. }
  112. // 7. Start
  113. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  114. [DebuggerHidden]
  115. public void Start<TStateMachine>(ref TStateMachine stateMachine) where TStateMachine : IAsyncStateMachine
  116. {
  117. stateMachine.MoveNext();
  118. }
  119. // 8. SetStateMachine
  120. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  121. [DebuggerHidden]
  122. public void SetStateMachine(IAsyncStateMachine stateMachine)
  123. {
  124. }
  125. }
  126. }