You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

138 lines
3.9 KiB

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