UniTaskSynchronizationContext.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. using System;
  2. using System.Runtime.InteropServices;
  3. using System.Threading;
  4. namespace Cysharp.Threading.Tasks
  5. {
  6. public class UniTaskSynchronizationContext : SynchronizationContext
  7. {
  8. const int MaxArrayLength = 0X7FEFFFFF;
  9. const int InitialSize = 16;
  10. static SpinLock gate = new SpinLock(false);
  11. static bool dequing = false;
  12. static int actionListCount = 0;
  13. static Callback[] actionList = new Callback[InitialSize];
  14. static int waitingListCount = 0;
  15. static Callback[] waitingList = new Callback[InitialSize];
  16. static int opCount;
  17. public override void Send(SendOrPostCallback d, object state)
  18. {
  19. d(state);
  20. }
  21. public override void Post(SendOrPostCallback d, object state)
  22. {
  23. bool lockTaken = false;
  24. try
  25. {
  26. gate.Enter(ref lockTaken);
  27. if (dequing)
  28. {
  29. // Ensure Capacity
  30. if (waitingList.Length == waitingListCount)
  31. {
  32. var newLength = waitingListCount * 2;
  33. if ((uint)newLength > MaxArrayLength) newLength = MaxArrayLength;
  34. var newArray = new Callback[newLength];
  35. Array.Copy(waitingList, newArray, waitingListCount);
  36. waitingList = newArray;
  37. }
  38. waitingList[waitingListCount] = new Callback(d, state);
  39. waitingListCount++;
  40. }
  41. else
  42. {
  43. // Ensure Capacity
  44. if (actionList.Length == actionListCount)
  45. {
  46. var newLength = actionListCount * 2;
  47. if ((uint)newLength > MaxArrayLength) newLength = MaxArrayLength;
  48. var newArray = new Callback[newLength];
  49. Array.Copy(actionList, newArray, actionListCount);
  50. actionList = newArray;
  51. }
  52. actionList[actionListCount] = new Callback(d, state);
  53. actionListCount++;
  54. }
  55. }
  56. finally
  57. {
  58. if (lockTaken) gate.Exit(false);
  59. }
  60. }
  61. public override void OperationStarted()
  62. {
  63. Interlocked.Increment(ref opCount);
  64. }
  65. public override void OperationCompleted()
  66. {
  67. Interlocked.Decrement(ref opCount);
  68. }
  69. public override SynchronizationContext CreateCopy()
  70. {
  71. return this;
  72. }
  73. // delegate entrypoint.
  74. internal static void Run()
  75. {
  76. {
  77. bool lockTaken = false;
  78. try
  79. {
  80. gate.Enter(ref lockTaken);
  81. if (actionListCount == 0) return;
  82. dequing = true;
  83. }
  84. finally
  85. {
  86. if (lockTaken) gate.Exit(false);
  87. }
  88. }
  89. for (int i = 0; i < actionListCount; i++)
  90. {
  91. var action = actionList[i];
  92. actionList[i] = default;
  93. action.Invoke();
  94. }
  95. {
  96. bool lockTaken = false;
  97. try
  98. {
  99. gate.Enter(ref lockTaken);
  100. dequing = false;
  101. var swapTempActionList = actionList;
  102. actionListCount = waitingListCount;
  103. actionList = waitingList;
  104. waitingListCount = 0;
  105. waitingList = swapTempActionList;
  106. }
  107. finally
  108. {
  109. if (lockTaken) gate.Exit(false);
  110. }
  111. }
  112. }
  113. [StructLayout(LayoutKind.Auto)]
  114. readonly struct Callback
  115. {
  116. readonly SendOrPostCallback callback;
  117. readonly object state;
  118. public Callback(SendOrPostCallback callback, object state)
  119. {
  120. this.callback = callback;
  121. this.state = state;
  122. }
  123. public void Invoke()
  124. {
  125. try
  126. {
  127. callback(state);
  128. }
  129. catch (Exception ex)
  130. {
  131. UnityEngine.Debug.LogException(ex);
  132. }
  133. }
  134. }
  135. }
  136. }