KProcessScheduler.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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. }
  78. private ConcurrentDictionary<KThread, SchedulerThread> AllThreads;
  79. private ThreadQueue[] WaitingToRun;
  80. private HashSet<int> ActiveProcessors;
  81. private object SchedLock;
  82. public KProcessScheduler()
  83. {
  84. AllThreads = new ConcurrentDictionary<KThread, SchedulerThread>();
  85. WaitingToRun = new ThreadQueue[4];
  86. for (int Index = 0; Index < 4; Index++)
  87. {
  88. WaitingToRun[Index] = new ThreadQueue();
  89. }
  90. ActiveProcessors = new HashSet<int>();
  91. SchedLock = new object();
  92. }
  93. public void StartThread(KThread Thread)
  94. {
  95. lock (SchedLock)
  96. {
  97. SchedulerThread SchedThread = new SchedulerThread(Thread);
  98. if (!AllThreads.TryAdd(Thread, SchedThread))
  99. {
  100. return;
  101. }
  102. if (ActiveProcessors.Add(Thread.ProcessorId))
  103. {
  104. Thread.Thread.Execute();
  105. PrintDbgThreadInfo(Thread, "running.");
  106. }
  107. else
  108. {
  109. WaitingToRun[Thread.ProcessorId].Push(SchedThread);
  110. PrintDbgThreadInfo(Thread, "waiting to run.");
  111. }
  112. }
  113. }
  114. public void RemoveThread(KThread Thread)
  115. {
  116. PrintDbgThreadInfo(Thread, "exited.");
  117. lock (SchedLock)
  118. {
  119. SchedulerThread NewThread = WaitingToRun[Thread.ProcessorId].Pop();
  120. if (NewThread == null)
  121. {
  122. Logging.Debug(LogClass.KernelScheduler, $"Nothing to run on core {Thread.ProcessorId}!");
  123. ActiveProcessors.Remove(Thread.ProcessorId);
  124. return;
  125. }
  126. RunThread(NewThread);
  127. }
  128. }
  129. public void Suspend(int ProcessorId)
  130. {
  131. lock (SchedLock)
  132. {
  133. SchedulerThread SchedThread = WaitingToRun[ProcessorId].Pop();
  134. if (SchedThread != null)
  135. {
  136. RunThread(SchedThread);
  137. }
  138. else
  139. {
  140. Logging.Debug(LogClass.KernelScheduler, $"Nothing to run on core {ProcessorId}!");
  141. ActiveProcessors.Remove(ProcessorId);
  142. }
  143. }
  144. }
  145. public void Yield(KThread Thread)
  146. {
  147. PrintDbgThreadInfo(Thread, "yielded execution.");
  148. lock (SchedLock)
  149. {
  150. SchedulerThread SchedThread = WaitingToRun[Thread.ProcessorId].Pop(Thread.Priority);
  151. if (SchedThread == null)
  152. {
  153. PrintDbgThreadInfo(Thread, "resumed because theres nothing better to run.");
  154. return;
  155. }
  156. RunThread(SchedThread);
  157. }
  158. Resume(Thread);
  159. }
  160. public void Resume(KThread Thread)
  161. {
  162. SchedulerThread SchedThread;
  163. if (!AllThreads.TryGetValue(Thread, out SchedThread))
  164. {
  165. throw new InvalidOperationException();
  166. }
  167. TryResumingExecution(SchedThread);
  168. }
  169. private void TryResumingExecution(SchedulerThread SchedThread)
  170. {
  171. KThread Thread = SchedThread.Thread;
  172. lock (SchedLock)
  173. {
  174. if (ActiveProcessors.Add(Thread.ProcessorId))
  175. {
  176. PrintDbgThreadInfo(Thread, "resuming execution...");
  177. return;
  178. }
  179. PrintDbgThreadInfo(Thread, "entering wait state...");
  180. WaitingToRun[Thread.ProcessorId].Push(SchedThread);
  181. }
  182. SchedThread.WaitEvent.WaitOne();
  183. PrintDbgThreadInfo(Thread, "resuming execution...");
  184. }
  185. private void RunThread(SchedulerThread SchedThread)
  186. {
  187. if (!SchedThread.Thread.Thread.Execute())
  188. {
  189. SchedThread.WaitEvent.Set();
  190. }
  191. else
  192. {
  193. PrintDbgThreadInfo(SchedThread.Thread, "running.");
  194. }
  195. }
  196. private void PrintDbgThreadInfo(KThread Thread, string Message)
  197. {
  198. Logging.Debug(LogClass.KernelScheduler, "(" +
  199. "ThreadId: " + Thread.ThreadId + ", " +
  200. "ProcessorId: " + Thread.ProcessorId + ", " +
  201. "Priority: " + Thread.Priority + ") " + Message);
  202. }
  203. public void Dispose()
  204. {
  205. Dispose(true);
  206. }
  207. protected virtual void Dispose(bool Disposing)
  208. {
  209. if (Disposing)
  210. {
  211. foreach (SchedulerThread SchedThread in AllThreads.Values)
  212. {
  213. SchedThread.Dispose();
  214. }
  215. }
  216. }
  217. }
  218. }