AsyncUniTaskMethodBuilder.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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 AsyncUniTaskMethodBuilder
  12. {
  13. IStateMachineRunnerPromise runnerPromise;
  14. Exception ex;
  15. // 1. Static Create method.
  16. [DebuggerHidden]
  17. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  18. public static AsyncUniTaskMethodBuilder Create()
  19. {
  20. return default;
  21. }
  22. // 2. TaskLike Task property.
  23. public UniTask Task
  24. {
  25. [DebuggerHidden]
  26. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  27. get
  28. {
  29. if (runnerPromise != null)
  30. {
  31. return runnerPromise.Task;
  32. }
  33. else if (ex != null)
  34. {
  35. return UniTask.FromException(ex);
  36. }
  37. else
  38. {
  39. return UniTask.CompletedTask;
  40. }
  41. }
  42. }
  43. // 3. SetException
  44. [DebuggerHidden]
  45. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  46. public void SetException(Exception exception)
  47. {
  48. if (runnerPromise == null)
  49. {
  50. ex = exception;
  51. }
  52. else
  53. {
  54. runnerPromise.SetException(exception);
  55. }
  56. }
  57. // 4. SetResult
  58. [DebuggerHidden]
  59. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  60. public void SetResult()
  61. {
  62. if (runnerPromise != null)
  63. {
  64. runnerPromise.SetResult();
  65. }
  66. }
  67. // 5. AwaitOnCompleted
  68. [DebuggerHidden]
  69. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  70. public void AwaitOnCompleted<TAwaiter, TStateMachine>(ref TAwaiter awaiter, ref TStateMachine stateMachine)
  71. where TAwaiter : INotifyCompletion
  72. where TStateMachine : IAsyncStateMachine
  73. {
  74. if (runnerPromise == null)
  75. {
  76. AsyncUniTask<TStateMachine>.SetStateMachine(ref stateMachine, ref runnerPromise);
  77. }
  78. awaiter.OnCompleted(runnerPromise.MoveNext);
  79. }
  80. // 6. AwaitUnsafeOnCompleted
  81. [DebuggerHidden]
  82. [SecuritySafeCritical]
  83. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  84. public void AwaitUnsafeOnCompleted<TAwaiter, TStateMachine>(ref TAwaiter awaiter, ref TStateMachine stateMachine)
  85. where TAwaiter : ICriticalNotifyCompletion
  86. where TStateMachine : IAsyncStateMachine
  87. {
  88. if (runnerPromise == null)
  89. {
  90. AsyncUniTask<TStateMachine>.SetStateMachine(ref stateMachine, ref runnerPromise);
  91. }
  92. awaiter.UnsafeOnCompleted(runnerPromise.MoveNext);
  93. }
  94. // 7. Start
  95. [DebuggerHidden]
  96. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  97. public void Start<TStateMachine>(ref TStateMachine stateMachine)
  98. where TStateMachine : IAsyncStateMachine
  99. {
  100. stateMachine.MoveNext();
  101. }
  102. // 8. SetStateMachine
  103. [DebuggerHidden]
  104. public void SetStateMachine(IAsyncStateMachine stateMachine)
  105. {
  106. // don't use boxed stateMachine.
  107. }
  108. #if DEBUG || !UNITY_2018_3_OR_NEWER
  109. // Important for IDE debugger.
  110. object debuggingId;
  111. private object ObjectIdForDebugger
  112. {
  113. get
  114. {
  115. if (debuggingId == null)
  116. {
  117. debuggingId = new object();
  118. }
  119. return debuggingId;
  120. }
  121. }
  122. #endif
  123. }
  124. [StructLayout(LayoutKind.Auto)]
  125. public struct AsyncUniTaskMethodBuilder<T>
  126. {
  127. IStateMachineRunnerPromise<T> runnerPromise;
  128. Exception ex;
  129. T result;
  130. // 1. Static Create method.
  131. [DebuggerHidden]
  132. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  133. public static AsyncUniTaskMethodBuilder<T> Create()
  134. {
  135. return default;
  136. }
  137. // 2. TaskLike Task property.
  138. public UniTask<T> Task
  139. {
  140. [DebuggerHidden]
  141. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  142. get
  143. {
  144. if (runnerPromise != null)
  145. {
  146. return runnerPromise.Task;
  147. }
  148. else if (ex != null)
  149. {
  150. return UniTask.FromException<T>(ex);
  151. }
  152. else
  153. {
  154. return UniTask.FromResult(result);
  155. }
  156. }
  157. }
  158. // 3. SetException
  159. [DebuggerHidden]
  160. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  161. public void SetException(Exception exception)
  162. {
  163. if (runnerPromise == null)
  164. {
  165. ex = exception;
  166. }
  167. else
  168. {
  169. runnerPromise.SetException(exception);
  170. }
  171. }
  172. // 4. SetResult
  173. [DebuggerHidden]
  174. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  175. public void SetResult(T result)
  176. {
  177. if (runnerPromise == null)
  178. {
  179. this.result = result;
  180. }
  181. else
  182. {
  183. runnerPromise.SetResult(result);
  184. }
  185. }
  186. // 5. AwaitOnCompleted
  187. [DebuggerHidden]
  188. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  189. public void AwaitOnCompleted<TAwaiter, TStateMachine>(ref TAwaiter awaiter, ref TStateMachine stateMachine)
  190. where TAwaiter : INotifyCompletion
  191. where TStateMachine : IAsyncStateMachine
  192. {
  193. if (runnerPromise == null)
  194. {
  195. AsyncUniTask<TStateMachine, T>.SetStateMachine(ref stateMachine, ref runnerPromise);
  196. }
  197. awaiter.OnCompleted(runnerPromise.MoveNext);
  198. }
  199. // 6. AwaitUnsafeOnCompleted
  200. [DebuggerHidden]
  201. [SecuritySafeCritical]
  202. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  203. public void AwaitUnsafeOnCompleted<TAwaiter, TStateMachine>(ref TAwaiter awaiter, ref TStateMachine stateMachine)
  204. where TAwaiter : ICriticalNotifyCompletion
  205. where TStateMachine : IAsyncStateMachine
  206. {
  207. if (runnerPromise == null)
  208. {
  209. AsyncUniTask<TStateMachine, T>.SetStateMachine(ref stateMachine, ref runnerPromise);
  210. }
  211. awaiter.UnsafeOnCompleted(runnerPromise.MoveNext);
  212. }
  213. // 7. Start
  214. [DebuggerHidden]
  215. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  216. public void Start<TStateMachine>(ref TStateMachine stateMachine)
  217. where TStateMachine : IAsyncStateMachine
  218. {
  219. stateMachine.MoveNext();
  220. }
  221. // 8. SetStateMachine
  222. [DebuggerHidden]
  223. public void SetStateMachine(IAsyncStateMachine stateMachine)
  224. {
  225. // don't use boxed stateMachine.
  226. }
  227. #if DEBUG || !UNITY_2018_3_OR_NEWER
  228. // Important for IDE debugger.
  229. object debuggingId;
  230. private object ObjectIdForDebugger
  231. {
  232. get
  233. {
  234. if (debuggingId == null)
  235. {
  236. debuggingId = new object();
  237. }
  238. return debuggingId;
  239. }
  240. }
  241. #endif
  242. }
  243. }