KProcessScheduler.cs 9.2 KB

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