AsyncManager.ContinuationProcessorGroup.ContinuationProcessor.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. using System;
  2. using System.Runtime.CompilerServices;
  3. using UnityEngine;
  4. namespace UnityAsync
  5. {
  6. public partial class AsyncManager
  7. {
  8. partial class ContinuationProcessorGroup
  9. {
  10. const int MaxQueueSize = 1 << 16;
  11. class ContinuationProcessor<T> : IContinuationProcessor where T : IAwaitInstructionAwaiter
  12. {
  13. public static ContinuationProcessor<T> instance;
  14. T[] currentQueue, futureQueue;
  15. int currentCount, currentIndex, futureCount, maxIndex;
  16. public ContinuationProcessor(int capacity)
  17. {
  18. AssertQueueSize(capacity);
  19. maxIndex = capacity - 1;
  20. currentQueue = new T[capacity];
  21. futureQueue = new T[capacity];
  22. }
  23. public void Process()
  24. {
  25. currentCount = futureCount;
  26. futureCount = 0;
  27. // swap queues
  28. var tmp = currentQueue;
  29. currentQueue = futureQueue;
  30. futureQueue = tmp;
  31. for(; currentIndex < currentCount; ++currentIndex)
  32. {
  33. ref var c = ref currentQueue[currentIndex];
  34. if(!c.Evaluate())
  35. {
  36. // this is hottest path so we don't do a bounds check here (see Add)
  37. futureQueue[futureCount] = c;
  38. ++futureCount;
  39. }
  40. }
  41. currentIndex = 0;
  42. currentCount = 0;
  43. Array.Clear(currentQueue, 0, currentCount);
  44. }
  45. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  46. public void Add(in T cont)
  47. {
  48. if(InUnityContext)
  49. AddFast(cont);
  50. else
  51. AddThreadSafe(cont);
  52. }
  53. // only call in UnitySynchronizationContext - not thread safe
  54. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  55. void AddFast(in T cont)
  56. {
  57. try
  58. {
  59. // we might have awaiters yet to be processed this frame; when they are re-added we skip any
  60. // bounds checks as an optimisation, so we need to make sure the queue has enough space to
  61. // re-add them all in case none of them have finished
  62. int potentialFutureCount = futureCount + currentCount - currentIndex;
  63. if(potentialFutureCount >= maxIndex)
  64. {
  65. AssertQueueSize(potentialFutureCount + 1);
  66. int newQueueSize = Math.Min(MaxQueueSize, Math.Max(potentialFutureCount, futureQueue.Length * 3 / 2));
  67. Array.Resize(ref futureQueue, newQueueSize);
  68. Array.Resize(ref currentQueue, newQueueSize);
  69. maxIndex = newQueueSize - 1;
  70. }
  71. futureQueue[futureCount] = cont;
  72. ++futureCount;
  73. }
  74. catch(Exception e)
  75. {
  76. Debug.LogException(e);
  77. }
  78. }
  79. // use when you may be adding an awaiter from outside of UnitySynchronizationContext
  80. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  81. async void AddThreadSafe(T cont)
  82. {
  83. await UnitySyncContext;
  84. AddFast(cont);
  85. }
  86. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  87. static void AssertQueueSize(int queueSize)
  88. {
  89. if(queueSize > MaxQueueSize)
  90. throw new InvalidOperationException($"Cannot exceed queue size of {MaxQueueSize}.");
  91. }
  92. }
  93. }
  94. }
  95. }