Adjust naming conventions and general refactoring in HLE Project (#490)
* Rename enum fields * Naming conventions * Remove unneeded ".this" * Remove unneeded semicolons * Remove unused Usings * Don't use var * Remove unneeded enum underlying types * Explicitly label class visibility * Remove unneeded @ prefixes * Remove unneeded commas * Remove unneeded if expressions * Method doesn't use unsafe code * Remove unneeded casts * Initialized objects don't need an empty constructor * Remove settings from DotSettings * Revert "Explicitly label class visibility" This reverts commit ad5eb5787cc5b27a4631cd46ef5f551c4ae95e51. * Small changes * Revert external enum renaming * Changes from feedback * Remove unneeded property setters
This commit is contained in:
parent
c86aacde76
commit
85dbb9559a
299 changed files with 12268 additions and 12276 deletions
|
@ -9,641 +9,641 @@ namespace Ryujinx.HLE.HOS.Kernel
|
|||
{
|
||||
private const int HasListenersMask = 0x40000000;
|
||||
|
||||
private Horizon System;
|
||||
private Horizon _system;
|
||||
|
||||
public List<KThread> CondVarThreads;
|
||||
public List<KThread> ArbiterThreads;
|
||||
|
||||
public KAddressArbiter(Horizon System)
|
||||
public KAddressArbiter(Horizon system)
|
||||
{
|
||||
this.System = System;
|
||||
_system = system;
|
||||
|
||||
CondVarThreads = new List<KThread>();
|
||||
ArbiterThreads = new List<KThread>();
|
||||
}
|
||||
|
||||
public long ArbitrateLock(int OwnerHandle, long MutexAddress, int RequesterHandle)
|
||||
public long ArbitrateLock(int ownerHandle, long mutexAddress, int requesterHandle)
|
||||
{
|
||||
KThread CurrentThread = System.Scheduler.GetCurrentThread();
|
||||
KThread currentThread = _system.Scheduler.GetCurrentThread();
|
||||
|
||||
System.CriticalSection.Enter();
|
||||
_system.CriticalSection.Enter();
|
||||
|
||||
CurrentThread.SignaledObj = null;
|
||||
CurrentThread.ObjSyncResult = 0;
|
||||
currentThread.SignaledObj = null;
|
||||
currentThread.ObjSyncResult = 0;
|
||||
|
||||
KProcess CurrentProcess = System.Scheduler.GetCurrentProcess();
|
||||
KProcess currentProcess = _system.Scheduler.GetCurrentProcess();
|
||||
|
||||
if (!KernelTransfer.UserToKernelInt32(System, MutexAddress, out int MutexValue))
|
||||
if (!KernelTransfer.UserToKernelInt32(_system, mutexAddress, out int mutexValue))
|
||||
{
|
||||
System.CriticalSection.Leave();
|
||||
_system.CriticalSection.Leave();
|
||||
|
||||
return MakeError(ErrorModule.Kernel, KernelErr.NoAccessPerm);;
|
||||
return MakeError(ErrorModule.Kernel, KernelErr.NoAccessPerm);
|
||||
}
|
||||
|
||||
if (MutexValue != (OwnerHandle | HasListenersMask))
|
||||
if (mutexValue != (ownerHandle | HasListenersMask))
|
||||
{
|
||||
System.CriticalSection.Leave();
|
||||
_system.CriticalSection.Leave();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
KThread MutexOwner = CurrentProcess.HandleTable.GetObject<KThread>(OwnerHandle);
|
||||
KThread mutexOwner = currentProcess.HandleTable.GetObject<KThread>(ownerHandle);
|
||||
|
||||
if (MutexOwner == null)
|
||||
if (mutexOwner == null)
|
||||
{
|
||||
System.CriticalSection.Leave();
|
||||
_system.CriticalSection.Leave();
|
||||
|
||||
return MakeError(ErrorModule.Kernel, KernelErr.InvalidHandle);
|
||||
}
|
||||
|
||||
CurrentThread.MutexAddress = MutexAddress;
|
||||
CurrentThread.ThreadHandleForUserMutex = RequesterHandle;
|
||||
currentThread.MutexAddress = mutexAddress;
|
||||
currentThread.ThreadHandleForUserMutex = requesterHandle;
|
||||
|
||||
MutexOwner.AddMutexWaiter(CurrentThread);
|
||||
mutexOwner.AddMutexWaiter(currentThread);
|
||||
|
||||
CurrentThread.Reschedule(ThreadSchedState.Paused);
|
||||
currentThread.Reschedule(ThreadSchedState.Paused);
|
||||
|
||||
System.CriticalSection.Leave();
|
||||
System.CriticalSection.Enter();
|
||||
_system.CriticalSection.Leave();
|
||||
_system.CriticalSection.Enter();
|
||||
|
||||
if (CurrentThread.MutexOwner != null)
|
||||
if (currentThread.MutexOwner != null)
|
||||
{
|
||||
CurrentThread.MutexOwner.RemoveMutexWaiter(CurrentThread);
|
||||
currentThread.MutexOwner.RemoveMutexWaiter(currentThread);
|
||||
}
|
||||
|
||||
System.CriticalSection.Leave();
|
||||
_system.CriticalSection.Leave();
|
||||
|
||||
return (uint)CurrentThread.ObjSyncResult;
|
||||
return (uint)currentThread.ObjSyncResult;
|
||||
}
|
||||
|
||||
public long ArbitrateUnlock(long MutexAddress)
|
||||
public long ArbitrateUnlock(long mutexAddress)
|
||||
{
|
||||
System.CriticalSection.Enter();
|
||||
_system.CriticalSection.Enter();
|
||||
|
||||
KThread CurrentThread = System.Scheduler.GetCurrentThread();
|
||||
KThread currentThread = _system.Scheduler.GetCurrentThread();
|
||||
|
||||
(long Result, KThread NewOwnerThread) = MutexUnlock(CurrentThread, MutexAddress);
|
||||
(long result, KThread newOwnerThread) = MutexUnlock(currentThread, mutexAddress);
|
||||
|
||||
if (Result != 0 && NewOwnerThread != null)
|
||||
if (result != 0 && newOwnerThread != null)
|
||||
{
|
||||
NewOwnerThread.SignaledObj = null;
|
||||
NewOwnerThread.ObjSyncResult = (int)Result;
|
||||
newOwnerThread.SignaledObj = null;
|
||||
newOwnerThread.ObjSyncResult = (int)result;
|
||||
}
|
||||
|
||||
System.CriticalSection.Leave();
|
||||
_system.CriticalSection.Leave();
|
||||
|
||||
return Result;
|
||||
return result;
|
||||
}
|
||||
|
||||
public long WaitProcessWideKeyAtomic(
|
||||
long MutexAddress,
|
||||
long CondVarAddress,
|
||||
int ThreadHandle,
|
||||
long Timeout)
|
||||
long mutexAddress,
|
||||
long condVarAddress,
|
||||
int threadHandle,
|
||||
long timeout)
|
||||
{
|
||||
System.CriticalSection.Enter();
|
||||
_system.CriticalSection.Enter();
|
||||
|
||||
KThread CurrentThread = System.Scheduler.GetCurrentThread();
|
||||
KThread currentThread = _system.Scheduler.GetCurrentThread();
|
||||
|
||||
CurrentThread.SignaledObj = null;
|
||||
CurrentThread.ObjSyncResult = (int)MakeError(ErrorModule.Kernel, KernelErr.Timeout);
|
||||
currentThread.SignaledObj = null;
|
||||
currentThread.ObjSyncResult = (int)MakeError(ErrorModule.Kernel, KernelErr.Timeout);
|
||||
|
||||
if (CurrentThread.ShallBeTerminated ||
|
||||
CurrentThread.SchedFlags == ThreadSchedState.TerminationPending)
|
||||
if (currentThread.ShallBeTerminated ||
|
||||
currentThread.SchedFlags == ThreadSchedState.TerminationPending)
|
||||
{
|
||||
System.CriticalSection.Leave();
|
||||
_system.CriticalSection.Leave();
|
||||
|
||||
return MakeError(ErrorModule.Kernel, KernelErr.ThreadTerminating);
|
||||
}
|
||||
|
||||
(long Result, _) = MutexUnlock(CurrentThread, MutexAddress);
|
||||
(long result, _) = MutexUnlock(currentThread, mutexAddress);
|
||||
|
||||
if (Result != 0)
|
||||
if (result != 0)
|
||||
{
|
||||
System.CriticalSection.Leave();
|
||||
_system.CriticalSection.Leave();
|
||||
|
||||
return Result;
|
||||
return result;
|
||||
}
|
||||
|
||||
CurrentThread.MutexAddress = MutexAddress;
|
||||
CurrentThread.ThreadHandleForUserMutex = ThreadHandle;
|
||||
CurrentThread.CondVarAddress = CondVarAddress;
|
||||
currentThread.MutexAddress = mutexAddress;
|
||||
currentThread.ThreadHandleForUserMutex = threadHandle;
|
||||
currentThread.CondVarAddress = condVarAddress;
|
||||
|
||||
CondVarThreads.Add(CurrentThread);
|
||||
CondVarThreads.Add(currentThread);
|
||||
|
||||
if (Timeout != 0)
|
||||
if (timeout != 0)
|
||||
{
|
||||
CurrentThread.Reschedule(ThreadSchedState.Paused);
|
||||
currentThread.Reschedule(ThreadSchedState.Paused);
|
||||
|
||||
if (Timeout > 0)
|
||||
if (timeout > 0)
|
||||
{
|
||||
System.TimeManager.ScheduleFutureInvocation(CurrentThread, Timeout);
|
||||
_system.TimeManager.ScheduleFutureInvocation(currentThread, timeout);
|
||||
}
|
||||
}
|
||||
|
||||
System.CriticalSection.Leave();
|
||||
_system.CriticalSection.Leave();
|
||||
|
||||
if (Timeout > 0)
|
||||
if (timeout > 0)
|
||||
{
|
||||
System.TimeManager.UnscheduleFutureInvocation(CurrentThread);
|
||||
_system.TimeManager.UnscheduleFutureInvocation(currentThread);
|
||||
}
|
||||
|
||||
System.CriticalSection.Enter();
|
||||
_system.CriticalSection.Enter();
|
||||
|
||||
if (CurrentThread.MutexOwner != null)
|
||||
if (currentThread.MutexOwner != null)
|
||||
{
|
||||
CurrentThread.MutexOwner.RemoveMutexWaiter(CurrentThread);
|
||||
currentThread.MutexOwner.RemoveMutexWaiter(currentThread);
|
||||
}
|
||||
|
||||
CondVarThreads.Remove(CurrentThread);
|
||||
CondVarThreads.Remove(currentThread);
|
||||
|
||||
System.CriticalSection.Leave();
|
||||
_system.CriticalSection.Leave();
|
||||
|
||||
return (uint)CurrentThread.ObjSyncResult;
|
||||
return (uint)currentThread.ObjSyncResult;
|
||||
}
|
||||
|
||||
private (long, KThread) MutexUnlock(KThread CurrentThread, long MutexAddress)
|
||||
private (long, KThread) MutexUnlock(KThread currentThread, long mutexAddress)
|
||||
{
|
||||
KThread NewOwnerThread = CurrentThread.RelinquishMutex(MutexAddress, out int Count);
|
||||
KThread newOwnerThread = currentThread.RelinquishMutex(mutexAddress, out int count);
|
||||
|
||||
int MutexValue = 0;
|
||||
int mutexValue = 0;
|
||||
|
||||
if (NewOwnerThread != null)
|
||||
if (newOwnerThread != null)
|
||||
{
|
||||
MutexValue = NewOwnerThread.ThreadHandleForUserMutex;
|
||||
mutexValue = newOwnerThread.ThreadHandleForUserMutex;
|
||||
|
||||
if (Count >= 2)
|
||||
if (count >= 2)
|
||||
{
|
||||
MutexValue |= HasListenersMask;
|
||||
mutexValue |= HasListenersMask;
|
||||
}
|
||||
|
||||
NewOwnerThread.SignaledObj = null;
|
||||
NewOwnerThread.ObjSyncResult = 0;
|
||||
newOwnerThread.SignaledObj = null;
|
||||
newOwnerThread.ObjSyncResult = 0;
|
||||
|
||||
NewOwnerThread.ReleaseAndResume();
|
||||
newOwnerThread.ReleaseAndResume();
|
||||
}
|
||||
|
||||
long Result = 0;
|
||||
long result = 0;
|
||||
|
||||
if (!KernelTransfer.KernelToUserInt32(System, MutexAddress, MutexValue))
|
||||
if (!KernelTransfer.KernelToUserInt32(_system, mutexAddress, mutexValue))
|
||||
{
|
||||
Result = MakeError(ErrorModule.Kernel, KernelErr.NoAccessPerm);
|
||||
result = MakeError(ErrorModule.Kernel, KernelErr.NoAccessPerm);
|
||||
}
|
||||
|
||||
return (Result, NewOwnerThread);
|
||||
return (result, newOwnerThread);
|
||||
}
|
||||
|
||||
public void SignalProcessWideKey(long Address, int Count)
|
||||
public void SignalProcessWideKey(long address, int count)
|
||||
{
|
||||
Queue<KThread> SignaledThreads = new Queue<KThread>();
|
||||
Queue<KThread> signaledThreads = new Queue<KThread>();
|
||||
|
||||
System.CriticalSection.Enter();
|
||||
_system.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))
|
||||
foreach (KThread thread in sortedThreads.Where(x => x.CondVarAddress == address))
|
||||
{
|
||||
TryAcquireMutex(Thread);
|
||||
TryAcquireMutex(thread);
|
||||
|
||||
SignaledThreads.Enqueue(Thread);
|
||||
signaledThreads.Enqueue(thread);
|
||||
|
||||
//If the count is <= 0, we should signal all threads waiting.
|
||||
if (Count >= 1 && --Count == 0)
|
||||
if (count >= 1 && --count == 0)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
while (SignaledThreads.TryDequeue(out KThread Thread))
|
||||
while (signaledThreads.TryDequeue(out KThread thread))
|
||||
{
|
||||
CondVarThreads.Remove(Thread);
|
||||
CondVarThreads.Remove(thread);
|
||||
}
|
||||
|
||||
System.CriticalSection.Leave();
|
||||
_system.CriticalSection.Leave();
|
||||
}
|
||||
|
||||
private KThread TryAcquireMutex(KThread Requester)
|
||||
private KThread TryAcquireMutex(KThread requester)
|
||||
{
|
||||
long Address = Requester.MutexAddress;
|
||||
long address = requester.MutexAddress;
|
||||
|
||||
KProcess CurrentProcess = System.Scheduler.GetCurrentProcess();
|
||||
KProcess currentProcess = _system.Scheduler.GetCurrentProcess();
|
||||
|
||||
CurrentProcess.CpuMemory.SetExclusive(0, Address);
|
||||
currentProcess.CpuMemory.SetExclusive(0, address);
|
||||
|
||||
if (!KernelTransfer.UserToKernelInt32(System, Address, out int MutexValue))
|
||||
if (!KernelTransfer.UserToKernelInt32(_system, address, out int mutexValue))
|
||||
{
|
||||
//Invalid address.
|
||||
CurrentProcess.CpuMemory.ClearExclusive(0);
|
||||
currentProcess.CpuMemory.ClearExclusive(0);
|
||||
|
||||
Requester.SignaledObj = null;
|
||||
Requester.ObjSyncResult = (int)MakeError(ErrorModule.Kernel, KernelErr.NoAccessPerm);
|
||||
requester.SignaledObj = null;
|
||||
requester.ObjSyncResult = (int)MakeError(ErrorModule.Kernel, KernelErr.NoAccessPerm);
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
while (true)
|
||||
{
|
||||
if (CurrentProcess.CpuMemory.TestExclusive(0, Address))
|
||||
if (currentProcess.CpuMemory.TestExclusive(0, address))
|
||||
{
|
||||
if (MutexValue != 0)
|
||||
if (mutexValue != 0)
|
||||
{
|
||||
//Update value to indicate there is a mutex waiter now.
|
||||
CurrentProcess.CpuMemory.WriteInt32(Address, MutexValue | HasListenersMask);
|
||||
currentProcess.CpuMemory.WriteInt32(address, mutexValue | HasListenersMask);
|
||||
}
|
||||
else
|
||||
{
|
||||
//No thread owning the mutex, assign to requesting thread.
|
||||
CurrentProcess.CpuMemory.WriteInt32(Address, Requester.ThreadHandleForUserMutex);
|
||||
currentProcess.CpuMemory.WriteInt32(address, requester.ThreadHandleForUserMutex);
|
||||
}
|
||||
|
||||
CurrentProcess.CpuMemory.ClearExclusiveForStore(0);
|
||||
currentProcess.CpuMemory.ClearExclusiveForStore(0);
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
CurrentProcess.CpuMemory.SetExclusive(0, Address);
|
||||
currentProcess.CpuMemory.SetExclusive(0, address);
|
||||
|
||||
MutexValue = CurrentProcess.CpuMemory.ReadInt32(Address);
|
||||
mutexValue = currentProcess.CpuMemory.ReadInt32(address);
|
||||
}
|
||||
|
||||
if (MutexValue == 0)
|
||||
if (mutexValue == 0)
|
||||
{
|
||||
//We now own the mutex.
|
||||
Requester.SignaledObj = null;
|
||||
Requester.ObjSyncResult = 0;
|
||||
requester.SignaledObj = null;
|
||||
requester.ObjSyncResult = 0;
|
||||
|
||||
Requester.ReleaseAndResume();
|
||||
requester.ReleaseAndResume();
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
MutexValue &= ~HasListenersMask;
|
||||
mutexValue &= ~HasListenersMask;
|
||||
|
||||
KThread MutexOwner = CurrentProcess.HandleTable.GetObject<KThread>(MutexValue);
|
||||
KThread mutexOwner = currentProcess.HandleTable.GetObject<KThread>(mutexValue);
|
||||
|
||||
if (MutexOwner != null)
|
||||
if (mutexOwner != null)
|
||||
{
|
||||
//Mutex already belongs to another thread, wait for it.
|
||||
MutexOwner.AddMutexWaiter(Requester);
|
||||
mutexOwner.AddMutexWaiter(requester);
|
||||
}
|
||||
else
|
||||
{
|
||||
//Invalid mutex owner.
|
||||
Requester.SignaledObj = null;
|
||||
Requester.ObjSyncResult = (int)MakeError(ErrorModule.Kernel, KernelErr.InvalidHandle);
|
||||
requester.SignaledObj = null;
|
||||
requester.ObjSyncResult = (int)MakeError(ErrorModule.Kernel, KernelErr.InvalidHandle);
|
||||
|
||||
Requester.ReleaseAndResume();
|
||||
requester.ReleaseAndResume();
|
||||
}
|
||||
|
||||
return MutexOwner;
|
||||
return mutexOwner;
|
||||
}
|
||||
|
||||
public long WaitForAddressIfEqual(long Address, int Value, long Timeout)
|
||||
public long WaitForAddressIfEqual(long address, int value, long timeout)
|
||||
{
|
||||
KThread CurrentThread = System.Scheduler.GetCurrentThread();
|
||||
KThread currentThread = _system.Scheduler.GetCurrentThread();
|
||||
|
||||
System.CriticalSection.Enter();
|
||||
_system.CriticalSection.Enter();
|
||||
|
||||
if (CurrentThread.ShallBeTerminated ||
|
||||
CurrentThread.SchedFlags == ThreadSchedState.TerminationPending)
|
||||
if (currentThread.ShallBeTerminated ||
|
||||
currentThread.SchedFlags == ThreadSchedState.TerminationPending)
|
||||
{
|
||||
System.CriticalSection.Leave();
|
||||
_system.CriticalSection.Leave();
|
||||
|
||||
return MakeError(ErrorModule.Kernel, KernelErr.ThreadTerminating);
|
||||
}
|
||||
|
||||
CurrentThread.SignaledObj = null;
|
||||
CurrentThread.ObjSyncResult = (int)MakeError(ErrorModule.Kernel, KernelErr.Timeout);
|
||||
currentThread.SignaledObj = null;
|
||||
currentThread.ObjSyncResult = (int)MakeError(ErrorModule.Kernel, KernelErr.Timeout);
|
||||
|
||||
if (!KernelTransfer.UserToKernelInt32(System, Address, out int CurrentValue))
|
||||
if (!KernelTransfer.UserToKernelInt32(_system, address, out int currentValue))
|
||||
{
|
||||
System.CriticalSection.Leave();
|
||||
_system.CriticalSection.Leave();
|
||||
|
||||
return MakeError(ErrorModule.Kernel, KernelErr.NoAccessPerm);
|
||||
}
|
||||
|
||||
if (CurrentValue == Value)
|
||||
if (currentValue == value)
|
||||
{
|
||||
if (Timeout == 0)
|
||||
if (timeout == 0)
|
||||
{
|
||||
System.CriticalSection.Leave();
|
||||
_system.CriticalSection.Leave();
|
||||
|
||||
return MakeError(ErrorModule.Kernel, KernelErr.Timeout);
|
||||
}
|
||||
|
||||
CurrentThread.MutexAddress = Address;
|
||||
CurrentThread.WaitingInArbitration = true;
|
||||
currentThread.MutexAddress = address;
|
||||
currentThread.WaitingInArbitration = true;
|
||||
|
||||
InsertSortedByPriority(ArbiterThreads, CurrentThread);
|
||||
InsertSortedByPriority(ArbiterThreads, currentThread);
|
||||
|
||||
CurrentThread.Reschedule(ThreadSchedState.Paused);
|
||||
currentThread.Reschedule(ThreadSchedState.Paused);
|
||||
|
||||
if (Timeout > 0)
|
||||
if (timeout > 0)
|
||||
{
|
||||
System.TimeManager.ScheduleFutureInvocation(CurrentThread, Timeout);
|
||||
_system.TimeManager.ScheduleFutureInvocation(currentThread, timeout);
|
||||
}
|
||||
|
||||
System.CriticalSection.Leave();
|
||||
_system.CriticalSection.Leave();
|
||||
|
||||
if (Timeout > 0)
|
||||
if (timeout > 0)
|
||||
{
|
||||
System.TimeManager.UnscheduleFutureInvocation(CurrentThread);
|
||||
_system.TimeManager.UnscheduleFutureInvocation(currentThread);
|
||||
}
|
||||
|
||||
System.CriticalSection.Enter();
|
||||
_system.CriticalSection.Enter();
|
||||
|
||||
if (CurrentThread.WaitingInArbitration)
|
||||
if (currentThread.WaitingInArbitration)
|
||||
{
|
||||
ArbiterThreads.Remove(CurrentThread);
|
||||
ArbiterThreads.Remove(currentThread);
|
||||
|
||||
CurrentThread.WaitingInArbitration = false;
|
||||
currentThread.WaitingInArbitration = false;
|
||||
}
|
||||
|
||||
System.CriticalSection.Leave();
|
||||
_system.CriticalSection.Leave();
|
||||
|
||||
return CurrentThread.ObjSyncResult;
|
||||
return currentThread.ObjSyncResult;
|
||||
}
|
||||
|
||||
System.CriticalSection.Leave();
|
||||
_system.CriticalSection.Leave();
|
||||
|
||||
return MakeError(ErrorModule.Kernel, KernelErr.InvalidState);
|
||||
}
|
||||
|
||||
public long WaitForAddressIfLessThan(long Address, int Value, bool ShouldDecrement, long Timeout)
|
||||
public long WaitForAddressIfLessThan(long address, int value, bool shouldDecrement, long timeout)
|
||||
{
|
||||
KThread CurrentThread = System.Scheduler.GetCurrentThread();
|
||||
KThread currentThread = _system.Scheduler.GetCurrentThread();
|
||||
|
||||
System.CriticalSection.Enter();
|
||||
_system.CriticalSection.Enter();
|
||||
|
||||
if (CurrentThread.ShallBeTerminated ||
|
||||
CurrentThread.SchedFlags == ThreadSchedState.TerminationPending)
|
||||
if (currentThread.ShallBeTerminated ||
|
||||
currentThread.SchedFlags == ThreadSchedState.TerminationPending)
|
||||
{
|
||||
System.CriticalSection.Leave();
|
||||
_system.CriticalSection.Leave();
|
||||
|
||||
return MakeError(ErrorModule.Kernel, KernelErr.ThreadTerminating);
|
||||
}
|
||||
|
||||
CurrentThread.SignaledObj = null;
|
||||
CurrentThread.ObjSyncResult = (int)MakeError(ErrorModule.Kernel, KernelErr.Timeout);
|
||||
currentThread.SignaledObj = null;
|
||||
currentThread.ObjSyncResult = (int)MakeError(ErrorModule.Kernel, KernelErr.Timeout);
|
||||
|
||||
KProcess CurrentProcess = System.Scheduler.GetCurrentProcess();
|
||||
KProcess currentProcess = _system.Scheduler.GetCurrentProcess();
|
||||
|
||||
//If ShouldDecrement is true, do atomic decrement of the value at Address.
|
||||
CurrentProcess.CpuMemory.SetExclusive(0, Address);
|
||||
currentProcess.CpuMemory.SetExclusive(0, address);
|
||||
|
||||
if (!KernelTransfer.UserToKernelInt32(System, Address, out int CurrentValue))
|
||||
if (!KernelTransfer.UserToKernelInt32(_system, address, out int currentValue))
|
||||
{
|
||||
System.CriticalSection.Leave();
|
||||
_system.CriticalSection.Leave();
|
||||
|
||||
return MakeError(ErrorModule.Kernel, KernelErr.NoAccessPerm);
|
||||
}
|
||||
|
||||
if (ShouldDecrement)
|
||||
if (shouldDecrement)
|
||||
{
|
||||
while (CurrentValue < Value)
|
||||
while (currentValue < value)
|
||||
{
|
||||
if (CurrentProcess.CpuMemory.TestExclusive(0, Address))
|
||||
if (currentProcess.CpuMemory.TestExclusive(0, address))
|
||||
{
|
||||
CurrentProcess.CpuMemory.WriteInt32(Address, CurrentValue - 1);
|
||||
currentProcess.CpuMemory.WriteInt32(address, currentValue - 1);
|
||||
|
||||
CurrentProcess.CpuMemory.ClearExclusiveForStore(0);
|
||||
currentProcess.CpuMemory.ClearExclusiveForStore(0);
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
CurrentProcess.CpuMemory.SetExclusive(0, Address);
|
||||
currentProcess.CpuMemory.SetExclusive(0, address);
|
||||
|
||||
CurrentValue = CurrentProcess.CpuMemory.ReadInt32(Address);
|
||||
currentValue = currentProcess.CpuMemory.ReadInt32(address);
|
||||
}
|
||||
}
|
||||
|
||||
CurrentProcess.CpuMemory.ClearExclusive(0);
|
||||
currentProcess.CpuMemory.ClearExclusive(0);
|
||||
|
||||
if (CurrentValue < Value)
|
||||
if (currentValue < value)
|
||||
{
|
||||
if (Timeout == 0)
|
||||
if (timeout == 0)
|
||||
{
|
||||
System.CriticalSection.Leave();
|
||||
_system.CriticalSection.Leave();
|
||||
|
||||
return MakeError(ErrorModule.Kernel, KernelErr.Timeout);
|
||||
}
|
||||
|
||||
CurrentThread.MutexAddress = Address;
|
||||
CurrentThread.WaitingInArbitration = true;
|
||||
currentThread.MutexAddress = address;
|
||||
currentThread.WaitingInArbitration = true;
|
||||
|
||||
InsertSortedByPriority(ArbiterThreads, CurrentThread);
|
||||
InsertSortedByPriority(ArbiterThreads, currentThread);
|
||||
|
||||
CurrentThread.Reschedule(ThreadSchedState.Paused);
|
||||
currentThread.Reschedule(ThreadSchedState.Paused);
|
||||
|
||||
if (Timeout > 0)
|
||||
if (timeout > 0)
|
||||
{
|
||||
System.TimeManager.ScheduleFutureInvocation(CurrentThread, Timeout);
|
||||
_system.TimeManager.ScheduleFutureInvocation(currentThread, timeout);
|
||||
}
|
||||
|
||||
System.CriticalSection.Leave();
|
||||
_system.CriticalSection.Leave();
|
||||
|
||||
if (Timeout > 0)
|
||||
if (timeout > 0)
|
||||
{
|
||||
System.TimeManager.UnscheduleFutureInvocation(CurrentThread);
|
||||
_system.TimeManager.UnscheduleFutureInvocation(currentThread);
|
||||
}
|
||||
|
||||
System.CriticalSection.Enter();
|
||||
_system.CriticalSection.Enter();
|
||||
|
||||
if (CurrentThread.WaitingInArbitration)
|
||||
if (currentThread.WaitingInArbitration)
|
||||
{
|
||||
ArbiterThreads.Remove(CurrentThread);
|
||||
ArbiterThreads.Remove(currentThread);
|
||||
|
||||
CurrentThread.WaitingInArbitration = false;
|
||||
currentThread.WaitingInArbitration = false;
|
||||
}
|
||||
|
||||
System.CriticalSection.Leave();
|
||||
_system.CriticalSection.Leave();
|
||||
|
||||
return CurrentThread.ObjSyncResult;
|
||||
return currentThread.ObjSyncResult;
|
||||
}
|
||||
|
||||
System.CriticalSection.Leave();
|
||||
_system.CriticalSection.Leave();
|
||||
|
||||
return MakeError(ErrorModule.Kernel, KernelErr.InvalidState);
|
||||
}
|
||||
|
||||
private void InsertSortedByPriority(List<KThread> Threads, KThread Thread)
|
||||
private void InsertSortedByPriority(List<KThread> threads, KThread thread)
|
||||
{
|
||||
int NextIndex = -1;
|
||||
int nextIndex = -1;
|
||||
|
||||
for (int Index = 0; Index < Threads.Count; Index++)
|
||||
for (int index = 0; index < threads.Count; index++)
|
||||
{
|
||||
if (Threads[Index].DynamicPriority > Thread.DynamicPriority)
|
||||
if (threads[index].DynamicPriority > thread.DynamicPriority)
|
||||
{
|
||||
NextIndex = Index;
|
||||
nextIndex = index;
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (NextIndex != -1)
|
||||
if (nextIndex != -1)
|
||||
{
|
||||
Threads.Insert(NextIndex, Thread);
|
||||
threads.Insert(nextIndex, thread);
|
||||
}
|
||||
else
|
||||
{
|
||||
Threads.Add(Thread);
|
||||
threads.Add(thread);
|
||||
}
|
||||
}
|
||||
|
||||
public long Signal(long Address, int Count)
|
||||
public long Signal(long address, int count)
|
||||
{
|
||||
System.CriticalSection.Enter();
|
||||
_system.CriticalSection.Enter();
|
||||
|
||||
WakeArbiterThreads(Address, Count);
|
||||
WakeArbiterThreads(address, count);
|
||||
|
||||
System.CriticalSection.Leave();
|
||||
_system.CriticalSection.Leave();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
public long SignalAndIncrementIfEqual(long Address, int Value, int Count)
|
||||
public long SignalAndIncrementIfEqual(long address, int value, int count)
|
||||
{
|
||||
System.CriticalSection.Enter();
|
||||
_system.CriticalSection.Enter();
|
||||
|
||||
KProcess CurrentProcess = System.Scheduler.GetCurrentProcess();
|
||||
KProcess currentProcess = _system.Scheduler.GetCurrentProcess();
|
||||
|
||||
CurrentProcess.CpuMemory.SetExclusive(0, Address);
|
||||
currentProcess.CpuMemory.SetExclusive(0, address);
|
||||
|
||||
if (!KernelTransfer.UserToKernelInt32(System, Address, out int CurrentValue))
|
||||
if (!KernelTransfer.UserToKernelInt32(_system, address, out int currentValue))
|
||||
{
|
||||
System.CriticalSection.Leave();
|
||||
_system.CriticalSection.Leave();
|
||||
|
||||
return MakeError(ErrorModule.Kernel, KernelErr.NoAccessPerm);
|
||||
}
|
||||
|
||||
while (CurrentValue == Value)
|
||||
while (currentValue == value)
|
||||
{
|
||||
if (CurrentProcess.CpuMemory.TestExclusive(0, Address))
|
||||
if (currentProcess.CpuMemory.TestExclusive(0, address))
|
||||
{
|
||||
CurrentProcess.CpuMemory.WriteInt32(Address, CurrentValue + 1);
|
||||
currentProcess.CpuMemory.WriteInt32(address, currentValue + 1);
|
||||
|
||||
CurrentProcess.CpuMemory.ClearExclusiveForStore(0);
|
||||
currentProcess.CpuMemory.ClearExclusiveForStore(0);
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
CurrentProcess.CpuMemory.SetExclusive(0, Address);
|
||||
currentProcess.CpuMemory.SetExclusive(0, address);
|
||||
|
||||
CurrentValue = CurrentProcess.CpuMemory.ReadInt32(Address);
|
||||
currentValue = currentProcess.CpuMemory.ReadInt32(address);
|
||||
}
|
||||
|
||||
CurrentProcess.CpuMemory.ClearExclusive(0);
|
||||
currentProcess.CpuMemory.ClearExclusive(0);
|
||||
|
||||
if (CurrentValue != Value)
|
||||
if (currentValue != value)
|
||||
{
|
||||
System.CriticalSection.Leave();
|
||||
_system.CriticalSection.Leave();
|
||||
|
||||
return MakeError(ErrorModule.Kernel, KernelErr.InvalidState);
|
||||
}
|
||||
|
||||
WakeArbiterThreads(Address, Count);
|
||||
WakeArbiterThreads(address, count);
|
||||
|
||||
System.CriticalSection.Leave();
|
||||
_system.CriticalSection.Leave();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
public long SignalAndModifyIfEqual(long Address, int Value, int Count)
|
||||
public long SignalAndModifyIfEqual(long address, int value, int count)
|
||||
{
|
||||
System.CriticalSection.Enter();
|
||||
_system.CriticalSection.Enter();
|
||||
|
||||
int Offset;
|
||||
int offset;
|
||||
|
||||
//The value is decremented if the number of threads waiting is less
|
||||
//or equal to the Count of threads to be signaled, or Count is zero
|
||||
//or negative. It is incremented if there are no threads waiting.
|
||||
int WaitingCount = 0;
|
||||
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)
|
||||
if (++waitingCount > count)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (WaitingCount > 0)
|
||||
if (waitingCount > 0)
|
||||
{
|
||||
Offset = WaitingCount <= Count || Count <= 0 ? -1 : 0;
|
||||
offset = waitingCount <= count || count <= 0 ? -1 : 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
Offset = 1;
|
||||
offset = 1;
|
||||
}
|
||||
|
||||
KProcess CurrentProcess = System.Scheduler.GetCurrentProcess();
|
||||
KProcess currentProcess = _system.Scheduler.GetCurrentProcess();
|
||||
|
||||
CurrentProcess.CpuMemory.SetExclusive(0, Address);
|
||||
currentProcess.CpuMemory.SetExclusive(0, address);
|
||||
|
||||
if (!KernelTransfer.UserToKernelInt32(System, Address, out int CurrentValue))
|
||||
if (!KernelTransfer.UserToKernelInt32(_system, address, out int currentValue))
|
||||
{
|
||||
System.CriticalSection.Leave();
|
||||
_system.CriticalSection.Leave();
|
||||
|
||||
return MakeError(ErrorModule.Kernel, KernelErr.NoAccessPerm);
|
||||
}
|
||||
|
||||
while (CurrentValue == Value)
|
||||
while (currentValue == value)
|
||||
{
|
||||
if (CurrentProcess.CpuMemory.TestExclusive(0, Address))
|
||||
if (currentProcess.CpuMemory.TestExclusive(0, address))
|
||||
{
|
||||
CurrentProcess.CpuMemory.WriteInt32(Address, CurrentValue + Offset);
|
||||
currentProcess.CpuMemory.WriteInt32(address, currentValue + offset);
|
||||
|
||||
CurrentProcess.CpuMemory.ClearExclusiveForStore(0);
|
||||
currentProcess.CpuMemory.ClearExclusiveForStore(0);
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
CurrentProcess.CpuMemory.SetExclusive(0, Address);
|
||||
currentProcess.CpuMemory.SetExclusive(0, address);
|
||||
|
||||
CurrentValue = CurrentProcess.CpuMemory.ReadInt32(Address);
|
||||
currentValue = currentProcess.CpuMemory.ReadInt32(address);
|
||||
}
|
||||
|
||||
CurrentProcess.CpuMemory.ClearExclusive(0);
|
||||
currentProcess.CpuMemory.ClearExclusive(0);
|
||||
|
||||
if (CurrentValue != Value)
|
||||
if (currentValue != value)
|
||||
{
|
||||
System.CriticalSection.Leave();
|
||||
_system.CriticalSection.Leave();
|
||||
|
||||
return MakeError(ErrorModule.Kernel, KernelErr.InvalidState);
|
||||
}
|
||||
|
||||
WakeArbiterThreads(Address, Count);
|
||||
WakeArbiterThreads(address, count);
|
||||
|
||||
System.CriticalSection.Leave();
|
||||
_system.CriticalSection.Leave();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
private void WakeArbiterThreads(long Address, int Count)
|
||||
private void WakeArbiterThreads(long address, int count)
|
||||
{
|
||||
Queue<KThread> SignaledThreads = new Queue<KThread>();
|
||||
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);
|
||||
signaledThreads.Enqueue(thread);
|
||||
|
||||
//If the count is <= 0, we should signal all threads waiting.
|
||||
if (Count >= 1 && --Count == 0)
|
||||
if (count >= 1 && --count == 0)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
while (SignaledThreads.TryDequeue(out KThread Thread))
|
||||
while (signaledThreads.TryDequeue(out KThread thread))
|
||||
{
|
||||
Thread.SignaledObj = null;
|
||||
Thread.ObjSyncResult = 0;
|
||||
thread.SignaledObj = null;
|
||||
thread.ObjSyncResult = 0;
|
||||
|
||||
Thread.ReleaseAndResume();
|
||||
thread.ReleaseAndResume();
|
||||
|
||||
Thread.WaitingInArbitration = false;
|
||||
thread.WaitingInArbitration = false;
|
||||
|
||||
ArbiterThreads.Remove(Thread);
|
||||
ArbiterThreads.Remove(thread);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue