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-02-20 21:09:23 +01:00
|
|
|
public class KProcessScheduler : IDisposable
|
2018-02-14 03:43:08 +01:00
|
|
|
{
|
|
|
|
private class SchedulerThread : IDisposable
|
|
|
|
{
|
|
|
|
public HThread Thread { get; private set; }
|
|
|
|
|
|
|
|
public AutoResetEvent WaitEvent { get; private set; }
|
|
|
|
|
|
|
|
public SchedulerThread(HThread Thread)
|
|
|
|
{
|
|
|
|
this.Thread = Thread;
|
|
|
|
|
|
|
|
WaitEvent = new AutoResetEvent(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
{
|
|
|
|
Dispose(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected virtual void Dispose(bool Disposing)
|
|
|
|
{
|
|
|
|
if (Disposing)
|
|
|
|
{
|
|
|
|
WaitEvent.Dispose();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public SchedulerThread Pop(int MinPriority = 0x40)
|
|
|
|
{
|
|
|
|
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.Priority)
|
|
|
|
{
|
|
|
|
HighestPriority = SchedThread.Thread.Priority;
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private ConcurrentDictionary<HThread, 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;
|
|
|
|
|
|
|
|
public KProcessScheduler()
|
|
|
|
{
|
2018-02-19 20:37:13 +01:00
|
|
|
AllThreads = new ConcurrentDictionary<HThread, 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();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void StartThread(HThread Thread)
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ActiveProcessors.Contains(Thread.ProcessorId))
|
|
|
|
{
|
|
|
|
ActiveProcessors.Add(Thread.ProcessorId);
|
|
|
|
|
|
|
|
Thread.Thread.Execute();
|
|
|
|
|
2018-02-19 20:37:13 +01:00
|
|
|
Logging.Debug($"{GetDbgThreadInfo(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
|
|
|
|
|
|
|
Logging.Debug($"{GetDbgThreadInfo(SchedThread.Thread)} waiting to run.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-19 20:37:13 +01:00
|
|
|
public void Suspend(int ProcessorId)
|
2018-02-14 03:43:08 +01:00
|
|
|
{
|
2018-02-19 20:37:13 +01:00
|
|
|
lock (SchedLock)
|
|
|
|
{
|
|
|
|
SchedulerThread SchedThread = WaitingToRun[ProcessorId].Pop();
|
2018-02-14 03:43:08 +01:00
|
|
|
|
2018-02-19 20:37:13 +01:00
|
|
|
if (SchedThread != null)
|
|
|
|
{
|
|
|
|
RunThread(SchedThread);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ActiveProcessors.Remove(ProcessorId);
|
|
|
|
}
|
|
|
|
}
|
2018-02-14 03:43:08 +01:00
|
|
|
}
|
|
|
|
|
2018-02-19 20:37:13 +01:00
|
|
|
public void Resume(HThread CurrThread)
|
2018-02-14 03:43:08 +01:00
|
|
|
{
|
2018-02-19 20:37:13 +01:00
|
|
|
SchedulerThread SchedThread;
|
2018-02-14 03:43:08 +01:00
|
|
|
|
2018-02-19 20:37:13 +01:00
|
|
|
Logging.Debug($"{GetDbgThreadInfo(CurrThread)} entering ipc delay wait state.");
|
2018-02-14 03:43:08 +01:00
|
|
|
|
2018-02-19 20:37:13 +01:00
|
|
|
lock (SchedLock)
|
2018-02-14 03:43:08 +01:00
|
|
|
{
|
2018-02-19 20:37:13 +01:00
|
|
|
if (!AllThreads.TryGetValue(CurrThread, out SchedThread))
|
|
|
|
{
|
|
|
|
Logging.Error($"{GetDbgThreadInfo(CurrThread)} was not found on the scheduler queue!");
|
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-02-19 20:37:13 +01:00
|
|
|
TryResumingExecution(SchedThread);
|
2018-02-14 03:43:08 +01:00
|
|
|
}
|
|
|
|
|
2018-02-19 20:37:13 +01:00
|
|
|
public void WaitForSignal(HThread Thread, int Timeout = -1)
|
2018-02-14 03:43:08 +01:00
|
|
|
{
|
|
|
|
SchedulerThread SchedThread;
|
|
|
|
|
2018-02-19 20:37:13 +01:00
|
|
|
Logging.Debug($"{GetDbgThreadInfo(Thread)} entering signal wait state.");
|
|
|
|
|
2018-02-14 03:43:08 +01:00
|
|
|
lock (SchedLock)
|
|
|
|
{
|
2018-02-19 20:37:13 +01:00
|
|
|
SchedThread = WaitingToRun[Thread.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-02-19 20:37:13 +01:00
|
|
|
ActiveProcessors.Remove(Thread.ProcessorId);
|
2018-02-14 03:43:08 +01:00
|
|
|
}
|
2018-02-19 20:37:13 +01:00
|
|
|
|
|
|
|
if (!AllThreads.TryGetValue(Thread, out SchedThread))
|
2018-02-14 03:43:08 +01:00
|
|
|
{
|
2018-02-19 20:37:13 +01:00
|
|
|
Logging.Error($"{GetDbgThreadInfo(Thread)} was not found on the scheduler queue!");
|
|
|
|
|
|
|
|
return;
|
2018-02-14 03:43:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-19 20:37:13 +01:00
|
|
|
if (Timeout >= 0)
|
2018-02-14 03:43:08 +01:00
|
|
|
{
|
2018-02-19 20:37:13 +01:00
|
|
|
Logging.Debug($"{GetDbgThreadInfo(Thread)} has wait timeout of {Timeout}ms.");
|
2018-02-14 03:43:08 +01:00
|
|
|
|
2018-02-19 20:37:13 +01:00
|
|
|
SchedThread.WaitEvent.WaitOne(Timeout);
|
2018-02-14 03:43:08 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
SchedThread.WaitEvent.WaitOne();
|
|
|
|
}
|
|
|
|
|
2018-02-19 20:37:13 +01:00
|
|
|
TryResumingExecution(SchedThread);
|
2018-02-14 03:43:08 +01:00
|
|
|
}
|
|
|
|
|
2018-02-19 20:37:13 +01:00
|
|
|
private void TryResumingExecution(SchedulerThread SchedThread)
|
2018-02-14 03:43:08 +01:00
|
|
|
{
|
2018-02-19 20:37:13 +01:00
|
|
|
HThread Thread = SchedThread.Thread;
|
|
|
|
|
2018-02-14 03:43:08 +01:00
|
|
|
lock (SchedLock)
|
|
|
|
{
|
2018-02-19 20:37:13 +01:00
|
|
|
if (ActiveProcessors.Add(Thread.ProcessorId))
|
2018-02-14 03:43:08 +01:00
|
|
|
{
|
2018-02-19 20:37:13 +01:00
|
|
|
Logging.Debug($"{GetDbgThreadInfo(Thread)} resuming execution...");
|
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-02-19 20:37:13 +01:00
|
|
|
WaitingToRun[Thread.ProcessorId].Push(SchedThread);
|
2018-02-14 03:43:08 +01:00
|
|
|
}
|
2018-02-19 20:37:13 +01:00
|
|
|
|
|
|
|
SchedThread.WaitEvent.WaitOne();
|
|
|
|
|
|
|
|
Logging.Debug($"{GetDbgThreadInfo(Thread)} resuming execution...");
|
2018-02-14 03:43:08 +01:00
|
|
|
}
|
|
|
|
|
2018-02-19 20:37:13 +01:00
|
|
|
public void Yield(HThread Thread)
|
2018-02-14 03:43:08 +01:00
|
|
|
{
|
2018-02-19 20:37:13 +01:00
|
|
|
SchedulerThread SchedThread;
|
|
|
|
|
|
|
|
Logging.Debug($"{GetDbgThreadInfo(Thread)} yielded execution.");
|
2018-02-14 03:43:08 +01:00
|
|
|
|
|
|
|
lock (SchedLock)
|
|
|
|
{
|
2018-02-19 20:37:13 +01:00
|
|
|
SchedThread = WaitingToRun[Thread.ProcessorId].Pop(Thread.Priority);
|
2018-02-14 03:43:08 +01:00
|
|
|
|
2018-02-19 20:37:13 +01:00
|
|
|
if (SchedThread == null)
|
|
|
|
{
|
|
|
|
Logging.Debug($"{GetDbgThreadInfo(Thread)} resumed because theres nothing better to run.");
|
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-02-19 20:37:13 +01:00
|
|
|
|
|
|
|
RunThread(SchedThread);
|
|
|
|
|
|
|
|
if (!AllThreads.TryGetValue(Thread, out SchedThread))
|
2018-02-14 03:43:08 +01:00
|
|
|
{
|
2018-02-19 20:37:13 +01:00
|
|
|
Logging.Error($"{GetDbgThreadInfo(Thread)} was not found on the scheduler queue!");
|
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-02-19 20:37:13 +01:00
|
|
|
WaitingToRun[Thread.ProcessorId].Push(SchedThread);
|
|
|
|
}
|
2018-02-14 03:43:08 +01:00
|
|
|
|
2018-02-19 20:37:13 +01:00
|
|
|
SchedThread.WaitEvent.WaitOne();
|
2018-02-14 03:43:08 +01:00
|
|
|
|
2018-02-19 20:37:13 +01:00
|
|
|
Logging.Debug($"{GetDbgThreadInfo(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-02-19 20:37:13 +01:00
|
|
|
SchedThread.WaitEvent.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-02-19 20:37:13 +01:00
|
|
|
Logging.Debug($"{GetDbgThreadInfo(SchedThread.Thread)} running.");
|
2018-02-14 03:43:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-19 20:37:13 +01:00
|
|
|
public void Signal(params HThread[] Threads)
|
2018-02-14 03:43:08 +01:00
|
|
|
{
|
2018-02-19 20:37:13 +01:00
|
|
|
lock (SchedLock)
|
|
|
|
{
|
|
|
|
foreach (HThread Thread in Threads)
|
|
|
|
{
|
|
|
|
if (AllThreads.TryGetValue(Thread, out SchedulerThread SchedThread))
|
|
|
|
{
|
|
|
|
if (!WaitingToRun[Thread.ProcessorId].HasThread(SchedThread))
|
|
|
|
{
|
|
|
|
Logging.Debug($"{GetDbgThreadInfo(Thread)} signaled.");
|
|
|
|
|
|
|
|
SchedThread.WaitEvent.Set();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-02-14 03:43:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private string GetDbgThreadInfo(HThread Thread)
|
|
|
|
{
|
|
|
|
return $"Thread {Thread.ThreadId} (core {Thread.ProcessorId}) prio {Thread.Priority}";
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|