2018-04-24 20:57:39 +02:00
|
|
|
using Ryujinx.Core.Logging;
|
2018-02-14 03:43:08 +01:00
|
|
|
using System;
|
2018-02-19 20:37:13 +01:00
|
|
|
using System.Collections.Concurrent;
|
2018-02-14 03:43:08 +01:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Threading;
|
|
|
|
|
2018-02-20 21:09:23 +01:00
|
|
|
namespace Ryujinx.Core.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-04-19 04:52:23 +02:00
|
|
|
private const int LowestPriority = 0x40;
|
|
|
|
|
2018-02-14 03:43:08 +01:00
|
|
|
private class SchedulerThread : IDisposable
|
|
|
|
{
|
2018-03-19 19:58:46 +01:00
|
|
|
public KThread Thread { get; private set; }
|
2018-02-14 03:43:08 +01:00
|
|
|
|
2018-04-26 04:11:26 +02:00
|
|
|
public bool IsActive { get; set; }
|
2018-02-14 03:43:08 +01:00
|
|
|
|
2018-04-26 04:11:26 +02:00
|
|
|
public AutoResetEvent WaitSync { get; private set; }
|
|
|
|
public ManualResetEvent WaitActivity { get; private set; }
|
|
|
|
public AutoResetEvent WaitSched { get; private set; }
|
2018-04-21 21:07:16 +02:00
|
|
|
|
2018-03-19 19:58:46 +01:00
|
|
|
public SchedulerThread(KThread Thread)
|
2018-02-14 03:43:08 +01:00
|
|
|
{
|
|
|
|
this.Thread = Thread;
|
|
|
|
|
2018-04-26 04:11:26 +02:00
|
|
|
IsActive = true;
|
|
|
|
|
|
|
|
WaitSync = new AutoResetEvent(false);
|
2018-04-19 21:18:30 +02:00
|
|
|
|
2018-04-26 04:11:26 +02:00
|
|
|
WaitActivity = new ManualResetEvent(true);
|
2018-04-21 21:07:16 +02:00
|
|
|
|
2018-04-26 04:11:26 +02:00
|
|
|
WaitSched = new AutoResetEvent(false);
|
2018-02-14 03:43:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
{
|
|
|
|
Dispose(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected virtual void Dispose(bool Disposing)
|
|
|
|
{
|
|
|
|
if (Disposing)
|
|
|
|
{
|
2018-04-26 04:11:26 +02:00
|
|
|
WaitSync.Dispose();
|
|
|
|
|
|
|
|
WaitActivity.Dispose();
|
|
|
|
|
|
|
|
WaitSched.Dispose();
|
2018-02-14 03:43:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-19 20:37:13 +01:00
|
|
|
private class ThreadQueue
|
|
|
|
{
|
|
|
|
private List<SchedulerThread> Threads;
|
|
|
|
|
|
|
|
public ThreadQueue()
|
|
|
|
{
|
|
|
|
Threads = new List<SchedulerThread>();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Push(SchedulerThread Thread)
|
|
|
|
{
|
|
|
|
lock (Threads)
|
|
|
|
{
|
|
|
|
Threads.Add(Thread);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-19 04:52:23 +02:00
|
|
|
public SchedulerThread Pop(int MinPriority = LowestPriority)
|
2018-02-19 20:37:13 +01:00
|
|
|
{
|
|
|
|
lock (Threads)
|
|
|
|
{
|
|
|
|
SchedulerThread SchedThread;
|
|
|
|
|
|
|
|
int HighestPriority = MinPriority;
|
|
|
|
|
|
|
|
int HighestPrioIndex = -1;
|
|
|
|
|
|
|
|
for (int Index = 0; Index < Threads.Count; Index++)
|
|
|
|
{
|
|
|
|
SchedThread = Threads[Index];
|
|
|
|
|
2018-04-21 21:07:16 +02:00
|
|
|
if (HighestPriority > SchedThread.Thread.ActualPriority)
|
2018-02-19 20:37:13 +01:00
|
|
|
{
|
2018-04-21 21:07:16 +02:00
|
|
|
HighestPriority = SchedThread.Thread.ActualPriority;
|
2018-02-19 20:37:13 +01:00
|
|
|
|
|
|
|
HighestPrioIndex = Index;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (HighestPrioIndex == -1)
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
SchedThread = Threads[HighestPrioIndex];
|
|
|
|
|
|
|
|
Threads.RemoveAt(HighestPrioIndex);
|
|
|
|
|
|
|
|
return SchedThread;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool HasThread(SchedulerThread SchedThread)
|
|
|
|
{
|
|
|
|
lock (Threads)
|
|
|
|
{
|
|
|
|
return Threads.Contains(SchedThread);
|
|
|
|
}
|
|
|
|
}
|
2018-04-19 05:19:22 +02:00
|
|
|
|
|
|
|
public bool Remove(SchedulerThread SchedThread)
|
|
|
|
{
|
2018-04-19 21:18:30 +02:00
|
|
|
lock (Threads)
|
|
|
|
{
|
|
|
|
return Threads.Remove(SchedThread);
|
|
|
|
}
|
2018-04-19 05:19:22 +02:00
|
|
|
}
|
2018-02-19 20:37:13 +01:00
|
|
|
}
|
|
|
|
|
2018-03-19 19:58:46 +01:00
|
|
|
private ConcurrentDictionary<KThread, SchedulerThread> AllThreads;
|
2018-02-14 03:43:08 +01:00
|
|
|
|
2018-02-19 20:37:13 +01:00
|
|
|
private ThreadQueue[] WaitingToRun;
|
2018-02-14 03:43:08 +01:00
|
|
|
|
|
|
|
private HashSet<int> ActiveProcessors;
|
|
|
|
|
|
|
|
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-02-19 20:37:13 +01:00
|
|
|
WaitingToRun = new ThreadQueue[4];
|
2018-02-14 03:43:08 +01:00
|
|
|
|
2018-02-19 20:37:13 +01:00
|
|
|
for (int Index = 0; Index < 4; Index++)
|
2018-02-14 03:43:08 +01:00
|
|
|
{
|
2018-02-19 20:37:13 +01:00
|
|
|
WaitingToRun[Index] = new ThreadQueue();
|
2018-02-14 03:43:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ActiveProcessors = new HashSet<int>();
|
|
|
|
|
|
|
|
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-04-19 04:52:23 +02:00
|
|
|
if (ActiveProcessors.Add(Thread.ProcessorId))
|
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-02-19 20:37:13 +01:00
|
|
|
WaitingToRun[Thread.ProcessorId].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))
|
|
|
|
{
|
|
|
|
WaitingToRun[Thread.ProcessorId].Remove(SchedThread);
|
2018-04-19 06:01:03 +02:00
|
|
|
|
|
|
|
SchedThread.Dispose();
|
2018-04-19 05:19:22 +02:00
|
|
|
}
|
|
|
|
|
2018-04-19 04:52:23 +02:00
|
|
|
SchedulerThread NewThread = WaitingToRun[Thread.ProcessorId].Pop();
|
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-04-24 20:57:39 +02:00
|
|
|
Log.PrintDebug(LogClass.KernelScheduler, $"Nothing to run on core {Thread.ProcessorId}!");
|
2018-02-14 03:43:08 +01:00
|
|
|
|
2018-04-19 04:52:23 +02:00
|
|
|
ActiveProcessors.Remove(Thread.ProcessorId);
|
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-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-04-26 04:11:26 +02:00
|
|
|
public void EnterWait(KThread Thread)
|
2018-04-21 21:07:16 +02:00
|
|
|
{
|
|
|
|
if (!AllThreads.TryGetValue(Thread, out SchedulerThread SchedThread))
|
2018-04-19 21:18:30 +02:00
|
|
|
{
|
2018-04-21 21:07:16 +02:00
|
|
|
throw new InvalidOperationException();
|
|
|
|
}
|
2018-04-19 21:18:30 +02:00
|
|
|
|
2018-04-26 04:11:26 +02:00
|
|
|
Suspend(Thread.ProcessorId);
|
2018-04-19 21:18:30 +02:00
|
|
|
|
2018-04-26 04:11:26 +02:00
|
|
|
SchedThread.WaitSync.WaitOne();
|
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-04-26 04:11:26 +02:00
|
|
|
public bool EnterWait(KThread Thread, int Timeout)
|
2018-04-21 21:07:16 +02:00
|
|
|
{
|
|
|
|
if (!AllThreads.TryGetValue(Thread, out SchedulerThread SchedThread))
|
|
|
|
{
|
|
|
|
throw new InvalidOperationException();
|
|
|
|
}
|
|
|
|
|
2018-04-26 04:11:26 +02:00
|
|
|
Suspend(Thread.ProcessorId);
|
2018-04-21 21:07:16 +02:00
|
|
|
|
2018-04-26 04:11:26 +02:00
|
|
|
bool Result = SchedThread.WaitSync.WaitOne(Timeout);
|
2018-04-21 21:07:16 +02:00
|
|
|
|
2018-04-26 04:11:26 +02:00
|
|
|
TryResumingExecution(SchedThread);
|
2018-04-21 21:07:16 +02:00
|
|
|
|
2018-04-26 04:11:26 +02:00
|
|
|
return Result;
|
2018-04-21 21:07:16 +02:00
|
|
|
}
|
2018-04-19 21:18:30 +02:00
|
|
|
|
2018-04-26 04:11:26 +02:00
|
|
|
public void WakeUp(KThread Thread)
|
2018-04-21 21:07:16 +02:00
|
|
|
{
|
2018-04-26 04:11:26 +02:00
|
|
|
if (!AllThreads.TryGetValue(Thread, out SchedulerThread SchedThread))
|
2018-04-21 21:07:16 +02:00
|
|
|
{
|
2018-04-26 04:11:26 +02:00
|
|
|
throw new InvalidOperationException();
|
2018-04-19 21:18:30 +02:00
|
|
|
}
|
2018-04-26 04:11:26 +02:00
|
|
|
|
|
|
|
SchedThread.WaitSync.Set();
|
2018-04-19 21:18:30 +02:00
|
|
|
}
|
|
|
|
|
2018-04-19 04:52:23 +02:00
|
|
|
public void Suspend(int ProcessorId)
|
2018-02-14 03:43:08 +01:00
|
|
|
{
|
|
|
|
lock (SchedLock)
|
|
|
|
{
|
2018-04-19 04:52:23 +02:00
|
|
|
SchedulerThread SchedThread = WaitingToRun[ProcessorId].Pop();
|
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-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-04-24 20:57:39 +02:00
|
|
|
Log.PrintDebug(LogClass.KernelScheduler, $"Nothing to run on core {ProcessorId}!");
|
2018-02-19 20:37:13 +01:00
|
|
|
|
2018-04-19 04:52:23 +02:00
|
|
|
ActiveProcessors.Remove(ProcessorId);
|
2018-02-14 03:43:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-19 04:52:23 +02:00
|
|
|
public void Yield(KThread Thread)
|
2018-02-14 03:43:08 +01:00
|
|
|
{
|
2018-04-19 04:52:23 +02:00
|
|
|
PrintDbgThreadInfo(Thread, "yielded execution.");
|
2018-02-19 20:37:13 +01:00
|
|
|
|
2018-04-26 04:11:26 +02:00
|
|
|
if (IsActive(Thread))
|
2018-02-14 03:43:08 +01:00
|
|
|
{
|
2018-04-26 04:11:26 +02:00
|
|
|
lock (SchedLock)
|
2018-02-14 03:43:08 +01:00
|
|
|
{
|
2018-04-26 04:11:26 +02:00
|
|
|
SchedulerThread SchedThread = WaitingToRun[Thread.ProcessorId].Pop(Thread.ActualPriority);
|
2018-02-14 03:43:08 +01:00
|
|
|
|
2018-04-26 04:11:26 +02:00
|
|
|
if (SchedThread == null)
|
|
|
|
{
|
|
|
|
PrintDbgThreadInfo(Thread, "resumed because theres nothing better to run.");
|
2018-02-14 03:43:08 +01:00
|
|
|
|
2018-04-26 04:11:26 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (SchedThread != null)
|
|
|
|
{
|
|
|
|
RunThread(SchedThread);
|
|
|
|
}
|
2018-04-21 21:07:16 +02:00
|
|
|
}
|
2018-02-14 03:43:08 +01:00
|
|
|
}
|
2018-04-26 04:11:26 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
//Just stop running the thread if it's not active,
|
|
|
|
//and run whatever is waiting to run with the higuest priority.
|
|
|
|
Suspend(Thread.ProcessorId);
|
|
|
|
}
|
2018-02-19 20:37:13 +01:00
|
|
|
|
2018-04-19 04:52:23 +02:00
|
|
|
Resume(Thread);
|
2018-02-14 03:43:08 +01:00
|
|
|
}
|
|
|
|
|
2018-04-26 04:11:26 +02:00
|
|
|
public void Resume(KThread Thread)
|
2018-02-14 03:43:08 +01:00
|
|
|
{
|
2018-04-19 21:18:30 +02:00
|
|
|
if (!AllThreads.TryGetValue(Thread, out SchedulerThread SchedThread))
|
2018-02-14 03:43:08 +01:00
|
|
|
{
|
2018-04-19 04:52:23 +02:00
|
|
|
throw new InvalidOperationException();
|
|
|
|
}
|
2018-02-14 03:43:08 +01:00
|
|
|
|
2018-04-26 04:11:26 +02:00
|
|
|
TryResumingExecution(SchedThread);
|
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-04-21 21:07:16 +02:00
|
|
|
if (ActiveProcessors.Add(Thread.ProcessorId))
|
|
|
|
{
|
|
|
|
PrintDbgThreadInfo(Thread, "resuming execution...");
|
|
|
|
|
2018-04-26 04:11:26 +02:00
|
|
|
return;
|
2018-04-21 21:07:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
WaitingToRun[Thread.ProcessorId].Push(SchedThread);
|
|
|
|
|
|
|
|
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-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-04-21 21:07:16 +02:00
|
|
|
private bool IsActive(KThread Thread)
|
|
|
|
{
|
|
|
|
if (!AllThreads.TryGetValue(Thread, out SchedulerThread SchedThread))
|
|
|
|
{
|
|
|
|
throw new InvalidOperationException();
|
|
|
|
}
|
|
|
|
|
2018-04-26 04:11:26 +02:00
|
|
|
return SchedThread.IsActive;
|
2018-04-21 21:07:16 +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-04-26 04:11:26 +02:00
|
|
|
"ThreadId = " + Thread.ThreadId + ", " +
|
|
|
|
"ProcessorId = " + Thread.ProcessorId + ", " +
|
|
|
|
"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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|