KProcessScheduler.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. using System;
  2. using System.Collections.Concurrent;
  3. using System.Collections.Generic;
  4. using System.Threading;
  5. namespace Ryujinx.Core.OsHle.Handles
  6. {
  7. class KProcessScheduler : IDisposable
  8. {
  9. private const int LowestPriority = 0x40;
  10. private class SchedulerThread : IDisposable
  11. {
  12. public KThread Thread { get; private set; }
  13. public AutoResetEvent WaitEvent { get; private set; }
  14. public SchedulerThread(KThread Thread)
  15. {
  16. this.Thread = Thread;
  17. WaitEvent = new AutoResetEvent(false);
  18. }
  19. public void Dispose()
  20. {
  21. Dispose(true);
  22. }
  23. protected virtual void Dispose(bool Disposing)
  24. {
  25. if (Disposing)
  26. {
  27. WaitEvent.Dispose();
  28. }
  29. }
  30. }
  31. private class ThreadQueue
  32. {
  33. private List<SchedulerThread> Threads;
  34. public ThreadQueue()
  35. {
  36. Threads = new List<SchedulerThread>();
  37. }
  38. public void Push(SchedulerThread Thread)
  39. {
  40. lock (Threads)
  41. {
  42. Threads.Add(Thread);
  43. }
  44. }
  45. public SchedulerThread Pop(int MinPriority = LowestPriority)
  46. {
  47. lock (Threads)
  48. {
  49. SchedulerThread SchedThread;
  50. int HighestPriority = MinPriority;
  51. int HighestPrioIndex = -1;
  52. for (int Index = 0; Index < Threads.Count; Index++)
  53. {
  54. SchedThread = Threads[Index];
  55. if (HighestPriority > SchedThread.Thread.Priority)
  56. {
  57. HighestPriority = SchedThread.Thread.Priority;
  58. HighestPrioIndex = Index;
  59. }
  60. }
  61. if (HighestPrioIndex == -1)
  62. {
  63. return null;
  64. }
  65. SchedThread = Threads[HighestPrioIndex];
  66. Threads.RemoveAt(HighestPrioIndex);
  67. return SchedThread;
  68. }
  69. }
  70. public bool HasThread(SchedulerThread SchedThread)
  71. {
  72. lock (Threads)
  73. {
  74. return Threads.Contains(SchedThread);
  75. }
  76. }
  77. public bool Remove(SchedulerThread SchedThread)
  78. {
  79. return Threads.Remove(SchedThread);
  80. }
  81. }
  82. private ConcurrentDictionary<KThread, SchedulerThread> AllThreads;
  83. private ThreadQueue[] WaitingToRun;
  84. private HashSet<int> ActiveProcessors;
  85. private object SchedLock;
  86. public KProcessScheduler()
  87. {
  88. AllThreads = new ConcurrentDictionary<KThread, SchedulerThread>();
  89. WaitingToRun = new ThreadQueue[4];
  90. for (int Index = 0; Index < 4; Index++)
  91. {
  92. WaitingToRun[Index] = new ThreadQueue();
  93. }
  94. ActiveProcessors = new HashSet<int>();
  95. SchedLock = new object();
  96. }
  97. public void StartThread(KThread Thread)
  98. {
  99. lock (SchedLock)
  100. {
  101. SchedulerThread SchedThread = new SchedulerThread(Thread);
  102. if (!AllThreads.TryAdd(Thread, SchedThread))
  103. {
  104. return;
  105. }
  106. if (ActiveProcessors.Add(Thread.ProcessorId))
  107. {
  108. Thread.Thread.Execute();
  109. PrintDbgThreadInfo(Thread, "running.");
  110. }
  111. else
  112. {
  113. WaitingToRun[Thread.ProcessorId].Push(SchedThread);
  114. PrintDbgThreadInfo(Thread, "waiting to run.");
  115. }
  116. }
  117. }
  118. public void RemoveThread(KThread Thread)
  119. {
  120. PrintDbgThreadInfo(Thread, "exited.");
  121. lock (SchedLock)
  122. {
  123. if (AllThreads.TryRemove(Thread, out SchedulerThread SchedThread))
  124. {
  125. WaitingToRun[Thread.ProcessorId].Remove(SchedThread);
  126. }
  127. SchedulerThread NewThread = WaitingToRun[Thread.ProcessorId].Pop();
  128. if (NewThread == null)
  129. {
  130. Logging.Debug(LogClass.KernelScheduler, $"Nothing to run on core {Thread.ProcessorId}!");
  131. ActiveProcessors.Remove(Thread.ProcessorId);
  132. return;
  133. }
  134. RunThread(NewThread);
  135. }
  136. }
  137. public void Suspend(int ProcessorId)
  138. {
  139. lock (SchedLock)
  140. {
  141. SchedulerThread SchedThread = WaitingToRun[ProcessorId].Pop();
  142. if (SchedThread != null)
  143. {
  144. RunThread(SchedThread);
  145. }
  146. else
  147. {
  148. Logging.Debug(LogClass.KernelScheduler, $"Nothing to run on core {ProcessorId}!");
  149. ActiveProcessors.Remove(ProcessorId);
  150. }
  151. }
  152. }
  153. public void Yield(KThread Thread)
  154. {
  155. PrintDbgThreadInfo(Thread, "yielded execution.");
  156. lock (SchedLock)
  157. {
  158. SchedulerThread SchedThread = WaitingToRun[Thread.ProcessorId].Pop(Thread.Priority);
  159. if (SchedThread == null)
  160. {
  161. PrintDbgThreadInfo(Thread, "resumed because theres nothing better to run.");
  162. return;
  163. }
  164. RunThread(SchedThread);
  165. }
  166. Resume(Thread);
  167. }
  168. public void Resume(KThread Thread)
  169. {
  170. SchedulerThread SchedThread;
  171. if (!AllThreads.TryGetValue(Thread, out SchedThread))
  172. {
  173. throw new InvalidOperationException();
  174. }
  175. TryResumingExecution(SchedThread);
  176. }
  177. private void TryResumingExecution(SchedulerThread SchedThread)
  178. {
  179. KThread Thread = SchedThread.Thread;
  180. lock (SchedLock)
  181. {
  182. if (ActiveProcessors.Add(Thread.ProcessorId))
  183. {
  184. PrintDbgThreadInfo(Thread, "resuming execution...");
  185. return;
  186. }
  187. PrintDbgThreadInfo(Thread, "entering wait state...");
  188. WaitingToRun[Thread.ProcessorId].Push(SchedThread);
  189. }
  190. SchedThread.WaitEvent.WaitOne();
  191. PrintDbgThreadInfo(Thread, "resuming execution...");
  192. }
  193. private void RunThread(SchedulerThread SchedThread)
  194. {
  195. if (!SchedThread.Thread.Thread.Execute())
  196. {
  197. SchedThread.WaitEvent.Set();
  198. }
  199. else
  200. {
  201. PrintDbgThreadInfo(SchedThread.Thread, "running.");
  202. }
  203. }
  204. private void PrintDbgThreadInfo(KThread Thread, string Message)
  205. {
  206. Logging.Debug(LogClass.KernelScheduler, "(" +
  207. "ThreadId: " + Thread.ThreadId + ", " +
  208. "ProcessorId: " + Thread.ProcessorId + ", " +
  209. "Priority: " + Thread.Priority + ") " + Message);
  210. }
  211. public void Dispose()
  212. {
  213. Dispose(true);
  214. }
  215. protected virtual void Dispose(bool Disposing)
  216. {
  217. if (Disposing)
  218. {
  219. foreach (SchedulerThread SchedThread in AllThreads.Values)
  220. {
  221. SchedThread.Dispose();
  222. }
  223. }
  224. }
  225. }
  226. }