|
@@ -1,6 +1,7 @@
|
|
|
using Ryujinx.Core.Logging;
|
|
using Ryujinx.Core.Logging;
|
|
|
using System;
|
|
using System;
|
|
|
using System.Collections.Concurrent;
|
|
using System.Collections.Concurrent;
|
|
|
|
|
+using System.Threading;
|
|
|
|
|
|
|
|
namespace Ryujinx.Core.OsHle.Handles
|
|
namespace Ryujinx.Core.OsHle.Handles
|
|
|
{
|
|
{
|
|
@@ -10,7 +11,7 @@ namespace Ryujinx.Core.OsHle.Handles
|
|
|
|
|
|
|
|
private ThreadQueue WaitingToRun;
|
|
private ThreadQueue WaitingToRun;
|
|
|
|
|
|
|
|
- private int ActiveCores;
|
|
|
|
|
|
|
+ private KThread[] CoreThreads;
|
|
|
|
|
|
|
|
private object SchedLock;
|
|
private object SchedLock;
|
|
|
|
|
|
|
@@ -24,6 +25,8 @@ namespace Ryujinx.Core.OsHle.Handles
|
|
|
|
|
|
|
|
WaitingToRun = new ThreadQueue();
|
|
WaitingToRun = new ThreadQueue();
|
|
|
|
|
|
|
|
|
|
+ CoreThreads = new KThread[4];
|
|
|
|
|
+
|
|
|
SchedLock = new object();
|
|
SchedLock = new object();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -38,7 +41,7 @@ namespace Ryujinx.Core.OsHle.Handles
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- if (AddActiveCore(Thread))
|
|
|
|
|
|
|
+ if (TryAddToCore(Thread))
|
|
|
{
|
|
{
|
|
|
Thread.Thread.Execute();
|
|
Thread.Thread.Execute();
|
|
|
|
|
|
|
@@ -74,7 +77,7 @@ namespace Ryujinx.Core.OsHle.Handles
|
|
|
{
|
|
{
|
|
|
Log.PrintDebug(LogClass.KernelScheduler, $"Nothing to run on core {ActualCore}!");
|
|
Log.PrintDebug(LogClass.KernelScheduler, $"Nothing to run on core {ActualCore}!");
|
|
|
|
|
|
|
|
- RemoveActiveCore(ActualCore);
|
|
|
|
|
|
|
+ CoreThreads[ActualCore] = null;
|
|
|
|
|
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
@@ -104,44 +107,38 @@ namespace Ryujinx.Core.OsHle.Handles
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- public void EnterWait(KThread Thread)
|
|
|
|
|
|
|
+ public void EnterWait(KThread Thread, int TimeoutMs = Timeout.Infinite)
|
|
|
{
|
|
{
|
|
|
- if (!AllThreads.TryGetValue(Thread, out SchedulerThread SchedThread))
|
|
|
|
|
- {
|
|
|
|
|
- throw new InvalidOperationException();
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ SchedulerThread SchedThread = AllThreads[Thread];
|
|
|
|
|
|
|
|
Suspend(Thread);
|
|
Suspend(Thread);
|
|
|
|
|
|
|
|
- SchedThread.WaitSync.WaitOne();
|
|
|
|
|
|
|
+ SchedThread.WaitSync.WaitOne(TimeoutMs);
|
|
|
|
|
|
|
|
TryResumingExecution(SchedThread);
|
|
TryResumingExecution(SchedThread);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- public bool EnterWait(KThread Thread, int Timeout)
|
|
|
|
|
|
|
+ public void WakeUp(KThread Thread)
|
|
|
{
|
|
{
|
|
|
- if (!AllThreads.TryGetValue(Thread, out SchedulerThread SchedThread))
|
|
|
|
|
- {
|
|
|
|
|
- throw new InvalidOperationException();
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- Suspend(Thread);
|
|
|
|
|
-
|
|
|
|
|
- bool Result = SchedThread.WaitSync.WaitOne(Timeout);
|
|
|
|
|
-
|
|
|
|
|
- TryResumingExecution(SchedThread);
|
|
|
|
|
-
|
|
|
|
|
- return Result;
|
|
|
|
|
|
|
+ AllThreads[Thread].WaitSync.Set();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- public void WakeUp(KThread Thread)
|
|
|
|
|
|
|
+ public void TryToRun(KThread Thread)
|
|
|
{
|
|
{
|
|
|
- if (!AllThreads.TryGetValue(Thread, out SchedulerThread SchedThread))
|
|
|
|
|
|
|
+ lock (SchedLock)
|
|
|
{
|
|
{
|
|
|
- throw new InvalidOperationException();
|
|
|
|
|
|
|
+ if (AllThreads.TryGetValue(Thread, out SchedulerThread SchedThread))
|
|
|
|
|
+ {
|
|
|
|
|
+ if (WaitingToRun.HasThread(SchedThread) && TryAddToCore(Thread))
|
|
|
|
|
+ {
|
|
|
|
|
+ RunThread(SchedThread);
|
|
|
|
|
+ }
|
|
|
|
|
+ else
|
|
|
|
|
+ {
|
|
|
|
|
+ SetReschedule(Thread.ProcessorId);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
- SchedThread.WaitSync.Set();
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public void Suspend(KThread Thread)
|
|
public void Suspend(KThread Thread)
|
|
@@ -150,6 +147,8 @@ namespace Ryujinx.Core.OsHle.Handles
|
|
|
{
|
|
{
|
|
|
PrintDbgThreadInfo(Thread, "suspended.");
|
|
PrintDbgThreadInfo(Thread, "suspended.");
|
|
|
|
|
|
|
|
|
|
+ AllThreads[Thread].NeedsReschedule = false;
|
|
|
|
|
+
|
|
|
int ActualCore = Thread.ActualCore;
|
|
int ActualCore = Thread.ActualCore;
|
|
|
|
|
|
|
|
SchedulerThread SchedThread = WaitingToRun.Pop(ActualCore);
|
|
SchedulerThread SchedThread = WaitingToRun.Pop(ActualCore);
|
|
@@ -158,84 +157,76 @@ namespace Ryujinx.Core.OsHle.Handles
|
|
|
{
|
|
{
|
|
|
SchedThread.Thread.ActualCore = ActualCore;
|
|
SchedThread.Thread.ActualCore = ActualCore;
|
|
|
|
|
|
|
|
|
|
+ CoreThreads[ActualCore] = SchedThread.Thread;
|
|
|
|
|
+
|
|
|
RunThread(SchedThread);
|
|
RunThread(SchedThread);
|
|
|
}
|
|
}
|
|
|
else
|
|
else
|
|
|
{
|
|
{
|
|
|
Log.PrintDebug(LogClass.KernelScheduler, $"Nothing to run on core {Thread.ActualCore}!");
|
|
Log.PrintDebug(LogClass.KernelScheduler, $"Nothing to run on core {Thread.ActualCore}!");
|
|
|
|
|
|
|
|
- RemoveActiveCore(ActualCore);
|
|
|
|
|
|
|
+ CoreThreads[ActualCore] = null;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public void SetReschedule(int Core)
|
|
|
|
|
+ {
|
|
|
|
|
+ lock (SchedLock)
|
|
|
|
|
+ {
|
|
|
|
|
+ KThread Thread = CoreThreads[Core];
|
|
|
|
|
+
|
|
|
|
|
+ if (Thread != null && AllThreads.TryGetValue(Thread, out SchedulerThread SchedThread))
|
|
|
|
|
+ {
|
|
|
|
|
+ SchedThread.NeedsReschedule = true;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- public void Yield(KThread Thread)
|
|
|
|
|
|
|
+ public void Reschedule(KThread Thread)
|
|
|
{
|
|
{
|
|
|
- PrintDbgThreadInfo(Thread, "yielded execution.");
|
|
|
|
|
|
|
+ SchedulerThread SchedThread = AllThreads[Thread];
|
|
|
|
|
|
|
|
- if (IsActive(Thread))
|
|
|
|
|
|
|
+ bool NeedsReschedule;
|
|
|
|
|
+
|
|
|
|
|
+ lock (SchedLock)
|
|
|
{
|
|
{
|
|
|
|
|
+ NeedsReschedule = SchedThread.NeedsReschedule;
|
|
|
|
|
+
|
|
|
|
|
+ SchedThread.NeedsReschedule = false;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if (NeedsReschedule)
|
|
|
|
|
+ {
|
|
|
|
|
+ PrintDbgThreadInfo(Thread, "yielded execution.");
|
|
|
|
|
+
|
|
|
lock (SchedLock)
|
|
lock (SchedLock)
|
|
|
{
|
|
{
|
|
|
int ActualCore = Thread.ActualCore;
|
|
int ActualCore = Thread.ActualCore;
|
|
|
|
|
|
|
|
- SchedulerThread SchedThread = WaitingToRun.Pop(ActualCore, Thread.ActualPriority);
|
|
|
|
|
|
|
+ SchedulerThread NewThread = WaitingToRun.Pop(ActualCore, Thread.ActualPriority);
|
|
|
|
|
|
|
|
- if (SchedThread == null)
|
|
|
|
|
|
|
+ if (NewThread == null)
|
|
|
{
|
|
{
|
|
|
PrintDbgThreadInfo(Thread, "resumed because theres nothing better to run.");
|
|
PrintDbgThreadInfo(Thread, "resumed because theres nothing better to run.");
|
|
|
|
|
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- if (SchedThread != null)
|
|
|
|
|
- {
|
|
|
|
|
- SchedThread.Thread.ActualCore = ActualCore;
|
|
|
|
|
-
|
|
|
|
|
- RunThread(SchedThread);
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- else
|
|
|
|
|
- {
|
|
|
|
|
- //Just stop running the thread if it's not active,
|
|
|
|
|
- //and run whatever is waiting to run with the higuest priority.
|
|
|
|
|
- Suspend(Thread);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- Resume(Thread);
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ NewThread.Thread.ActualCore = ActualCore;
|
|
|
|
|
|
|
|
- public bool TryRunning(KThread Thread)
|
|
|
|
|
- {
|
|
|
|
|
- //Failing to get the thread here is fine,
|
|
|
|
|
- //the thread may not have been started yet.
|
|
|
|
|
- if (AllThreads.TryGetValue(Thread, out SchedulerThread SchedThread))
|
|
|
|
|
- {
|
|
|
|
|
- lock (SchedLock)
|
|
|
|
|
- {
|
|
|
|
|
- if (WaitingToRun.HasThread(SchedThread) && AddActiveCore(Thread))
|
|
|
|
|
- {
|
|
|
|
|
- WaitingToRun.Remove(SchedThread);
|
|
|
|
|
-
|
|
|
|
|
- RunThread(SchedThread);
|
|
|
|
|
|
|
+ CoreThreads[ActualCore] = NewThread.Thread;
|
|
|
|
|
|
|
|
- return true;
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ RunThread(NewThread);
|
|
|
}
|
|
}
|
|
|
- }
|
|
|
|
|
|
|
|
|
|
- return false;
|
|
|
|
|
|
|
+ TryResumingExecution(SchedThread);
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public void Resume(KThread Thread)
|
|
public void Resume(KThread Thread)
|
|
|
{
|
|
{
|
|
|
- if (!AllThreads.TryGetValue(Thread, out SchedulerThread SchedThread))
|
|
|
|
|
- {
|
|
|
|
|
- throw new InvalidOperationException();
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- TryResumingExecution(SchedThread);
|
|
|
|
|
|
|
+ TryResumingExecution(AllThreads[Thread]);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
private void TryResumingExecution(SchedulerThread SchedThread)
|
|
private void TryResumingExecution(SchedulerThread SchedThread)
|
|
@@ -248,7 +239,7 @@ namespace Ryujinx.Core.OsHle.Handles
|
|
|
|
|
|
|
|
lock (SchedLock)
|
|
lock (SchedLock)
|
|
|
{
|
|
{
|
|
|
- if (AddActiveCore(Thread))
|
|
|
|
|
|
|
+ if (TryAddToCore(Thread))
|
|
|
{
|
|
{
|
|
|
PrintDbgThreadInfo(Thread, "resuming execution...");
|
|
PrintDbgThreadInfo(Thread, "resuming execution...");
|
|
|
|
|
|
|
@@ -257,6 +248,8 @@ namespace Ryujinx.Core.OsHle.Handles
|
|
|
|
|
|
|
|
WaitingToRun.Push(SchedThread);
|
|
WaitingToRun.Push(SchedThread);
|
|
|
|
|
|
|
|
|
|
+ SetReschedule(Thread.ProcessorId);
|
|
|
|
|
+
|
|
|
PrintDbgThreadInfo(Thread, "entering wait state...");
|
|
PrintDbgThreadInfo(Thread, "entering wait state...");
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -287,71 +280,36 @@ namespace Ryujinx.Core.OsHle.Handles
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- private bool IsActive(KThread Thread)
|
|
|
|
|
- {
|
|
|
|
|
- if (!AllThreads.TryGetValue(Thread, out SchedulerThread SchedThread))
|
|
|
|
|
- {
|
|
|
|
|
- throw new InvalidOperationException();
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- return SchedThread.IsActive;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- private bool AddActiveCore(KThread Thread)
|
|
|
|
|
|
|
+ private bool TryAddToCore(KThread Thread)
|
|
|
{
|
|
{
|
|
|
- int CoreMask;
|
|
|
|
|
|
|
+ //First, try running it on Ideal Core.
|
|
|
|
|
+ int IdealCore = Thread.IdealCore;
|
|
|
|
|
|
|
|
- lock (SchedLock)
|
|
|
|
|
|
|
+ if (IdealCore != -1 && CoreThreads[IdealCore] == null)
|
|
|
{
|
|
{
|
|
|
- //First, try running it on Ideal Core.
|
|
|
|
|
- int IdealCore = Thread.IdealCore;
|
|
|
|
|
|
|
+ Thread.ActualCore = IdealCore;
|
|
|
|
|
|
|
|
- if (IdealCore != -1)
|
|
|
|
|
- {
|
|
|
|
|
- CoreMask = 1 << IdealCore;
|
|
|
|
|
-
|
|
|
|
|
- if ((ActiveCores & CoreMask) == 0)
|
|
|
|
|
- {
|
|
|
|
|
- ActiveCores |= CoreMask;
|
|
|
|
|
|
|
+ CoreThreads[IdealCore] = Thread;
|
|
|
|
|
|
|
|
- Thread.ActualCore = IdealCore;
|
|
|
|
|
-
|
|
|
|
|
- return true;
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ return true;
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- //If that fails, then try running on any core allowed by Core Mask.
|
|
|
|
|
- CoreMask = Thread.CoreMask & ~ActiveCores;
|
|
|
|
|
|
|
+ //If that fails, then try running on any core allowed by Core Mask.
|
|
|
|
|
+ int CoreMask = Thread.CoreMask;
|
|
|
|
|
|
|
|
- if (CoreMask != 0)
|
|
|
|
|
|
|
+ for (int Core = 0; Core < CoreThreads.Length; Core++, CoreMask >>= 1)
|
|
|
|
|
+ {
|
|
|
|
|
+ if ((CoreMask & 1) != 0 && CoreThreads[Core] == null)
|
|
|
{
|
|
{
|
|
|
- CoreMask &= -CoreMask;
|
|
|
|
|
|
|
+ Thread.ActualCore = Core;
|
|
|
|
|
|
|
|
- ActiveCores |= CoreMask;
|
|
|
|
|
-
|
|
|
|
|
- for (int Bit = 0; Bit < 32; Bit++)
|
|
|
|
|
- {
|
|
|
|
|
- if (((CoreMask >> Bit) & 1) != 0)
|
|
|
|
|
- {
|
|
|
|
|
- Thread.ActualCore = Bit;
|
|
|
|
|
|
|
+ CoreThreads[Core] = Thread;
|
|
|
|
|
|
|
|
- return true;
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- throw new InvalidOperationException();
|
|
|
|
|
|
|
+ return true;
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
- return false;
|
|
|
|
|
}
|
|
}
|
|
|
- }
|
|
|
|
|
|
|
|
|
|
- private void RemoveActiveCore(int Core)
|
|
|
|
|
- {
|
|
|
|
|
- lock (SchedLock)
|
|
|
|
|
- {
|
|
|
|
|
- ActiveCores &= ~(1 << Core);
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ return false;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
private void PrintDbgThreadInfo(KThread Thread, string Message)
|
|
private void PrintDbgThreadInfo(KThread Thread, string Message)
|