2018-09-18 23:36:43 +00:00
|
|
|
using System.Collections.Generic;
|
2020-12-09 22:20:05 +00:00
|
|
|
using System.Numerics;
|
2018-09-18 23:36:43 +00:00
|
|
|
|
2018-12-18 05:33:36 +00:00
|
|
|
namespace Ryujinx.HLE.HOS.Kernel.Threading
|
2018-09-18 23:36:43 +00:00
|
|
|
{
|
2020-12-09 22:20:05 +00:00
|
|
|
class KPriorityQueue
|
2018-09-18 23:36:43 +00:00
|
|
|
{
|
2023-03-17 12:14:50 +00:00
|
|
|
private readonly LinkedList<KThread>[][] _scheduledThreadsPerPrioPerCore;
|
|
|
|
private readonly LinkedList<KThread>[][] _suggestedThreadsPerPrioPerCore;
|
2018-09-18 23:36:43 +00:00
|
|
|
|
2023-03-17 12:14:50 +00:00
|
|
|
private readonly long[] _scheduledPrioritiesPerCore;
|
|
|
|
private readonly long[] _suggestedPrioritiesPerCore;
|
2018-09-18 23:36:43 +00:00
|
|
|
|
2020-12-09 22:20:05 +00:00
|
|
|
public KPriorityQueue()
|
2018-09-18 23:36:43 +00:00
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
_suggestedThreadsPerPrioPerCore = new LinkedList<KThread>[KScheduler.PrioritiesCount][];
|
|
|
|
_scheduledThreadsPerPrioPerCore = new LinkedList<KThread>[KScheduler.PrioritiesCount][];
|
2018-09-18 23:36:43 +00:00
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
for (int prio = 0; prio < KScheduler.PrioritiesCount; prio++)
|
2018-09-18 23:36:43 +00:00
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
_suggestedThreadsPerPrioPerCore[prio] = new LinkedList<KThread>[KScheduler.CpuCoresCount];
|
|
|
|
_scheduledThreadsPerPrioPerCore[prio] = new LinkedList<KThread>[KScheduler.CpuCoresCount];
|
2018-09-18 23:36:43 +00:00
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
for (int core = 0; core < KScheduler.CpuCoresCount; core++)
|
2018-09-18 23:36:43 +00:00
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
_suggestedThreadsPerPrioPerCore[prio][core] = new LinkedList<KThread>();
|
|
|
|
_scheduledThreadsPerPrioPerCore[prio][core] = new LinkedList<KThread>();
|
2018-09-18 23:36:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
_scheduledPrioritiesPerCore = new long[KScheduler.CpuCoresCount];
|
|
|
|
_suggestedPrioritiesPerCore = new long[KScheduler.CpuCoresCount];
|
2018-09-18 23:36:43 +00:00
|
|
|
}
|
|
|
|
|
2023-03-17 12:14:50 +00:00
|
|
|
public readonly ref struct KThreadEnumerable
|
2018-09-18 23:36:43 +00:00
|
|
|
{
|
2023-03-17 12:14:50 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
public 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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-09-18 23:36:43 +00:00
|
|
|
}
|
|
|
|
|
2023-03-17 12:14:50 +00:00
|
|
|
public KThreadEnumerable ScheduledThreads(int core)
|
2018-09-18 23:36:43 +00:00
|
|
|
{
|
2023-03-17 12:14:50 +00:00
|
|
|
return new KThreadEnumerable(_scheduledThreadsPerPrioPerCore, _scheduledPrioritiesPerCore, core);
|
2018-09-18 23:36:43 +00:00
|
|
|
}
|
|
|
|
|
2023-03-17 12:14:50 +00:00
|
|
|
public KThreadEnumerable SuggestedThreads(int core)
|
2018-09-18 23:36:43 +00:00
|
|
|
{
|
2023-03-17 12:14:50 +00:00
|
|
|
return new KThreadEnumerable(_suggestedThreadsPerPrioPerCore, _suggestedPrioritiesPerCore, core);
|
|
|
|
}
|
2018-09-18 23:36:43 +00:00
|
|
|
|
2023-03-17 12:14:50 +00:00
|
|
|
public KThread ScheduledThreadsFirstOrDefault(int core)
|
|
|
|
{
|
|
|
|
return ScheduledThreadsElementAtOrDefault(core, 0);
|
|
|
|
}
|
2018-09-18 23:36:43 +00:00
|
|
|
|
2023-03-17 12:14:50 +00:00
|
|
|
public KThread ScheduledThreadsElementAtOrDefault(int core, int index)
|
|
|
|
{
|
|
|
|
int currentIndex = 0;
|
|
|
|
foreach (var scheduledThread in ScheduledThreads(core))
|
2018-09-18 23:36:43 +00:00
|
|
|
{
|
2023-03-17 12:14:50 +00:00
|
|
|
if (currentIndex == index)
|
|
|
|
{
|
|
|
|
return scheduledThread;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
currentIndex++;
|
|
|
|
}
|
|
|
|
}
|
2018-09-18 23:36:43 +00:00
|
|
|
|
2023-03-17 12:14:50 +00:00
|
|
|
return null;
|
|
|
|
}
|
2018-09-18 23:36:43 +00:00
|
|
|
|
2023-03-17 12:14:50 +00:00
|
|
|
public KThread ScheduledThreadsWithDynamicPriorityFirstOrDefault(int core, int dynamicPriority)
|
|
|
|
{
|
|
|
|
foreach (var scheduledThread in ScheduledThreads(core))
|
|
|
|
{
|
|
|
|
if (scheduledThread.DynamicPriority == dynamicPriority)
|
2018-09-18 23:36:43 +00:00
|
|
|
{
|
2023-03-17 12:14:50 +00:00
|
|
|
return scheduledThread;
|
2018-09-18 23:36:43 +00:00
|
|
|
}
|
2023-03-17 12:14:50 +00:00
|
|
|
}
|
2018-09-18 23:36:43 +00:00
|
|
|
|
2023-03-17 12:14:50 +00:00
|
|
|
return null;
|
|
|
|
}
|
2018-09-18 23:36:43 +00:00
|
|
|
|
2023-03-17 12:14:50 +00:00
|
|
|
public bool HasScheduledThreads(int core)
|
|
|
|
{
|
|
|
|
return ScheduledThreadsFirstOrDefault(core) != null;
|
2018-09-18 23:36:43 +00:00
|
|
|
}
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
public void TransferToCore(int prio, int dstCore, KThread thread)
|
2018-09-18 23:36:43 +00:00
|
|
|
{
|
2020-12-09 22:20:05 +00:00
|
|
|
int srcCore = thread.ActiveCore;
|
|
|
|
if (srcCore == dstCore)
|
2018-09-18 23:36:43 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-12-09 22:20:05 +00:00
|
|
|
thread.ActiveCore = dstCore;
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
if (srcCore >= 0)
|
2018-09-18 23:36:43 +00:00
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
Unschedule(prio, srcCore, thread);
|
2018-09-18 23:36:43 +00:00
|
|
|
}
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
if (dstCore >= 0)
|
2018-09-18 23:36:43 +00:00
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
Unsuggest(prio, dstCore, thread);
|
|
|
|
Schedule(prio, dstCore, thread);
|
2018-09-18 23:36:43 +00:00
|
|
|
}
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
if (srcCore >= 0)
|
2018-09-18 23:36:43 +00:00
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
Suggest(prio, srcCore, thread);
|
2018-09-18 23:36:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
public void Suggest(int prio, int core, KThread thread)
|
2018-09-18 23:36:43 +00:00
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
if (prio >= KScheduler.PrioritiesCount)
|
2018-09-18 23:36:43 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
thread.SiblingsPerCore[core] = SuggestedQueue(prio, core).AddFirst(thread);
|
2018-09-18 23:36:43 +00:00
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
_suggestedPrioritiesPerCore[core] |= 1L << prio;
|
2018-09-18 23:36:43 +00:00
|
|
|
}
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
public void Unsuggest(int prio, int core, KThread thread)
|
2018-09-18 23:36:43 +00:00
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
if (prio >= KScheduler.PrioritiesCount)
|
2018-09-18 23:36:43 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
LinkedList<KThread> queue = SuggestedQueue(prio, core);
|
2018-09-18 23:36:43 +00:00
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
queue.Remove(thread.SiblingsPerCore[core]);
|
2018-09-18 23:36:43 +00:00
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
if (queue.First == null)
|
2018-09-18 23:36:43 +00:00
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
_suggestedPrioritiesPerCore[core] &= ~(1L << prio);
|
2018-09-18 23:36:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
public void Schedule(int prio, int core, KThread thread)
|
2018-09-18 23:36:43 +00:00
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
if (prio >= KScheduler.PrioritiesCount)
|
2018-09-18 23:36:43 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
thread.SiblingsPerCore[core] = ScheduledQueue(prio, core).AddLast(thread);
|
2018-09-18 23:36:43 +00:00
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
_scheduledPrioritiesPerCore[core] |= 1L << prio;
|
2018-09-18 23:36:43 +00:00
|
|
|
}
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
public void SchedulePrepend(int prio, int core, KThread thread)
|
2018-09-18 23:36:43 +00:00
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
if (prio >= KScheduler.PrioritiesCount)
|
2018-09-18 23:36:43 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
thread.SiblingsPerCore[core] = ScheduledQueue(prio, core).AddFirst(thread);
|
2018-09-18 23:36:43 +00:00
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
_scheduledPrioritiesPerCore[core] |= 1L << prio;
|
2018-09-18 23:36:43 +00:00
|
|
|
}
|
|
|
|
|
2020-12-09 22:20:05 +00:00
|
|
|
public KThread Reschedule(int prio, int core, KThread thread)
|
2018-09-18 23:36:43 +00:00
|
|
|
{
|
2020-12-09 22:20:05 +00:00
|
|
|
if (prio >= KScheduler.PrioritiesCount)
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
LinkedList<KThread> queue = ScheduledQueue(prio, core);
|
2018-09-18 23:36:43 +00:00
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
queue.Remove(thread.SiblingsPerCore[core]);
|
2018-09-18 23:36:43 +00:00
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
thread.SiblingsPerCore[core] = queue.AddLast(thread);
|
2020-12-09 22:20:05 +00:00
|
|
|
|
|
|
|
return queue.First.Value;
|
2018-09-18 23:36:43 +00:00
|
|
|
}
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
public void Unschedule(int prio, int core, KThread thread)
|
2018-09-18 23:36:43 +00:00
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
if (prio >= KScheduler.PrioritiesCount)
|
2018-09-18 23:36:43 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
LinkedList<KThread> queue = ScheduledQueue(prio, core);
|
2018-09-18 23:36:43 +00:00
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
queue.Remove(thread.SiblingsPerCore[core]);
|
2018-09-18 23:36:43 +00:00
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
if (queue.First == null)
|
2018-09-18 23:36:43 +00:00
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
_scheduledPrioritiesPerCore[core] &= ~(1L << prio);
|
2018-09-18 23:36:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
private LinkedList<KThread> SuggestedQueue(int prio, int core)
|
2018-09-18 23:36:43 +00:00
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
return _suggestedThreadsPerPrioPerCore[prio][core];
|
2018-09-18 23:36:43 +00:00
|
|
|
}
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
private LinkedList<KThread> ScheduledQueue(int prio, int core)
|
2018-09-18 23:36:43 +00:00
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
return _scheduledThreadsPerPrioPerCore[prio][core];
|
2018-09-18 23:36:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|