| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400 |
- using Ryujinx.Core.Logging;
- using System;
- 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();
- }
- }
- }
- 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)
- {
- 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)
- {
- HighestPriority = SchedThread.Thread.ActualPriority;
- 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);
- }
- }
- public bool Remove(SchedulerThread SchedThread)
- {
- lock (Threads)
- {
- return Threads.Remove(SchedThread);
- }
- }
- }
- private ConcurrentDictionary<KThread, SchedulerThread> AllThreads;
- private ThreadQueue[] WaitingToRun;
- private HashSet<int> ActiveProcessors;
- private object SchedLock;
- private Logger Log;
- public KProcessScheduler(Logger Log)
- {
- this.Log = Log;
- AllThreads = new ConcurrentDictionary<KThread, SchedulerThread>();
- WaitingToRun = new ThreadQueue[4];
- for (int Index = 0; Index < 4; Index++)
- {
- WaitingToRun[Index] = new ThreadQueue();
- }
- ActiveProcessors = new HashSet<int>();
- SchedLock = new object();
- }
- public void StartThread(KThread Thread)
- {
- lock (SchedLock)
- {
- SchedulerThread SchedThread = new SchedulerThread(Thread);
- if (!AllThreads.TryAdd(Thread, SchedThread))
- {
- return;
- }
- if (ActiveProcessors.Add(Thread.ProcessorId))
- {
- Thread.Thread.Execute();
- PrintDbgThreadInfo(Thread, "running.");
- }
- else
- {
- WaitingToRun[Thread.ProcessorId].Push(SchedThread);
- PrintDbgThreadInfo(Thread, "waiting to run.");
- }
- }
- }
- public void RemoveThread(KThread Thread)
- {
- PrintDbgThreadInfo(Thread, "exited.");
- lock (SchedLock)
- {
- if (AllThreads.TryRemove(Thread, out SchedulerThread SchedThread))
- {
- WaitingToRun[Thread.ProcessorId].Remove(SchedThread);
- SchedThread.Dispose();
- }
- SchedulerThread NewThread = WaitingToRun[Thread.ProcessorId].Pop();
- if (NewThread == null)
- {
- Log.PrintDebug(LogClass.KernelScheduler, $"Nothing to run on core {Thread.ProcessorId}!");
- ActiveProcessors.Remove(Thread.ProcessorId);
- 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();
- if (SchedThread != null)
- {
- RunThread(SchedThread);
- }
- else
- {
- Log.PrintDebug(LogClass.KernelScheduler, $"Nothing to run on core {ProcessorId}!");
- ActiveProcessors.Remove(ProcessorId);
- }
- }
- }
- public void Yield(KThread Thread)
- {
- PrintDbgThreadInfo(Thread, "yielded execution.");
- 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);
- }
- 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;
- 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...");
- }
- SchedThread.WaitSched.WaitOne();
- PrintDbgThreadInfo(Thread, "resuming execution...");
- }
- private void RunThread(SchedulerThread SchedThread)
- {
- if (!SchedThread.Thread.Thread.Execute())
- {
- SchedThread.WaitSched.Set();
- }
- else
- {
- PrintDbgThreadInfo(SchedThread.Thread, "running.");
- }
- }
- 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)
- {
- 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)
- {
- foreach (SchedulerThread SchedThread in AllThreads.Values)
- {
- SchedThread.Dispose();
- }
- }
- }
- }
- }
|