ETTaskCompletionSource.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. using System;
  2. using System.Runtime.CompilerServices;
  3. using System.Runtime.ExceptionServices;
  4. namespace ETModel
  5. {
  6. public class ETTaskCompletionSource: IAwaiter
  7. {
  8. // State(= AwaiterStatus)
  9. private const int Pending = 0;
  10. private const int Succeeded = 1;
  11. private const int Faulted = 2;
  12. private const int Canceled = 3;
  13. private int state;
  14. private ExceptionDispatchInfo exception;
  15. private Action continuation; // action or list
  16. AwaiterStatus IAwaiter.Status => (AwaiterStatus) state;
  17. bool IAwaiter.IsCompleted => state != Pending;
  18. public ETTask Task => new ETTask(this);
  19. void IAwaiter.GetResult()
  20. {
  21. switch (this.state)
  22. {
  23. case Succeeded:
  24. return;
  25. case Faulted:
  26. this.exception?.Throw();
  27. this.exception = null;
  28. return;
  29. case Canceled:
  30. {
  31. this.exception?.Throw(); // guranteed operation canceled exception.
  32. this.exception = null;
  33. throw new OperationCanceledException();
  34. }
  35. default:
  36. throw new NotSupportedException("ETTask does not allow call GetResult directly when task not completed. Please use 'await'.");
  37. }
  38. }
  39. void ICriticalNotifyCompletion.UnsafeOnCompleted(Action action)
  40. {
  41. this.continuation = action;
  42. if (state != Pending)
  43. {
  44. TryInvokeContinuation();
  45. }
  46. }
  47. private void TryInvokeContinuation()
  48. {
  49. this.continuation?.Invoke();
  50. this.continuation = null;
  51. }
  52. public void SetResult()
  53. {
  54. if (this.TrySetResult())
  55. {
  56. return;
  57. }
  58. throw new InvalidOperationException("TaskT_TransitionToFinal_AlreadyCompleted");
  59. }
  60. public void SetException(Exception e)
  61. {
  62. if (this.TrySetException(e))
  63. {
  64. return;
  65. }
  66. throw new InvalidOperationException("TaskT_TransitionToFinal_AlreadyCompleted");
  67. }
  68. public bool TrySetResult()
  69. {
  70. if (this.state != Pending)
  71. {
  72. return false;
  73. }
  74. this.state = Succeeded;
  75. this.TryInvokeContinuation();
  76. return true;
  77. }
  78. public bool TrySetException(Exception e)
  79. {
  80. if (this.state != Pending)
  81. {
  82. return false;
  83. }
  84. this.state = Faulted;
  85. this.exception = ExceptionDispatchInfo.Capture(e);
  86. this.TryInvokeContinuation();
  87. return true;
  88. }
  89. public bool TrySetCanceled()
  90. {
  91. if (this.state != Pending)
  92. {
  93. return false;
  94. }
  95. this.state = Canceled;
  96. this.TryInvokeContinuation();
  97. return true;
  98. }
  99. public bool TrySetCanceled(OperationCanceledException e)
  100. {
  101. if (this.state != Pending)
  102. {
  103. return false;
  104. }
  105. this.state = Canceled;
  106. this.exception = ExceptionDispatchInfo.Capture(e);
  107. this.TryInvokeContinuation();
  108. return true;
  109. }
  110. void INotifyCompletion.OnCompleted(Action action)
  111. {
  112. ((ICriticalNotifyCompletion) this).UnsafeOnCompleted(action);
  113. }
  114. }
  115. public class ETTaskCompletionSource<T>: IAwaiter<T>
  116. {
  117. // State(= AwaiterStatus)
  118. private const int Pending = 0;
  119. private const int Succeeded = 1;
  120. private const int Faulted = 2;
  121. private const int Canceled = 3;
  122. private int state;
  123. private T value;
  124. private ExceptionDispatchInfo exception;
  125. private Action continuation; // action or list
  126. bool IAwaiter.IsCompleted => state != Pending;
  127. public ETTask<T> Task => new ETTask<T>(this);
  128. AwaiterStatus IAwaiter.Status => (AwaiterStatus) state;
  129. T IAwaiter<T>.GetResult()
  130. {
  131. switch (this.state)
  132. {
  133. case Succeeded:
  134. return this.value;
  135. case Faulted:
  136. this.exception?.Throw();
  137. this.exception = null;
  138. return default;
  139. case Canceled:
  140. {
  141. this.exception?.Throw(); // guranteed operation canceled exception.
  142. this.exception = null;
  143. throw new OperationCanceledException();
  144. }
  145. default:
  146. throw new NotSupportedException("ETTask does not allow call GetResult directly when task not completed. Please use 'await'.");
  147. }
  148. }
  149. void ICriticalNotifyCompletion.UnsafeOnCompleted(Action action)
  150. {
  151. this.continuation = action;
  152. if (state != Pending)
  153. {
  154. TryInvokeContinuation();
  155. }
  156. }
  157. private void TryInvokeContinuation()
  158. {
  159. this.continuation?.Invoke();
  160. this.continuation = null;
  161. }
  162. public void SetResult(T result)
  163. {
  164. if (this.TrySetResult(result))
  165. {
  166. return;
  167. }
  168. throw new InvalidOperationException("TaskT_TransitionToFinal_AlreadyCompleted");
  169. }
  170. public void SetException(Exception e)
  171. {
  172. if (this.TrySetException(e))
  173. {
  174. return;
  175. }
  176. throw new InvalidOperationException("TaskT_TransitionToFinal_AlreadyCompleted");
  177. }
  178. public bool TrySetResult(T result)
  179. {
  180. if (this.state != Pending)
  181. {
  182. return false;
  183. }
  184. this.state = Succeeded;
  185. this.value = result;
  186. this.TryInvokeContinuation();
  187. return true;
  188. }
  189. public bool TrySetException(Exception e)
  190. {
  191. if (this.state != Pending)
  192. {
  193. return false;
  194. }
  195. this.state = Faulted;
  196. this.exception = ExceptionDispatchInfo.Capture(e);
  197. this.TryInvokeContinuation();
  198. return true;
  199. }
  200. public bool TrySetCanceled()
  201. {
  202. if (this.state != Pending)
  203. {
  204. return false;
  205. }
  206. this.state = Canceled;
  207. this.TryInvokeContinuation();
  208. return true;
  209. }
  210. public bool TrySetCanceled(OperationCanceledException e)
  211. {
  212. if (this.state != Pending)
  213. {
  214. return false;
  215. }
  216. this.state = Canceled;
  217. this.exception = ExceptionDispatchInfo.Capture(e);
  218. this.TryInvokeContinuation();
  219. return true;
  220. }
  221. void IAwaiter.GetResult()
  222. {
  223. ((IAwaiter<T>) this).GetResult();
  224. }
  225. void INotifyCompletion.OnCompleted(Action action)
  226. {
  227. ((ICriticalNotifyCompletion) this).UnsafeOnCompleted(action);
  228. }
  229. }
  230. }