ETTask.cs 8.4 KB

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