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);
}
}
}

View file

@ -5,11 +5,11 @@ namespace Ryujinx.HLE.HOS.Kernel.Threading
{
static class KConditionVariable
{
public static void Wait(Horizon system, LinkedList<KThread> threadList, object mutex, long timeout)
public static void Wait(KernelContext context, LinkedList<KThread> threadList, object mutex, long timeout)
{
KThread currentThread = system.Scheduler.GetCurrentThread();
KThread currentThread = context.Scheduler.GetCurrentThread();
system.CriticalSection.Enter();
context.CriticalSection.Enter();
Monitor.Exit(mutex);
@ -28,29 +28,29 @@ namespace Ryujinx.HLE.HOS.Kernel.Threading
currentThread.Withholder = null;
system.CriticalSection.Leave();
context.CriticalSection.Leave();
}
else
{
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);
}
}
Monitor.Enter(mutex);
}
public static void NotifyAll(Horizon system, LinkedList<KThread> threadList)
public static void NotifyAll(KernelContext context, LinkedList<KThread> threadList)
{
system.CriticalSection.Enter();
context.CriticalSection.Enter();
LinkedListNode<KThread> node = threadList.First;
@ -65,7 +65,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Threading
thread.Reschedule(ThreadSchedState.Running);
}
system.CriticalSection.Leave();
context.CriticalSection.Leave();
}
}
}

View file

@ -1,19 +1,18 @@
using ARMeilleure;
using System.Threading;
namespace Ryujinx.HLE.HOS.Kernel.Threading
{
class KCriticalSection
{
private Horizon _system;
private readonly KernelContext _context;
public object LockObj { get; private set; }
private int _recursionCount;
public KCriticalSection(Horizon system)
public KCriticalSection(KernelContext context)
{
_system = system;
_context = context;
LockObj = new object();
}
@ -36,20 +35,20 @@ namespace Ryujinx.HLE.HOS.Kernel.Threading
if (--_recursionCount == 0)
{
if (_system.Scheduler.ThreadReselectionRequested)
if (_context.Scheduler.ThreadReselectionRequested)
{
_system.Scheduler.SelectThreads();
_context.Scheduler.SelectThreads();
}
Monitor.Exit(LockObj);
if (_system.Scheduler.MultiCoreScheduling)
if (_context.Scheduler.MultiCoreScheduling)
{
lock (_system.Scheduler.CoreContexts)
lock (_context.Scheduler.CoreContexts)
{
for (int core = 0; core < KScheduler.CpuCoresCount; core++)
{
KCoreContext coreContext = _system.Scheduler.CoreContexts[core];
KCoreContext coreContext = _context.Scheduler.CoreContexts[core];
if (coreContext.ContextSwitchNeeded)
{
@ -86,7 +85,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Threading
if (doContextSwitch)
{
_system.Scheduler.ContextSwitch();
_context.Scheduler.ContextSwitch();
}
}
}

View file

@ -5,10 +5,10 @@ namespace Ryujinx.HLE.HOS.Kernel.Threading
public KReadableEvent ReadableEvent { get; private set; }
public KWritableEvent WritableEvent { get; private set; }
public KEvent(Horizon system)
public KEvent(KernelContext context)
{
ReadableEvent = new KReadableEvent(system, this);
WritableEvent = new KWritableEvent(system, this);
ReadableEvent = new KReadableEvent(context, this);
WritableEvent = new KWritableEvent(context, this);
}
}
}

View file

@ -4,18 +4,18 @@ namespace Ryujinx.HLE.HOS.Kernel.Threading
{
class KReadableEvent : KSynchronizationObject
{
private KEvent _parent;
private readonly KEvent _parent;
private bool _signaled;
public KReadableEvent(Horizon system, KEvent parent) : base(system)
public KReadableEvent(KernelContext context, KEvent parent) : base(context)
{
_parent = parent;
}
public override void Signal()
{
System.CriticalSection.Enter();
KernelContext.CriticalSection.Enter();
if (!_signaled)
{
@ -24,7 +24,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Threading
base.Signal();
}
System.CriticalSection.Leave();
KernelContext.CriticalSection.Leave();
}
public KernelResult Clear()
@ -38,7 +38,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Threading
{
KernelResult result;
System.CriticalSection.Enter();
KernelContext.CriticalSection.Enter();
if (_signaled)
{
@ -51,7 +51,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Threading
result = KernelResult.InvalidState;
}
System.CriticalSection.Leave();
KernelContext.CriticalSection.Leave();
return result;
}

View file

@ -13,7 +13,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Threading
private const int PreemptionPriorityCores012 = 59;
private const int PreemptionPriorityCore3 = 63;
private Horizon _system;
private readonly KernelContext _context;
public KSchedulingData SchedulingData { get; private set; }
@ -21,9 +21,9 @@ namespace Ryujinx.HLE.HOS.Kernel.Threading
public bool ThreadReselectionRequested { get; set; }
public KScheduler(Horizon system)
public KScheduler(KernelContext context)
{
_system = system;
_context = context;
SchedulingData = new KSchedulingData();
@ -39,14 +39,14 @@ namespace Ryujinx.HLE.HOS.Kernel.Threading
private void PreemptThreads()
{
_system.CriticalSection.Enter();
_context.CriticalSection.Enter();
PreemptThread(PreemptionPriorityCores012, 0);
PreemptThread(PreemptionPriorityCores012, 1);
PreemptThread(PreemptionPriorityCores012, 2);
PreemptThread(PreemptionPriorityCore3, 3);
_system.CriticalSection.Leave();
_context.CriticalSection.Leave();
}
private void PreemptThread(int prio, int core)
@ -224,9 +224,9 @@ namespace Ryujinx.HLE.HOS.Kernel.Threading
return _dummyThread;
}
KProcess dummyProcess = new KProcess(_system);
KProcess dummyProcess = new KProcess(_context);
KThread dummyThread = new KThread(_system);
KThread dummyThread = new KThread(_context);
dummyThread.Initialize(0, 0, 0, 44, 0, dummyProcess, ThreadType.Dummy);

View file

@ -5,11 +5,11 @@ namespace Ryujinx.HLE.HOS.Kernel.Threading
{
class KSynchronization
{
private Horizon _system;
private KernelContext _context;
public KSynchronization(Horizon system)
public KSynchronization(KernelContext context)
{
_system = system;
_context = context;
}
public KernelResult WaitFor(KSynchronizationObject[] syncObjs, long timeout, out int handleIndex)
@ -18,7 +18,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Threading
KernelResult result = KernelResult.TimedOut;
_system.CriticalSection.Enter();
_context.CriticalSection.Enter();
// Check if objects are already signaled before waiting.
for (int index = 0; index < syncObjs.Length; index++)
@ -30,19 +30,19 @@ namespace Ryujinx.HLE.HOS.Kernel.Threading
handleIndex = index;
_system.CriticalSection.Leave();
_context.CriticalSection.Leave();
return KernelResult.Success;
}
if (timeout == 0)
{
_system.CriticalSection.Leave();
_context.CriticalSection.Leave();
return result;
}
KThread currentThread = _system.Scheduler.GetCurrentThread();
KThread currentThread = _context.Scheduler.GetCurrentThread();
if (currentThread.ShallBeTerminated ||
currentThread.SchedFlags == ThreadSchedState.TerminationPending)
@ -72,19 +72,19 @@ 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();
currentThread.WaitingSync = false;
if (timeout > 0)
{
_system.TimeManager.UnscheduleFutureInvocation(currentThread);
_context.TimeManager.UnscheduleFutureInvocation(currentThread);
}
_system.CriticalSection.Enter();
_context.CriticalSection.Enter();
result = currentThread.ObjSyncResult;
@ -101,14 +101,14 @@ namespace Ryujinx.HLE.HOS.Kernel.Threading
}
}
_system.CriticalSection.Leave();
_context.CriticalSection.Leave();
return result;
}
public void SignalObject(KSynchronizationObject syncObj)
{
_system.CriticalSection.Enter();
_context.CriticalSection.Enter();
if (syncObj.IsSignaled())
{
@ -130,7 +130,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Threading
}
}
_system.CriticalSection.Leave();
_context.CriticalSection.Leave();
}
}
}

View file

@ -91,10 +91,10 @@ namespace Ryujinx.HLE.HOS.Kernel.Threading
public long LastPc { get; set; }
public KThread(Horizon system) : base(system)
public KThread(KernelContext context) : base(context)
{
_scheduler = system.Scheduler;
_schedulingData = system.Scheduler.SchedulingData;
_scheduler = KernelContext.Scheduler;
_schedulingData = KernelContext.Scheduler.SchedulingData;
SiblingsPerCore = new LinkedListNode<KThread>[KScheduler.CpuCoresCount];
@ -185,7 +185,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Threading
owner.SubscribeThreadEventHandlers(Context);
ThreadUid = System.GetThreadUid();
ThreadUid = KernelContext.NewThreadUid();
HostThread.Name = $"HLE.HostThread.{ThreadUid}";
@ -197,11 +197,11 @@ namespace Ryujinx.HLE.HOS.Kernel.Threading
if (owner.IsPaused)
{
System.CriticalSection.Enter();
KernelContext.CriticalSection.Enter();
if (ShallBeTerminated || SchedFlags == ThreadSchedState.TerminationPending)
{
System.CriticalSection.Leave();
KernelContext.CriticalSection.Leave();
return KernelResult.Success;
}
@ -210,7 +210,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Threading
CombineForcePauseFlags();
System.CriticalSection.Leave();
KernelContext.CriticalSection.Leave();
}
}
@ -219,9 +219,9 @@ namespace Ryujinx.HLE.HOS.Kernel.Threading
public KernelResult Start()
{
if (!System.KernelInitialized)
if (!KernelContext.KernelInitialized)
{
System.CriticalSection.Enter();
KernelContext.CriticalSection.Enter();
if (!ShallBeTerminated && SchedFlags != ThreadSchedState.TerminationPending)
{
@ -230,16 +230,16 @@ namespace Ryujinx.HLE.HOS.Kernel.Threading
CombineForcePauseFlags();
}
System.CriticalSection.Leave();
KernelContext.CriticalSection.Leave();
}
KernelResult result = KernelResult.ThreadTerminating;
System.CriticalSection.Enter();
KernelContext.CriticalSection.Enter();
if (!ShallBeTerminated)
{
KThread currentThread = System.Scheduler.GetCurrentThread();
KThread currentThread = KernelContext.Scheduler.GetCurrentThread();
while (SchedFlags != ThreadSchedState.TerminationPending &&
currentThread.SchedFlags != ThreadSchedState.TerminationPending &&
@ -269,8 +269,8 @@ namespace Ryujinx.HLE.HOS.Kernel.Threading
{
currentThread.CombineForcePauseFlags();
System.CriticalSection.Leave();
System.CriticalSection.Enter();
KernelContext.CriticalSection.Leave();
KernelContext.CriticalSection.Enter();
if (currentThread.ShallBeTerminated)
{
@ -280,7 +280,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Threading
}
}
System.CriticalSection.Leave();
KernelContext.CriticalSection.Leave();
return result;
}
@ -296,20 +296,20 @@ namespace Ryujinx.HLE.HOS.Kernel.Threading
_hasBeenReleased = true;
}
System.CriticalSection.Enter();
KernelContext.CriticalSection.Enter();
_forcePauseFlags &= ~ThreadSchedState.ForcePauseMask;
ExitImpl();
System.CriticalSection.Leave();
KernelContext.CriticalSection.Leave();
DecrementReferenceCount();
}
public ThreadSchedState PrepareForTermination()
{
System.CriticalSection.Enter();
KernelContext.CriticalSection.Enter();
ThreadSchedState result;
@ -351,7 +351,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Threading
result = SchedFlags;
System.CriticalSection.Leave();
KernelContext.CriticalSection.Leave();
return result & ThreadSchedState.LowMask;
}
@ -362,7 +362,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Threading
if (state != ThreadSchedState.TerminationPending)
{
System.Synchronization.WaitFor(new KSynchronizationObject[] { this }, -1, out _);
KernelContext.Synchronization.WaitFor(new KSynchronizationObject[] { this }, -1, out _);
}
}
@ -374,14 +374,14 @@ namespace Ryujinx.HLE.HOS.Kernel.Threading
{
if (ShallBeTerminated || SchedFlags == ThreadSchedState.TerminationPending)
{
System.Scheduler.ExitThread(this);
KernelContext.Scheduler.ExitThread(this);
Exit();
// As the death of the thread is handled by the CPU emulator, we differ from the official kernel and return here.
break;
}
System.CriticalSection.Enter();
KernelContext.CriticalSection.Enter();
if (ShallBeTerminated || SchedFlags == ThreadSchedState.TerminationPending)
{
@ -397,13 +397,13 @@ namespace Ryujinx.HLE.HOS.Kernel.Threading
state = ThreadSchedState.Running;
}
System.CriticalSection.Leave();
KernelContext.CriticalSection.Leave();
} while (state == ThreadSchedState.TerminationPending);
}
private void ExitImpl()
{
System.CriticalSection.Enter();
KernelContext.CriticalSection.Enter();
SetNewSchedFlags(ThreadSchedState.TerminationPending);
@ -411,16 +411,16 @@ namespace Ryujinx.HLE.HOS.Kernel.Threading
Signal();
System.CriticalSection.Leave();
KernelContext.CriticalSection.Leave();
}
public KernelResult Sleep(long timeout)
{
System.CriticalSection.Enter();
KernelContext.CriticalSection.Enter();
if (ShallBeTerminated || SchedFlags == ThreadSchedState.TerminationPending)
{
System.CriticalSection.Leave();
KernelContext.CriticalSection.Leave();
return KernelResult.ThreadTerminating;
}
@ -429,14 +429,14 @@ namespace Ryujinx.HLE.HOS.Kernel.Threading
if (timeout > 0)
{
System.TimeManager.ScheduleFutureInvocation(this, timeout);
KernelContext.TimeManager.ScheduleFutureInvocation(this, timeout);
}
System.CriticalSection.Leave();
KernelContext.CriticalSection.Leave();
if (timeout > 0)
{
System.TimeManager.UnscheduleFutureInvocation(this);
KernelContext.TimeManager.UnscheduleFutureInvocation(this);
}
return 0;
@ -444,13 +444,13 @@ namespace Ryujinx.HLE.HOS.Kernel.Threading
public void Yield()
{
System.CriticalSection.Enter();
KernelContext.CriticalSection.Enter();
if (SchedFlags != ThreadSchedState.Running)
{
System.CriticalSection.Leave();
KernelContext.CriticalSection.Leave();
System.Scheduler.ContextSwitch();
KernelContext.Scheduler.ContextSwitch();
return;
}
@ -463,20 +463,20 @@ namespace Ryujinx.HLE.HOS.Kernel.Threading
_scheduler.ThreadReselectionRequested = true;
System.CriticalSection.Leave();
KernelContext.CriticalSection.Leave();
System.Scheduler.ContextSwitch();
KernelContext.Scheduler.ContextSwitch();
}
public void YieldWithLoadBalancing()
{
System.CriticalSection.Enter();
KernelContext.CriticalSection.Enter();
if (SchedFlags != ThreadSchedState.Running)
{
System.CriticalSection.Leave();
KernelContext.CriticalSection.Leave();
System.Scheduler.ContextSwitch();
KernelContext.Scheduler.ContextSwitch();
return;
}
@ -536,20 +536,20 @@ namespace Ryujinx.HLE.HOS.Kernel.Threading
_scheduler.ThreadReselectionRequested = true;
}
System.CriticalSection.Leave();
KernelContext.CriticalSection.Leave();
System.Scheduler.ContextSwitch();
KernelContext.Scheduler.ContextSwitch();
}
public void YieldAndWaitForLoadBalancing()
{
System.CriticalSection.Enter();
KernelContext.CriticalSection.Enter();
if (SchedFlags != ThreadSchedState.Running)
{
System.CriticalSection.Leave();
KernelContext.CriticalSection.Leave();
System.Scheduler.ContextSwitch();
KernelContext.Scheduler.ContextSwitch();
return;
}
@ -592,38 +592,38 @@ namespace Ryujinx.HLE.HOS.Kernel.Threading
_scheduler.ThreadReselectionRequested = true;
}
System.CriticalSection.Leave();
KernelContext.CriticalSection.Leave();
System.Scheduler.ContextSwitch();
KernelContext.Scheduler.ContextSwitch();
}
public void SetPriority(int priority)
{
System.CriticalSection.Enter();
KernelContext.CriticalSection.Enter();
BasePriority = priority;
UpdatePriorityInheritance();
System.CriticalSection.Leave();
KernelContext.CriticalSection.Leave();
}
public KernelResult SetActivity(bool pause)
{
KernelResult result = KernelResult.Success;
System.CriticalSection.Enter();
KernelContext.CriticalSection.Enter();
ThreadSchedState lowNibble = SchedFlags & ThreadSchedState.LowMask;
if (lowNibble != ThreadSchedState.Paused && lowNibble != ThreadSchedState.Running)
{
System.CriticalSection.Leave();
KernelContext.CriticalSection.Leave();
return KernelResult.InvalidState;
}
System.CriticalSection.Enter();
KernelContext.CriticalSection.Enter();
if (!ShallBeTerminated && SchedFlags != ThreadSchedState.TerminationPending)
{
@ -666,15 +666,15 @@ namespace Ryujinx.HLE.HOS.Kernel.Threading
}
}
System.CriticalSection.Leave();
System.CriticalSection.Leave();
KernelContext.CriticalSection.Leave();
KernelContext.CriticalSection.Leave();
return result;
}
public void CancelSynchronization()
{
System.CriticalSection.Enter();
KernelContext.CriticalSection.Enter();
if ((SchedFlags & ThreadSchedState.LowMask) != ThreadSchedState.Paused || !WaitingSync)
{
@ -700,12 +700,12 @@ namespace Ryujinx.HLE.HOS.Kernel.Threading
SyncCancelled = false;
}
System.CriticalSection.Leave();
KernelContext.CriticalSection.Leave();
}
public KernelResult SetCoreAndAffinityMask(int newCore, long newAffinityMask)
{
System.CriticalSection.Enter();
KernelContext.CriticalSection.Enter();
bool useOverride = _affinityOverrideCount != 0;
@ -716,7 +716,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Threading
if ((newAffinityMask & (1 << newCore)) == 0)
{
System.CriticalSection.Leave();
KernelContext.CriticalSection.Leave();
return KernelResult.InvalidCombination;
}
@ -754,7 +754,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Threading
}
}
System.CriticalSection.Leave();
KernelContext.CriticalSection.Leave();
return KernelResult.Success;
}
@ -784,7 +784,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Threading
private void SetNewSchedFlags(ThreadSchedState newFlags)
{
System.CriticalSection.Enter();
KernelContext.CriticalSection.Enter();
ThreadSchedState oldFlags = SchedFlags;
@ -795,12 +795,12 @@ namespace Ryujinx.HLE.HOS.Kernel.Threading
AdjustScheduling(oldFlags);
}
System.CriticalSection.Leave();
KernelContext.CriticalSection.Leave();
}
public void ReleaseAndResume()
{
System.CriticalSection.Enter();
KernelContext.CriticalSection.Enter();
if ((SchedFlags & ThreadSchedState.LowMask) == ThreadSchedState.Paused)
{
@ -818,12 +818,12 @@ namespace Ryujinx.HLE.HOS.Kernel.Threading
}
}
System.CriticalSection.Leave();
KernelContext.CriticalSection.Leave();
}
public void Reschedule(ThreadSchedState newFlags)
{
System.CriticalSection.Enter();
KernelContext.CriticalSection.Enter();
ThreadSchedState oldFlags = SchedFlags;
@ -832,7 +832,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Threading
AdjustScheduling(oldFlags);
System.CriticalSection.Leave();
KernelContext.CriticalSection.Leave();
}
public void AddMutexWaiter(KThread requester)
@ -1150,8 +1150,8 @@ namespace Ryujinx.HLE.HOS.Kernel.Threading
private void ThreadExit()
{
System.Scheduler.ExitThread(this);
System.Scheduler.RemoveThread(this);
KernelContext.Scheduler.ExitThread(this);
KernelContext.Scheduler.RemoveThread(this);
}
public bool IsCurrentHostThread()
@ -1180,7 +1180,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Threading
}
else
{
System.ResourceLimit.Release(LimitableResource.Thread, 1, released ? 0 : 1);
KernelContext.ResourceLimit.Release(LimitableResource.Thread, 1, released ? 0 : 1);
}
}
}
@ -1194,7 +1194,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Threading
throw new InvalidOperationException("Unexpected failure freeing thread local storage.");
}
System.CriticalSection.Enter();
KernelContext.CriticalSection.Enter();
// Wake up all threads that may be waiting for a mutex being held by this thread.
foreach (KThread thread in _mutexWaiters)
@ -1206,7 +1206,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Threading
thread.ReleaseAndResume();
}
System.CriticalSection.Leave();
KernelContext.CriticalSection.Leave();
Owner?.DecrementThreadCountAndTerminateIfZero();
}

View file

@ -4,9 +4,9 @@ namespace Ryujinx.HLE.HOS.Kernel.Threading
{
class KWritableEvent : KAutoObject
{
private KEvent _parent;
private readonly KEvent _parent;
public KWritableEvent(Horizon system, KEvent parent) : base(system)
public KWritableEvent(KernelContext context, KEvent parent) : base(context)
{
_parent = parent;
}