AsyncUniTaskVoidMethodBuilder.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. 
  2. #pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
  3. using System;
  4. using System.Diagnostics;
  5. using System.Runtime.CompilerServices;
  6. using System.Runtime.InteropServices;
  7. using System.Security;
  8. namespace Cysharp.Threading.Tasks.CompilerServices
  9. {
  10. [StructLayout(LayoutKind.Auto)]
  11. public struct AsyncUniTaskVoidMethodBuilder
  12. {
  13. IStateMachineRunner runner;
  14. // 1. Static Create method.
  15. [DebuggerHidden]
  16. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  17. public static AsyncUniTaskVoidMethodBuilder Create()
  18. {
  19. return default;
  20. }
  21. // 2. TaskLike Task property(void)
  22. public UniTaskVoid Task
  23. {
  24. [DebuggerHidden]
  25. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  26. get
  27. {
  28. return default;
  29. }
  30. }
  31. // 3. SetException
  32. [DebuggerHidden]
  33. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  34. public void SetException(Exception exception)
  35. {
  36. // runner is finished, return first.
  37. if (runner != null)
  38. {
  39. #if ENABLE_IL2CPP
  40. // workaround for IL2CPP bug.
  41. PlayerLoopHelper.AddContinuation(PlayerLoopTiming.LastPostLateUpdate, runner.ReturnAction);
  42. #else
  43. runner.Return();
  44. #endif
  45. runner = null;
  46. }
  47. UniTaskScheduler.PublishUnobservedTaskException(exception);
  48. }
  49. // 4. SetResult
  50. [DebuggerHidden]
  51. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  52. public void SetResult()
  53. {
  54. // runner is finished, return.
  55. if (runner != null)
  56. {
  57. #if ENABLE_IL2CPP
  58. // workaround for IL2CPP bug.
  59. PlayerLoopHelper.AddContinuation(PlayerLoopTiming.LastPostLateUpdate, runner.ReturnAction);
  60. #else
  61. runner.Return();
  62. #endif
  63. runner = null;
  64. }
  65. }
  66. // 5. AwaitOnCompleted
  67. [DebuggerHidden]
  68. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  69. public void AwaitOnCompleted<TAwaiter, TStateMachine>(ref TAwaiter awaiter, ref TStateMachine stateMachine)
  70. where TAwaiter : INotifyCompletion
  71. where TStateMachine : IAsyncStateMachine
  72. {
  73. if (runner == null)
  74. {
  75. AsyncUniTaskVoid<TStateMachine>.SetStateMachine(ref stateMachine, ref runner);
  76. }
  77. awaiter.OnCompleted(runner.MoveNext);
  78. }
  79. // 6. AwaitUnsafeOnCompleted
  80. [DebuggerHidden]
  81. [SecuritySafeCritical]
  82. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  83. public void AwaitUnsafeOnCompleted<TAwaiter, TStateMachine>(ref TAwaiter awaiter, ref TStateMachine stateMachine)
  84. where TAwaiter : ICriticalNotifyCompletion
  85. where TStateMachine : IAsyncStateMachine
  86. {
  87. if (runner == null)
  88. {
  89. AsyncUniTaskVoid<TStateMachine>.SetStateMachine(ref stateMachine, ref runner);
  90. }
  91. awaiter.UnsafeOnCompleted(runner.MoveNext);
  92. }
  93. // 7. Start
  94. [DebuggerHidden]
  95. public void Start<TStateMachine>(ref TStateMachine stateMachine)
  96. where TStateMachine : IAsyncStateMachine
  97. {
  98. stateMachine.MoveNext();
  99. }
  100. // 8. SetStateMachine
  101. [DebuggerHidden]
  102. public void SetStateMachine(IAsyncStateMachine stateMachine)
  103. {
  104. // don't use boxed stateMachine.
  105. }
  106. #if DEBUG || !UNITY_2018_3_OR_NEWER
  107. // Important for IDE debugger.
  108. object debuggingId;
  109. private object ObjectIdForDebugger
  110. {
  111. get
  112. {
  113. if (debuggingId == null)
  114. {
  115. debuggingId = new object();
  116. }
  117. return debuggingId;
  118. }
  119. }
  120. #endif
  121. }
  122. }