Ryujinx/src/Ryujinx.HLE/HOS/Kernel/Threading/KPriorityQueue.cs

287 lines
8.5 KiB
C#
Raw Normal View History

using System.Collections.Generic;
using System.Numerics;
namespace Ryujinx.HLE.HOS.Kernel.Threading
{
class KPriorityQueue
{
private readonly LinkedList<KThread>[][] _scheduledThreadsPerPrioPerCore;
private readonly LinkedList<KThread>[][] _suggestedThreadsPerPrioPerCore;
private readonly long[] _scheduledPrioritiesPerCore;
private readonly long[] _suggestedPrioritiesPerCore;
public KPriorityQueue()
{
_suggestedThreadsPerPrioPerCore = new LinkedList<KThread>[KScheduler.PrioritiesCount][];
_scheduledThreadsPerPrioPerCore = new LinkedList<KThread>[KScheduler.PrioritiesCount][];
for (int prio = 0; prio < KScheduler.PrioritiesCount; prio++)
{
_suggestedThreadsPerPrioPerCore[prio] = new LinkedList<KThread>[KScheduler.CpuCoresCount];
_scheduledThreadsPerPrioPerCore[prio] = new LinkedList<KThread>[KScheduler.CpuCoresCount];
for (int core = 0; core < KScheduler.CpuCoresCount; core++)
{
_suggestedThreadsPerPrioPerCore[prio][core] = [];
_scheduledThreadsPerPrioPerCore[prio][core] = [];
}
}
_scheduledPrioritiesPerCore = new long[KScheduler.CpuCoresCount];
_suggestedPrioritiesPerCore = new long[KScheduler.CpuCoresCount];
}
public readonly ref struct KThreadEnumerable
{
readonly LinkedList<KThread>[][] _listPerPrioPerCore;
readonly long[] _prios;
readonly int _core;
public KThreadEnumerable(LinkedList<KThread>[][] listPerPrioPerCore, long[] prios, int core)
{
_listPerPrioPerCore = listPerPrioPerCore;
_prios = prios;
_core = core;
}
public Enumerator GetEnumerator()
{
return new Enumerator(_listPerPrioPerCore, _prios, _core);
}
public ref struct Enumerator
{
private readonly LinkedList<KThread>[][] _listPerPrioPerCore;
private readonly int _core;
private long _prioMask;
private int _prio;
private LinkedList<KThread> _list;
private LinkedListNode<KThread> _node;
public Enumerator(LinkedList<KThread>[][] listPerPrioPerCore, long[] prios, int core)
{
_listPerPrioPerCore = listPerPrioPerCore;
_core = core;
_prioMask = prios[core];
_prio = BitOperations.TrailingZeroCount(_prioMask);
_prioMask &= ~(1L << _prio);
}
[Ryujinx.HLE] Address dotnet-format issues (#5380) * dotnet format style --severity info Some changes were manually reverted. * dotnet format analyzers --serverity info Some changes have been minimally adapted. * Restore a few unused methods and variables * Silence dotnet format IDE0060 warnings * Silence dotnet format IDE0052 warnings * Address or silence dotnet format IDE1006 warnings * Address dotnet format CA1816 warnings * Address or silence dotnet format CA2208 warnings * Address or silence dotnet format CA1806 and a few CA1854 warnings * Address dotnet format CA2211 warnings * Address dotnet format CA1822 warnings * Address or silence dotnet format CA1069 warnings * Make dotnet format succeed in style mode * Address or silence dotnet format CA2211 warnings * Address review comments * Address dotnet format CA2208 warnings properly * Make ProcessResult readonly * Address most dotnet format whitespace warnings * Apply dotnet format whitespace formatting A few of them have been manually reverted and the corresponding warning was silenced * Add previously silenced warnings back I have no clue how these disappeared * Revert formatting changes for while and for-loops * Format if-blocks correctly * Run dotnet format style after rebase * Run dotnet format whitespace after rebase * Run dotnet format style after rebase * Run dotnet format analyzers after rebase * Run dotnet format after rebase and remove unused usings - analyzers - style - whitespace * Disable 'prefer switch expression' rule * Add comments to disabled warnings * Fix a few disabled warnings * Fix naming rule violation, Convert shader properties to auto-property and convert values to const * Simplify properties and array initialization, Use const when possible, Remove trailing commas * Start working on disabled warnings * Fix and silence a few dotnet-format warnings again * Run dotnet format after rebase * Use using declaration instead of block syntax * Address IDE0251 warnings * Address a few disabled IDE0060 warnings * Silence IDE0060 in .editorconfig * Revert "Simplify properties and array initialization, Use const when possible, Remove trailing commas" This reverts commit 9462e4136c0a2100dc28b20cf9542e06790aa67e. * dotnet format whitespace after rebase * First dotnet format pass * Fix naming rule violations * Fix typo * Add trailing commas, use targeted new and use array initializer * Fix build issues * Fix remaining build issues * Remove SuppressMessage for CA1069 where possible * Address dotnet format issues * Address formatting issues Co-authored-by: Ac_K <acoustik666@gmail.com> * Add GetHashCode implementation for RenderingSurfaceInfo * Explicitly silence CA1822 for every affected method in Syscall * Address formatting issues in Demangler.cs * Address review feedback Co-authored-by: Ac_K <acoustik666@gmail.com> * Revert marking service methods as static * Next dotnet format pass * Address review feedback --------- Co-authored-by: Ac_K <acoustik666@gmail.com>
2023-07-16 19:31:14 +02:00
public readonly KThread Current => _node?.Value;
public bool MoveNext()
{
_node = _node?.Next;
if (_node == null)
{
if (!MoveNextListAndFirstNode())
{
return false;
}
}
return _node != null;
}
private bool MoveNextListAndFirstNode()
{
if (_prio < KScheduler.PrioritiesCount)
{
_list = _listPerPrioPerCore[_prio][_core];
_node = _list.First;
_prio = BitOperations.TrailingZeroCount(_prioMask);
_prioMask &= ~(1L << _prio);
return true;
}
else
{
_list = null;
_node = null;
return false;
}
}
}
}
public KThreadEnumerable ScheduledThreads(int core)
{
return new KThreadEnumerable(_scheduledThreadsPerPrioPerCore, _scheduledPrioritiesPerCore, core);
}
public KThreadEnumerable SuggestedThreads(int core)
{
return new KThreadEnumerable(_suggestedThreadsPerPrioPerCore, _suggestedPrioritiesPerCore, core);
}
public KThread ScheduledThreadsFirstOrDefault(int core)
{
return ScheduledThreadsElementAtOrDefault(core, 0);
}
public KThread ScheduledThreadsElementAtOrDefault(int core, int index)
{
int currentIndex = 0;
foreach (KThread scheduledThread in ScheduledThreads(core))
{
if (currentIndex == index)
{
return scheduledThread;
}
else
{
currentIndex++;
}
}
return null;
}
public KThread ScheduledThreadsWithDynamicPriorityFirstOrDefault(int core, int dynamicPriority)
{
foreach (KThread scheduledThread in ScheduledThreads(core))
{
if (scheduledThread.DynamicPriority == dynamicPriority)
{
return scheduledThread;
}
}
return null;
}
public bool HasScheduledThreads(int core)
{
return ScheduledThreadsFirstOrDefault(core) != null;
}
public void TransferToCore(int prio, int dstCore, KThread thread)
{
int srcCore = thread.ActiveCore;
if (srcCore == dstCore)
{
return;
}
thread.ActiveCore = dstCore;
if (srcCore >= 0)
{
Unschedule(prio, srcCore, thread);
}
if (dstCore >= 0)
{
Unsuggest(prio, dstCore, thread);
Schedule(prio, dstCore, thread);
}
if (srcCore >= 0)
{
Suggest(prio, srcCore, thread);
}
}
public void Suggest(int prio, int core, KThread thread)
{
if (prio >= KScheduler.PrioritiesCount)
{
return;
}
SuggestedQueue(prio, core).AddFirst(thread.SiblingsPerCore[core]);
_suggestedPrioritiesPerCore[core] |= 1L << prio;
}
public void Unsuggest(int prio, int core, KThread thread)
{
if (prio >= KScheduler.PrioritiesCount)
{
return;
}
LinkedList<KThread> queue = SuggestedQueue(prio, core);
queue.Remove(thread.SiblingsPerCore[core]);
if (queue.First == null)
{
_suggestedPrioritiesPerCore[core] &= ~(1L << prio);
}
}
public void Schedule(int prio, int core, KThread thread)
{
if (prio >= KScheduler.PrioritiesCount)
{
return;
}
ScheduledQueue(prio, core).AddLast(thread.SiblingsPerCore[core]);
_scheduledPrioritiesPerCore[core] |= 1L << prio;
}
public void SchedulePrepend(int prio, int core, KThread thread)
{
if (prio >= KScheduler.PrioritiesCount)
{
return;
}
ScheduledQueue(prio, core).AddFirst(thread.SiblingsPerCore[core]);
_scheduledPrioritiesPerCore[core] |= 1L << prio;
}
public KThread Reschedule(int prio, int core, KThread thread)
{
if (prio >= KScheduler.PrioritiesCount)
{
return null;
}
LinkedList<KThread> queue = ScheduledQueue(prio, core);
queue.Remove(thread.SiblingsPerCore[core]);
queue.AddLast(thread.SiblingsPerCore[core]);
return queue.First.Value;
}
public void Unschedule(int prio, int core, KThread thread)
{
if (prio >= KScheduler.PrioritiesCount)
{
return;
}
LinkedList<KThread> queue = ScheduledQueue(prio, core);
queue.Remove(thread.SiblingsPerCore[core]);
if (queue.First == null)
{
_scheduledPrioritiesPerCore[core] &= ~(1L << prio);
}
}
private LinkedList<KThread> SuggestedQueue(int prio, int core)
{
return _suggestedThreadsPerPrioPerCore[prio][core];
}
private LinkedList<KThread> ScheduledQueue(int prio, int core)
{
return _scheduledThreadsPerPrioPerCore[prio][core];
}
}
[Ryujinx.HLE] Address dotnet-format issues (#5380) * dotnet format style --severity info Some changes were manually reverted. * dotnet format analyzers --serverity info Some changes have been minimally adapted. * Restore a few unused methods and variables * Silence dotnet format IDE0060 warnings * Silence dotnet format IDE0052 warnings * Address or silence dotnet format IDE1006 warnings * Address dotnet format CA1816 warnings * Address or silence dotnet format CA2208 warnings * Address or silence dotnet format CA1806 and a few CA1854 warnings * Address dotnet format CA2211 warnings * Address dotnet format CA1822 warnings * Address or silence dotnet format CA1069 warnings * Make dotnet format succeed in style mode * Address or silence dotnet format CA2211 warnings * Address review comments * Address dotnet format CA2208 warnings properly * Make ProcessResult readonly * Address most dotnet format whitespace warnings * Apply dotnet format whitespace formatting A few of them have been manually reverted and the corresponding warning was silenced * Add previously silenced warnings back I have no clue how these disappeared * Revert formatting changes for while and for-loops * Format if-blocks correctly * Run dotnet format style after rebase * Run dotnet format whitespace after rebase * Run dotnet format style after rebase * Run dotnet format analyzers after rebase * Run dotnet format after rebase and remove unused usings - analyzers - style - whitespace * Disable 'prefer switch expression' rule * Add comments to disabled warnings * Fix a few disabled warnings * Fix naming rule violation, Convert shader properties to auto-property and convert values to const * Simplify properties and array initialization, Use const when possible, Remove trailing commas * Start working on disabled warnings * Fix and silence a few dotnet-format warnings again * Run dotnet format after rebase * Use using declaration instead of block syntax * Address IDE0251 warnings * Address a few disabled IDE0060 warnings * Silence IDE0060 in .editorconfig * Revert "Simplify properties and array initialization, Use const when possible, Remove trailing commas" This reverts commit 9462e4136c0a2100dc28b20cf9542e06790aa67e. * dotnet format whitespace after rebase * First dotnet format pass * Fix naming rule violations * Fix typo * Add trailing commas, use targeted new and use array initializer * Fix build issues * Fix remaining build issues * Remove SuppressMessage for CA1069 where possible * Address dotnet format issues * Address formatting issues Co-authored-by: Ac_K <acoustik666@gmail.com> * Add GetHashCode implementation for RenderingSurfaceInfo * Explicitly silence CA1822 for every affected method in Syscall * Address formatting issues in Demangler.cs * Address review feedback Co-authored-by: Ac_K <acoustik666@gmail.com> * Revert marking service methods as static * Next dotnet format pass * Address review feedback --------- Co-authored-by: Ac_K <acoustik666@gmail.com>
2023-07-16 19:31:14 +02:00
}