KProcessScheduler.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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. public class KProcessScheduler : IDisposable
  8. {
  9. private class SchedulerThread : IDisposable
  10. {
  11. public HThread Thread { get; private set; }
  12. public AutoResetEvent WaitEvent { get; private set; }
  13. public SchedulerThread(HThread 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<HThread, SchedulerThread> AllThreads;
  78. private ThreadQueue[] WaitingToRun;
  79. private HashSet<int> ActiveProcessors;
  80. private object SchedLock;
  81. public KProcessScheduler()
  82. {
  83. AllThreads = new ConcurrentDictionary<HThread, 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(HThread 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(HThread 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 void WaitForSignal(HThread 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;
  162. }
  163. }
  164. if (Timeout >= 0)
  165. {
  166. Logging.Debug($"{GetDbgThreadInfo(Thread)} has wait timeout of {Timeout}ms.");
  167. SchedThread.WaitEvent.WaitOne(Timeout);
  168. }
  169. else
  170. {
  171. SchedThread.WaitEvent.WaitOne();
  172. }
  173. TryResumingExecution(SchedThread);
  174. }
  175. private void TryResumingExecution(SchedulerThread SchedThread)
  176. {
  177. HThread Thread = SchedThread.Thread;
  178. lock (SchedLock)
  179. {
  180. if (ActiveProcessors.Add(Thread.ProcessorId))
  181. {
  182. Logging.Debug($"{GetDbgThreadInfo(Thread)} resuming execution...");
  183. return;
  184. }
  185. WaitingToRun[Thread.ProcessorId].Push(SchedThread);
  186. }
  187. SchedThread.WaitEvent.WaitOne();
  188. Logging.Debug($"{GetDbgThreadInfo(Thread)} resuming execution...");
  189. }
  190. public void Yield(HThread Thread)
  191. {
  192. SchedulerThread SchedThread;
  193. Logging.Debug($"{GetDbgThreadInfo(Thread)} yielded execution.");
  194. lock (SchedLock)
  195. {
  196. SchedThread = WaitingToRun[Thread.ProcessorId].Pop(Thread.Priority);
  197. if (SchedThread == null)
  198. {
  199. Logging.Debug($"{GetDbgThreadInfo(Thread)} resumed because theres nothing better to run.");
  200. return;
  201. }
  202. RunThread(SchedThread);
  203. if (!AllThreads.TryGetValue(Thread, out SchedThread))
  204. {
  205. Logging.Error($"{GetDbgThreadInfo(Thread)} was not found on the scheduler queue!");
  206. return;
  207. }
  208. WaitingToRun[Thread.ProcessorId].Push(SchedThread);
  209. }
  210. SchedThread.WaitEvent.WaitOne();
  211. Logging.Debug($"{GetDbgThreadInfo(Thread)} resuming execution...");
  212. }
  213. private void RunThread(SchedulerThread SchedThread)
  214. {
  215. if (!SchedThread.Thread.Thread.Execute())
  216. {
  217. SchedThread.WaitEvent.Set();
  218. }
  219. else
  220. {
  221. Logging.Debug($"{GetDbgThreadInfo(SchedThread.Thread)} running.");
  222. }
  223. }
  224. public void Signal(params HThread[] Threads)
  225. {
  226. lock (SchedLock)
  227. {
  228. foreach (HThread Thread in Threads)
  229. {
  230. if (AllThreads.TryGetValue(Thread, out SchedulerThread SchedThread))
  231. {
  232. if (!WaitingToRun[Thread.ProcessorId].HasThread(SchedThread))
  233. {
  234. Logging.Debug($"{GetDbgThreadInfo(Thread)} signaled.");
  235. SchedThread.WaitEvent.Set();
  236. }
  237. }
  238. }
  239. }
  240. }
  241. private string GetDbgThreadInfo(HThread Thread)
  242. {
  243. return $"Thread {Thread.ThreadId} (core {Thread.ProcessorId}) prio {Thread.Priority}";
  244. }
  245. public void Dispose()
  246. {
  247. Dispose(true);
  248. }
  249. protected virtual void Dispose(bool Disposing)
  250. {
  251. if (Disposing)
  252. {
  253. foreach (SchedulerThread SchedThread in AllThreads.Values)
  254. {
  255. SchedThread.Dispose();
  256. }
  257. }
  258. }
  259. }
  260. }