PlayerLoopRunner.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. 
  2. using System;
  3. using UnityEngine;
  4. namespace Cysharp.Threading.Tasks.Internal
  5. {
  6. internal sealed class PlayerLoopRunner
  7. {
  8. const int InitialSize = 16;
  9. readonly PlayerLoopTiming timing;
  10. readonly object runningAndQueueLock = new object();
  11. readonly object arrayLock = new object();
  12. readonly Action<Exception> unhandledExceptionCallback;
  13. int tail = 0;
  14. bool running = false;
  15. IPlayerLoopItem[] loopItems = new IPlayerLoopItem[InitialSize];
  16. MinimumQueue<IPlayerLoopItem> waitQueue = new MinimumQueue<IPlayerLoopItem>(InitialSize);
  17. public PlayerLoopRunner(PlayerLoopTiming timing)
  18. {
  19. this.unhandledExceptionCallback = ex => Debug.LogException(ex);
  20. this.timing = timing;
  21. }
  22. public void AddAction(IPlayerLoopItem item)
  23. {
  24. lock (runningAndQueueLock)
  25. {
  26. if (running)
  27. {
  28. waitQueue.Enqueue(item);
  29. return;
  30. }
  31. }
  32. lock (arrayLock)
  33. {
  34. // Ensure Capacity
  35. if (loopItems.Length == tail)
  36. {
  37. Array.Resize(ref loopItems, checked(tail * 2));
  38. }
  39. loopItems[tail++] = item;
  40. }
  41. }
  42. public int Clear()
  43. {
  44. lock (arrayLock)
  45. {
  46. var rest = 0;
  47. for (var index = 0; index < loopItems.Length; index++)
  48. {
  49. if (loopItems[index] != null)
  50. {
  51. rest++;
  52. }
  53. loopItems[index] = null;
  54. }
  55. tail = 0;
  56. return rest;
  57. }
  58. }
  59. // delegate entrypoint.
  60. public void Run()
  61. {
  62. // for debugging, create named stacktrace.
  63. #if DEBUG
  64. switch (timing)
  65. {
  66. case PlayerLoopTiming.Initialization:
  67. Initialization();
  68. break;
  69. case PlayerLoopTiming.LastInitialization:
  70. LastInitialization();
  71. break;
  72. case PlayerLoopTiming.EarlyUpdate:
  73. EarlyUpdate();
  74. break;
  75. case PlayerLoopTiming.LastEarlyUpdate:
  76. LastEarlyUpdate();
  77. break;
  78. case PlayerLoopTiming.FixedUpdate:
  79. FixedUpdate();
  80. break;
  81. case PlayerLoopTiming.LastFixedUpdate:
  82. LastFixedUpdate();
  83. break;
  84. case PlayerLoopTiming.PreUpdate:
  85. PreUpdate();
  86. break;
  87. case PlayerLoopTiming.LastPreUpdate:
  88. LastPreUpdate();
  89. break;
  90. case PlayerLoopTiming.Update:
  91. Update();
  92. break;
  93. case PlayerLoopTiming.LastUpdate:
  94. LastUpdate();
  95. break;
  96. case PlayerLoopTiming.PreLateUpdate:
  97. PreLateUpdate();
  98. break;
  99. case PlayerLoopTiming.LastPreLateUpdate:
  100. LastPreLateUpdate();
  101. break;
  102. case PlayerLoopTiming.PostLateUpdate:
  103. PostLateUpdate();
  104. break;
  105. case PlayerLoopTiming.LastPostLateUpdate:
  106. LastPostLateUpdate();
  107. break;
  108. #if UNITY_2020_2_OR_NEWER
  109. case PlayerLoopTiming.TimeUpdate:
  110. TimeUpdate();
  111. break;
  112. case PlayerLoopTiming.LastTimeUpdate:
  113. LastTimeUpdate();
  114. break;
  115. #endif
  116. default:
  117. break;
  118. }
  119. #else
  120. RunCore();
  121. #endif
  122. }
  123. void Initialization() => RunCore();
  124. void LastInitialization() => RunCore();
  125. void EarlyUpdate() => RunCore();
  126. void LastEarlyUpdate() => RunCore();
  127. void FixedUpdate() => RunCore();
  128. void LastFixedUpdate() => RunCore();
  129. void PreUpdate() => RunCore();
  130. void LastPreUpdate() => RunCore();
  131. void Update() => RunCore();
  132. void LastUpdate() => RunCore();
  133. void PreLateUpdate() => RunCore();
  134. void LastPreLateUpdate() => RunCore();
  135. void PostLateUpdate() => RunCore();
  136. void LastPostLateUpdate() => RunCore();
  137. #if UNITY_2020_2_OR_NEWER
  138. void TimeUpdate() => RunCore();
  139. void LastTimeUpdate() => RunCore();
  140. #endif
  141. [System.Diagnostics.DebuggerHidden]
  142. void RunCore()
  143. {
  144. lock (runningAndQueueLock)
  145. {
  146. running = true;
  147. }
  148. lock (arrayLock)
  149. {
  150. var j = tail - 1;
  151. for (int i = 0; i < loopItems.Length; i++)
  152. {
  153. var action = loopItems[i];
  154. if (action != null)
  155. {
  156. try
  157. {
  158. if (!action.MoveNext())
  159. {
  160. loopItems[i] = null;
  161. }
  162. else
  163. {
  164. continue; // next i
  165. }
  166. }
  167. catch (Exception ex)
  168. {
  169. loopItems[i] = null;
  170. try
  171. {
  172. unhandledExceptionCallback(ex);
  173. }
  174. catch { }
  175. }
  176. }
  177. // find null, loop from tail
  178. while (i < j)
  179. {
  180. var fromTail = loopItems[j];
  181. if (fromTail != null)
  182. {
  183. try
  184. {
  185. if (!fromTail.MoveNext())
  186. {
  187. loopItems[j] = null;
  188. j--;
  189. continue; // next j
  190. }
  191. else
  192. {
  193. // swap
  194. loopItems[i] = fromTail;
  195. loopItems[j] = null;
  196. j--;
  197. goto NEXT_LOOP; // next i
  198. }
  199. }
  200. catch (Exception ex)
  201. {
  202. loopItems[j] = null;
  203. j--;
  204. try
  205. {
  206. unhandledExceptionCallback(ex);
  207. }
  208. catch { }
  209. continue; // next j
  210. }
  211. }
  212. else
  213. {
  214. j--;
  215. }
  216. }
  217. tail = i; // loop end
  218. break; // LOOP END
  219. NEXT_LOOP:
  220. continue;
  221. }
  222. lock (runningAndQueueLock)
  223. {
  224. running = false;
  225. while (waitQueue.Count != 0)
  226. {
  227. if (loopItems.Length == tail)
  228. {
  229. Array.Resize(ref loopItems, checked(tail * 2));
  230. }
  231. loopItems[tail++] = waitQueue.Dequeue();
  232. }
  233. }
  234. }
  235. }
  236. }
  237. }