Move kernel state out of the Horizon class (#1107)

* Move kernel state from Horizon to KernelContext

* Merge syscalls partial classes, split 32 and 64-bit variants

* Sort usings
This commit is contained in:
gdkchan 2020-05-04 00:41:29 -03:00 committed by GitHub
parent cd48576f58
commit 15d1cc806b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
68 changed files with 3678 additions and 3570 deletions

View file

@ -10,40 +10,40 @@ namespace Ryujinx.HLE.HOS.Kernel.Threading
{
private const int HasListenersMask = 0x40000000;
private Horizon _system;
private readonly KernelContext _context;
public List<KThread> CondVarThreads;
public List<KThread> ArbiterThreads;
private readonly List<KThread> _condVarThreads;
private readonly List<KThread> _arbiterThreads;
public KAddressArbiter(Horizon system)
public KAddressArbiter(KernelContext context)
{
_system = system;
_context = context;
CondVarThreads = new List<KThread>();
ArbiterThreads = new List<KThread>();
_condVarThreads = new List<KThread>();
_arbiterThreads = new List<KThread>();
}
public KernelResult ArbitrateLock(int ownerHandle, ulong mutexAddress, int requesterHandle)
{
KThread currentThread = _system.Scheduler.GetCurrentThread();
KThread currentThread = _context.Scheduler.GetCurrentThread();
_system.CriticalSection.Enter();
_context.CriticalSection.Enter();
currentThread.SignaledObj = null;
currentThread.ObjSyncResult = KernelResult.Success;
KProcess currentProcess = _system.Scheduler.GetCurrentProcess();
KProcess currentProcess = _context.Scheduler.GetCurrentProcess();
if (!KernelTransfer.UserToKernelInt32(_system, mutexAddress, out int mutexValue))
if (!KernelTransfer.UserToKernelInt32(_context, mutexAddress, out int mutexValue))
{
_system.CriticalSection.Leave();
_context.CriticalSection.Leave();
return KernelResult.InvalidMemState;
}
if (mutexValue != (ownerHandle | HasListenersMask))
{
_system.CriticalSection.Leave();
_context.CriticalSection.Leave();
return 0;
}
@ -52,7 +52,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Threading
if (mutexOwner == null)
{
_system.CriticalSection.Leave();
_context.CriticalSection.Leave();
return KernelResult.InvalidHandle;
}
@ -64,24 +64,24 @@ namespace Ryujinx.HLE.HOS.Kernel.Threading
currentThread.Reschedule(ThreadSchedState.Paused);
_system.CriticalSection.Leave();
_system.CriticalSection.Enter();
_context.CriticalSection.Leave();
_context.CriticalSection.Enter();
if (currentThread.MutexOwner != null)
{
currentThread.MutexOwner.RemoveMutexWaiter(currentThread);
}
_system.CriticalSection.Leave();
_context.CriticalSection.Leave();
return (KernelResult)currentThread.ObjSyncResult;
return currentThread.ObjSyncResult;
}
public KernelResult ArbitrateUnlock(ulong mutexAddress)
{
_system.CriticalSection.Enter();
_context.CriticalSection.Enter();
KThread currentThread = _system.Scheduler.GetCurrentThread();
KThread currentThread = _context.Scheduler.GetCurrentThread();
(KernelResult result, KThread newOwnerThread) = MutexUnlock(currentThread, mutexAddress);
@ -91,7 +91,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Threading
newOwnerThread.ObjSyncResult = result;
}
_system.CriticalSection.Leave();
_context.CriticalSection.Leave();
return result;
}
@ -102,9 +102,9 @@ namespace Ryujinx.HLE.HOS.Kernel.Threading
int threadHandle,
long timeout)
{
_system.CriticalSection.Enter();
_context.CriticalSection.Enter();
KThread currentThread = _system.Scheduler.GetCurrentThread();
KThread currentThread = _context.Scheduler.GetCurrentThread();
currentThread.SignaledObj = null;
currentThread.ObjSyncResult = KernelResult.TimedOut;
@ -112,7 +112,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Threading
if (currentThread.ShallBeTerminated ||
currentThread.SchedFlags == ThreadSchedState.TerminationPending)
{
_system.CriticalSection.Leave();
_context.CriticalSection.Leave();
return KernelResult.ThreadTerminating;
}
@ -121,7 +121,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Threading
if (result != KernelResult.Success)
{
_system.CriticalSection.Leave();
_context.CriticalSection.Leave();
return result;
}
@ -130,7 +130,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Threading
currentThread.ThreadHandleForUserMutex = threadHandle;
currentThread.CondVarAddress = condVarAddress;
CondVarThreads.Add(currentThread);
_condVarThreads.Add(currentThread);
if (timeout != 0)
{
@ -138,29 +138,29 @@ namespace Ryujinx.HLE.HOS.Kernel.Threading
if (timeout > 0)
{
_system.TimeManager.ScheduleFutureInvocation(currentThread, timeout);
_context.TimeManager.ScheduleFutureInvocation(currentThread, timeout);
}
}
_system.CriticalSection.Leave();
_context.CriticalSection.Leave();
if (timeout > 0)
{
_system.TimeManager.UnscheduleFutureInvocation(currentThread);
_context.TimeManager.UnscheduleFutureInvocation(currentThread);
}
_system.CriticalSection.Enter();
_context.CriticalSection.Enter();
if (currentThread.MutexOwner != null)
{
currentThread.MutexOwner.RemoveMutexWaiter(currentThread);
}
CondVarThreads.Remove(currentThread);
_condVarThreads.Remove(currentThread);
_system.CriticalSection.Leave();
_context.CriticalSection.Leave();
return (KernelResult)currentThread.ObjSyncResult;
return currentThread.ObjSyncResult;
}
private (KernelResult, KThread) MutexUnlock(KThread currentThread, ulong mutexAddress)
@ -186,7 +186,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Threading
KernelResult result = KernelResult.Success;
if (!KernelTransfer.KernelToUserInt32(_system, mutexAddress, mutexValue))
if (!KernelTransfer.KernelToUserInt32(_context, mutexAddress, mutexValue))
{
result = KernelResult.InvalidMemState;
}
@ -198,9 +198,9 @@ namespace Ryujinx.HLE.HOS.Kernel.Threading
{
Queue<KThread> signaledThreads = new Queue<KThread>();
_system.CriticalSection.Enter();
_context.CriticalSection.Enter();
IOrderedEnumerable<KThread> sortedThreads = CondVarThreads.OrderBy(x => x.DynamicPriority);
IOrderedEnumerable<KThread> sortedThreads = _condVarThreads.OrderBy(x => x.DynamicPriority);
foreach (KThread thread in sortedThreads.Where(x => x.CondVarAddress == address))
{
@ -217,17 +217,17 @@ namespace Ryujinx.HLE.HOS.Kernel.Threading
while (signaledThreads.TryDequeue(out KThread thread))
{
CondVarThreads.Remove(thread);
_condVarThreads.Remove(thread);
}
_system.CriticalSection.Leave();
_context.CriticalSection.Leave();
}
private KThread TryAcquireMutex(KThread requester)
{
ulong address = requester.MutexAddress;
KProcess currentProcess = _system.Scheduler.GetCurrentProcess();
KProcess currentProcess = _context.Scheduler.GetCurrentProcess();
if (!currentProcess.CpuMemory.IsMapped(address))
{
@ -293,14 +293,14 @@ namespace Ryujinx.HLE.HOS.Kernel.Threading
public KernelResult WaitForAddressIfEqual(ulong address, int value, long timeout)
{
KThread currentThread = _system.Scheduler.GetCurrentThread();
KThread currentThread = _context.Scheduler.GetCurrentThread();
_system.CriticalSection.Enter();
_context.CriticalSection.Enter();
if (currentThread.ShallBeTerminated ||
currentThread.SchedFlags == ThreadSchedState.TerminationPending)
{
_system.CriticalSection.Leave();
_context.CriticalSection.Leave();
return KernelResult.ThreadTerminating;
}
@ -308,9 +308,9 @@ namespace Ryujinx.HLE.HOS.Kernel.Threading
currentThread.SignaledObj = null;
currentThread.ObjSyncResult = KernelResult.TimedOut;
if (!KernelTransfer.UserToKernelInt32(_system, address, out int currentValue))
if (!KernelTransfer.UserToKernelInt32(_context, address, out int currentValue))
{
_system.CriticalSection.Leave();
_context.CriticalSection.Leave();
return KernelResult.InvalidMemState;
}
@ -319,7 +319,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Threading
{
if (timeout == 0)
{
_system.CriticalSection.Leave();
_context.CriticalSection.Leave();
return KernelResult.TimedOut;
}
@ -327,37 +327,37 @@ namespace Ryujinx.HLE.HOS.Kernel.Threading
currentThread.MutexAddress = address;
currentThread.WaitingInArbitration = true;
InsertSortedByPriority(ArbiterThreads, currentThread);
InsertSortedByPriority(_arbiterThreads, currentThread);
currentThread.Reschedule(ThreadSchedState.Paused);
if (timeout > 0)
{
_system.TimeManager.ScheduleFutureInvocation(currentThread, timeout);
_context.TimeManager.ScheduleFutureInvocation(currentThread, timeout);
}
_system.CriticalSection.Leave();
_context.CriticalSection.Leave();
if (timeout > 0)
{
_system.TimeManager.UnscheduleFutureInvocation(currentThread);
_context.TimeManager.UnscheduleFutureInvocation(currentThread);
}
_system.CriticalSection.Enter();
_context.CriticalSection.Enter();
if (currentThread.WaitingInArbitration)
{
ArbiterThreads.Remove(currentThread);
_arbiterThreads.Remove(currentThread);
currentThread.WaitingInArbitration = false;
}
_system.CriticalSection.Leave();
_context.CriticalSection.Leave();
return (KernelResult)currentThread.ObjSyncResult;
return currentThread.ObjSyncResult;
}
_system.CriticalSection.Leave();
_context.CriticalSection.Leave();
return KernelResult.InvalidState;
}
@ -368,14 +368,14 @@ namespace Ryujinx.HLE.HOS.Kernel.Threading
bool shouldDecrement,
long timeout)
{
KThread currentThread = _system.Scheduler.GetCurrentThread();
KThread currentThread = _context.Scheduler.GetCurrentThread();
_system.CriticalSection.Enter();
_context.CriticalSection.Enter();
if (currentThread.ShallBeTerminated ||
currentThread.SchedFlags == ThreadSchedState.TerminationPending)
{
_system.CriticalSection.Leave();
_context.CriticalSection.Leave();
return KernelResult.ThreadTerminating;
}
@ -383,11 +383,11 @@ namespace Ryujinx.HLE.HOS.Kernel.Threading
currentThread.SignaledObj = null;
currentThread.ObjSyncResult = KernelResult.TimedOut;
KProcess currentProcess = _system.Scheduler.GetCurrentProcess();
KProcess currentProcess = _context.Scheduler.GetCurrentProcess();
if (!KernelTransfer.UserToKernelInt32(_system, address, out int currentValue))
if (!KernelTransfer.UserToKernelInt32(_context, address, out int currentValue))
{
_system.CriticalSection.Leave();
_context.CriticalSection.Leave();
return KernelResult.InvalidMemState;
}
@ -401,7 +401,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Threading
{
if (timeout == 0)
{
_system.CriticalSection.Leave();
_context.CriticalSection.Leave();
return KernelResult.TimedOut;
}
@ -409,37 +409,37 @@ namespace Ryujinx.HLE.HOS.Kernel.Threading
currentThread.MutexAddress = address;
currentThread.WaitingInArbitration = true;
InsertSortedByPriority(ArbiterThreads, currentThread);
InsertSortedByPriority(_arbiterThreads, currentThread);
currentThread.Reschedule(ThreadSchedState.Paused);
if (timeout > 0)
{
_system.TimeManager.ScheduleFutureInvocation(currentThread, timeout);
_context.TimeManager.ScheduleFutureInvocation(currentThread, timeout);
}
_system.CriticalSection.Leave();
_context.CriticalSection.Leave();
if (timeout > 0)
{
_system.TimeManager.UnscheduleFutureInvocation(currentThread);
_context.TimeManager.UnscheduleFutureInvocation(currentThread);
}
_system.CriticalSection.Enter();
_context.CriticalSection.Enter();
if (currentThread.WaitingInArbitration)
{
ArbiterThreads.Remove(currentThread);
_arbiterThreads.Remove(currentThread);
currentThread.WaitingInArbitration = false;
}
_system.CriticalSection.Leave();
_context.CriticalSection.Leave();
return (KernelResult)currentThread.ObjSyncResult;
return currentThread.ObjSyncResult;
}
_system.CriticalSection.Leave();
_context.CriticalSection.Leave();
return KernelResult.InvalidState;
}
@ -470,24 +470,24 @@ namespace Ryujinx.HLE.HOS.Kernel.Threading
public KernelResult Signal(ulong address, int count)
{
_system.CriticalSection.Enter();
_context.CriticalSection.Enter();
WakeArbiterThreads(address, count);
_system.CriticalSection.Leave();
_context.CriticalSection.Leave();
return KernelResult.Success;
}
public KernelResult SignalAndIncrementIfEqual(ulong address, int value, int count)
{
_system.CriticalSection.Enter();
_context.CriticalSection.Enter();
KProcess currentProcess = _system.Scheduler.GetCurrentProcess();
KProcess currentProcess = _context.Scheduler.GetCurrentProcess();
if (!currentProcess.CpuMemory.IsMapped(address))
{
_system.CriticalSection.Leave();
_context.CriticalSection.Leave();
return KernelResult.InvalidMemState;
}
@ -502,7 +502,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Threading
if (currentValue != value)
{
_system.CriticalSection.Leave();
_context.CriticalSection.Leave();
return KernelResult.InvalidState;
}
@ -511,14 +511,14 @@ namespace Ryujinx.HLE.HOS.Kernel.Threading
WakeArbiterThreads(address, count);
_system.CriticalSection.Leave();
_context.CriticalSection.Leave();
return KernelResult.Success;
}
public KernelResult SignalAndModifyIfEqual(ulong address, int value, int count)
{
_system.CriticalSection.Enter();
_context.CriticalSection.Enter();
int offset;
@ -527,7 +527,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Threading
// or negative. It is incremented if there are no threads waiting.
int waitingCount = 0;
foreach (KThread thread in ArbiterThreads.Where(x => x.MutexAddress == address))
foreach (KThread thread in _arbiterThreads.Where(x => x.MutexAddress == address))
{
if (++waitingCount > count)
{
@ -544,11 +544,11 @@ namespace Ryujinx.HLE.HOS.Kernel.Threading
offset = 1;
}
KProcess currentProcess = _system.Scheduler.GetCurrentProcess();
KProcess currentProcess = _context.Scheduler.GetCurrentProcess();
if (!currentProcess.CpuMemory.IsMapped(address))
{
_system.CriticalSection.Leave();
_context.CriticalSection.Leave();
return KernelResult.InvalidMemState;
}
@ -563,7 +563,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Threading
if (currentValue != value)
{
_system.CriticalSection.Leave();
_context.CriticalSection.Leave();
return KernelResult.InvalidState;
}
@ -572,7 +572,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Threading
WakeArbiterThreads(address, count);
_system.CriticalSection.Leave();
_context.CriticalSection.Leave();
return KernelResult.Success;
}
@ -581,7 +581,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Threading
{
Queue<KThread> signaledThreads = new Queue<KThread>();
foreach (KThread thread in ArbiterThreads.Where(x => x.MutexAddress == address))
foreach (KThread thread in _arbiterThreads.Where(x => x.MutexAddress == address))
{
signaledThreads.Enqueue(thread);
@ -601,7 +601,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Threading
thread.WaitingInArbitration = false;
ArbiterThreads.Remove(thread);
_arbiterThreads.Remove(thread);
}
}
}