Ryujinx/Ryujinx.HLE/HOS/Kernel/KSchedulingData.cs

207 lines
6 KiB
C#
Raw Normal View History

using System.Collections.Generic;
namespace Ryujinx.HLE.HOS.Kernel
{
class KSchedulingData
{
2018-12-01 21:01:59 +01:00
private LinkedList<KThread>[][] _scheduledThreadsPerPrioPerCore;
private LinkedList<KThread>[][] _suggestedThreadsPerPrioPerCore;
2018-12-01 21:01:59 +01:00
private long[] _scheduledPrioritiesPerCore;
private long[] _suggestedPrioritiesPerCore;
public KSchedulingData()
{
2018-12-01 21:01:59 +01:00
_suggestedThreadsPerPrioPerCore = new LinkedList<KThread>[KScheduler.PrioritiesCount][];
_scheduledThreadsPerPrioPerCore = new LinkedList<KThread>[KScheduler.PrioritiesCount][];
2018-12-01 21:01:59 +01:00
for (int prio = 0; prio < KScheduler.PrioritiesCount; prio++)
{
2018-12-01 21:01:59 +01:00
_suggestedThreadsPerPrioPerCore[prio] = new LinkedList<KThread>[KScheduler.CpuCoresCount];
_scheduledThreadsPerPrioPerCore[prio] = new LinkedList<KThread>[KScheduler.CpuCoresCount];
2018-12-01 21:01:59 +01:00
for (int core = 0; core < KScheduler.CpuCoresCount; core++)
{
2018-12-01 21:01:59 +01:00
_suggestedThreadsPerPrioPerCore[prio][core] = new LinkedList<KThread>();
_scheduledThreadsPerPrioPerCore[prio][core] = new LinkedList<KThread>();
}
}
2018-12-01 21:01:59 +01:00
_scheduledPrioritiesPerCore = new long[KScheduler.CpuCoresCount];
_suggestedPrioritiesPerCore = new long[KScheduler.CpuCoresCount];
}
2018-12-01 21:01:59 +01:00
public IEnumerable<KThread> SuggestedThreads(int core)
{
2018-12-01 21:01:59 +01:00
return Iterate(_suggestedThreadsPerPrioPerCore, _suggestedPrioritiesPerCore, core);
}
2018-12-01 21:01:59 +01:00
public IEnumerable<KThread> ScheduledThreads(int core)
{
2018-12-01 21:01:59 +01:00
return Iterate(_scheduledThreadsPerPrioPerCore, _scheduledPrioritiesPerCore, core);
}
2018-12-01 21:01:59 +01:00
private IEnumerable<KThread> Iterate(LinkedList<KThread>[][] listPerPrioPerCore, long[] prios, int core)
{
2018-12-01 21:01:59 +01:00
long prioMask = prios[core];
2018-12-01 21:01:59 +01:00
int prio = CountTrailingZeros(prioMask);
2018-12-01 21:01:59 +01:00
prioMask &= ~(1L << prio);
2018-12-01 21:01:59 +01:00
while (prio < KScheduler.PrioritiesCount)
{
2018-12-01 21:01:59 +01:00
LinkedList<KThread> list = listPerPrioPerCore[prio][core];
2018-12-01 21:01:59 +01:00
LinkedListNode<KThread> node = list.First;
2018-12-01 21:01:59 +01:00
while (node != null)
{
2018-12-01 21:01:59 +01:00
yield return node.Value;
2018-12-01 21:01:59 +01:00
node = node.Next;
}
2018-12-01 21:01:59 +01:00
prio = CountTrailingZeros(prioMask);
2018-12-01 21:01:59 +01:00
prioMask &= ~(1L << prio);
}
}
2018-12-01 21:01:59 +01:00
private int CountTrailingZeros(long value)
{
2018-12-01 21:01:59 +01:00
int count = 0;
2018-12-01 21:01:59 +01:00
while (((value >> count) & 0xf) == 0 && count < 64)
{
2018-12-01 21:01:59 +01:00
count += 4;
}
2018-12-01 21:01:59 +01:00
while (((value >> count) & 1) == 0 && count < 64)
{
2018-12-01 21:01:59 +01:00
count++;
}
2018-12-01 21:01:59 +01:00
return count;
}
2018-12-01 21:01:59 +01:00
public void TransferToCore(int prio, int dstCore, KThread thread)
{
2018-12-01 21:01:59 +01:00
bool schedulable = thread.DynamicPriority < KScheduler.PrioritiesCount;
2018-12-01 21:01:59 +01:00
int srcCore = thread.CurrentCore;
2018-12-01 21:01:59 +01:00
thread.CurrentCore = dstCore;
2018-12-01 21:01:59 +01:00
if (srcCore == dstCore || !schedulable)
{
return;
}
2018-12-01 21:01:59 +01:00
if (srcCore >= 0)
{
2018-12-01 21:01:59 +01:00
Unschedule(prio, srcCore, thread);
}
2018-12-01 21:01:59 +01:00
if (dstCore >= 0)
{
2018-12-01 21:01:59 +01:00
Unsuggest(prio, dstCore, thread);
Schedule(prio, dstCore, thread);
}
2018-12-01 21:01:59 +01:00
if (srcCore >= 0)
{
2018-12-01 21:01:59 +01:00
Suggest(prio, srcCore, thread);
}
}
2018-12-01 21:01:59 +01:00
public void Suggest(int prio, int core, KThread thread)
{
2018-12-01 21:01:59 +01:00
if (prio >= KScheduler.PrioritiesCount)
{
return;
}
2018-12-01 21:01:59 +01:00
thread.SiblingsPerCore[core] = SuggestedQueue(prio, core).AddFirst(thread);
2018-12-01 21:01:59 +01:00
_suggestedPrioritiesPerCore[core] |= 1L << prio;
}
2018-12-01 21:01:59 +01:00
public void Unsuggest(int prio, int core, KThread thread)
{
2018-12-01 21:01:59 +01:00
if (prio >= KScheduler.PrioritiesCount)
{
return;
}
2018-12-01 21:01:59 +01:00
LinkedList<KThread> queue = SuggestedQueue(prio, core);
2018-12-01 21:01:59 +01:00
queue.Remove(thread.SiblingsPerCore[core]);
2018-12-01 21:01:59 +01:00
if (queue.First == null)
{
2018-12-01 21:01:59 +01:00
_suggestedPrioritiesPerCore[core] &= ~(1L << prio);
}
}
2018-12-01 21:01:59 +01:00
public void Schedule(int prio, int core, KThread thread)
{
2018-12-01 21:01:59 +01:00
if (prio >= KScheduler.PrioritiesCount)
{
return;
}
2018-12-01 21:01:59 +01:00
thread.SiblingsPerCore[core] = ScheduledQueue(prio, core).AddLast(thread);
2018-12-01 21:01:59 +01:00
_scheduledPrioritiesPerCore[core] |= 1L << prio;
}
2018-12-01 21:01:59 +01:00
public void SchedulePrepend(int prio, int core, KThread thread)
{
2018-12-01 21:01:59 +01:00
if (prio >= KScheduler.PrioritiesCount)
{
return;
}
2018-12-01 21:01:59 +01:00
thread.SiblingsPerCore[core] = ScheduledQueue(prio, core).AddFirst(thread);
2018-12-01 21:01:59 +01:00
_scheduledPrioritiesPerCore[core] |= 1L << prio;
}
2018-12-01 21:01:59 +01:00
public void Reschedule(int prio, int core, KThread thread)
{
2018-12-01 21:01:59 +01:00
LinkedList<KThread> queue = ScheduledQueue(prio, core);
2018-12-01 21:01:59 +01:00
queue.Remove(thread.SiblingsPerCore[core]);
2018-12-01 21:01:59 +01:00
thread.SiblingsPerCore[core] = queue.AddLast(thread);
}
2018-12-01 21:01:59 +01:00
public void Unschedule(int prio, int core, KThread thread)
{
2018-12-01 21:01:59 +01:00
if (prio >= KScheduler.PrioritiesCount)
{
return;
}
2018-12-01 21:01:59 +01:00
LinkedList<KThread> queue = ScheduledQueue(prio, core);
2018-12-01 21:01:59 +01:00
queue.Remove(thread.SiblingsPerCore[core]);
2018-12-01 21:01:59 +01:00
if (queue.First == null)
{
2018-12-01 21:01:59 +01:00
_scheduledPrioritiesPerCore[core] &= ~(1L << prio);
}
}
2018-12-01 21:01:59 +01:00
private LinkedList<KThread> SuggestedQueue(int prio, int core)
{
2018-12-01 21:01:59 +01:00
return _suggestedThreadsPerPrioPerCore[prio][core];
}
2018-12-01 21:01:59 +01:00
private LinkedList<KThread> ScheduledQueue(int prio, int core)
{
2018-12-01 21:01:59 +01:00
return _scheduledThreadsPerPrioPerCore[prio][core];
}
}
}