AsyncEnumeratorBase.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. using System;
  2. using System.Threading;
  3. namespace Cysharp.Threading.Tasks.Linq
  4. {
  5. // note: refactor all inherit class and should remove this.
  6. // see Select and Where.
  7. internal abstract class AsyncEnumeratorBase<TSource, TResult> : MoveNextSource, IUniTaskAsyncEnumerator<TResult>
  8. {
  9. static readonly Action<object> moveNextCallbackDelegate = MoveNextCallBack;
  10. readonly IUniTaskAsyncEnumerable<TSource> source;
  11. protected CancellationToken cancellationToken;
  12. IUniTaskAsyncEnumerator<TSource> enumerator;
  13. UniTask<bool>.Awaiter sourceMoveNext;
  14. public AsyncEnumeratorBase(IUniTaskAsyncEnumerable<TSource> source, CancellationToken cancellationToken)
  15. {
  16. this.source = source;
  17. this.cancellationToken = cancellationToken;
  18. TaskTracker.TrackActiveTask(this, 4);
  19. }
  20. // abstract
  21. /// <summary>
  22. /// If return value is false, continue source.MoveNext.
  23. /// </summary>
  24. protected abstract bool TryMoveNextCore(bool sourceHasCurrent, out bool result);
  25. // Util
  26. protected TSource SourceCurrent => enumerator.Current;
  27. // IUniTaskAsyncEnumerator<T>
  28. public TResult Current { get; protected set; }
  29. public UniTask<bool> MoveNextAsync()
  30. {
  31. if (enumerator == null)
  32. {
  33. enumerator = source.GetAsyncEnumerator(cancellationToken);
  34. }
  35. completionSource.Reset();
  36. if (!OnFirstIteration())
  37. {
  38. SourceMoveNext();
  39. }
  40. return new UniTask<bool>(this, completionSource.Version);
  41. }
  42. protected virtual bool OnFirstIteration()
  43. {
  44. return false;
  45. }
  46. protected void SourceMoveNext()
  47. {
  48. CONTINUE:
  49. sourceMoveNext = enumerator.MoveNextAsync().GetAwaiter();
  50. if (sourceMoveNext.IsCompleted)
  51. {
  52. bool result = false;
  53. try
  54. {
  55. if (!TryMoveNextCore(sourceMoveNext.GetResult(), out result))
  56. {
  57. goto CONTINUE;
  58. }
  59. }
  60. catch (Exception ex)
  61. {
  62. completionSource.TrySetException(ex);
  63. return;
  64. }
  65. if (cancellationToken.IsCancellationRequested)
  66. {
  67. completionSource.TrySetCanceled(cancellationToken);
  68. }
  69. else
  70. {
  71. completionSource.TrySetResult(result);
  72. }
  73. }
  74. else
  75. {
  76. sourceMoveNext.SourceOnCompleted(moveNextCallbackDelegate, this);
  77. }
  78. }
  79. static void MoveNextCallBack(object state)
  80. {
  81. var self = (AsyncEnumeratorBase<TSource, TResult>)state;
  82. bool result;
  83. try
  84. {
  85. if (!self.TryMoveNextCore(self.sourceMoveNext.GetResult(), out result))
  86. {
  87. self.SourceMoveNext();
  88. return;
  89. }
  90. }
  91. catch (Exception ex)
  92. {
  93. self.completionSource.TrySetException(ex);
  94. return;
  95. }
  96. if (self.cancellationToken.IsCancellationRequested)
  97. {
  98. self.completionSource.TrySetCanceled(self.cancellationToken);
  99. }
  100. else
  101. {
  102. self.completionSource.TrySetResult(result);
  103. }
  104. }
  105. // if require additional resource to dispose, override and call base.DisposeAsync.
  106. public virtual UniTask DisposeAsync()
  107. {
  108. TaskTracker.RemoveTracking(this);
  109. if (enumerator != null)
  110. {
  111. return enumerator.DisposeAsync();
  112. }
  113. return default;
  114. }
  115. }
  116. internal abstract class AsyncEnumeratorAwaitSelectorBase<TSource, TResult, TAwait> : MoveNextSource, IUniTaskAsyncEnumerator<TResult>
  117. {
  118. static readonly Action<object> moveNextCallbackDelegate = MoveNextCallBack;
  119. static readonly Action<object> setCurrentCallbackDelegate = SetCurrentCallBack;
  120. readonly IUniTaskAsyncEnumerable<TSource> source;
  121. protected CancellationToken cancellationToken;
  122. IUniTaskAsyncEnumerator<TSource> enumerator;
  123. UniTask<bool>.Awaiter sourceMoveNext;
  124. UniTask<TAwait>.Awaiter resultAwaiter;
  125. public AsyncEnumeratorAwaitSelectorBase(IUniTaskAsyncEnumerable<TSource> source, CancellationToken cancellationToken)
  126. {
  127. this.source = source;
  128. this.cancellationToken = cancellationToken;
  129. TaskTracker.TrackActiveTask(this, 4);
  130. }
  131. // abstract
  132. protected abstract UniTask<TAwait> TransformAsync(TSource sourceCurrent);
  133. protected abstract bool TrySetCurrentCore(TAwait awaitResult, out bool terminateIteration);
  134. // Util
  135. protected TSource SourceCurrent { get; private set; }
  136. protected (bool waitCallback, bool requireNextIteration) ActionCompleted(bool trySetCurrentResult, out bool moveNextResult)
  137. {
  138. if (trySetCurrentResult)
  139. {
  140. moveNextResult = true;
  141. return (false, false);
  142. }
  143. else
  144. {
  145. moveNextResult = default;
  146. return (false, true);
  147. }
  148. }
  149. protected (bool waitCallback, bool requireNextIteration) WaitAwaitCallback(out bool moveNextResult) { moveNextResult = default; return (true, false); }
  150. protected (bool waitCallback, bool requireNextIteration) IterateFinished(out bool moveNextResult) { moveNextResult = false; return (false, false); }
  151. // IUniTaskAsyncEnumerator<T>
  152. public TResult Current { get; protected set; }
  153. public UniTask<bool> MoveNextAsync()
  154. {
  155. if (enumerator == null)
  156. {
  157. enumerator = source.GetAsyncEnumerator(cancellationToken);
  158. }
  159. completionSource.Reset();
  160. SourceMoveNext();
  161. return new UniTask<bool>(this, completionSource.Version);
  162. }
  163. protected void SourceMoveNext()
  164. {
  165. CONTINUE:
  166. sourceMoveNext = enumerator.MoveNextAsync().GetAwaiter();
  167. if (sourceMoveNext.IsCompleted)
  168. {
  169. bool result = false;
  170. try
  171. {
  172. (bool waitCallback, bool requireNextIteration) = TryMoveNextCore(sourceMoveNext.GetResult(), out result);
  173. if (waitCallback)
  174. {
  175. return;
  176. }
  177. if (requireNextIteration)
  178. {
  179. goto CONTINUE;
  180. }
  181. else
  182. {
  183. completionSource.TrySetResult(result);
  184. }
  185. }
  186. catch (Exception ex)
  187. {
  188. completionSource.TrySetException(ex);
  189. return;
  190. }
  191. }
  192. else
  193. {
  194. sourceMoveNext.SourceOnCompleted(moveNextCallbackDelegate, this);
  195. }
  196. }
  197. (bool waitCallback, bool requireNextIteration) TryMoveNextCore(bool sourceHasCurrent, out bool result)
  198. {
  199. if (sourceHasCurrent)
  200. {
  201. SourceCurrent = enumerator.Current;
  202. var task = TransformAsync(SourceCurrent);
  203. if (UnwarapTask(task, out var taskResult))
  204. {
  205. var currentResult = TrySetCurrentCore(taskResult, out var terminateIteration);
  206. if (terminateIteration)
  207. {
  208. return IterateFinished(out result);
  209. }
  210. return ActionCompleted(currentResult, out result);
  211. }
  212. else
  213. {
  214. return WaitAwaitCallback(out result);
  215. }
  216. }
  217. return IterateFinished(out result);
  218. }
  219. protected bool UnwarapTask(UniTask<TAwait> taskResult, out TAwait result)
  220. {
  221. resultAwaiter = taskResult.GetAwaiter();
  222. if (resultAwaiter.IsCompleted)
  223. {
  224. result = resultAwaiter.GetResult();
  225. return true;
  226. }
  227. else
  228. {
  229. resultAwaiter.SourceOnCompleted(setCurrentCallbackDelegate, this);
  230. result = default;
  231. return false;
  232. }
  233. }
  234. static void MoveNextCallBack(object state)
  235. {
  236. var self = (AsyncEnumeratorAwaitSelectorBase<TSource, TResult, TAwait>)state;
  237. bool result = false;
  238. try
  239. {
  240. (bool waitCallback, bool requireNextIteration) = self.TryMoveNextCore(self.sourceMoveNext.GetResult(), out result);
  241. if (waitCallback)
  242. {
  243. return;
  244. }
  245. if (requireNextIteration)
  246. {
  247. self.SourceMoveNext();
  248. return;
  249. }
  250. else
  251. {
  252. self.completionSource.TrySetResult(result);
  253. }
  254. }
  255. catch (Exception ex)
  256. {
  257. self.completionSource.TrySetException(ex);
  258. return;
  259. }
  260. }
  261. static void SetCurrentCallBack(object state)
  262. {
  263. var self = (AsyncEnumeratorAwaitSelectorBase<TSource, TResult, TAwait>)state;
  264. bool doneSetCurrent;
  265. bool terminateIteration;
  266. try
  267. {
  268. var result = self.resultAwaiter.GetResult();
  269. doneSetCurrent = self.TrySetCurrentCore(result, out terminateIteration);
  270. }
  271. catch (Exception ex)
  272. {
  273. self.completionSource.TrySetException(ex);
  274. return;
  275. }
  276. if (self.cancellationToken.IsCancellationRequested)
  277. {
  278. self.completionSource.TrySetCanceled(self.cancellationToken);
  279. }
  280. else
  281. {
  282. if (doneSetCurrent)
  283. {
  284. self.completionSource.TrySetResult(true);
  285. }
  286. else
  287. {
  288. if (terminateIteration)
  289. {
  290. self.completionSource.TrySetResult(false);
  291. }
  292. else
  293. {
  294. self.SourceMoveNext();
  295. }
  296. }
  297. }
  298. }
  299. // if require additional resource to dispose, override and call base.DisposeAsync.
  300. public virtual UniTask DisposeAsync()
  301. {
  302. TaskTracker.RemoveTracking(this);
  303. if (enumerator != null)
  304. {
  305. return enumerator.DisposeAsync();
  306. }
  307. return default;
  308. }
  309. }
  310. }