KProcessScheduler.cs 9.2 KB

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