| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883 |
- using ChocolArm64;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using static Ryujinx.HLE.HOS.ErrorCode;
- namespace Ryujinx.HLE.HOS.Kernel
- {
- class KThread : KSynchronizationObject, IKFutureSchedulerObject
- {
- public CpuThread Context { get; private set; }
- public long AffinityMask { get; set; }
- public int ThreadId { get; private set; }
- public KSynchronizationObject SignaledObj;
- public long CondVarAddress { get; set; }
- public long MutexAddress { get; set; }
- public Process Owner { get; private set; }
- public long LastScheduledTicks { get; set; }
- public LinkedListNode<KThread>[] SiblingsPerCore { get; private set; }
- private LinkedListNode<KThread> WithholderNode;
- private LinkedList<KThread> MutexWaiters;
- private LinkedListNode<KThread> MutexWaiterNode;
- public KThread MutexOwner { get; private set; }
- public int ThreadHandleForUserMutex { get; set; }
- private ThreadSchedState ForcePauseFlags;
- public int ObjSyncResult { get; set; }
- public int DynamicPriority { get; set; }
- public int CurrentCore { get; set; }
- public int BasePriority { get; set; }
- public int PreferredCore { get; set; }
- private long AffinityMaskOverride;
- private int PreferredCoreOverride;
- private int AffinityOverrideCount;
- public ThreadSchedState SchedFlags { get; private set; }
- public bool ShallBeTerminated { get; private set; }
- public bool SyncCancelled { get; set; }
- public bool WaitingSync { get; set; }
- private bool HasExited;
- public bool WaitingInArbitration { get; set; }
- private KScheduler Scheduler;
- private KSchedulingData SchedulingData;
- public long LastPc { get; set; }
- public KThread(
- CpuThread Thread,
- Process Process,
- Horizon System,
- int ProcessorId,
- int Priority,
- int ThreadId) : base(System)
- {
- this.ThreadId = ThreadId;
- Context = Thread;
- Owner = Process;
- PreferredCore = ProcessorId;
- Scheduler = System.Scheduler;
- SchedulingData = System.Scheduler.SchedulingData;
- SiblingsPerCore = new LinkedListNode<KThread>[KScheduler.CpuCoresCount];
- MutexWaiters = new LinkedList<KThread>();
- AffinityMask = 1 << ProcessorId;
- DynamicPriority = BasePriority = Priority;
- CurrentCore = PreferredCore;
- }
- public long Start()
- {
- long Result = MakeError(ErrorModule.Kernel, KernelErr.ThreadTerminating);
- System.CriticalSectionLock.Lock();
- if (!ShallBeTerminated)
- {
- KThread CurrentThread = System.Scheduler.GetCurrentThread();
- while (SchedFlags != ThreadSchedState.TerminationPending &&
- CurrentThread.SchedFlags != ThreadSchedState.TerminationPending &&
- !CurrentThread.ShallBeTerminated)
- {
- if ((SchedFlags & ThreadSchedState.LowNibbleMask) != ThreadSchedState.None)
- {
- Result = MakeError(ErrorModule.Kernel, KernelErr.InvalidState);
- break;
- }
- if (CurrentThread.ForcePauseFlags == ThreadSchedState.None)
- {
- if (Owner != null && ForcePauseFlags != ThreadSchedState.None)
- {
- CombineForcePauseFlags();
- }
- SetNewSchedFlags(ThreadSchedState.Running);
- Result = 0;
- break;
- }
- else
- {
- CurrentThread.CombineForcePauseFlags();
- System.CriticalSectionLock.Unlock();
- System.CriticalSectionLock.Lock();
- if (CurrentThread.ShallBeTerminated)
- {
- break;
- }
- }
- }
- }
- System.CriticalSectionLock.Unlock();
- return Result;
- }
- public void Exit()
- {
- System.CriticalSectionLock.Lock();
- ForcePauseFlags &= ~ThreadSchedState.ExceptionalMask;
- ExitImpl();
- System.CriticalSectionLock.Unlock();
- }
- private void ExitImpl()
- {
- System.CriticalSectionLock.Lock();
- SetNewSchedFlags(ThreadSchedState.TerminationPending);
- HasExited = true;
- Signal();
- System.CriticalSectionLock.Unlock();
- }
- public long Sleep(long Timeout)
- {
- System.CriticalSectionLock.Lock();
- if (ShallBeTerminated || SchedFlags == ThreadSchedState.TerminationPending)
- {
- System.CriticalSectionLock.Unlock();
- return MakeError(ErrorModule.Kernel, KernelErr.ThreadTerminating);
- }
- SetNewSchedFlags(ThreadSchedState.Paused);
- if (Timeout > 0)
- {
- System.TimeManager.ScheduleFutureInvocation(this, Timeout);
- }
- System.CriticalSectionLock.Unlock();
- if (Timeout > 0)
- {
- System.TimeManager.UnscheduleFutureInvocation(this);
- }
- return 0;
- }
- public void Yield()
- {
- System.CriticalSectionLock.Lock();
- if (SchedFlags != ThreadSchedState.Running)
- {
- System.CriticalSectionLock.Unlock();
- System.Scheduler.ContextSwitch();
- return;
- }
- if (DynamicPriority < KScheduler.PrioritiesCount)
- {
- //Move current thread to the end of the queue.
- SchedulingData.Reschedule(DynamicPriority, CurrentCore, this);
- }
- Scheduler.ThreadReselectionRequested = true;
- System.CriticalSectionLock.Unlock();
- System.Scheduler.ContextSwitch();
- }
- public void YieldWithLoadBalancing()
- {
- System.CriticalSectionLock.Lock();
- int Prio = DynamicPriority;
- int Core = CurrentCore;
- if (SchedFlags != ThreadSchedState.Running)
- {
- System.CriticalSectionLock.Unlock();
- System.Scheduler.ContextSwitch();
- return;
- }
- KThread NextThreadOnCurrentQueue = null;
- if (DynamicPriority < KScheduler.PrioritiesCount)
- {
- //Move current thread to the end of the queue.
- SchedulingData.Reschedule(Prio, Core, this);
- Func<KThread, bool> Predicate = x => x.DynamicPriority == Prio;
- NextThreadOnCurrentQueue = SchedulingData.ScheduledThreads(Core).FirstOrDefault(Predicate);
- }
- IEnumerable<KThread> SuitableCandidates()
- {
- foreach (KThread Thread in SchedulingData.SuggestedThreads(Core))
- {
- int SrcCore = Thread.CurrentCore;
- if (SrcCore >= 0)
- {
- KThread SelectedSrcCore = Scheduler.CoreContexts[SrcCore].SelectedThread;
- if (SelectedSrcCore == Thread || ((SelectedSrcCore?.DynamicPriority ?? 2) < 2))
- {
- continue;
- }
- }
- //If the candidate was scheduled after the current thread, then it's not worth it,
- //unless the priority is higher than the current one.
- if (NextThreadOnCurrentQueue.LastScheduledTicks >= Thread.LastScheduledTicks ||
- NextThreadOnCurrentQueue.DynamicPriority < Thread.DynamicPriority)
- {
- yield return Thread;
- }
- }
- }
- KThread Dst = SuitableCandidates().FirstOrDefault(x => x.DynamicPriority <= Prio);
- if (Dst != null)
- {
- SchedulingData.TransferToCore(Dst.DynamicPriority, Core, Dst);
- Scheduler.ThreadReselectionRequested = true;
- }
- if (this != NextThreadOnCurrentQueue)
- {
- Scheduler.ThreadReselectionRequested = true;
- }
- System.CriticalSectionLock.Unlock();
- System.Scheduler.ContextSwitch();
- }
- public void YieldAndWaitForLoadBalancing()
- {
- System.CriticalSectionLock.Lock();
- if (SchedFlags != ThreadSchedState.Running)
- {
- System.CriticalSectionLock.Unlock();
- System.Scheduler.ContextSwitch();
- return;
- }
- int Core = CurrentCore;
- SchedulingData.TransferToCore(DynamicPriority, -1, this);
- KThread SelectedThread = null;
- if (!SchedulingData.ScheduledThreads(Core).Any())
- {
- foreach (KThread Thread in SchedulingData.SuggestedThreads(Core))
- {
- if (Thread.CurrentCore < 0)
- {
- continue;
- }
- KThread FirstCandidate = SchedulingData.ScheduledThreads(Thread.CurrentCore).FirstOrDefault();
- if (FirstCandidate == Thread)
- {
- continue;
- }
- if (FirstCandidate == null || FirstCandidate.DynamicPriority >= 2)
- {
- SchedulingData.TransferToCore(Thread.DynamicPriority, Core, Thread);
- SelectedThread = Thread;
- }
- break;
- }
- }
- if (SelectedThread != this)
- {
- Scheduler.ThreadReselectionRequested = true;
- }
- System.CriticalSectionLock.Unlock();
- System.Scheduler.ContextSwitch();
- }
- public void SetPriority(int Priority)
- {
- System.CriticalSectionLock.Lock();
- BasePriority = Priority;
- UpdatePriorityInheritance();
- System.CriticalSectionLock.Unlock();
- }
- public long SetActivity(bool Pause)
- {
- long Result = 0;
- System.CriticalSectionLock.Lock();
- ThreadSchedState LowNibble = SchedFlags & ThreadSchedState.LowNibbleMask;
- if (LowNibble != ThreadSchedState.Paused && LowNibble != ThreadSchedState.Running)
- {
- System.CriticalSectionLock.Unlock();
- return MakeError(ErrorModule.Kernel, KernelErr.InvalidState);
- }
- System.CriticalSectionLock.Lock();
- if (!ShallBeTerminated && SchedFlags != ThreadSchedState.TerminationPending)
- {
- if (Pause)
- {
- //Pause, the force pause flag should be clear (thread is NOT paused).
- if ((ForcePauseFlags & ThreadSchedState.ForcePauseFlag) == 0)
- {
- ForcePauseFlags |= ThreadSchedState.ForcePauseFlag;
- CombineForcePauseFlags();
- }
- else
- {
- Result = MakeError(ErrorModule.Kernel, KernelErr.InvalidState);
- }
- }
- else
- {
- //Unpause, the force pause flag should be set (thread is paused).
- if ((ForcePauseFlags & ThreadSchedState.ForcePauseFlag) != 0)
- {
- ThreadSchedState OldForcePauseFlags = ForcePauseFlags;
- ForcePauseFlags &= ~ThreadSchedState.ForcePauseFlag;
- if ((OldForcePauseFlags & ~ThreadSchedState.ForcePauseFlag) == ThreadSchedState.None)
- {
- ThreadSchedState OldSchedFlags = SchedFlags;
- SchedFlags &= ThreadSchedState.LowNibbleMask;
- AdjustScheduling(OldSchedFlags);
- }
- }
- else
- {
- Result = MakeError(ErrorModule.Kernel, KernelErr.InvalidState);
- }
- }
- }
- System.CriticalSectionLock.Unlock();
- System.CriticalSectionLock.Unlock();
- return Result;
- }
- public void CancelSynchronization()
- {
- System.CriticalSectionLock.Lock();
- if ((SchedFlags & ThreadSchedState.LowNibbleMask) != ThreadSchedState.Paused || !WaitingSync)
- {
- SyncCancelled = true;
- }
- else if (WithholderNode != null)
- {
- System.Withholders.Remove(WithholderNode);
- SetNewSchedFlags(ThreadSchedState.Running);
- WithholderNode = null;
- SyncCancelled = true;
- }
- else
- {
- SignaledObj = null;
- ObjSyncResult = (int)MakeError(ErrorModule.Kernel, KernelErr.Cancelled);
- SetNewSchedFlags(ThreadSchedState.Running);
- SyncCancelled = false;
- }
- System.CriticalSectionLock.Unlock();
- }
- public long SetCoreAndAffinityMask(int NewCore, long NewAffinityMask)
- {
- System.CriticalSectionLock.Lock();
- bool UseOverride = AffinityOverrideCount != 0;
- //The value -3 is "do not change the preferred core".
- if (NewCore == -3)
- {
- NewCore = UseOverride ? PreferredCoreOverride : PreferredCore;
- if ((NewAffinityMask & (1 << NewCore)) == 0)
- {
- System.CriticalSectionLock.Unlock();
- return MakeError(ErrorModule.Kernel, KernelErr.InvalidMaskValue);
- }
- }
- if (UseOverride)
- {
- PreferredCoreOverride = NewCore;
- AffinityMaskOverride = NewAffinityMask;
- }
- else
- {
- long OldAffinityMask = AffinityMask;
- PreferredCore = NewCore;
- AffinityMask = NewAffinityMask;
- if (OldAffinityMask != NewAffinityMask)
- {
- int OldCore = CurrentCore;
- if (CurrentCore >= 0 && ((AffinityMask >> CurrentCore) & 1) == 0)
- {
- if (PreferredCore < 0)
- {
- CurrentCore = HighestSetCore(AffinityMask);
- }
- else
- {
- CurrentCore = PreferredCore;
- }
- }
- AdjustSchedulingForNewAffinity(OldAffinityMask, OldCore);
- }
- }
- System.CriticalSectionLock.Unlock();
- return 0;
- }
- private static int HighestSetCore(long Mask)
- {
- for (int Core = KScheduler.CpuCoresCount - 1; Core >= 0; Core--)
- {
- if (((Mask >> Core) & 1) != 0)
- {
- return Core;
- }
- }
- return -1;
- }
- private void CombineForcePauseFlags()
- {
- ThreadSchedState OldFlags = SchedFlags;
- ThreadSchedState LowNibble = SchedFlags & ThreadSchedState.LowNibbleMask;
- SchedFlags = LowNibble | ForcePauseFlags;
- AdjustScheduling(OldFlags);
- }
- private void SetNewSchedFlags(ThreadSchedState NewFlags)
- {
- System.CriticalSectionLock.Lock();
- ThreadSchedState OldFlags = SchedFlags;
- SchedFlags = (OldFlags & ThreadSchedState.HighNibbleMask) | NewFlags;
- if ((OldFlags & ThreadSchedState.LowNibbleMask) != NewFlags)
- {
- AdjustScheduling(OldFlags);
- }
- System.CriticalSectionLock.Unlock();
- }
- public void ReleaseAndResume()
- {
- System.CriticalSectionLock.Lock();
- if ((SchedFlags & ThreadSchedState.LowNibbleMask) == ThreadSchedState.Paused)
- {
- if (WithholderNode != null)
- {
- System.Withholders.Remove(WithholderNode);
- SetNewSchedFlags(ThreadSchedState.Running);
- WithholderNode = null;
- }
- else
- {
- SetNewSchedFlags(ThreadSchedState.Running);
- }
- }
- System.CriticalSectionLock.Unlock();
- }
- public void Reschedule(ThreadSchedState NewFlags)
- {
- System.CriticalSectionLock.Lock();
- ThreadSchedState OldFlags = SchedFlags;
- SchedFlags = (OldFlags & ThreadSchedState.HighNibbleMask) |
- (NewFlags & ThreadSchedState.LowNibbleMask);
- AdjustScheduling(OldFlags);
- System.CriticalSectionLock.Unlock();
- }
- public void AddMutexWaiter(KThread Requester)
- {
- AddToMutexWaitersList(Requester);
- Requester.MutexOwner = this;
- UpdatePriorityInheritance();
- }
- public void RemoveMutexWaiter(KThread Thread)
- {
- if (Thread.MutexWaiterNode?.List != null)
- {
- MutexWaiters.Remove(Thread.MutexWaiterNode);
- }
- Thread.MutexOwner = null;
- UpdatePriorityInheritance();
- }
- public KThread RelinquishMutex(long MutexAddress, out int Count)
- {
- Count = 0;
- if (MutexWaiters.First == null)
- {
- return null;
- }
- KThread NewMutexOwner = null;
- LinkedListNode<KThread> CurrentNode = MutexWaiters.First;
- do
- {
- //Skip all threads that are not waiting for this mutex.
- while (CurrentNode != null && CurrentNode.Value.MutexAddress != MutexAddress)
- {
- CurrentNode = CurrentNode.Next;
- }
- if (CurrentNode == null)
- {
- break;
- }
- LinkedListNode<KThread> NextNode = CurrentNode.Next;
- MutexWaiters.Remove(CurrentNode);
- CurrentNode.Value.MutexOwner = NewMutexOwner;
- if (NewMutexOwner != null)
- {
- //New owner was already selected, re-insert on new owner list.
- NewMutexOwner.AddToMutexWaitersList(CurrentNode.Value);
- }
- else
- {
- //New owner not selected yet, use current thread.
- NewMutexOwner = CurrentNode.Value;
- }
- Count++;
- CurrentNode = NextNode;
- }
- while (CurrentNode != null);
- if (NewMutexOwner != null)
- {
- UpdatePriorityInheritance();
- NewMutexOwner.UpdatePriorityInheritance();
- }
- return NewMutexOwner;
- }
- private void UpdatePriorityInheritance()
- {
- //If any of the threads waiting for the mutex has
- //higher priority than the current thread, then
- //the current thread inherits that priority.
- int HighestPriority = BasePriority;
- if (MutexWaiters.First != null)
- {
- int WaitingDynamicPriority = MutexWaiters.First.Value.DynamicPriority;
- if (WaitingDynamicPriority < HighestPriority)
- {
- HighestPriority = WaitingDynamicPriority;
- }
- }
- if (HighestPriority != DynamicPriority)
- {
- int OldPriority = DynamicPriority;
- DynamicPriority = HighestPriority;
- AdjustSchedulingForNewPriority(OldPriority);
- if (MutexOwner != null)
- {
- //Remove and re-insert to ensure proper sorting based on new priority.
- MutexOwner.MutexWaiters.Remove(MutexWaiterNode);
- MutexOwner.AddToMutexWaitersList(this);
- MutexOwner.UpdatePriorityInheritance();
- }
- }
- }
- private void AddToMutexWaitersList(KThread Thread)
- {
- LinkedListNode<KThread> NextPrio = MutexWaiters.First;
- int CurrentPriority = Thread.DynamicPriority;
- while (NextPrio != null && NextPrio.Value.DynamicPriority <= CurrentPriority)
- {
- NextPrio = NextPrio.Next;
- }
- if (NextPrio != null)
- {
- Thread.MutexWaiterNode = MutexWaiters.AddBefore(NextPrio, Thread);
- }
- else
- {
- Thread.MutexWaiterNode = MutexWaiters.AddLast(Thread);
- }
- }
- private void AdjustScheduling(ThreadSchedState OldFlags)
- {
- if (OldFlags == SchedFlags)
- {
- return;
- }
- if (OldFlags == ThreadSchedState.Running)
- {
- //Was running, now it's stopped.
- if (CurrentCore >= 0)
- {
- SchedulingData.Unschedule(DynamicPriority, CurrentCore, this);
- }
- for (int Core = 0; Core < KScheduler.CpuCoresCount; Core++)
- {
- if (Core != CurrentCore && ((AffinityMask >> Core) & 1) != 0)
- {
- SchedulingData.Unsuggest(DynamicPriority, Core, this);
- }
- }
- }
- else if (SchedFlags == ThreadSchedState.Running)
- {
- //Was stopped, now it's running.
- if (CurrentCore >= 0)
- {
- SchedulingData.Schedule(DynamicPriority, CurrentCore, this);
- }
- for (int Core = 0; Core < KScheduler.CpuCoresCount; Core++)
- {
- if (Core != CurrentCore && ((AffinityMask >> Core) & 1) != 0)
- {
- SchedulingData.Suggest(DynamicPriority, Core, this);
- }
- }
- }
- Scheduler.ThreadReselectionRequested = true;
- }
- private void AdjustSchedulingForNewPriority(int OldPriority)
- {
- if (SchedFlags != ThreadSchedState.Running)
- {
- return;
- }
- //Remove thread from the old priority queues.
- if (CurrentCore >= 0)
- {
- SchedulingData.Unschedule(OldPriority, CurrentCore, this);
- }
- for (int Core = 0; Core < KScheduler.CpuCoresCount; Core++)
- {
- if (Core != CurrentCore && ((AffinityMask >> Core) & 1) != 0)
- {
- SchedulingData.Unsuggest(OldPriority, Core, this);
- }
- }
- //Add thread to the new priority queues.
- KThread CurrentThread = Scheduler.GetCurrentThread();
- if (CurrentCore >= 0)
- {
- if (CurrentThread == this)
- {
- SchedulingData.SchedulePrepend(DynamicPriority, CurrentCore, this);
- }
- else
- {
- SchedulingData.Schedule(DynamicPriority, CurrentCore, this);
- }
- }
- for (int Core = 0; Core < KScheduler.CpuCoresCount; Core++)
- {
- if (Core != CurrentCore && ((AffinityMask >> Core) & 1) != 0)
- {
- SchedulingData.Suggest(DynamicPriority, Core, this);
- }
- }
- Scheduler.ThreadReselectionRequested = true;
- }
- private void AdjustSchedulingForNewAffinity(long OldAffinityMask, int OldCore)
- {
- if (SchedFlags != ThreadSchedState.Running || DynamicPriority >= KScheduler.PrioritiesCount)
- {
- return;
- }
- //Remove from old queues.
- for (int Core = 0; Core < KScheduler.CpuCoresCount; Core++)
- {
- if (((OldAffinityMask >> Core) & 1) != 0)
- {
- if (Core == OldCore)
- {
- SchedulingData.Unschedule(DynamicPriority, Core, this);
- }
- else
- {
- SchedulingData.Unsuggest(DynamicPriority, Core, this);
- }
- }
- }
- //Insert on new queues.
- for (int Core = 0; Core < KScheduler.CpuCoresCount; Core++)
- {
- if (((AffinityMask >> Core) & 1) != 0)
- {
- if (Core == CurrentCore)
- {
- SchedulingData.Schedule(DynamicPriority, Core, this);
- }
- else
- {
- SchedulingData.Suggest(DynamicPriority, Core, this);
- }
- }
- }
- Scheduler.ThreadReselectionRequested = true;
- }
- public override bool IsSignaled()
- {
- return HasExited;
- }
- public void ClearExclusive()
- {
- Owner.Memory.ClearExclusive(CurrentCore);
- }
- public void TimeUp()
- {
- System.CriticalSectionLock.Lock();
- SetNewSchedFlags(ThreadSchedState.Running);
- System.CriticalSectionLock.Unlock();
- }
- }
- }
|