KProcessScheduler.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. using Ryujinx.Core.Logging;
  2. using System;
  3. using System.Collections.Concurrent;
  4. using System.Collections.Generic;
  5. using System.Threading;
  6. namespace Ryujinx.Core.OsHle.Handles
  7. {
  8. class KProcessScheduler : IDisposable
  9. {
  10. private const int LowestPriority = 0x40;
  11. private class SchedulerThread : IDisposable
  12. {
  13. public KThread Thread { get; private set; }
  14. public bool IsActive { get; set; }
  15. public AutoResetEvent WaitSync { get; private set; }
  16. public ManualResetEvent WaitActivity { get; private set; }
  17. public AutoResetEvent WaitSched { get; private set; }
  18. public SchedulerThread(KThread Thread)
  19. {
  20. this.Thread = Thread;
  21. IsActive = true;
  22. WaitSync = new AutoResetEvent(false);
  23. WaitActivity = new ManualResetEvent(true);
  24. WaitSched = new AutoResetEvent(false);
  25. }
  26. public void Dispose()
  27. {
  28. Dispose(true);
  29. }
  30. protected virtual void Dispose(bool Disposing)
  31. {
  32. if (Disposing)
  33. {
  34. WaitSync.Dispose();
  35. WaitActivity.Dispose();
  36. WaitSched.Dispose();
  37. }
  38. }
  39. }
  40. private class ThreadQueue
  41. {
  42. private List<SchedulerThread> Threads;
  43. public ThreadQueue()
  44. {
  45. Threads = new List<SchedulerThread>();
  46. }
  47. public void Push(SchedulerThread Thread)
  48. {
  49. lock (Threads)
  50. {
  51. Threads.Add(Thread);
  52. }
  53. }
  54. public SchedulerThread Pop(int MinPriority = LowestPriority)
  55. {
  56. lock (Threads)
  57. {
  58. SchedulerThread SchedThread;
  59. int HighestPriority = MinPriority;
  60. int HighestPrioIndex = -1;
  61. for (int Index = 0; Index < Threads.Count; Index++)
  62. {
  63. SchedThread = Threads[Index];
  64. if (HighestPriority > SchedThread.Thread.ActualPriority)
  65. {
  66. HighestPriority = SchedThread.Thread.ActualPriority;
  67. HighestPrioIndex = Index;
  68. }
  69. }
  70. if (HighestPrioIndex == -1)
  71. {
  72. return null;
  73. }
  74. SchedThread = Threads[HighestPrioIndex];
  75. Threads.RemoveAt(HighestPrioIndex);
  76. return SchedThread;
  77. }
  78. }
  79. public bool HasThread(SchedulerThread SchedThread)
  80. {
  81. lock (Threads)
  82. {
  83. return Threads.Contains(SchedThread);
  84. }
  85. }
  86. public bool Remove(SchedulerThread SchedThread)
  87. {
  88. lock (Threads)
  89. {
  90. return Threads.Remove(SchedThread);
  91. }
  92. }
  93. }
  94. private ConcurrentDictionary<KThread, SchedulerThread> AllThreads;
  95. private ThreadQueue[] WaitingToRun;
  96. private HashSet<int> ActiveProcessors;
  97. private object SchedLock;
  98. private Logger Log;
  99. public KProcessScheduler(Logger Log)
  100. {
  101. this.Log = Log;
  102. AllThreads = new ConcurrentDictionary<KThread, SchedulerThread>();
  103. WaitingToRun = new ThreadQueue[4];
  104. for (int Index = 0; Index < 4; Index++)
  105. {
  106. WaitingToRun[Index] = new ThreadQueue();
  107. }
  108. ActiveProcessors = new HashSet<int>();
  109. SchedLock = new object();
  110. }
  111. public void StartThread(KThread Thread)
  112. {
  113. lock (SchedLock)
  114. {
  115. SchedulerThread SchedThread = new SchedulerThread(Thread);
  116. if (!AllThreads.TryAdd(Thread, SchedThread))
  117. {
  118. return;
  119. }
  120. if (ActiveProcessors.Add(Thread.ProcessorId))
  121. {
  122. Thread.Thread.Execute();
  123. PrintDbgThreadInfo(Thread, "running.");
  124. }
  125. else
  126. {
  127. WaitingToRun[Thread.ProcessorId].Push(SchedThread);
  128. PrintDbgThreadInfo(Thread, "waiting to run.");
  129. }
  130. }
  131. }
  132. public void RemoveThread(KThread Thread)
  133. {
  134. PrintDbgThreadInfo(Thread, "exited.");
  135. lock (SchedLock)
  136. {
  137. if (AllThreads.TryRemove(Thread, out SchedulerThread SchedThread))
  138. {
  139. WaitingToRun[Thread.ProcessorId].Remove(SchedThread);
  140. SchedThread.Dispose();
  141. }
  142. SchedulerThread NewThread = WaitingToRun[Thread.ProcessorId].Pop();
  143. if (NewThread == null)
  144. {
  145. Log.PrintDebug(LogClass.KernelScheduler, $"Nothing to run on core {Thread.ProcessorId}!");
  146. ActiveProcessors.Remove(Thread.ProcessorId);
  147. return;
  148. }
  149. RunThread(NewThread);
  150. }
  151. }
  152. public void SetThreadActivity(KThread Thread, bool Active)
  153. {
  154. if (!AllThreads.TryGetValue(Thread, out SchedulerThread SchedThread))
  155. {
  156. throw new InvalidOperationException();
  157. }
  158. SchedThread.IsActive = Active;
  159. if (Active)
  160. {
  161. SchedThread.WaitActivity.Set();
  162. }
  163. else
  164. {
  165. SchedThread.WaitActivity.Reset();
  166. }
  167. }
  168. public void EnterWait(KThread Thread)
  169. {
  170. if (!AllThreads.TryGetValue(Thread, out SchedulerThread SchedThread))
  171. {
  172. throw new InvalidOperationException();
  173. }
  174. Suspend(Thread.ProcessorId);
  175. SchedThread.WaitSync.WaitOne();
  176. TryResumingExecution(SchedThread);
  177. }
  178. public bool EnterWait(KThread Thread, int Timeout)
  179. {
  180. if (!AllThreads.TryGetValue(Thread, out SchedulerThread SchedThread))
  181. {
  182. throw new InvalidOperationException();
  183. }
  184. Suspend(Thread.ProcessorId);
  185. bool Result = SchedThread.WaitSync.WaitOne(Timeout);
  186. TryResumingExecution(SchedThread);
  187. return Result;
  188. }
  189. public void WakeUp(KThread Thread)
  190. {
  191. if (!AllThreads.TryGetValue(Thread, out SchedulerThread SchedThread))
  192. {
  193. throw new InvalidOperationException();
  194. }
  195. SchedThread.WaitSync.Set();
  196. }
  197. public void Suspend(int ProcessorId)
  198. {
  199. lock (SchedLock)
  200. {
  201. SchedulerThread SchedThread = WaitingToRun[ProcessorId].Pop();
  202. if (SchedThread != null)
  203. {
  204. RunThread(SchedThread);
  205. }
  206. else
  207. {
  208. Log.PrintDebug(LogClass.KernelScheduler, $"Nothing to run on core {ProcessorId}!");
  209. ActiveProcessors.Remove(ProcessorId);
  210. }
  211. }
  212. }
  213. public void Yield(KThread Thread)
  214. {
  215. PrintDbgThreadInfo(Thread, "yielded execution.");
  216. if (IsActive(Thread))
  217. {
  218. lock (SchedLock)
  219. {
  220. SchedulerThread SchedThread = WaitingToRun[Thread.ProcessorId].Pop(Thread.ActualPriority);
  221. if (SchedThread == null)
  222. {
  223. PrintDbgThreadInfo(Thread, "resumed because theres nothing better to run.");
  224. return;
  225. }
  226. if (SchedThread != null)
  227. {
  228. RunThread(SchedThread);
  229. }
  230. }
  231. }
  232. else
  233. {
  234. //Just stop running the thread if it's not active,
  235. //and run whatever is waiting to run with the higuest priority.
  236. Suspend(Thread.ProcessorId);
  237. }
  238. Resume(Thread);
  239. }
  240. public void Resume(KThread Thread)
  241. {
  242. if (!AllThreads.TryGetValue(Thread, out SchedulerThread SchedThread))
  243. {
  244. throw new InvalidOperationException();
  245. }
  246. TryResumingExecution(SchedThread);
  247. }
  248. private void TryResumingExecution(SchedulerThread SchedThread)
  249. {
  250. KThread Thread = SchedThread.Thread;
  251. PrintDbgThreadInfo(Thread, "trying to resume...");
  252. SchedThread.WaitActivity.WaitOne();
  253. lock (SchedLock)
  254. {
  255. if (ActiveProcessors.Add(Thread.ProcessorId))
  256. {
  257. PrintDbgThreadInfo(Thread, "resuming execution...");
  258. return;
  259. }
  260. WaitingToRun[Thread.ProcessorId].Push(SchedThread);
  261. PrintDbgThreadInfo(Thread, "entering wait state...");
  262. }
  263. SchedThread.WaitSched.WaitOne();
  264. PrintDbgThreadInfo(Thread, "resuming execution...");
  265. }
  266. private void RunThread(SchedulerThread SchedThread)
  267. {
  268. if (!SchedThread.Thread.Thread.Execute())
  269. {
  270. SchedThread.WaitSched.Set();
  271. }
  272. else
  273. {
  274. PrintDbgThreadInfo(SchedThread.Thread, "running.");
  275. }
  276. }
  277. private bool IsActive(KThread Thread)
  278. {
  279. if (!AllThreads.TryGetValue(Thread, out SchedulerThread SchedThread))
  280. {
  281. throw new InvalidOperationException();
  282. }
  283. return SchedThread.IsActive;
  284. }
  285. private void PrintDbgThreadInfo(KThread Thread, string Message)
  286. {
  287. Log.PrintDebug(LogClass.KernelScheduler, "(" +
  288. "ThreadId = " + Thread.ThreadId + ", " +
  289. "ProcessorId = " + Thread.ProcessorId + ", " +
  290. "ActualPriority = " + Thread.ActualPriority + ", " +
  291. "WantedPriority = " + Thread.WantedPriority + ") " + Message);
  292. }
  293. public void Dispose()
  294. {
  295. Dispose(true);
  296. }
  297. protected virtual void Dispose(bool Disposing)
  298. {
  299. if (Disposing)
  300. {
  301. foreach (SchedulerThread SchedThread in AllThreads.Values)
  302. {
  303. SchedThread.Dispose();
  304. }
  305. }
  306. }
  307. }
  308. }