KProcessScheduler.cs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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 bool TryRunning(KThread Thread)
  153. {
  154. if (!AllThreads.TryGetValue(Thread, out SchedulerThread SchedThread))
  155. {
  156. throw new InvalidOperationException();
  157. }
  158. lock (SchedLock)
  159. {
  160. if (WaitingToRun.HasThread(SchedThread) && AddActiveCore(Thread))
  161. {
  162. WaitingToRun.Remove(SchedThread);
  163. RunThread(SchedThread);
  164. return true;
  165. }
  166. return false;
  167. }
  168. }
  169. public void Resume(KThread Thread)
  170. {
  171. if (!AllThreads.TryGetValue(Thread, out SchedulerThread SchedThread))
  172. {
  173. throw new InvalidOperationException();
  174. }
  175. TryResumingExecution(SchedThread);
  176. }
  177. private void TryResumingExecution(SchedulerThread SchedThread)
  178. {
  179. KThread Thread = SchedThread.Thread;
  180. PrintDbgThreadInfo(Thread, "trying to resume...");
  181. SchedThread.WaitActivity.WaitOne();
  182. lock (SchedLock)
  183. {
  184. if (AddActiveCore(Thread))
  185. {
  186. PrintDbgThreadInfo(Thread, "resuming execution...");
  187. return;
  188. }
  189. WaitingToRun.Push(SchedThread);
  190. PrintDbgThreadInfo(Thread, "entering wait state...");
  191. }
  192. SchedThread.WaitSched.WaitOne();
  193. PrintDbgThreadInfo(Thread, "resuming execution...");
  194. }
  195. private void RunThread(SchedulerThread SchedThread)
  196. {
  197. if (!SchedThread.Thread.Thread.Execute())
  198. {
  199. PrintDbgThreadInfo(SchedThread.Thread, "waked.");
  200. SchedThread.WaitSched.Set();
  201. }
  202. else
  203. {
  204. PrintDbgThreadInfo(SchedThread.Thread, "running.");
  205. }
  206. }
  207. public void Resort(KThread Thread)
  208. {
  209. if (AllThreads.TryGetValue(Thread, out SchedulerThread SchedThread))
  210. {
  211. WaitingToRun.Resort(SchedThread);
  212. }
  213. }
  214. private bool IsActive(KThread Thread)
  215. {
  216. if (!AllThreads.TryGetValue(Thread, out SchedulerThread SchedThread))
  217. {
  218. throw new InvalidOperationException();
  219. }
  220. return SchedThread.IsActive;
  221. }
  222. private bool AddActiveCore(KThread Thread)
  223. {
  224. lock (SchedLock)
  225. {
  226. //First, try running it on Ideal Core.
  227. int CoreMask = 1 << Thread.IdealCore;
  228. if ((ActiveCores & CoreMask) == 0)
  229. {
  230. ActiveCores |= CoreMask;
  231. Thread.ActualCore = Thread.IdealCore;
  232. return true;
  233. }
  234. //If that fails, then try running on any core allowed by Core Mask.
  235. CoreMask = Thread.CoreMask & ~ActiveCores;
  236. if (CoreMask != 0)
  237. {
  238. CoreMask &= -CoreMask;
  239. ActiveCores |= CoreMask;
  240. for (int Bit = 0; Bit < 32; Bit++)
  241. {
  242. if (((CoreMask >> Bit) & 1) != 0)
  243. {
  244. Thread.ActualCore = Bit;
  245. return true;
  246. }
  247. }
  248. throw new InvalidOperationException();
  249. }
  250. return false;
  251. }
  252. }
  253. private void RemoveActiveCore(int Core)
  254. {
  255. lock (SchedLock)
  256. {
  257. ActiveCores &= ~(1 << Core);
  258. }
  259. }
  260. private void PrintDbgThreadInfo(KThread Thread, string Message)
  261. {
  262. Log.PrintDebug(LogClass.KernelScheduler, "(" +
  263. "ThreadId = " + Thread.ThreadId + ", " +
  264. "ActualCore = " + Thread.ActualCore + ", " +
  265. "IdealCore = " + Thread.IdealCore + ", " +
  266. "ActualPriority = " + Thread.ActualPriority + ", " +
  267. "WantedPriority = " + Thread.WantedPriority + ") " + Message);
  268. }
  269. public void Dispose()
  270. {
  271. Dispose(true);
  272. }
  273. protected virtual void Dispose(bool Disposing)
  274. {
  275. if (Disposing)
  276. {
  277. foreach (SchedulerThread SchedThread in AllThreads.Values)
  278. {
  279. SchedThread.Dispose();
  280. }
  281. }
  282. }
  283. }
  284. }