2018-04-24 18:57:39 +00:00
|
|
|
using Ryujinx.Core.Logging;
|
2018-02-14 02:43:08 +00:00
|
|
|
using System;
|
2018-02-19 19:37:13 +00:00
|
|
|
using System.Collections.Concurrent;
|
2018-02-14 02:43:08 +00:00
|
|
|
|
2018-02-20 20:09:23 +00:00
|
|
|
namespace Ryujinx.Core.OsHle.Handles
|
2018-02-14 02:43:08 +00:00
|
|
|
{
|
2018-03-19 18:58:46 +00:00
|
|
|
class KProcessScheduler : IDisposable
|
2018-02-14 02:43:08 +00:00
|
|
|
{
|
2018-03-19 18:58:46 +00:00
|
|
|
private ConcurrentDictionary<KThread, SchedulerThread> AllThreads;
|
2018-02-14 02:43:08 +00:00
|
|
|
|
2018-05-13 03:22:42 +00:00
|
|
|
private ThreadQueue WaitingToRun;
|
2018-02-14 02:43:08 +00:00
|
|
|
|
2018-05-13 03:22:42 +00:00
|
|
|
private int ActiveCores;
|
2018-02-14 02:43:08 +00:00
|
|
|
|
|
|
|
private object SchedLock;
|
|
|
|
|
2018-04-24 18:57:39 +00:00
|
|
|
private Logger Log;
|
|
|
|
|
|
|
|
public KProcessScheduler(Logger Log)
|
2018-02-14 02:43:08 +00:00
|
|
|
{
|
2018-04-24 18:57:39 +00:00
|
|
|
this.Log = Log;
|
|
|
|
|
2018-03-19 18:58:46 +00:00
|
|
|
AllThreads = new ConcurrentDictionary<KThread, SchedulerThread>();
|
2018-02-14 02:43:08 +00:00
|
|
|
|
2018-05-13 03:22:42 +00:00
|
|
|
WaitingToRun = new ThreadQueue();
|
2018-02-14 02:43:08 +00:00
|
|
|
|
|
|
|
SchedLock = new object();
|
|
|
|
}
|
|
|
|
|
2018-03-19 18:58:46 +00:00
|
|
|
public void StartThread(KThread Thread)
|
2018-02-14 02:43:08 +00:00
|
|
|
{
|
|
|
|
lock (SchedLock)
|
|
|
|
{
|
2018-02-19 19:37:13 +00:00
|
|
|
SchedulerThread SchedThread = new SchedulerThread(Thread);
|
|
|
|
|
|
|
|
if (!AllThreads.TryAdd(Thread, SchedThread))
|
2018-02-14 02:43:08 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-05-13 03:22:42 +00:00
|
|
|
if (AddActiveCore(Thread))
|
2018-02-14 02:43:08 +00:00
|
|
|
{
|
|
|
|
Thread.Thread.Execute();
|
|
|
|
|
2018-04-19 02:52:23 +00:00
|
|
|
PrintDbgThreadInfo(Thread, "running.");
|
2018-02-14 02:43:08 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-05-13 03:22:42 +00:00
|
|
|
WaitingToRun.Push(SchedThread);
|
2018-02-14 02:43:08 +00:00
|
|
|
|
2018-04-19 02:52:23 +00:00
|
|
|
PrintDbgThreadInfo(Thread, "waiting to run.");
|
2018-02-14 02:43:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-19 02:52:23 +00:00
|
|
|
public void RemoveThread(KThread Thread)
|
2018-02-14 02:43:08 +00:00
|
|
|
{
|
2018-04-19 02:52:23 +00:00
|
|
|
PrintDbgThreadInfo(Thread, "exited.");
|
|
|
|
|
2018-02-19 19:37:13 +00:00
|
|
|
lock (SchedLock)
|
|
|
|
{
|
2018-04-19 03:19:22 +00:00
|
|
|
if (AllThreads.TryRemove(Thread, out SchedulerThread SchedThread))
|
|
|
|
{
|
2018-05-13 03:22:42 +00:00
|
|
|
WaitingToRun.Remove(SchedThread);
|
2018-04-19 04:01:03 +00:00
|
|
|
|
|
|
|
SchedThread.Dispose();
|
2018-04-19 03:19:22 +00:00
|
|
|
}
|
|
|
|
|
2018-05-14 06:01:10 +00:00
|
|
|
int ActualCore = Thread.ActualCore;
|
|
|
|
|
|
|
|
SchedulerThread NewThread = WaitingToRun.Pop(ActualCore);
|
2018-02-14 02:43:08 +00:00
|
|
|
|
2018-04-19 02:52:23 +00:00
|
|
|
if (NewThread == null)
|
2018-02-19 19:37:13 +00:00
|
|
|
{
|
2018-05-14 06:01:10 +00:00
|
|
|
Log.PrintDebug(LogClass.KernelScheduler, $"Nothing to run on core {ActualCore}!");
|
2018-02-14 02:43:08 +00:00
|
|
|
|
2018-05-14 06:01:10 +00:00
|
|
|
RemoveActiveCore(ActualCore);
|
2018-02-14 02:43:08 +00:00
|
|
|
|
2018-02-19 19:37:13 +00:00
|
|
|
return;
|
|
|
|
}
|
2018-02-14 02:43:08 +00:00
|
|
|
|
2018-05-14 06:01:10 +00:00
|
|
|
NewThread.Thread.ActualCore = ActualCore;
|
|
|
|
|
2018-04-19 02:52:23 +00:00
|
|
|
RunThread(NewThread);
|
|
|
|
}
|
2018-02-14 02:43:08 +00:00
|
|
|
}
|
|
|
|
|
2018-04-19 19:18:30 +00:00
|
|
|
public void SetThreadActivity(KThread Thread, bool Active)
|
|
|
|
{
|
|
|
|
if (!AllThreads.TryGetValue(Thread, out SchedulerThread SchedThread))
|
|
|
|
{
|
|
|
|
throw new InvalidOperationException();
|
|
|
|
}
|
|
|
|
|
2018-04-26 02:11:26 +00:00
|
|
|
SchedThread.IsActive = Active;
|
2018-04-21 19:07:16 +00:00
|
|
|
|
2018-04-26 02:11:26 +00:00
|
|
|
if (Active)
|
|
|
|
{
|
|
|
|
SchedThread.WaitActivity.Set();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
SchedThread.WaitActivity.Reset();
|
|
|
|
}
|
2018-04-21 19:07:16 +00:00
|
|
|
}
|
|
|
|
|
2018-04-26 02:11:26 +00:00
|
|
|
public void EnterWait(KThread Thread)
|
2018-04-21 19:07:16 +00:00
|
|
|
{
|
|
|
|
if (!AllThreads.TryGetValue(Thread, out SchedulerThread SchedThread))
|
2018-04-19 19:18:30 +00:00
|
|
|
{
|
2018-04-21 19:07:16 +00:00
|
|
|
throw new InvalidOperationException();
|
|
|
|
}
|
2018-04-19 19:18:30 +00:00
|
|
|
|
2018-05-13 03:22:42 +00:00
|
|
|
Suspend(Thread);
|
2018-04-19 19:18:30 +00:00
|
|
|
|
2018-04-26 02:11:26 +00:00
|
|
|
SchedThread.WaitSync.WaitOne();
|
2018-04-19 19:18:30 +00:00
|
|
|
|
2018-04-26 02:11:26 +00:00
|
|
|
TryResumingExecution(SchedThread);
|
2018-04-21 19:07:16 +00:00
|
|
|
}
|
2018-04-19 19:18:30 +00:00
|
|
|
|
2018-04-26 02:11:26 +00:00
|
|
|
public bool EnterWait(KThread Thread, int Timeout)
|
2018-04-21 19:07:16 +00:00
|
|
|
{
|
|
|
|
if (!AllThreads.TryGetValue(Thread, out SchedulerThread SchedThread))
|
|
|
|
{
|
|
|
|
throw new InvalidOperationException();
|
|
|
|
}
|
|
|
|
|
2018-05-13 03:22:42 +00:00
|
|
|
Suspend(Thread);
|
2018-04-21 19:07:16 +00:00
|
|
|
|
2018-04-26 02:11:26 +00:00
|
|
|
bool Result = SchedThread.WaitSync.WaitOne(Timeout);
|
2018-04-21 19:07:16 +00:00
|
|
|
|
2018-04-26 02:11:26 +00:00
|
|
|
TryResumingExecution(SchedThread);
|
2018-04-21 19:07:16 +00:00
|
|
|
|
2018-04-26 02:11:26 +00:00
|
|
|
return Result;
|
2018-04-21 19:07:16 +00:00
|
|
|
}
|
2018-04-19 19:18:30 +00:00
|
|
|
|
2018-04-26 02:11:26 +00:00
|
|
|
public void WakeUp(KThread Thread)
|
2018-04-21 19:07:16 +00:00
|
|
|
{
|
2018-04-26 02:11:26 +00:00
|
|
|
if (!AllThreads.TryGetValue(Thread, out SchedulerThread SchedThread))
|
2018-04-21 19:07:16 +00:00
|
|
|
{
|
2018-04-26 02:11:26 +00:00
|
|
|
throw new InvalidOperationException();
|
2018-04-19 19:18:30 +00:00
|
|
|
}
|
2018-04-26 02:11:26 +00:00
|
|
|
|
|
|
|
SchedThread.WaitSync.Set();
|
2018-04-19 19:18:30 +00:00
|
|
|
}
|
|
|
|
|
2018-05-13 03:22:42 +00:00
|
|
|
public void Suspend(KThread Thread)
|
2018-02-14 02:43:08 +00:00
|
|
|
{
|
|
|
|
lock (SchedLock)
|
|
|
|
{
|
2018-05-13 03:22:42 +00:00
|
|
|
PrintDbgThreadInfo(Thread, "suspended.");
|
|
|
|
|
2018-05-14 06:01:10 +00:00
|
|
|
int ActualCore = Thread.ActualCore;
|
|
|
|
|
|
|
|
SchedulerThread SchedThread = WaitingToRun.Pop(ActualCore);
|
2018-02-14 02:43:08 +00:00
|
|
|
|
2018-02-19 19:37:13 +00:00
|
|
|
if (SchedThread != null)
|
2018-02-14 02:43:08 +00:00
|
|
|
{
|
2018-05-14 06:01:10 +00:00
|
|
|
SchedThread.Thread.ActualCore = ActualCore;
|
|
|
|
|
2018-02-19 19:37:13 +00:00
|
|
|
RunThread(SchedThread);
|
2018-02-14 02:43:08 +00:00
|
|
|
}
|
2018-02-19 19:37:13 +00:00
|
|
|
else
|
2018-02-14 02:43:08 +00:00
|
|
|
{
|
2018-05-13 03:22:42 +00:00
|
|
|
Log.PrintDebug(LogClass.KernelScheduler, $"Nothing to run on core {Thread.ActualCore}!");
|
2018-02-19 19:37:13 +00:00
|
|
|
|
2018-05-14 06:01:10 +00:00
|
|
|
RemoveActiveCore(ActualCore);
|
2018-02-14 02:43:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-19 02:52:23 +00:00
|
|
|
public void Yield(KThread Thread)
|
2018-02-14 02:43:08 +00:00
|
|
|
{
|
2018-04-19 02:52:23 +00:00
|
|
|
PrintDbgThreadInfo(Thread, "yielded execution.");
|
2018-02-19 19:37:13 +00:00
|
|
|
|
2018-04-26 02:11:26 +00:00
|
|
|
if (IsActive(Thread))
|
2018-02-14 02:43:08 +00:00
|
|
|
{
|
2018-04-26 02:11:26 +00:00
|
|
|
lock (SchedLock)
|
2018-02-14 02:43:08 +00:00
|
|
|
{
|
2018-05-14 06:01:10 +00:00
|
|
|
int ActualCore = Thread.ActualCore;
|
|
|
|
|
|
|
|
SchedulerThread SchedThread = WaitingToRun.Pop(ActualCore, Thread.ActualPriority);
|
2018-02-14 02:43:08 +00:00
|
|
|
|
2018-04-26 02:11:26 +00:00
|
|
|
if (SchedThread == null)
|
|
|
|
{
|
|
|
|
PrintDbgThreadInfo(Thread, "resumed because theres nothing better to run.");
|
2018-02-14 02:43:08 +00:00
|
|
|
|
2018-04-26 02:11:26 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (SchedThread != null)
|
|
|
|
{
|
2018-05-14 06:01:10 +00:00
|
|
|
SchedThread.Thread.ActualCore = ActualCore;
|
|
|
|
|
2018-04-26 02:11:26 +00:00
|
|
|
RunThread(SchedThread);
|
|
|
|
}
|
2018-04-21 19:07:16 +00:00
|
|
|
}
|
2018-02-14 02:43:08 +00:00
|
|
|
}
|
2018-04-26 02:11:26 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
//Just stop running the thread if it's not active,
|
|
|
|
//and run whatever is waiting to run with the higuest priority.
|
2018-05-13 03:22:42 +00:00
|
|
|
Suspend(Thread);
|
2018-04-26 02:11:26 +00:00
|
|
|
}
|
2018-02-19 19:37:13 +00:00
|
|
|
|
2018-04-19 02:52:23 +00:00
|
|
|
Resume(Thread);
|
2018-02-14 02:43:08 +00:00
|
|
|
}
|
|
|
|
|
2018-05-14 01:00:29 +00:00
|
|
|
public bool TryRunning(KThread Thread)
|
|
|
|
{
|
2018-05-14 06:01:10 +00:00
|
|
|
//Failing to get the thread here is fine,
|
|
|
|
//the thread may not have been started yet.
|
|
|
|
if (AllThreads.TryGetValue(Thread, out SchedulerThread SchedThread))
|
2018-05-14 01:00:29 +00:00
|
|
|
{
|
2018-05-14 06:01:10 +00:00
|
|
|
lock (SchedLock)
|
2018-05-14 01:00:29 +00:00
|
|
|
{
|
2018-05-14 06:01:10 +00:00
|
|
|
if (WaitingToRun.HasThread(SchedThread) && AddActiveCore(Thread))
|
|
|
|
{
|
|
|
|
WaitingToRun.Remove(SchedThread);
|
2018-05-14 01:00:29 +00:00
|
|
|
|
2018-05-14 06:01:10 +00:00
|
|
|
RunThread(SchedThread);
|
2018-05-14 01:00:29 +00:00
|
|
|
|
2018-05-14 06:01:10 +00:00
|
|
|
return true;
|
|
|
|
}
|
2018-05-14 01:00:29 +00:00
|
|
|
}
|
|
|
|
}
|
2018-05-14 06:01:10 +00:00
|
|
|
|
|
|
|
return false;
|
2018-05-14 01:00:29 +00:00
|
|
|
}
|
|
|
|
|
2018-04-26 02:11:26 +00:00
|
|
|
public void Resume(KThread Thread)
|
2018-02-14 02:43:08 +00:00
|
|
|
{
|
2018-04-19 19:18:30 +00:00
|
|
|
if (!AllThreads.TryGetValue(Thread, out SchedulerThread SchedThread))
|
2018-02-14 02:43:08 +00:00
|
|
|
{
|
2018-04-19 02:52:23 +00:00
|
|
|
throw new InvalidOperationException();
|
|
|
|
}
|
2018-02-14 02:43:08 +00:00
|
|
|
|
2018-04-26 02:11:26 +00:00
|
|
|
TryResumingExecution(SchedThread);
|
2018-04-19 02:52:23 +00:00
|
|
|
}
|
2018-02-14 02:43:08 +00:00
|
|
|
|
2018-04-26 02:11:26 +00:00
|
|
|
private void TryResumingExecution(SchedulerThread SchedThread)
|
2018-04-19 02:52:23 +00:00
|
|
|
{
|
|
|
|
KThread Thread = SchedThread.Thread;
|
2018-02-19 19:37:13 +00:00
|
|
|
|
2018-04-26 02:11:26 +00:00
|
|
|
PrintDbgThreadInfo(Thread, "trying to resume...");
|
2018-04-19 02:52:23 +00:00
|
|
|
|
2018-04-26 02:11:26 +00:00
|
|
|
SchedThread.WaitActivity.WaitOne();
|
2018-04-21 19:07:16 +00:00
|
|
|
|
|
|
|
lock (SchedLock)
|
2018-04-19 19:18:30 +00:00
|
|
|
{
|
2018-05-13 03:22:42 +00:00
|
|
|
if (AddActiveCore(Thread))
|
2018-04-21 19:07:16 +00:00
|
|
|
{
|
|
|
|
PrintDbgThreadInfo(Thread, "resuming execution...");
|
|
|
|
|
2018-04-26 02:11:26 +00:00
|
|
|
return;
|
2018-04-21 19:07:16 +00:00
|
|
|
}
|
|
|
|
|
2018-05-13 03:22:42 +00:00
|
|
|
WaitingToRun.Push(SchedThread);
|
2018-04-21 19:07:16 +00:00
|
|
|
|
|
|
|
PrintDbgThreadInfo(Thread, "entering wait state...");
|
2018-02-19 19:37:13 +00:00
|
|
|
}
|
2018-02-14 02:43:08 +00:00
|
|
|
|
2018-04-26 02:11:26 +00:00
|
|
|
SchedThread.WaitSched.WaitOne();
|
2018-02-14 02:43:08 +00:00
|
|
|
|
2018-04-19 02:52:23 +00:00
|
|
|
PrintDbgThreadInfo(Thread, "resuming execution...");
|
2018-02-14 02:43:08 +00:00
|
|
|
}
|
|
|
|
|
2018-02-19 19:37:13 +00:00
|
|
|
private void RunThread(SchedulerThread SchedThread)
|
2018-02-14 02:43:08 +00:00
|
|
|
{
|
2018-02-19 19:37:13 +00:00
|
|
|
if (!SchedThread.Thread.Thread.Execute())
|
2018-02-14 02:43:08 +00:00
|
|
|
{
|
2018-05-13 03:22:42 +00:00
|
|
|
PrintDbgThreadInfo(SchedThread.Thread, "waked.");
|
|
|
|
|
2018-04-26 02:11:26 +00:00
|
|
|
SchedThread.WaitSched.Set();
|
2018-02-14 02:43:08 +00:00
|
|
|
}
|
2018-02-19 19:37:13 +00:00
|
|
|
else
|
2018-02-14 02:43:08 +00:00
|
|
|
{
|
2018-04-19 02:52:23 +00:00
|
|
|
PrintDbgThreadInfo(SchedThread.Thread, "running.");
|
2018-02-19 19:37:13 +00:00
|
|
|
}
|
2018-02-14 02:43:08 +00:00
|
|
|
}
|
|
|
|
|
2018-05-13 03:22:42 +00:00
|
|
|
public void Resort(KThread Thread)
|
|
|
|
{
|
|
|
|
if (AllThreads.TryGetValue(Thread, out SchedulerThread SchedThread))
|
|
|
|
{
|
|
|
|
WaitingToRun.Resort(SchedThread);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-21 19:07:16 +00:00
|
|
|
private bool IsActive(KThread Thread)
|
|
|
|
{
|
|
|
|
if (!AllThreads.TryGetValue(Thread, out SchedulerThread SchedThread))
|
|
|
|
{
|
|
|
|
throw new InvalidOperationException();
|
|
|
|
}
|
|
|
|
|
2018-04-26 02:11:26 +00:00
|
|
|
return SchedThread.IsActive;
|
2018-04-21 19:07:16 +00:00
|
|
|
}
|
|
|
|
|
2018-05-13 03:22:42 +00:00
|
|
|
private bool AddActiveCore(KThread Thread)
|
|
|
|
{
|
2018-05-14 06:01:10 +00:00
|
|
|
int CoreMask;
|
|
|
|
|
2018-05-13 03:22:42 +00:00
|
|
|
lock (SchedLock)
|
|
|
|
{
|
|
|
|
//First, try running it on Ideal Core.
|
2018-05-14 06:01:10 +00:00
|
|
|
int IdealCore = Thread.IdealCore;
|
2018-05-13 03:22:42 +00:00
|
|
|
|
2018-05-14 06:01:10 +00:00
|
|
|
if (IdealCore != -1)
|
2018-05-13 03:22:42 +00:00
|
|
|
{
|
2018-05-14 06:01:10 +00:00
|
|
|
CoreMask = 1 << IdealCore;
|
2018-05-13 03:22:42 +00:00
|
|
|
|
2018-05-14 06:01:10 +00:00
|
|
|
if ((ActiveCores & CoreMask) == 0)
|
|
|
|
{
|
|
|
|
ActiveCores |= CoreMask;
|
|
|
|
|
|
|
|
Thread.ActualCore = IdealCore;
|
2018-05-13 03:22:42 +00:00
|
|
|
|
2018-05-14 06:01:10 +00:00
|
|
|
return true;
|
|
|
|
}
|
2018-05-13 03:22:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//If that fails, then try running on any core allowed by Core Mask.
|
|
|
|
CoreMask = Thread.CoreMask & ~ActiveCores;
|
|
|
|
|
|
|
|
if (CoreMask != 0)
|
|
|
|
{
|
|
|
|
CoreMask &= -CoreMask;
|
|
|
|
|
|
|
|
ActiveCores |= CoreMask;
|
|
|
|
|
|
|
|
for (int Bit = 0; Bit < 32; Bit++)
|
|
|
|
{
|
|
|
|
if (((CoreMask >> Bit) & 1) != 0)
|
|
|
|
{
|
|
|
|
Thread.ActualCore = Bit;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new InvalidOperationException();
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void RemoveActiveCore(int Core)
|
|
|
|
{
|
|
|
|
lock (SchedLock)
|
|
|
|
{
|
|
|
|
ActiveCores &= ~(1 << Core);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-19 02:52:23 +00:00
|
|
|
private void PrintDbgThreadInfo(KThread Thread, string Message)
|
2018-02-14 02:43:08 +00:00
|
|
|
{
|
2018-04-24 18:57:39 +00:00
|
|
|
Log.PrintDebug(LogClass.KernelScheduler, "(" +
|
2018-05-14 06:01:10 +00: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 02:43:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
{
|
|
|
|
Dispose(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected virtual void Dispose(bool Disposing)
|
|
|
|
{
|
|
|
|
if (Disposing)
|
|
|
|
{
|
2018-02-19 19:37:13 +00:00
|
|
|
foreach (SchedulerThread SchedThread in AllThreads.Values)
|
2018-02-14 02:43:08 +00:00
|
|
|
{
|
2018-02-19 19:37:13 +00:00
|
|
|
SchedThread.Dispose();
|
2018-02-14 02:43:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|