123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260 |
-
- using System;
- using UnityEngine;
- namespace Cysharp.Threading.Tasks.Internal
- {
- internal sealed class PlayerLoopRunner
- {
- const int InitialSize = 16;
- readonly PlayerLoopTiming timing;
- readonly object runningAndQueueLock = new object();
- readonly object arrayLock = new object();
- readonly Action<Exception> unhandledExceptionCallback;
- int tail = 0;
- bool running = false;
- IPlayerLoopItem[] loopItems = new IPlayerLoopItem[InitialSize];
- MinimumQueue<IPlayerLoopItem> waitQueue = new MinimumQueue<IPlayerLoopItem>(InitialSize);
- public PlayerLoopRunner(PlayerLoopTiming timing)
- {
- this.unhandledExceptionCallback = ex => Debug.LogException(ex);
- this.timing = timing;
- }
- public void AddAction(IPlayerLoopItem item)
- {
- lock (runningAndQueueLock)
- {
- if (running)
- {
- waitQueue.Enqueue(item);
- return;
- }
- }
- lock (arrayLock)
- {
- // Ensure Capacity
- if (loopItems.Length == tail)
- {
- Array.Resize(ref loopItems, checked(tail * 2));
- }
- loopItems[tail++] = item;
- }
- }
- public int Clear()
- {
- lock (arrayLock)
- {
- var rest = 0;
- for (var index = 0; index < loopItems.Length; index++)
- {
- if (loopItems[index] != null)
- {
- rest++;
- }
- loopItems[index] = null;
- }
- tail = 0;
- return rest;
- }
- }
- // delegate entrypoint.
- public void Run()
- {
- // for debugging, create named stacktrace.
- #if DEBUG
- switch (timing)
- {
- case PlayerLoopTiming.Initialization:
- Initialization();
- break;
- case PlayerLoopTiming.LastInitialization:
- LastInitialization();
- break;
- case PlayerLoopTiming.EarlyUpdate:
- EarlyUpdate();
- break;
- case PlayerLoopTiming.LastEarlyUpdate:
- LastEarlyUpdate();
- break;
- case PlayerLoopTiming.FixedUpdate:
- FixedUpdate();
- break;
- case PlayerLoopTiming.LastFixedUpdate:
- LastFixedUpdate();
- break;
- case PlayerLoopTiming.PreUpdate:
- PreUpdate();
- break;
- case PlayerLoopTiming.LastPreUpdate:
- LastPreUpdate();
- break;
- case PlayerLoopTiming.Update:
- Update();
- break;
- case PlayerLoopTiming.LastUpdate:
- LastUpdate();
- break;
- case PlayerLoopTiming.PreLateUpdate:
- PreLateUpdate();
- break;
- case PlayerLoopTiming.LastPreLateUpdate:
- LastPreLateUpdate();
- break;
- case PlayerLoopTiming.PostLateUpdate:
- PostLateUpdate();
- break;
- case PlayerLoopTiming.LastPostLateUpdate:
- LastPostLateUpdate();
- break;
- #if UNITY_2020_2_OR_NEWER
- case PlayerLoopTiming.TimeUpdate:
- TimeUpdate();
- break;
- case PlayerLoopTiming.LastTimeUpdate:
- LastTimeUpdate();
- break;
- #endif
- default:
- break;
- }
- #else
- RunCore();
- #endif
- }
- void Initialization() => RunCore();
- void LastInitialization() => RunCore();
- void EarlyUpdate() => RunCore();
- void LastEarlyUpdate() => RunCore();
- void FixedUpdate() => RunCore();
- void LastFixedUpdate() => RunCore();
- void PreUpdate() => RunCore();
- void LastPreUpdate() => RunCore();
- void Update() => RunCore();
- void LastUpdate() => RunCore();
- void PreLateUpdate() => RunCore();
- void LastPreLateUpdate() => RunCore();
- void PostLateUpdate() => RunCore();
- void LastPostLateUpdate() => RunCore();
- #if UNITY_2020_2_OR_NEWER
- void TimeUpdate() => RunCore();
- void LastTimeUpdate() => RunCore();
- #endif
- [System.Diagnostics.DebuggerHidden]
- void RunCore()
- {
- lock (runningAndQueueLock)
- {
- running = true;
- }
- lock (arrayLock)
- {
- var j = tail - 1;
- for (int i = 0; i < loopItems.Length; i++)
- {
- var action = loopItems[i];
- if (action != null)
- {
- try
- {
- if (!action.MoveNext())
- {
- loopItems[i] = null;
- }
- else
- {
- continue; // next i
- }
- }
- catch (Exception ex)
- {
- loopItems[i] = null;
- try
- {
- unhandledExceptionCallback(ex);
- }
- catch { }
- }
- }
- // find null, loop from tail
- while (i < j)
- {
- var fromTail = loopItems[j];
- if (fromTail != null)
- {
- try
- {
- if (!fromTail.MoveNext())
- {
- loopItems[j] = null;
- j--;
- continue; // next j
- }
- else
- {
- // swap
- loopItems[i] = fromTail;
- loopItems[j] = null;
- j--;
- goto NEXT_LOOP; // next i
- }
- }
- catch (Exception ex)
- {
- loopItems[j] = null;
- j--;
- try
- {
- unhandledExceptionCallback(ex);
- }
- catch { }
- continue; // next j
- }
- }
- else
- {
- j--;
- }
- }
- tail = i; // loop end
- break; // LOOP END
- NEXT_LOOP:
- continue;
- }
- lock (runningAndQueueLock)
- {
- running = false;
- while (waitQueue.Count != 0)
- {
- if (loopItems.Length == tail)
- {
- Array.Resize(ref loopItems, checked(tail * 2));
- }
- loopItems[tail++] = waitQueue.Dequeue();
- }
- }
- }
- }
- }
- }
|