ETTask.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Runtime.CompilerServices;
  5. using System.Runtime.ExceptionServices;
  6. namespace ET
  7. {
  8. [AsyncMethodBuilder(typeof (ETAsyncTaskMethodBuilder))]
  9. public class ETTask: ICriticalNotifyCompletion
  10. {
  11. public static Action<Exception> ExceptionHandler;
  12. public static ETTaskCompleted CompletedTask
  13. {
  14. get
  15. {
  16. return new ETTaskCompleted();
  17. }
  18. }
  19. private static readonly Queue<ETTask> queue = new Queue<ETTask>();
  20. /// <summary>
  21. /// 请不要随便使用ETTask的对象池,除非你完全搞懂了ETTask!!!
  22. /// 假如开启了池,await之后不能再操作ETTask,否则可能操作到再次从池中分配出来的ETTask,产生灾难性的后果
  23. /// SetResult的时候请现将tcs置空,避免多次对同一个ETTask SetResult
  24. /// </summary>
  25. public static ETTask Create(bool fromPool = false)
  26. {
  27. if (!fromPool)
  28. {
  29. return new ETTask();
  30. }
  31. if (queue.Count == 0)
  32. {
  33. return new ETTask() {fromPool = true};
  34. }
  35. return queue.Dequeue();
  36. }
  37. private void Recycle()
  38. {
  39. if (!this.fromPool)
  40. {
  41. return;
  42. }
  43. this.state = AwaiterStatus.Pending;
  44. this.callback = null;
  45. if (queue.Count > 1000)
  46. {
  47. return;
  48. }
  49. queue.Enqueue(this);
  50. }
  51. private bool fromPool;
  52. private AwaiterStatus state;
  53. private object callback; // Action or ExceptionDispatchInfo
  54. private ETTask()
  55. {
  56. }
  57. [DebuggerHidden]
  58. private async ETVoid InnerCoroutine()
  59. {
  60. await this;
  61. }
  62. [DebuggerHidden]
  63. public void Coroutine()
  64. {
  65. InnerCoroutine().Coroutine();
  66. }
  67. [DebuggerHidden]
  68. public ETTask GetAwaiter()
  69. {
  70. return this;
  71. }
  72. public bool IsCompleted
  73. {
  74. [DebuggerHidden]
  75. get
  76. {
  77. return this.state != AwaiterStatus.Pending;
  78. }
  79. }
  80. [DebuggerHidden]
  81. public void UnsafeOnCompleted(Action action)
  82. {
  83. if (this.state != AwaiterStatus.Pending)
  84. {
  85. action?.Invoke();
  86. return;
  87. }
  88. this.callback = action;
  89. }
  90. [DebuggerHidden]
  91. public void OnCompleted(Action action)
  92. {
  93. this.UnsafeOnCompleted(action);
  94. }
  95. [DebuggerHidden]
  96. public void GetResult()
  97. {
  98. switch (this.state)
  99. {
  100. case AwaiterStatus.Succeeded:
  101. this.Recycle();
  102. break;
  103. case AwaiterStatus.Faulted:
  104. ExceptionDispatchInfo c = this.callback as ExceptionDispatchInfo;
  105. this.callback = null;
  106. this.Recycle();
  107. c?.Throw();
  108. break;
  109. default:
  110. throw new NotSupportedException("ETTask does not allow call GetResult directly when task not completed. Please use 'await'.");
  111. }
  112. }
  113. [DebuggerHidden]
  114. public void SetResult()
  115. {
  116. if (this.state != AwaiterStatus.Pending)
  117. {
  118. throw new InvalidOperationException("TaskT_TransitionToFinal_AlreadyCompleted");
  119. }
  120. this.state = AwaiterStatus.Succeeded;
  121. Action c = this.callback as Action;
  122. this.callback = null;
  123. c?.Invoke();
  124. }
  125. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  126. [DebuggerHidden]
  127. public void SetException(Exception e)
  128. {
  129. if (this.state != AwaiterStatus.Pending)
  130. {
  131. throw new InvalidOperationException("TaskT_TransitionToFinal_AlreadyCompleted");
  132. }
  133. this.state = AwaiterStatus.Faulted;
  134. Action c = this.callback as Action;
  135. this.callback = ExceptionDispatchInfo.Capture(e);
  136. c?.Invoke();
  137. }
  138. }
  139. [AsyncMethodBuilder(typeof (ETAsyncTaskMethodBuilder<>))]
  140. public class ETTask<T>: ICriticalNotifyCompletion
  141. {
  142. private static readonly Queue<ETTask<T>> queue = new Queue<ETTask<T>>();
  143. /// <summary>
  144. /// 请不要随便使用ETTask的对象池,除非你完全搞懂了ETTask!!!
  145. /// 假如开启了池,await之后不能再操作ETTask,否则可能操作到再次从池中分配出来的ETTask,产生灾难性的后果
  146. /// SetResult的时候请现将tcs置空,避免多次对同一个ETTask SetResult
  147. /// </summary>
  148. public static ETTask<T> Create(bool fromPool = false)
  149. {
  150. if (!fromPool)
  151. {
  152. return new ETTask<T>();
  153. }
  154. if (queue.Count == 0)
  155. {
  156. return new ETTask<T>() { fromPool = true };
  157. }
  158. return queue.Dequeue();
  159. }
  160. private void Recycle()
  161. {
  162. if (!this.fromPool)
  163. {
  164. return;
  165. }
  166. this.callback = null;
  167. this.value = default;
  168. this.state = AwaiterStatus.Pending;
  169. if (queue.Count > 1000)
  170. {
  171. return;
  172. }
  173. queue.Enqueue(this);
  174. }
  175. private bool fromPool;
  176. private AwaiterStatus state;
  177. private T value;
  178. private object callback; // Action or ExceptionDispatchInfo
  179. private ETTask()
  180. {
  181. }
  182. [DebuggerHidden]
  183. private async ETVoid InnerCoroutine()
  184. {
  185. await this;
  186. }
  187. [DebuggerHidden]
  188. public void Coroutine()
  189. {
  190. InnerCoroutine().Coroutine();
  191. }
  192. [DebuggerHidden]
  193. public ETTask<T> GetAwaiter()
  194. {
  195. return this;
  196. }
  197. [DebuggerHidden]
  198. public T GetResult()
  199. {
  200. switch (this.state)
  201. {
  202. case AwaiterStatus.Succeeded:
  203. T v = this.value;
  204. this.Recycle();
  205. return v;
  206. case AwaiterStatus.Faulted:
  207. ExceptionDispatchInfo c = this.callback as ExceptionDispatchInfo;
  208. this.callback = null;
  209. this.Recycle();
  210. c?.Throw();
  211. return default;
  212. default:
  213. throw new NotSupportedException("ETask does not allow call GetResult directly when task not completed. Please use 'await'.");
  214. }
  215. }
  216. public bool IsCompleted
  217. {
  218. [DebuggerHidden]
  219. get
  220. {
  221. return state != AwaiterStatus.Pending;
  222. }
  223. }
  224. [DebuggerHidden]
  225. public void UnsafeOnCompleted(Action action)
  226. {
  227. if (this.state != AwaiterStatus.Pending)
  228. {
  229. action?.Invoke();
  230. return;
  231. }
  232. this.callback = action;
  233. }
  234. [DebuggerHidden]
  235. public void OnCompleted(Action action)
  236. {
  237. this.UnsafeOnCompleted(action);
  238. }
  239. [DebuggerHidden]
  240. public void SetResult(T result)
  241. {
  242. if (this.state != AwaiterStatus.Pending)
  243. {
  244. throw new InvalidOperationException("TaskT_TransitionToFinal_AlreadyCompleted");
  245. }
  246. this.state = AwaiterStatus.Succeeded;
  247. this.value = result;
  248. Action c = this.callback as Action;
  249. this.callback = null;
  250. c?.Invoke();
  251. }
  252. [DebuggerHidden]
  253. public void SetException(Exception e)
  254. {
  255. if (this.state != AwaiterStatus.Pending)
  256. {
  257. throw new InvalidOperationException("TaskT_TransitionToFinal_AlreadyCompleted");
  258. }
  259. this.state = AwaiterStatus.Faulted;
  260. Action c = this.callback as Action;
  261. this.callback = ExceptionDispatchInfo.Capture(e);
  262. c?.Invoke();
  263. }
  264. }
  265. }