2018-09-18 19:36:43 -04:00
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
|
|
namespace Ryujinx.HLE.HOS.Kernel
|
|
|
|
{
|
|
|
|
class KSchedulingData
|
|
|
|
{
|
2018-12-04 19:52:39 -05:00
|
|
|
private LinkedList<KThread>[][] ScheduledThreadsPerPrioPerCore;
|
|
|
|
private LinkedList<KThread>[][] SuggestedThreadsPerPrioPerCore;
|
2018-09-18 19:36:43 -04:00
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
private long[] ScheduledPrioritiesPerCore;
|
|
|
|
private long[] SuggestedPrioritiesPerCore;
|
2018-09-18 19:36:43 -04:00
|
|
|
|
|
|
|
public KSchedulingData()
|
|
|
|
{
|
2018-12-04 19:52:39 -05:00
|
|
|
SuggestedThreadsPerPrioPerCore = new LinkedList<KThread>[KScheduler.PrioritiesCount][];
|
|
|
|
ScheduledThreadsPerPrioPerCore = new LinkedList<KThread>[KScheduler.PrioritiesCount][];
|
2018-09-18 19:36:43 -04:00
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
for (int Prio = 0; Prio < KScheduler.PrioritiesCount; Prio++)
|
2018-09-18 19:36:43 -04:00
|
|
|
{
|
2018-12-04 19:52:39 -05:00
|
|
|
SuggestedThreadsPerPrioPerCore[Prio] = new LinkedList<KThread>[KScheduler.CpuCoresCount];
|
|
|
|
ScheduledThreadsPerPrioPerCore[Prio] = new LinkedList<KThread>[KScheduler.CpuCoresCount];
|
2018-09-18 19:36:43 -04:00
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
for (int Core = 0; Core < KScheduler.CpuCoresCount; Core++)
|
2018-09-18 19:36:43 -04:00
|
|
|
{
|
2018-12-04 19:52:39 -05:00
|
|
|
SuggestedThreadsPerPrioPerCore[Prio][Core] = new LinkedList<KThread>();
|
|
|
|
ScheduledThreadsPerPrioPerCore[Prio][Core] = new LinkedList<KThread>();
|
2018-09-18 19:36:43 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
ScheduledPrioritiesPerCore = new long[KScheduler.CpuCoresCount];
|
|
|
|
SuggestedPrioritiesPerCore = new long[KScheduler.CpuCoresCount];
|
2018-09-18 19:36:43 -04:00
|
|
|
}
|
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
public IEnumerable<KThread> SuggestedThreads(int Core)
|
2018-09-18 19:36:43 -04:00
|
|
|
{
|
2018-12-04 19:52:39 -05:00
|
|
|
return Iterate(SuggestedThreadsPerPrioPerCore, SuggestedPrioritiesPerCore, Core);
|
2018-09-18 19:36:43 -04:00
|
|
|
}
|
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
public IEnumerable<KThread> ScheduledThreads(int Core)
|
2018-09-18 19:36:43 -04:00
|
|
|
{
|
2018-12-04 19:52:39 -05:00
|
|
|
return Iterate(ScheduledThreadsPerPrioPerCore, ScheduledPrioritiesPerCore, Core);
|
2018-09-18 19:36:43 -04:00
|
|
|
}
|
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
private IEnumerable<KThread> Iterate(LinkedList<KThread>[][] ListPerPrioPerCore, long[] Prios, int Core)
|
2018-09-18 19:36:43 -04:00
|
|
|
{
|
2018-12-04 19:52:39 -05:00
|
|
|
long PrioMask = Prios[Core];
|
2018-09-18 19:36:43 -04:00
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
int Prio = CountTrailingZeros(PrioMask);
|
2018-09-18 19:36:43 -04:00
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
PrioMask &= ~(1L << Prio);
|
2018-09-18 19:36:43 -04:00
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
while (Prio < KScheduler.PrioritiesCount)
|
2018-09-18 19:36:43 -04:00
|
|
|
{
|
2018-12-04 19:52:39 -05:00
|
|
|
LinkedList<KThread> List = ListPerPrioPerCore[Prio][Core];
|
2018-09-18 19:36:43 -04:00
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
LinkedListNode<KThread> Node = List.First;
|
2018-09-18 19:36:43 -04:00
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
while (Node != null)
|
2018-09-18 19:36:43 -04:00
|
|
|
{
|
2018-12-04 19:52:39 -05:00
|
|
|
yield return Node.Value;
|
2018-09-18 19:36:43 -04:00
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
Node = Node.Next;
|
2018-09-18 19:36:43 -04:00
|
|
|
}
|
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
Prio = CountTrailingZeros(PrioMask);
|
2018-09-18 19:36:43 -04:00
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
PrioMask &= ~(1L << Prio);
|
2018-09-18 19:36:43 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
private int CountTrailingZeros(long Value)
|
2018-09-18 19:36:43 -04:00
|
|
|
{
|
2018-12-04 19:52:39 -05:00
|
|
|
int Count = 0;
|
2018-09-18 19:36:43 -04:00
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
while (((Value >> Count) & 0xf) == 0 && Count < 64)
|
2018-09-18 19:36:43 -04:00
|
|
|
{
|
2018-12-04 19:52:39 -05:00
|
|
|
Count += 4;
|
2018-09-18 19:36:43 -04:00
|
|
|
}
|
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
while (((Value >> Count) & 1) == 0 && Count < 64)
|
2018-09-18 19:36:43 -04:00
|
|
|
{
|
2018-12-04 19:52:39 -05:00
|
|
|
Count++;
|
2018-09-18 19:36:43 -04:00
|
|
|
}
|
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
return Count;
|
2018-09-18 19:36:43 -04:00
|
|
|
}
|
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
public void TransferToCore(int Prio, int DstCore, KThread Thread)
|
2018-09-18 19:36:43 -04:00
|
|
|
{
|
2018-12-04 19:52:39 -05:00
|
|
|
bool Schedulable = Thread.DynamicPriority < KScheduler.PrioritiesCount;
|
2018-09-18 19:36:43 -04:00
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
int SrcCore = Thread.CurrentCore;
|
2018-09-18 19:36:43 -04:00
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
Thread.CurrentCore = DstCore;
|
2018-09-18 19:36:43 -04:00
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
if (SrcCore == DstCore || !Schedulable)
|
2018-09-18 19:36:43 -04:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
if (SrcCore >= 0)
|
2018-09-18 19:36:43 -04:00
|
|
|
{
|
2018-12-04 19:52:39 -05:00
|
|
|
Unschedule(Prio, SrcCore, Thread);
|
2018-09-18 19:36:43 -04:00
|
|
|
}
|
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
if (DstCore >= 0)
|
2018-09-18 19:36:43 -04:00
|
|
|
{
|
2018-12-04 19:52:39 -05:00
|
|
|
Unsuggest(Prio, DstCore, Thread);
|
|
|
|
Schedule(Prio, DstCore, Thread);
|
2018-09-18 19:36:43 -04:00
|
|
|
}
|
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
if (SrcCore >= 0)
|
2018-09-18 19:36:43 -04:00
|
|
|
{
|
2018-12-04 19:52:39 -05:00
|
|
|
Suggest(Prio, SrcCore, Thread);
|
2018-09-18 19:36:43 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
public void Suggest(int Prio, int Core, KThread Thread)
|
2018-09-18 19:36:43 -04:00
|
|
|
{
|
2018-12-04 19:52:39 -05:00
|
|
|
if (Prio >= KScheduler.PrioritiesCount)
|
2018-09-18 19:36:43 -04:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
Thread.SiblingsPerCore[Core] = SuggestedQueue(Prio, Core).AddFirst(Thread);
|
2018-09-18 19:36:43 -04:00
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
SuggestedPrioritiesPerCore[Core] |= 1L << Prio;
|
2018-09-18 19:36:43 -04:00
|
|
|
}
|
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
public void Unsuggest(int Prio, int Core, KThread Thread)
|
2018-09-18 19:36:43 -04:00
|
|
|
{
|
2018-12-04 19:52:39 -05:00
|
|
|
if (Prio >= KScheduler.PrioritiesCount)
|
2018-09-18 19:36:43 -04:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
LinkedList<KThread> Queue = SuggestedQueue(Prio, Core);
|
2018-09-18 19:36:43 -04:00
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
Queue.Remove(Thread.SiblingsPerCore[Core]);
|
2018-09-18 19:36:43 -04:00
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
if (Queue.First == null)
|
2018-09-18 19:36:43 -04:00
|
|
|
{
|
2018-12-04 19:52:39 -05:00
|
|
|
SuggestedPrioritiesPerCore[Core] &= ~(1L << Prio);
|
2018-09-18 19:36:43 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
public void Schedule(int Prio, int Core, KThread Thread)
|
2018-09-18 19:36:43 -04:00
|
|
|
{
|
2018-12-04 19:52:39 -05:00
|
|
|
if (Prio >= KScheduler.PrioritiesCount)
|
2018-09-18 19:36:43 -04:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
Thread.SiblingsPerCore[Core] = ScheduledQueue(Prio, Core).AddLast(Thread);
|
2018-09-18 19:36:43 -04:00
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
ScheduledPrioritiesPerCore[Core] |= 1L << Prio;
|
2018-09-18 19:36:43 -04:00
|
|
|
}
|
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
public void SchedulePrepend(int Prio, int Core, KThread Thread)
|
2018-09-18 19:36:43 -04:00
|
|
|
{
|
2018-12-04 19:52:39 -05:00
|
|
|
if (Prio >= KScheduler.PrioritiesCount)
|
2018-09-18 19:36:43 -04:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
Thread.SiblingsPerCore[Core] = ScheduledQueue(Prio, Core).AddFirst(Thread);
|
2018-09-18 19:36:43 -04:00
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
ScheduledPrioritiesPerCore[Core] |= 1L << Prio;
|
2018-09-18 19:36:43 -04:00
|
|
|
}
|
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
public void Reschedule(int Prio, int Core, KThread Thread)
|
2018-09-18 19:36:43 -04:00
|
|
|
{
|
2018-12-04 19:52:39 -05:00
|
|
|
LinkedList<KThread> Queue = ScheduledQueue(Prio, Core);
|
2018-09-18 19:36:43 -04:00
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
Queue.Remove(Thread.SiblingsPerCore[Core]);
|
2018-09-18 19:36:43 -04:00
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
Thread.SiblingsPerCore[Core] = Queue.AddLast(Thread);
|
2018-09-18 19:36:43 -04:00
|
|
|
}
|
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
public void Unschedule(int Prio, int Core, KThread Thread)
|
2018-09-18 19:36:43 -04:00
|
|
|
{
|
2018-12-04 19:52:39 -05:00
|
|
|
if (Prio >= KScheduler.PrioritiesCount)
|
2018-09-18 19:36:43 -04:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
LinkedList<KThread> Queue = ScheduledQueue(Prio, Core);
|
2018-09-18 19:36:43 -04:00
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
Queue.Remove(Thread.SiblingsPerCore[Core]);
|
2018-09-18 19:36:43 -04:00
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
if (Queue.First == null)
|
2018-09-18 19:36:43 -04:00
|
|
|
{
|
2018-12-04 19:52:39 -05:00
|
|
|
ScheduledPrioritiesPerCore[Core] &= ~(1L << Prio);
|
2018-09-18 19:36:43 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
private LinkedList<KThread> SuggestedQueue(int Prio, int Core)
|
2018-09-18 19:36:43 -04:00
|
|
|
{
|
2018-12-04 19:52:39 -05:00
|
|
|
return SuggestedThreadsPerPrioPerCore[Prio][Core];
|
2018-09-18 19:36:43 -04:00
|
|
|
}
|
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
private LinkedList<KThread> ScheduledQueue(int Prio, int Core)
|
2018-09-18 19:36:43 -04:00
|
|
|
{
|
2018-12-04 19:52:39 -05:00
|
|
|
return ScheduledThreadsPerPrioPerCore[Prio][Core];
|
2018-09-18 19:36:43 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|