0
0
Fork 0
This repository has been archived on 2024-10-12. You can view files and clone it, but cannot push or open issues or pull requests.
ryujinx-final/Ryujinx.Core/OsHle/Handles/KProcessScheduler.cs

400 lines
11 KiB
C#
Raw Normal View History

2018-04-24 18:57:39 +00:00
using Ryujinx.Core.Logging;
using System;
2018-02-19 19:37:13 +00:00
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Threading;
namespace Ryujinx.Core.OsHle.Handles
{
class KProcessScheduler : IDisposable
{
private const int LowestPriority = 0x40;
private class SchedulerThread : IDisposable
{
public KThread Thread { get; private set; }
public bool IsActive { get; set; }
public AutoResetEvent WaitSync { get; private set; }
public ManualResetEvent WaitActivity { get; private set; }
public AutoResetEvent WaitSched { get; private set; }
public SchedulerThread(KThread Thread)
{
this.Thread = Thread;
IsActive = true;
WaitSync = new AutoResetEvent(false);
WaitActivity = new ManualResetEvent(true);
WaitSched = new AutoResetEvent(false);
}
public void Dispose()
{
Dispose(true);
}
protected virtual void Dispose(bool Disposing)
{
if (Disposing)
{
WaitSync.Dispose();
WaitActivity.Dispose();
WaitSched.Dispose();
}
}
}
2018-02-19 19:37:13 +00:00
private class ThreadQueue
{
private List<SchedulerThread> Threads;
public ThreadQueue()
{
Threads = new List<SchedulerThread>();
}
public void Push(SchedulerThread Thread)
{
lock (Threads)
{
Threads.Add(Thread);
}
}
public SchedulerThread Pop(int MinPriority = LowestPriority)
2018-02-19 19:37:13 +00:00
{
lock (Threads)
{
SchedulerThread SchedThread;
int HighestPriority = MinPriority;
int HighestPrioIndex = -1;
for (int Index = 0; Index < Threads.Count; Index++)
{
SchedThread = Threads[Index];
if (HighestPriority > SchedThread.Thread.ActualPriority)
2018-02-19 19:37:13 +00:00
{
HighestPriority = SchedThread.Thread.ActualPriority;
2018-02-19 19:37:13 +00: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 03:19:22 +00:00
public bool Remove(SchedulerThread SchedThread)
{
lock (Threads)
{
return Threads.Remove(SchedThread);
}
2018-04-19 03:19:22 +00:00
}
2018-02-19 19:37:13 +00:00
}
private ConcurrentDictionary<KThread, SchedulerThread> AllThreads;
2018-02-19 19:37:13 +00:00
private ThreadQueue[] WaitingToRun;
private HashSet<int> ActiveProcessors;
private object SchedLock;
2018-04-24 18:57:39 +00:00
private Logger Log;
public KProcessScheduler(Logger Log)
{
2018-04-24 18:57:39 +00:00
this.Log = Log;
AllThreads = new ConcurrentDictionary<KThread, SchedulerThread>();
2018-02-19 19:37:13 +00:00
WaitingToRun = new ThreadQueue[4];
2018-02-19 19:37:13 +00:00
for (int Index = 0; Index < 4; Index++)
{
2018-02-19 19:37:13 +00:00
WaitingToRun[Index] = new ThreadQueue();
}
ActiveProcessors = new HashSet<int>();
SchedLock = new object();
}
public void StartThread(KThread Thread)
{
lock (SchedLock)
{
2018-02-19 19:37:13 +00:00
SchedulerThread SchedThread = new SchedulerThread(Thread);
if (!AllThreads.TryAdd(Thread, SchedThread))
{
return;
}
if (ActiveProcessors.Add(Thread.ProcessorId))
{
Thread.Thread.Execute();
PrintDbgThreadInfo(Thread, "running.");
}
else
{
2018-02-19 19:37:13 +00:00
WaitingToRun[Thread.ProcessorId].Push(SchedThread);
PrintDbgThreadInfo(Thread, "waiting to run.");
}
}
}
public void RemoveThread(KThread Thread)
{
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))
{
WaitingToRun[Thread.ProcessorId].Remove(SchedThread);
SchedThread.Dispose();
2018-04-19 03:19:22 +00:00
}
SchedulerThread NewThread = WaitingToRun[Thread.ProcessorId].Pop();
if (NewThread == null)
2018-02-19 19:37:13 +00:00
{
2018-04-24 18:57:39 +00:00
Log.PrintDebug(LogClass.KernelScheduler, $"Nothing to run on core {Thread.ProcessorId}!");
ActiveProcessors.Remove(Thread.ProcessorId);
2018-02-19 19:37:13 +00:00
return;
}
RunThread(NewThread);
}
}
public void SetThreadActivity(KThread Thread, bool Active)
{
if (!AllThreads.TryGetValue(Thread, out SchedulerThread SchedThread))
{
throw new InvalidOperationException();
}
SchedThread.IsActive = Active;
if (Active)
{
SchedThread.WaitActivity.Set();
}
else
{
SchedThread.WaitActivity.Reset();
}
}
public void EnterWait(KThread Thread)
{
if (!AllThreads.TryGetValue(Thread, out SchedulerThread SchedThread))
{
throw new InvalidOperationException();
}
Suspend(Thread.ProcessorId);
SchedThread.WaitSync.WaitOne();
TryResumingExecution(SchedThread);
}
public bool EnterWait(KThread Thread, int Timeout)
{
if (!AllThreads.TryGetValue(Thread, out SchedulerThread SchedThread))
{
throw new InvalidOperationException();
}
Suspend(Thread.ProcessorId);
bool Result = SchedThread.WaitSync.WaitOne(Timeout);
TryResumingExecution(SchedThread);
return Result;
}
public void WakeUp(KThread Thread)
{
if (!AllThreads.TryGetValue(Thread, out SchedulerThread SchedThread))
{
throw new InvalidOperationException();
}
SchedThread.WaitSync.Set();
}
public void Suspend(int ProcessorId)
{
lock (SchedLock)
{
SchedulerThread SchedThread = WaitingToRun[ProcessorId].Pop();
2018-02-19 19:37:13 +00:00
if (SchedThread != null)
{
2018-02-19 19:37:13 +00:00
RunThread(SchedThread);
}
2018-02-19 19:37:13 +00:00
else
{
2018-04-24 18:57:39 +00:00
Log.PrintDebug(LogClass.KernelScheduler, $"Nothing to run on core {ProcessorId}!");
2018-02-19 19:37:13 +00:00
ActiveProcessors.Remove(ProcessorId);
}
}
}
public void Yield(KThread Thread)
{
PrintDbgThreadInfo(Thread, "yielded execution.");
2018-02-19 19:37:13 +00:00
if (IsActive(Thread))
{
lock (SchedLock)
{
SchedulerThread SchedThread = WaitingToRun[Thread.ProcessorId].Pop(Thread.ActualPriority);
if (SchedThread == null)
{
PrintDbgThreadInfo(Thread, "resumed because theres nothing better to run.");
return;
}
if (SchedThread != null)
{
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.ProcessorId);
}
2018-02-19 19:37:13 +00:00
Resume(Thread);
}
public void Resume(KThread Thread)
{
if (!AllThreads.TryGetValue(Thread, out SchedulerThread SchedThread))
{
throw new InvalidOperationException();
}
TryResumingExecution(SchedThread);
}
private void TryResumingExecution(SchedulerThread SchedThread)
{
KThread Thread = SchedThread.Thread;
2018-02-19 19:37:13 +00:00
PrintDbgThreadInfo(Thread, "trying to resume...");
SchedThread.WaitActivity.WaitOne();
lock (SchedLock)
{
if (ActiveProcessors.Add(Thread.ProcessorId))
{
PrintDbgThreadInfo(Thread, "resuming execution...");
return;
}
WaitingToRun[Thread.ProcessorId].Push(SchedThread);
PrintDbgThreadInfo(Thread, "entering wait state...");
2018-02-19 19:37:13 +00:00
}
SchedThread.WaitSched.WaitOne();
PrintDbgThreadInfo(Thread, "resuming execution...");
}
2018-02-19 19:37:13 +00:00
private void RunThread(SchedulerThread SchedThread)
{
2018-02-19 19:37:13 +00:00
if (!SchedThread.Thread.Thread.Execute())
{
SchedThread.WaitSched.Set();
}
2018-02-19 19:37:13 +00:00
else
{
PrintDbgThreadInfo(SchedThread.Thread, "running.");
2018-02-19 19:37:13 +00:00
}
}
private bool IsActive(KThread Thread)
{
if (!AllThreads.TryGetValue(Thread, out SchedulerThread SchedThread))
{
throw new InvalidOperationException();
}
return SchedThread.IsActive;
}
private void PrintDbgThreadInfo(KThread Thread, string Message)
{
2018-04-24 18:57:39 +00:00
Log.PrintDebug(LogClass.KernelScheduler, "(" +
"ThreadId = " + Thread.ThreadId + ", " +
"ProcessorId = " + Thread.ProcessorId + ", " +
"ActualPriority = " + Thread.ActualPriority + ", " +
"WantedPriority = " + Thread.WantedPriority + ") " + Message);
}
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-19 19:37:13 +00:00
SchedThread.Dispose();
}
}
}
}
}