KProcessScheduler.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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 bool Active { get; set; }
  15. public SchedulerThread(KThread Thread)
  16. {
  17. this.Thread = Thread;
  18. WaitEvent = new AutoResetEvent(false);
  19. Active = true;
  20. }
  21. public void Dispose()
  22. {
  23. Dispose(true);
  24. }
  25. protected virtual void Dispose(bool Disposing)
  26. {
  27. if (Disposing)
  28. {
  29. WaitEvent.Dispose();
  30. }
  31. }
  32. }
  33. private class ThreadQueue
  34. {
  35. private List<SchedulerThread> Threads;
  36. public ThreadQueue()
  37. {
  38. Threads = new List<SchedulerThread>();
  39. }
  40. public void Push(SchedulerThread Thread)
  41. {
  42. lock (Threads)
  43. {
  44. Threads.Add(Thread);
  45. }
  46. }
  47. public SchedulerThread Pop(int MinPriority = LowestPriority)
  48. {
  49. lock (Threads)
  50. {
  51. SchedulerThread SchedThread;
  52. int HighestPriority = MinPriority;
  53. int HighestPrioIndex = -1;
  54. for (int Index = 0; Index < Threads.Count; Index++)
  55. {
  56. SchedThread = Threads[Index];
  57. if (HighestPriority > SchedThread.Thread.Priority)
  58. {
  59. HighestPriority = SchedThread.Thread.Priority;
  60. HighestPrioIndex = Index;
  61. }
  62. }
  63. if (HighestPrioIndex == -1)
  64. {
  65. return null;
  66. }
  67. SchedThread = Threads[HighestPrioIndex];
  68. Threads.RemoveAt(HighestPrioIndex);
  69. return SchedThread;
  70. }
  71. }
  72. public bool HasThread(SchedulerThread SchedThread)
  73. {
  74. lock (Threads)
  75. {
  76. return Threads.Contains(SchedThread);
  77. }
  78. }
  79. public bool Remove(SchedulerThread SchedThread)
  80. {
  81. lock (Threads)
  82. {
  83. return Threads.Remove(SchedThread);
  84. }
  85. }
  86. }
  87. private ConcurrentDictionary<KThread, SchedulerThread> AllThreads;
  88. private ThreadQueue[] WaitingToRun;
  89. private HashSet<int> ActiveProcessors;
  90. private object SchedLock;
  91. public KProcessScheduler()
  92. {
  93. AllThreads = new ConcurrentDictionary<KThread, SchedulerThread>();
  94. WaitingToRun = new ThreadQueue[4];
  95. for (int Index = 0; Index < 4; Index++)
  96. {
  97. WaitingToRun[Index] = new ThreadQueue();
  98. }
  99. ActiveProcessors = new HashSet<int>();
  100. SchedLock = new object();
  101. }
  102. public void StartThread(KThread Thread)
  103. {
  104. lock (SchedLock)
  105. {
  106. SchedulerThread SchedThread = new SchedulerThread(Thread);
  107. if (!AllThreads.TryAdd(Thread, SchedThread))
  108. {
  109. return;
  110. }
  111. if (ActiveProcessors.Add(Thread.ProcessorId))
  112. {
  113. Thread.Thread.Execute();
  114. PrintDbgThreadInfo(Thread, "running.");
  115. }
  116. else
  117. {
  118. WaitingToRun[Thread.ProcessorId].Push(SchedThread);
  119. PrintDbgThreadInfo(Thread, "waiting to run.");
  120. }
  121. }
  122. }
  123. public void RemoveThread(KThread Thread)
  124. {
  125. PrintDbgThreadInfo(Thread, "exited.");
  126. lock (SchedLock)
  127. {
  128. if (AllThreads.TryRemove(Thread, out SchedulerThread SchedThread))
  129. {
  130. WaitingToRun[Thread.ProcessorId].Remove(SchedThread);
  131. SchedThread.Dispose();
  132. }
  133. SchedulerThread NewThread = WaitingToRun[Thread.ProcessorId].Pop();
  134. if (NewThread == null)
  135. {
  136. Logging.Debug(LogClass.KernelScheduler, $"Nothing to run on core {Thread.ProcessorId}!");
  137. ActiveProcessors.Remove(Thread.ProcessorId);
  138. return;
  139. }
  140. RunThread(NewThread);
  141. }
  142. }
  143. public void SetThreadActivity(KThread Thread, bool Active)
  144. {
  145. if (!AllThreads.TryGetValue(Thread, out SchedulerThread SchedThread))
  146. {
  147. throw new InvalidOperationException();
  148. }
  149. lock (SchedLock)
  150. {
  151. bool OldState = SchedThread.Active;
  152. SchedThread.Active = Active;
  153. if (!OldState && Active)
  154. {
  155. if (ActiveProcessors.Add(Thread.ProcessorId))
  156. {
  157. RunThread(SchedThread);
  158. }
  159. else
  160. {
  161. WaitingToRun[Thread.ProcessorId].Push(SchedThread);
  162. PrintDbgThreadInfo(Thread, "entering wait state...");
  163. }
  164. }
  165. else if (OldState && !Active)
  166. {
  167. if (Thread.Thread.IsCurrentThread())
  168. {
  169. Suspend(Thread.ProcessorId);
  170. PrintDbgThreadInfo(Thread, "entering inactive wait state...");
  171. }
  172. else
  173. {
  174. WaitingToRun[Thread.ProcessorId].Remove(SchedThread);
  175. }
  176. }
  177. }
  178. if (!Active && Thread.Thread.IsCurrentThread())
  179. {
  180. SchedThread.WaitEvent.WaitOne();
  181. PrintDbgThreadInfo(Thread, "resuming execution...");
  182. }
  183. }
  184. public void Suspend(int ProcessorId)
  185. {
  186. lock (SchedLock)
  187. {
  188. SchedulerThread SchedThread = WaitingToRun[ProcessorId].Pop();
  189. if (SchedThread != null)
  190. {
  191. RunThread(SchedThread);
  192. }
  193. else
  194. {
  195. Logging.Debug(LogClass.KernelScheduler, $"Nothing to run on core {ProcessorId}!");
  196. ActiveProcessors.Remove(ProcessorId);
  197. }
  198. }
  199. }
  200. public void Yield(KThread Thread)
  201. {
  202. PrintDbgThreadInfo(Thread, "yielded execution.");
  203. lock (SchedLock)
  204. {
  205. SchedulerThread SchedThread = WaitingToRun[Thread.ProcessorId].Pop(Thread.Priority);
  206. if (SchedThread == null)
  207. {
  208. PrintDbgThreadInfo(Thread, "resumed because theres nothing better to run.");
  209. return;
  210. }
  211. RunThread(SchedThread);
  212. }
  213. Resume(Thread);
  214. }
  215. public void Resume(KThread Thread)
  216. {
  217. if (!AllThreads.TryGetValue(Thread, out SchedulerThread SchedThread))
  218. {
  219. throw new InvalidOperationException();
  220. }
  221. TryResumingExecution(SchedThread);
  222. }
  223. private void TryResumingExecution(SchedulerThread SchedThread)
  224. {
  225. KThread Thread = SchedThread.Thread;
  226. if (SchedThread.Active)
  227. {
  228. lock (SchedLock)
  229. {
  230. if (ActiveProcessors.Add(Thread.ProcessorId))
  231. {
  232. PrintDbgThreadInfo(Thread, "resuming execution...");
  233. return;
  234. }
  235. WaitingToRun[Thread.ProcessorId].Push(SchedThread);
  236. PrintDbgThreadInfo(Thread, "entering wait state...");
  237. }
  238. }
  239. else
  240. {
  241. PrintDbgThreadInfo(Thread, "entering inactive wait state...");
  242. }
  243. SchedThread.WaitEvent.WaitOne();
  244. PrintDbgThreadInfo(Thread, "resuming execution...");
  245. }
  246. private void RunThread(SchedulerThread SchedThread)
  247. {
  248. if (!SchedThread.Thread.Thread.Execute())
  249. {
  250. SchedThread.WaitEvent.Set();
  251. }
  252. else
  253. {
  254. PrintDbgThreadInfo(SchedThread.Thread, "running.");
  255. }
  256. }
  257. private void PrintDbgThreadInfo(KThread Thread, string Message)
  258. {
  259. Logging.Debug(LogClass.KernelScheduler, "(" +
  260. "ThreadId: " + Thread.ThreadId + ", " +
  261. "ProcessorId: " + Thread.ProcessorId + ", " +
  262. "Priority: " + Thread.Priority + ") " + Message);
  263. }
  264. public void Dispose()
  265. {
  266. Dispose(true);
  267. }
  268. protected virtual void Dispose(bool Disposing)
  269. {
  270. if (Disposing)
  271. {
  272. foreach (SchedulerThread SchedThread in AllThreads.Values)
  273. {
  274. SchedThread.Dispose();
  275. }
  276. }
  277. }
  278. }
  279. }