KPriorityQueue.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. using System.Collections.Generic;
  2. using System.Numerics;
  3. namespace Ryujinx.HLE.HOS.Kernel.Threading
  4. {
  5. class KPriorityQueue
  6. {
  7. private readonly LinkedList<KThread>[][] _scheduledThreadsPerPrioPerCore;
  8. private readonly LinkedList<KThread>[][] _suggestedThreadsPerPrioPerCore;
  9. private readonly long[] _scheduledPrioritiesPerCore;
  10. private readonly long[] _suggestedPrioritiesPerCore;
  11. public KPriorityQueue()
  12. {
  13. _suggestedThreadsPerPrioPerCore = new LinkedList<KThread>[KScheduler.PrioritiesCount][];
  14. _scheduledThreadsPerPrioPerCore = new LinkedList<KThread>[KScheduler.PrioritiesCount][];
  15. for (int prio = 0; prio < KScheduler.PrioritiesCount; prio++)
  16. {
  17. _suggestedThreadsPerPrioPerCore[prio] = new LinkedList<KThread>[KScheduler.CpuCoresCount];
  18. _scheduledThreadsPerPrioPerCore[prio] = new LinkedList<KThread>[KScheduler.CpuCoresCount];
  19. for (int core = 0; core < KScheduler.CpuCoresCount; core++)
  20. {
  21. _suggestedThreadsPerPrioPerCore[prio][core] = new LinkedList<KThread>();
  22. _scheduledThreadsPerPrioPerCore[prio][core] = new LinkedList<KThread>();
  23. }
  24. }
  25. _scheduledPrioritiesPerCore = new long[KScheduler.CpuCoresCount];
  26. _suggestedPrioritiesPerCore = new long[KScheduler.CpuCoresCount];
  27. }
  28. public readonly ref struct KThreadEnumerable
  29. {
  30. readonly LinkedList<KThread>[][] _listPerPrioPerCore;
  31. readonly long[] _prios;
  32. readonly int _core;
  33. public KThreadEnumerable(LinkedList<KThread>[][] listPerPrioPerCore, long[] prios, int core)
  34. {
  35. _listPerPrioPerCore = listPerPrioPerCore;
  36. _prios = prios;
  37. _core = core;
  38. }
  39. public Enumerator GetEnumerator()
  40. {
  41. return new Enumerator(_listPerPrioPerCore, _prios, _core);
  42. }
  43. public ref struct Enumerator
  44. {
  45. private readonly LinkedList<KThread>[][] _listPerPrioPerCore;
  46. private readonly int _core;
  47. private long _prioMask;
  48. private int _prio;
  49. private LinkedList<KThread> _list;
  50. private LinkedListNode<KThread> _node;
  51. public Enumerator(LinkedList<KThread>[][] listPerPrioPerCore, long[] prios, int core)
  52. {
  53. _listPerPrioPerCore = listPerPrioPerCore;
  54. _core = core;
  55. _prioMask = prios[core];
  56. _prio = BitOperations.TrailingZeroCount(_prioMask);
  57. _prioMask &= ~(1L << _prio);
  58. }
  59. public KThread Current => _node?.Value;
  60. public bool MoveNext()
  61. {
  62. _node = _node?.Next;
  63. if (_node == null)
  64. {
  65. if (!MoveNextListAndFirstNode())
  66. {
  67. return false;
  68. }
  69. }
  70. return _node != null;
  71. }
  72. private bool MoveNextListAndFirstNode()
  73. {
  74. if (_prio < KScheduler.PrioritiesCount)
  75. {
  76. _list = _listPerPrioPerCore[_prio][_core];
  77. _node = _list.First;
  78. _prio = BitOperations.TrailingZeroCount(_prioMask);
  79. _prioMask &= ~(1L << _prio);
  80. return true;
  81. }
  82. else
  83. {
  84. _list = null;
  85. _node = null;
  86. return false;
  87. }
  88. }
  89. }
  90. }
  91. public KThreadEnumerable ScheduledThreads(int core)
  92. {
  93. return new KThreadEnumerable(_scheduledThreadsPerPrioPerCore, _scheduledPrioritiesPerCore, core);
  94. }
  95. public KThreadEnumerable SuggestedThreads(int core)
  96. {
  97. return new KThreadEnumerable(_suggestedThreadsPerPrioPerCore, _suggestedPrioritiesPerCore, core);
  98. }
  99. public KThread ScheduledThreadsFirstOrDefault(int core)
  100. {
  101. return ScheduledThreadsElementAtOrDefault(core, 0);
  102. }
  103. public KThread ScheduledThreadsElementAtOrDefault(int core, int index)
  104. {
  105. int currentIndex = 0;
  106. foreach (var scheduledThread in ScheduledThreads(core))
  107. {
  108. if (currentIndex == index)
  109. {
  110. return scheduledThread;
  111. }
  112. else
  113. {
  114. currentIndex++;
  115. }
  116. }
  117. return null;
  118. }
  119. public KThread ScheduledThreadsWithDynamicPriorityFirstOrDefault(int core, int dynamicPriority)
  120. {
  121. foreach (var scheduledThread in ScheduledThreads(core))
  122. {
  123. if (scheduledThread.DynamicPriority == dynamicPriority)
  124. {
  125. return scheduledThread;
  126. }
  127. }
  128. return null;
  129. }
  130. public bool HasScheduledThreads(int core)
  131. {
  132. return ScheduledThreadsFirstOrDefault(core) != null;
  133. }
  134. public void TransferToCore(int prio, int dstCore, KThread thread)
  135. {
  136. int srcCore = thread.ActiveCore;
  137. if (srcCore == dstCore)
  138. {
  139. return;
  140. }
  141. thread.ActiveCore = dstCore;
  142. if (srcCore >= 0)
  143. {
  144. Unschedule(prio, srcCore, thread);
  145. }
  146. if (dstCore >= 0)
  147. {
  148. Unsuggest(prio, dstCore, thread);
  149. Schedule(prio, dstCore, thread);
  150. }
  151. if (srcCore >= 0)
  152. {
  153. Suggest(prio, srcCore, thread);
  154. }
  155. }
  156. public void Suggest(int prio, int core, KThread thread)
  157. {
  158. if (prio >= KScheduler.PrioritiesCount)
  159. {
  160. return;
  161. }
  162. thread.SiblingsPerCore[core] = SuggestedQueue(prio, core).AddFirst(thread);
  163. _suggestedPrioritiesPerCore[core] |= 1L << prio;
  164. }
  165. public void Unsuggest(int prio, int core, KThread thread)
  166. {
  167. if (prio >= KScheduler.PrioritiesCount)
  168. {
  169. return;
  170. }
  171. LinkedList<KThread> queue = SuggestedQueue(prio, core);
  172. queue.Remove(thread.SiblingsPerCore[core]);
  173. if (queue.First == null)
  174. {
  175. _suggestedPrioritiesPerCore[core] &= ~(1L << prio);
  176. }
  177. }
  178. public void Schedule(int prio, int core, KThread thread)
  179. {
  180. if (prio >= KScheduler.PrioritiesCount)
  181. {
  182. return;
  183. }
  184. thread.SiblingsPerCore[core] = ScheduledQueue(prio, core).AddLast(thread);
  185. _scheduledPrioritiesPerCore[core] |= 1L << prio;
  186. }
  187. public void SchedulePrepend(int prio, int core, KThread thread)
  188. {
  189. if (prio >= KScheduler.PrioritiesCount)
  190. {
  191. return;
  192. }
  193. thread.SiblingsPerCore[core] = ScheduledQueue(prio, core).AddFirst(thread);
  194. _scheduledPrioritiesPerCore[core] |= 1L << prio;
  195. }
  196. public KThread Reschedule(int prio, int core, KThread thread)
  197. {
  198. if (prio >= KScheduler.PrioritiesCount)
  199. {
  200. return null;
  201. }
  202. LinkedList<KThread> queue = ScheduledQueue(prio, core);
  203. queue.Remove(thread.SiblingsPerCore[core]);
  204. thread.SiblingsPerCore[core] = queue.AddLast(thread);
  205. return queue.First.Value;
  206. }
  207. public void Unschedule(int prio, int core, KThread thread)
  208. {
  209. if (prio >= KScheduler.PrioritiesCount)
  210. {
  211. return;
  212. }
  213. LinkedList<KThread> queue = ScheduledQueue(prio, core);
  214. queue.Remove(thread.SiblingsPerCore[core]);
  215. if (queue.First == null)
  216. {
  217. _scheduledPrioritiesPerCore[core] &= ~(1L << prio);
  218. }
  219. }
  220. private LinkedList<KThread> SuggestedQueue(int prio, int core)
  221. {
  222. return _suggestedThreadsPerPrioPerCore[prio][core];
  223. }
  224. private LinkedList<KThread> ScheduledQueue(int prio, int core)
  225. {
  226. return _scheduledThreadsPerPrioPerCore[prio][core];
  227. }
  228. }
  229. }