Loom.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. using UnityEngine;
  2. using System.Collections.Generic;
  3. using System;
  4. using System.Threading;
  5. using System.Linq;
  6. namespace EZXR.Glass.Core.Threading
  7. {
  8. public class Loom : MonoBehaviour
  9. {
  10. public static int maxThreads = 8;
  11. static int numThreads;
  12. private static Loom _current;
  13. //private int _count;
  14. public static Loom Current
  15. {
  16. get
  17. {
  18. Initialize();
  19. return _current;
  20. }
  21. }
  22. void Awake()
  23. {
  24. _current = this;
  25. initialized = true;
  26. }
  27. static bool initialized;
  28. // [RuntimeInitializeOnLoadMethod]
  29. public static void Initialize()
  30. {
  31. if (!initialized)
  32. {
  33. if (!Application.isPlaying)
  34. return;
  35. initialized = true;
  36. var g = new GameObject("Loom");
  37. _current = g.AddComponent<Loom>();
  38. #if !ARTIST_BUILD
  39. UnityEngine.Object.DontDestroyOnLoad(g);
  40. #endif
  41. }
  42. }
  43. public struct NoDelayedQueueItem
  44. {
  45. public Action<object> action;
  46. public object param;
  47. }
  48. private List<NoDelayedQueueItem> _actions = new List<NoDelayedQueueItem>();
  49. public struct DelayedQueueItem
  50. {
  51. public float time;
  52. public Action<object> action;
  53. public object param;
  54. }
  55. private List<DelayedQueueItem> _delayed = new List<DelayedQueueItem>();
  56. List<DelayedQueueItem> _currentDelayed = new List<DelayedQueueItem>();
  57. public static void QueueOnMainThread(Action<object> taction, object tparam)
  58. {
  59. QueueOnMainThread(taction, tparam, 0f);
  60. }
  61. public static void QueueOnMainThread(Action<object> taction, object tparam, float time)
  62. {
  63. if (time != 0)
  64. {
  65. lock (Current._delayed)
  66. {
  67. Current._delayed.Add(new DelayedQueueItem { time = Time.time + time, action = taction, param = tparam });
  68. }
  69. }
  70. else
  71. {
  72. lock (Current._actions)
  73. {
  74. Current._actions.Add(new NoDelayedQueueItem { action = taction, param = tparam });
  75. }
  76. }
  77. }
  78. public static Thread RunAsync(Action a)
  79. {
  80. Initialize();
  81. while (numThreads >= maxThreads)
  82. {
  83. Thread.Sleep(100);
  84. }
  85. Interlocked.Increment(ref numThreads);
  86. ThreadPool.QueueUserWorkItem(RunAction, a);
  87. return null;
  88. }
  89. private static void RunAction(object action)
  90. {
  91. try
  92. {
  93. ((Action)action)();
  94. }
  95. catch
  96. {
  97. }
  98. finally
  99. {
  100. Interlocked.Decrement(ref numThreads);
  101. }
  102. }
  103. void OnDisable()
  104. {
  105. if (_current == this)
  106. {
  107. _current = null;
  108. }
  109. }
  110. // Use this for initialization
  111. void Start()
  112. {
  113. }
  114. List<NoDelayedQueueItem> _currentActions = new List<NoDelayedQueueItem>();
  115. // Update is called once per frame
  116. void Update()
  117. {
  118. if (_actions.Count > 0)
  119. {
  120. lock (_actions)
  121. {
  122. _currentActions.Clear();
  123. _currentActions.AddRange(_actions);
  124. _actions.Clear();
  125. }
  126. for (int i = 0; i < _currentActions.Count; i++)
  127. {
  128. Debug.Log("Loom action begin");
  129. _currentActions[i].action(_currentActions[i].param);
  130. Debug.Log("Loom action end");
  131. }
  132. }
  133. if (_delayed.Count > 0)
  134. {
  135. lock (_delayed)
  136. {
  137. _currentDelayed.Clear();
  138. _currentDelayed.AddRange(_delayed.Where(d => d.time <= Time.time));
  139. for (int i = 0; i < _currentDelayed.Count; i++)
  140. {
  141. _delayed.Remove(_currentDelayed[i]);
  142. }
  143. }
  144. for (int i = 0; i < _currentDelayed.Count; i++)
  145. {
  146. _currentDelayed[i].action(_currentDelayed[i].param);
  147. }
  148. }
  149. }
  150. }
  151. }