2018-06-11 02:46:42 +02:00
|
|
|
using Ryujinx.HLE.Logging;
|
2018-02-14 03:43:08 +01:00
|
|
|
using System;
|
2018-02-19 20:37:13 +01:00
|
|
|
using System.Collections.Concurrent;
|
2018-05-16 03:36:08 +02:00
|
|
|
using System.Threading;
|
2018-02-14 03:43:08 +01:00
|
|
|
|
2018-06-11 02:46:42 +02:00
|
|
|
namespace Ryujinx.HLE.OsHle.Handles
|
2018-02-14 03:43:08 +01:00
|
|
|
{
|
2018-03-19 19:58:46 +01:00
|
|
|
class KProcessScheduler : IDisposable
|
2018-02-14 03:43:08 +01:00
|
|
|
{
|
2018-03-19 19:58:46 +01:00
|
|
|
private ConcurrentDictionary<KThread, SchedulerThread> AllThreads;
|
2018-02-14 03:43:08 +01:00
|
|
|
|
2018-05-13 05:22:42 +02:00
|
|
|
private ThreadQueue WaitingToRun;
|
2018-02-14 03:43:08 +01:00
|
|
|
|
2018-05-16 03:36:08 +02:00
|
|
|
private KThread[] CoreThreads;
|
2018-02-14 03:43:08 +01:00
|
|
|
|
2018-05-16 04:49:57 +02:00
|
|
|
private bool[] CoreReschedule;
|
|
|
|
|
2018-02-14 03:43:08 +01:00
|
|
|
private object SchedLock;
|
|
|
|
|
2018-04-24 20:57:39 +02:00
|
|
|
private Logger Log;
|
|
|
|
|
|
|
|
public KProcessScheduler(Logger Log)
|
2018-02-14 03:43:08 +01:00
|
|
|
{
|
2018-04-24 20:57:39 +02:00
|
|
|
this.Log = Log;
|
|
|
|
|
2018-03-19 19:58:46 +01:00
|
|
|
AllThreads = new ConcurrentDictionary<KThread, SchedulerThread>();
|
2018-02-14 03:43:08 +01:00
|
|
|
|
2018-05-13 05:22:42 +02:00
|
|
|
WaitingToRun = new ThreadQueue();
|
2018-02-14 03:43:08 +01:00
|
|
|
|
2018-05-16 03:36:08 +02:00
|
|
|
CoreThreads = new KThread[4];
|
|
|
|
|
2018-05-16 04:49:57 +02:00
|
|
|
CoreReschedule = new bool[4];
|
|
|
|
|
2018-02-14 03:43:08 +01:00
|
|
|
SchedLock = new object();
|
|
|
|
}
|
|
|
|
|
2018-03-19 19:58:46 +01:00
|
|
|
public void StartThread(KThread Thread)
|
2018-02-14 03:43:08 +01:00
|
|
|
{
|
|
|
|
lock (SchedLock)
|
|
|
|
{
|
2018-02-19 20:37:13 +01:00
|
|
|
SchedulerThread SchedThread = new SchedulerThread(Thread);
|
|
|
|
|
|
|
|
if (!AllThreads.TryAdd(Thread, SchedThread))
|
2018-02-14 03:43:08 +01:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-05-16 03:36:08 +02:00
|
|
|
if (TryAddToCore(Thread))
|
2018-02-14 03:43:08 +01:00
|
|
|
{
|
|
|
|
Thread.Thread.Execute();
|
|
|
|
|
2018-04-19 04:52:23 +02:00
|
|
|
PrintDbgThreadInfo(Thread, "running.");
|
2018-02-14 03:43:08 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-05-13 05:22:42 +02:00
|
|
|
WaitingToRun.Push(SchedThread);
|
2018-02-14 03:43:08 +01:00
|
|
|
|
2018-04-19 04:52:23 +02:00
|
|
|
PrintDbgThreadInfo(Thread, "waiting to run.");
|
2018-02-14 03:43:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-19 04:52:23 +02:00
|
|
|
public void RemoveThread(KThread Thread)
|
2018-02-14 03:43:08 +01:00
|
|
|
{
|
2018-04-19 04:52:23 +02:00
|
|
|
PrintDbgThreadInfo(Thread, "exited.");
|
|
|
|
|
2018-02-19 20:37:13 +01:00
|
|
|
lock (SchedLock)
|
|
|
|
{
|
2018-04-19 05:19:22 +02:00
|
|
|
if (AllThreads.TryRemove(Thread, out SchedulerThread SchedThread))
|
|
|
|
{
|
2018-05-13 05:22:42 +02:00
|
|
|
WaitingToRun.Remove(SchedThread);
|
2018-04-19 06:01:03 +02:00
|
|
|
|
|
|
|
SchedThread.Dispose();
|
2018-04-19 05:19:22 +02:00
|
|
|
}
|
|
|
|
|
2018-05-14 08:01:10 +02:00
|
|
|
int ActualCore = Thread.ActualCore;
|
|
|
|
|
|
|
|
SchedulerThread NewThread = WaitingToRun.Pop(ActualCore);
|
2018-02-14 03:43:08 +01:00
|
|
|
|
2018-04-19 04:52:23 +02:00
|
|
|
if (NewThread == null)
|
2018-02-19 20:37:13 +01:00
|
|
|
{
|
2018-05-14 08:01:10 +02:00
|
|
|
Log.PrintDebug(LogClass.KernelScheduler, $"Nothing to run on core {ActualCore}!");
|
2018-02-14 03:43:08 +01:00
|
|
|
|
2018-05-16 03:36:08 +02:00
|
|
|
CoreThreads[ActualCore] = null;
|
2018-02-14 03:43:08 +01:00
|
|
|
|
2018-02-19 20:37:13 +01:00
|
|
|
return;
|
|
|
|
}
|
2018-02-14 03:43:08 +01:00
|
|
|
|
2018-05-14 08:01:10 +02:00
|
|
|
NewThread.Thread.ActualCore = ActualCore;
|
|
|
|
|
2018-04-19 04:52:23 +02:00
|
|
|
RunThread(NewThread);
|
|
|
|
}
|
2018-02-14 03:43:08 +01:00
|
|
|
}
|
|
|
|
|
2018-04-19 21:18:30 +02:00
|
|
|
public void SetThreadActivity(KThread Thread, bool Active)
|
|
|
|
{
|
|
|
|
if (!AllThreads.TryGetValue(Thread, out SchedulerThread SchedThread))
|
|
|
|
{
|
|
|
|
throw new InvalidOperationException();
|
|
|
|
}
|
|
|
|
|
2018-04-26 04:11:26 +02:00
|
|
|
SchedThread.IsActive = Active;
|
2018-04-21 21:07:16 +02:00
|
|
|
|
2018-04-26 04:11:26 +02:00
|
|
|
if (Active)
|
|
|
|
{
|
|
|
|
SchedThread.WaitActivity.Set();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
SchedThread.WaitActivity.Reset();
|
|
|
|
}
|
2018-04-21 21:07:16 +02:00
|
|
|
}
|
|
|
|
|
2018-05-16 03:36:08 +02:00
|
|
|
public void EnterWait(KThread Thread, int TimeoutMs = Timeout.Infinite)
|
2018-04-21 21:07:16 +02:00
|
|
|
{
|
2018-05-16 03:36:08 +02:00
|
|
|
SchedulerThread SchedThread = AllThreads[Thread];
|
2018-04-19 21:18:30 +02:00
|
|
|
|
2018-05-13 05:22:42 +02:00
|
|
|
Suspend(Thread);
|
2018-04-19 21:18:30 +02:00
|
|
|
|
2018-05-16 03:36:08 +02:00
|
|
|
SchedThread.WaitSync.WaitOne(TimeoutMs);
|
2018-04-19 21:18:30 +02:00
|
|
|
|
2018-04-26 04:11:26 +02:00
|
|
|
TryResumingExecution(SchedThread);
|
2018-04-21 21:07:16 +02:00
|
|
|
}
|
2018-04-19 21:18:30 +02:00
|
|
|
|
2018-05-16 03:36:08 +02:00
|
|
|
public void WakeUp(KThread Thread)
|
2018-04-21 21:07:16 +02:00
|
|
|
{
|
2018-05-16 03:36:08 +02:00
|
|
|
AllThreads[Thread].WaitSync.Set();
|
2018-04-21 21:07:16 +02:00
|
|
|
}
|
2018-04-19 21:18:30 +02:00
|
|
|
|
2018-05-16 03:36:08 +02:00
|
|
|
public void TryToRun(KThread Thread)
|
2018-04-21 21:07:16 +02:00
|
|
|
{
|
2018-05-16 03:36:08 +02:00
|
|
|
lock (SchedLock)
|
2018-04-21 21:07:16 +02:00
|
|
|
{
|
2018-05-16 03:36:08 +02:00
|
|
|
if (AllThreads.TryGetValue(Thread, out SchedulerThread SchedThread))
|
|
|
|
{
|
|
|
|
if (WaitingToRun.HasThread(SchedThread) && TryAddToCore(Thread))
|
|
|
|
{
|
|
|
|
RunThread(SchedThread);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
SetReschedule(Thread.ProcessorId);
|
|
|
|
}
|
|
|
|
}
|
2018-04-19 21:18:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-13 05:22:42 +02:00
|
|
|
public void Suspend(KThread Thread)
|
2018-02-14 03:43:08 +01:00
|
|
|
{
|
|
|
|
lock (SchedLock)
|
|
|
|
{
|
2018-05-13 05:22:42 +02:00
|
|
|
PrintDbgThreadInfo(Thread, "suspended.");
|
|
|
|
|
2018-05-14 08:01:10 +02:00
|
|
|
int ActualCore = Thread.ActualCore;
|
|
|
|
|
2018-05-16 04:49:57 +02:00
|
|
|
CoreReschedule[ActualCore] = false;
|
|
|
|
|
2018-05-14 08:01:10 +02:00
|
|
|
SchedulerThread SchedThread = WaitingToRun.Pop(ActualCore);
|
2018-02-14 03:43:08 +01:00
|
|
|
|
2018-02-19 20:37:13 +01:00
|
|
|
if (SchedThread != null)
|
2018-02-14 03:43:08 +01:00
|
|
|
{
|
2018-05-14 08:01:10 +02:00
|
|
|
SchedThread.Thread.ActualCore = ActualCore;
|
|
|
|
|
2018-05-16 03:36:08 +02:00
|
|
|
CoreThreads[ActualCore] = SchedThread.Thread;
|
|
|
|
|
2018-02-19 20:37:13 +01:00
|
|
|
RunThread(SchedThread);
|
2018-02-14 03:43:08 +01:00
|
|
|
}
|
2018-02-19 20:37:13 +01:00
|
|
|
else
|
2018-02-14 03:43:08 +01:00
|
|
|
{
|
2018-05-13 05:22:42 +02:00
|
|
|
Log.PrintDebug(LogClass.KernelScheduler, $"Nothing to run on core {Thread.ActualCore}!");
|
2018-02-19 20:37:13 +01:00
|
|
|
|
2018-05-16 03:36:08 +02:00
|
|
|
CoreThreads[ActualCore] = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void SetReschedule(int Core)
|
|
|
|
{
|
|
|
|
lock (SchedLock)
|
|
|
|
{
|
2018-05-16 04:49:57 +02:00
|
|
|
CoreReschedule[Core] = true;
|
2018-02-14 03:43:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-16 03:36:08 +02:00
|
|
|
public void Reschedule(KThread Thread)
|
2018-02-14 03:43:08 +01:00
|
|
|
{
|
2018-05-16 03:36:08 +02:00
|
|
|
bool NeedsReschedule;
|
|
|
|
|
|
|
|
lock (SchedLock)
|
2018-02-14 03:43:08 +01:00
|
|
|
{
|
2018-05-16 04:49:57 +02:00
|
|
|
int ActualCore = Thread.ActualCore;
|
|
|
|
|
|
|
|
NeedsReschedule = CoreReschedule[ActualCore];
|
2018-05-16 03:36:08 +02:00
|
|
|
|
2018-05-16 04:49:57 +02:00
|
|
|
CoreReschedule[ActualCore] = false;
|
2018-05-16 03:36:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (NeedsReschedule)
|
|
|
|
{
|
2018-06-09 23:19:14 +02:00
|
|
|
Yield(Thread, Thread.ActualPriority - 1);
|
|
|
|
}
|
|
|
|
}
|
2018-05-14 08:01:10 +02:00
|
|
|
|
2018-06-09 23:19:14 +02:00
|
|
|
public void Yield(KThread Thread)
|
|
|
|
{
|
|
|
|
Yield(Thread, Thread.ActualPriority);
|
|
|
|
}
|
2018-02-14 03:43:08 +01:00
|
|
|
|
2018-06-09 23:19:14 +02:00
|
|
|
private void Yield(KThread Thread, int MinPriority)
|
|
|
|
{
|
|
|
|
PrintDbgThreadInfo(Thread, "yielded execution.");
|
2018-02-14 03:43:08 +01:00
|
|
|
|
2018-06-09 23:19:14 +02:00
|
|
|
lock (SchedLock)
|
|
|
|
{
|
|
|
|
int ActualCore = Thread.ActualCore;
|
2018-04-26 04:11:26 +02:00
|
|
|
|
2018-06-09 23:19:14 +02:00
|
|
|
SchedulerThread NewThread = WaitingToRun.Pop(ActualCore, MinPriority);
|
2018-02-14 03:43:08 +01:00
|
|
|
|
2018-06-09 23:19:14 +02:00
|
|
|
if (NewThread == null)
|
|
|
|
{
|
|
|
|
PrintDbgThreadInfo(Thread, "resumed because theres nothing better to run.");
|
2018-05-14 03:00:29 +02:00
|
|
|
|
2018-06-09 23:19:14 +02:00
|
|
|
return;
|
2018-05-14 03:00:29 +02:00
|
|
|
}
|
2018-05-14 08:01:10 +02:00
|
|
|
|
2018-06-09 23:19:14 +02:00
|
|
|
NewThread.Thread.ActualCore = ActualCore;
|
|
|
|
|
|
|
|
CoreThreads[ActualCore] = NewThread.Thread;
|
|
|
|
|
|
|
|
RunThread(NewThread);
|
2018-05-16 03:36:08 +02:00
|
|
|
}
|
2018-06-09 23:19:14 +02:00
|
|
|
|
|
|
|
Resume(Thread);
|
2018-05-14 03:00:29 +02:00
|
|
|
}
|
|
|
|
|
2018-04-26 04:11:26 +02:00
|
|
|
public void Resume(KThread Thread)
|
2018-02-14 03:43:08 +01:00
|
|
|
{
|
2018-05-16 03:36:08 +02:00
|
|
|
TryResumingExecution(AllThreads[Thread]);
|
2018-04-19 04:52:23 +02:00
|
|
|
}
|
2018-02-14 03:43:08 +01:00
|
|
|
|
2018-04-26 04:11:26 +02:00
|
|
|
private void TryResumingExecution(SchedulerThread SchedThread)
|
2018-04-19 04:52:23 +02:00
|
|
|
{
|
|
|
|
KThread Thread = SchedThread.Thread;
|
2018-02-19 20:37:13 +01:00
|
|
|
|
2018-04-26 04:11:26 +02:00
|
|
|
PrintDbgThreadInfo(Thread, "trying to resume...");
|
2018-04-19 04:52:23 +02:00
|
|
|
|
2018-04-26 04:11:26 +02:00
|
|
|
SchedThread.WaitActivity.WaitOne();
|
2018-04-21 21:07:16 +02:00
|
|
|
|
|
|
|
lock (SchedLock)
|
2018-04-19 21:18:30 +02:00
|
|
|
{
|
2018-05-16 03:36:08 +02:00
|
|
|
if (TryAddToCore(Thread))
|
2018-04-21 21:07:16 +02:00
|
|
|
{
|
|
|
|
PrintDbgThreadInfo(Thread, "resuming execution...");
|
|
|
|
|
2018-04-26 04:11:26 +02:00
|
|
|
return;
|
2018-04-21 21:07:16 +02:00
|
|
|
}
|
|
|
|
|
2018-05-13 05:22:42 +02:00
|
|
|
WaitingToRun.Push(SchedThread);
|
2018-04-21 21:07:16 +02:00
|
|
|
|
2018-05-16 03:36:08 +02:00
|
|
|
SetReschedule(Thread.ProcessorId);
|
|
|
|
|
2018-04-21 21:07:16 +02:00
|
|
|
PrintDbgThreadInfo(Thread, "entering wait state...");
|
2018-02-19 20:37:13 +01:00
|
|
|
}
|
2018-02-14 03:43:08 +01:00
|
|
|
|
2018-04-26 04:11:26 +02:00
|
|
|
SchedThread.WaitSched.WaitOne();
|
2018-02-14 03:43:08 +01:00
|
|
|
|
2018-04-19 04:52:23 +02:00
|
|
|
PrintDbgThreadInfo(Thread, "resuming execution...");
|
2018-02-14 03:43:08 +01:00
|
|
|
}
|
|
|
|
|
2018-02-19 20:37:13 +01:00
|
|
|
private void RunThread(SchedulerThread SchedThread)
|
2018-02-14 03:43:08 +01:00
|
|
|
{
|
2018-02-19 20:37:13 +01:00
|
|
|
if (!SchedThread.Thread.Thread.Execute())
|
2018-02-14 03:43:08 +01:00
|
|
|
{
|
2018-05-13 05:22:42 +02:00
|
|
|
PrintDbgThreadInfo(SchedThread.Thread, "waked.");
|
|
|
|
|
2018-04-26 04:11:26 +02:00
|
|
|
SchedThread.WaitSched.Set();
|
2018-02-14 03:43:08 +01:00
|
|
|
}
|
2018-02-19 20:37:13 +01:00
|
|
|
else
|
2018-02-14 03:43:08 +01:00
|
|
|
{
|
2018-04-19 04:52:23 +02:00
|
|
|
PrintDbgThreadInfo(SchedThread.Thread, "running.");
|
2018-02-19 20:37:13 +01:00
|
|
|
}
|
2018-02-14 03:43:08 +01:00
|
|
|
}
|
|
|
|
|
2018-05-13 05:22:42 +02:00
|
|
|
public void Resort(KThread Thread)
|
|
|
|
{
|
|
|
|
if (AllThreads.TryGetValue(Thread, out SchedulerThread SchedThread))
|
|
|
|
{
|
|
|
|
WaitingToRun.Resort(SchedThread);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-16 03:36:08 +02:00
|
|
|
private bool TryAddToCore(KThread Thread)
|
2018-05-13 05:22:42 +02:00
|
|
|
{
|
2018-05-16 03:36:08 +02:00
|
|
|
//First, try running it on Ideal Core.
|
|
|
|
int IdealCore = Thread.IdealCore;
|
2018-05-14 08:01:10 +02:00
|
|
|
|
2018-05-16 03:36:08 +02:00
|
|
|
if (IdealCore != -1 && CoreThreads[IdealCore] == null)
|
2018-05-13 05:22:42 +02:00
|
|
|
{
|
2018-05-16 03:36:08 +02:00
|
|
|
Thread.ActualCore = IdealCore;
|
2018-05-13 05:22:42 +02:00
|
|
|
|
2018-05-16 03:36:08 +02:00
|
|
|
CoreThreads[IdealCore] = Thread;
|
2018-05-14 08:01:10 +02:00
|
|
|
|
2018-05-16 03:36:08 +02:00
|
|
|
return true;
|
|
|
|
}
|
2018-05-13 05:22:42 +02:00
|
|
|
|
2018-05-16 03:36:08 +02:00
|
|
|
//If that fails, then try running on any core allowed by Core Mask.
|
|
|
|
int CoreMask = Thread.CoreMask;
|
2018-05-13 05:22:42 +02:00
|
|
|
|
2018-05-16 03:36:08 +02:00
|
|
|
for (int Core = 0; Core < CoreThreads.Length; Core++, CoreMask >>= 1)
|
|
|
|
{
|
|
|
|
if ((CoreMask & 1) != 0 && CoreThreads[Core] == null)
|
2018-05-13 05:22:42 +02:00
|
|
|
{
|
2018-05-16 03:36:08 +02:00
|
|
|
Thread.ActualCore = Core;
|
2018-05-13 05:22:42 +02:00
|
|
|
|
2018-05-16 03:36:08 +02:00
|
|
|
CoreThreads[Core] = Thread;
|
2018-05-13 05:22:42 +02:00
|
|
|
|
2018-05-16 03:36:08 +02:00
|
|
|
return true;
|
2018-05-13 05:22:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-16 03:36:08 +02:00
|
|
|
return false;
|
2018-05-13 05:22:42 +02:00
|
|
|
}
|
|
|
|
|
2018-04-19 04:52:23 +02:00
|
|
|
private void PrintDbgThreadInfo(KThread Thread, string Message)
|
2018-02-14 03:43:08 +01:00
|
|
|
{
|
2018-04-24 20:57:39 +02:00
|
|
|
Log.PrintDebug(LogClass.KernelScheduler, "(" +
|
2018-05-14 08:01:10 +02:00
|
|
|
"ThreadId = " + Thread.ThreadId + ", " +
|
|
|
|
"CoreMask = 0x" + Thread.CoreMask.ToString("x1") + ", " +
|
|
|
|
"ActualCore = " + Thread.ActualCore + ", " +
|
|
|
|
"IdealCore = " + Thread.IdealCore + ", " +
|
|
|
|
"ActualPriority = " + Thread.ActualPriority + ", " +
|
|
|
|
"WantedPriority = " + Thread.WantedPriority + ") " + Message);
|
2018-02-14 03:43:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
{
|
|
|
|
Dispose(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected virtual void Dispose(bool Disposing)
|
|
|
|
{
|
|
|
|
if (Disposing)
|
|
|
|
{
|
2018-02-19 20:37:13 +01:00
|
|
|
foreach (SchedulerThread SchedThread in AllThreads.Values)
|
2018-02-14 03:43:08 +01:00
|
|
|
{
|
2018-02-19 20:37:13 +01:00
|
|
|
SchedThread.Dispose();
|
2018-02-14 03:43:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|