2018-09-18 16:36:43 -07:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
|
|
|
|
|
|
|
using static Ryujinx.HLE.HOS.ErrorCode;
|
|
|
|
|
|
|
|
namespace Ryujinx.HLE.HOS.Kernel
|
|
|
|
{
|
|
|
|
class KAddressArbiter
|
|
|
|
{
|
|
|
|
private const int HasListenersMask = 0x40000000;
|
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
private Horizon _system;
|
2018-09-18 16:36:43 -07:00
|
|
|
|
|
|
|
public List<KThread> CondVarThreads;
|
|
|
|
public List<KThread> ArbiterThreads;
|
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
public KAddressArbiter(Horizon system)
|
2018-09-18 16:36:43 -07:00
|
|
|
{
|
2018-12-06 03:16:24 -08:00
|
|
|
_system = system;
|
2018-09-18 16:36:43 -07:00
|
|
|
|
|
|
|
CondVarThreads = new List<KThread>();
|
|
|
|
ArbiterThreads = new List<KThread>();
|
|
|
|
}
|
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
public long ArbitrateLock(int ownerHandle, long mutexAddress, int requesterHandle)
|
2018-09-18 16:36:43 -07:00
|
|
|
{
|
2018-12-06 03:16:24 -08:00
|
|
|
KThread currentThread = _system.Scheduler.GetCurrentThread();
|
2018-09-18 16:36:43 -07:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
_system.CriticalSection.Enter();
|
2018-11-28 14:18:09 -08:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
currentThread.SignaledObj = null;
|
|
|
|
currentThread.ObjSyncResult = 0;
|
2018-09-18 16:36:43 -07:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
KProcess currentProcess = _system.Scheduler.GetCurrentProcess();
|
2018-11-28 14:18:09 -08:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
if (!KernelTransfer.UserToKernelInt32(_system, mutexAddress, out int mutexValue))
|
2018-09-18 16:36:43 -07:00
|
|
|
{
|
2018-12-06 03:16:24 -08:00
|
|
|
_system.CriticalSection.Leave();
|
2018-09-18 16:36:43 -07:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
return MakeError(ErrorModule.Kernel, KernelErr.NoAccessPerm);
|
2018-09-18 16:36:43 -07:00
|
|
|
}
|
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
if (mutexValue != (ownerHandle | HasListenersMask))
|
2018-09-18 16:36:43 -07:00
|
|
|
{
|
2018-12-06 03:16:24 -08:00
|
|
|
_system.CriticalSection.Leave();
|
2018-09-18 16:36:43 -07:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
KThread mutexOwner = currentProcess.HandleTable.GetObject<KThread>(ownerHandle);
|
2018-09-18 16:36:43 -07:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
if (mutexOwner == null)
|
2018-09-18 16:36:43 -07:00
|
|
|
{
|
2018-12-06 03:16:24 -08:00
|
|
|
_system.CriticalSection.Leave();
|
2018-09-18 16:36:43 -07:00
|
|
|
|
|
|
|
return MakeError(ErrorModule.Kernel, KernelErr.InvalidHandle);
|
|
|
|
}
|
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
currentThread.MutexAddress = mutexAddress;
|
|
|
|
currentThread.ThreadHandleForUserMutex = requesterHandle;
|
2018-09-18 16:36:43 -07:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
mutexOwner.AddMutexWaiter(currentThread);
|
2018-09-18 16:36:43 -07:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
currentThread.Reschedule(ThreadSchedState.Paused);
|
2018-09-18 16:36:43 -07:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
_system.CriticalSection.Leave();
|
|
|
|
_system.CriticalSection.Enter();
|
2018-09-18 16:36:43 -07:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
if (currentThread.MutexOwner != null)
|
2018-09-18 16:36:43 -07:00
|
|
|
{
|
2018-12-06 03:16:24 -08:00
|
|
|
currentThread.MutexOwner.RemoveMutexWaiter(currentThread);
|
2018-09-18 16:36:43 -07:00
|
|
|
}
|
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
_system.CriticalSection.Leave();
|
2018-09-18 16:36:43 -07:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
return (uint)currentThread.ObjSyncResult;
|
2018-09-18 16:36:43 -07:00
|
|
|
}
|
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
public long ArbitrateUnlock(long mutexAddress)
|
2018-09-18 16:36:43 -07:00
|
|
|
{
|
2018-12-06 03:16:24 -08:00
|
|
|
_system.CriticalSection.Enter();
|
2018-09-18 16:36:43 -07:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
KThread currentThread = _system.Scheduler.GetCurrentThread();
|
2018-09-18 16:36:43 -07:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
(long result, KThread newOwnerThread) = MutexUnlock(currentThread, mutexAddress);
|
2018-09-18 16:36:43 -07:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
if (result != 0 && newOwnerThread != null)
|
2018-09-18 16:36:43 -07:00
|
|
|
{
|
2018-12-06 03:16:24 -08:00
|
|
|
newOwnerThread.SignaledObj = null;
|
|
|
|
newOwnerThread.ObjSyncResult = (int)result;
|
2018-09-18 16:36:43 -07:00
|
|
|
}
|
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
_system.CriticalSection.Leave();
|
2018-09-18 16:36:43 -07:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
return result;
|
2018-09-18 16:36:43 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
public long WaitProcessWideKeyAtomic(
|
2018-12-06 03:16:24 -08:00
|
|
|
long mutexAddress,
|
|
|
|
long condVarAddress,
|
|
|
|
int threadHandle,
|
|
|
|
long timeout)
|
2018-09-18 16:36:43 -07:00
|
|
|
{
|
2018-12-06 03:16:24 -08:00
|
|
|
_system.CriticalSection.Enter();
|
2018-09-18 16:36:43 -07:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
KThread currentThread = _system.Scheduler.GetCurrentThread();
|
2018-09-18 16:36:43 -07:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
currentThread.SignaledObj = null;
|
|
|
|
currentThread.ObjSyncResult = (int)MakeError(ErrorModule.Kernel, KernelErr.Timeout);
|
2018-09-18 16:36:43 -07:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
if (currentThread.ShallBeTerminated ||
|
|
|
|
currentThread.SchedFlags == ThreadSchedState.TerminationPending)
|
2018-09-18 16:36:43 -07:00
|
|
|
{
|
2018-12-06 03:16:24 -08:00
|
|
|
_system.CriticalSection.Leave();
|
2018-09-18 16:36:43 -07:00
|
|
|
|
|
|
|
return MakeError(ErrorModule.Kernel, KernelErr.ThreadTerminating);
|
|
|
|
}
|
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
(long result, _) = MutexUnlock(currentThread, mutexAddress);
|
2018-09-18 16:36:43 -07:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
if (result != 0)
|
2018-09-18 16:36:43 -07:00
|
|
|
{
|
2018-12-06 03:16:24 -08:00
|
|
|
_system.CriticalSection.Leave();
|
2018-09-18 16:36:43 -07:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
return result;
|
2018-09-18 16:36:43 -07:00
|
|
|
}
|
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
currentThread.MutexAddress = mutexAddress;
|
|
|
|
currentThread.ThreadHandleForUserMutex = threadHandle;
|
|
|
|
currentThread.CondVarAddress = condVarAddress;
|
2018-09-18 16:36:43 -07:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
CondVarThreads.Add(currentThread);
|
2018-09-18 16:36:43 -07:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
if (timeout != 0)
|
2018-09-18 16:36:43 -07:00
|
|
|
{
|
2018-12-06 03:16:24 -08:00
|
|
|
currentThread.Reschedule(ThreadSchedState.Paused);
|
2018-09-18 16:36:43 -07:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
if (timeout > 0)
|
2018-09-18 16:36:43 -07:00
|
|
|
{
|
2018-12-06 03:16:24 -08:00
|
|
|
_system.TimeManager.ScheduleFutureInvocation(currentThread, timeout);
|
2018-09-18 16:36:43 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
_system.CriticalSection.Leave();
|
2018-09-18 16:36:43 -07:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
if (timeout > 0)
|
2018-09-18 16:36:43 -07:00
|
|
|
{
|
2018-12-06 03:16:24 -08:00
|
|
|
_system.TimeManager.UnscheduleFutureInvocation(currentThread);
|
2018-09-18 16:36:43 -07:00
|
|
|
}
|
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
_system.CriticalSection.Enter();
|
2018-09-18 16:36:43 -07:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
if (currentThread.MutexOwner != null)
|
2018-09-18 16:36:43 -07:00
|
|
|
{
|
2018-12-06 03:16:24 -08:00
|
|
|
currentThread.MutexOwner.RemoveMutexWaiter(currentThread);
|
2018-09-18 16:36:43 -07:00
|
|
|
}
|
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
CondVarThreads.Remove(currentThread);
|
2018-09-18 16:36:43 -07:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
_system.CriticalSection.Leave();
|
2018-09-18 16:36:43 -07:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
return (uint)currentThread.ObjSyncResult;
|
2018-09-18 16:36:43 -07:00
|
|
|
}
|
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
private (long, KThread) MutexUnlock(KThread currentThread, long mutexAddress)
|
2018-09-18 16:36:43 -07:00
|
|
|
{
|
2018-12-06 03:16:24 -08:00
|
|
|
KThread newOwnerThread = currentThread.RelinquishMutex(mutexAddress, out int count);
|
2018-09-18 16:36:43 -07:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
int mutexValue = 0;
|
2018-09-18 16:36:43 -07:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
if (newOwnerThread != null)
|
2018-09-18 16:36:43 -07:00
|
|
|
{
|
2018-12-06 03:16:24 -08:00
|
|
|
mutexValue = newOwnerThread.ThreadHandleForUserMutex;
|
2018-09-18 16:36:43 -07:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
if (count >= 2)
|
2018-09-18 16:36:43 -07:00
|
|
|
{
|
2018-12-06 03:16:24 -08:00
|
|
|
mutexValue |= HasListenersMask;
|
2018-09-18 16:36:43 -07:00
|
|
|
}
|
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
newOwnerThread.SignaledObj = null;
|
|
|
|
newOwnerThread.ObjSyncResult = 0;
|
2018-09-18 16:36:43 -07:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
newOwnerThread.ReleaseAndResume();
|
2018-09-18 16:36:43 -07:00
|
|
|
}
|
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
long result = 0;
|
2018-09-18 16:36:43 -07:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
if (!KernelTransfer.KernelToUserInt32(_system, mutexAddress, mutexValue))
|
2018-09-18 16:36:43 -07:00
|
|
|
{
|
2018-12-06 03:16:24 -08:00
|
|
|
result = MakeError(ErrorModule.Kernel, KernelErr.NoAccessPerm);
|
2018-09-18 16:36:43 -07:00
|
|
|
}
|
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
return (result, newOwnerThread);
|
2018-09-18 16:36:43 -07:00
|
|
|
}
|
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
public void SignalProcessWideKey(long address, int count)
|
2018-09-18 16:36:43 -07:00
|
|
|
{
|
2018-12-06 03:16:24 -08:00
|
|
|
Queue<KThread> signaledThreads = new Queue<KThread>();
|
2018-09-18 16:36:43 -07:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
_system.CriticalSection.Enter();
|
2018-09-18 16:36:43 -07:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
IOrderedEnumerable<KThread> sortedThreads = CondVarThreads.OrderBy(x => x.DynamicPriority);
|
2018-09-18 16:36:43 -07:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
foreach (KThread thread in sortedThreads.Where(x => x.CondVarAddress == address))
|
2018-09-18 16:36:43 -07:00
|
|
|
{
|
2018-12-06 03:16:24 -08:00
|
|
|
TryAcquireMutex(thread);
|
2018-09-18 16:36:43 -07:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
signaledThreads.Enqueue(thread);
|
2018-09-18 16:36:43 -07:00
|
|
|
|
|
|
|
//If the count is <= 0, we should signal all threads waiting.
|
2018-12-06 03:16:24 -08:00
|
|
|
if (count >= 1 && --count == 0)
|
2018-09-18 16:36:43 -07:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
while (signaledThreads.TryDequeue(out KThread thread))
|
2018-09-18 16:36:43 -07:00
|
|
|
{
|
2018-12-06 03:16:24 -08:00
|
|
|
CondVarThreads.Remove(thread);
|
2018-09-18 16:36:43 -07:00
|
|
|
}
|
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
_system.CriticalSection.Leave();
|
2018-09-18 16:36:43 -07:00
|
|
|
}
|
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
private KThread TryAcquireMutex(KThread requester)
|
2018-09-18 16:36:43 -07:00
|
|
|
{
|
2018-12-06 03:16:24 -08:00
|
|
|
long address = requester.MutexAddress;
|
2018-09-18 16:36:43 -07:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
KProcess currentProcess = _system.Scheduler.GetCurrentProcess();
|
2018-09-18 16:36:43 -07:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
currentProcess.CpuMemory.SetExclusive(0, address);
|
2018-11-28 14:18:09 -08:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
if (!KernelTransfer.UserToKernelInt32(_system, address, out int mutexValue))
|
2018-09-18 16:36:43 -07:00
|
|
|
{
|
|
|
|
//Invalid address.
|
2018-12-06 03:16:24 -08:00
|
|
|
currentProcess.CpuMemory.ClearExclusive(0);
|
2018-09-18 16:36:43 -07:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
requester.SignaledObj = null;
|
|
|
|
requester.ObjSyncResult = (int)MakeError(ErrorModule.Kernel, KernelErr.NoAccessPerm);
|
2018-09-18 16:36:43 -07:00
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (true)
|
|
|
|
{
|
2018-12-06 03:16:24 -08:00
|
|
|
if (currentProcess.CpuMemory.TestExclusive(0, address))
|
2018-09-18 16:36:43 -07:00
|
|
|
{
|
2018-12-06 03:16:24 -08:00
|
|
|
if (mutexValue != 0)
|
2018-09-18 16:36:43 -07:00
|
|
|
{
|
|
|
|
//Update value to indicate there is a mutex waiter now.
|
2018-12-06 03:16:24 -08:00
|
|
|
currentProcess.CpuMemory.WriteInt32(address, mutexValue | HasListenersMask);
|
2018-09-18 16:36:43 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
//No thread owning the mutex, assign to requesting thread.
|
2018-12-06 03:16:24 -08:00
|
|
|
currentProcess.CpuMemory.WriteInt32(address, requester.ThreadHandleForUserMutex);
|
2018-09-18 16:36:43 -07:00
|
|
|
}
|
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
currentProcess.CpuMemory.ClearExclusiveForStore(0);
|
2018-09-18 16:36:43 -07:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
currentProcess.CpuMemory.SetExclusive(0, address);
|
2018-09-18 16:36:43 -07:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
mutexValue = currentProcess.CpuMemory.ReadInt32(address);
|
2018-09-18 16:36:43 -07:00
|
|
|
}
|
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
if (mutexValue == 0)
|
2018-09-18 16:36:43 -07:00
|
|
|
{
|
|
|
|
//We now own the mutex.
|
2018-12-06 03:16:24 -08:00
|
|
|
requester.SignaledObj = null;
|
|
|
|
requester.ObjSyncResult = 0;
|
2018-09-18 16:36:43 -07:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
requester.ReleaseAndResume();
|
2018-09-18 16:36:43 -07:00
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
mutexValue &= ~HasListenersMask;
|
2018-09-18 16:36:43 -07:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
KThread mutexOwner = currentProcess.HandleTable.GetObject<KThread>(mutexValue);
|
2018-09-18 16:36:43 -07:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
if (mutexOwner != null)
|
2018-09-18 16:36:43 -07:00
|
|
|
{
|
|
|
|
//Mutex already belongs to another thread, wait for it.
|
2018-12-06 03:16:24 -08:00
|
|
|
mutexOwner.AddMutexWaiter(requester);
|
2018-09-18 16:36:43 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
//Invalid mutex owner.
|
2018-12-06 03:16:24 -08:00
|
|
|
requester.SignaledObj = null;
|
|
|
|
requester.ObjSyncResult = (int)MakeError(ErrorModule.Kernel, KernelErr.InvalidHandle);
|
2018-09-18 16:36:43 -07:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
requester.ReleaseAndResume();
|
2018-09-18 16:36:43 -07:00
|
|
|
}
|
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
return mutexOwner;
|
2018-09-18 16:36:43 -07:00
|
|
|
}
|
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
public long WaitForAddressIfEqual(long address, int value, long timeout)
|
2018-09-18 16:36:43 -07:00
|
|
|
{
|
2018-12-06 03:16:24 -08:00
|
|
|
KThread currentThread = _system.Scheduler.GetCurrentThread();
|
2018-09-18 16:36:43 -07:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
_system.CriticalSection.Enter();
|
2018-09-18 16:36:43 -07:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
if (currentThread.ShallBeTerminated ||
|
|
|
|
currentThread.SchedFlags == ThreadSchedState.TerminationPending)
|
2018-09-18 16:36:43 -07:00
|
|
|
{
|
2018-12-06 03:16:24 -08:00
|
|
|
_system.CriticalSection.Leave();
|
2018-09-18 16:36:43 -07:00
|
|
|
|
|
|
|
return MakeError(ErrorModule.Kernel, KernelErr.ThreadTerminating);
|
|
|
|
}
|
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
currentThread.SignaledObj = null;
|
|
|
|
currentThread.ObjSyncResult = (int)MakeError(ErrorModule.Kernel, KernelErr.Timeout);
|
2018-09-18 16:36:43 -07:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
if (!KernelTransfer.UserToKernelInt32(_system, address, out int currentValue))
|
2018-09-18 16:36:43 -07:00
|
|
|
{
|
2018-12-06 03:16:24 -08:00
|
|
|
_system.CriticalSection.Leave();
|
2018-09-18 16:36:43 -07:00
|
|
|
|
|
|
|
return MakeError(ErrorModule.Kernel, KernelErr.NoAccessPerm);
|
|
|
|
}
|
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
if (currentValue == value)
|
2018-09-18 16:36:43 -07:00
|
|
|
{
|
2018-12-06 03:16:24 -08:00
|
|
|
if (timeout == 0)
|
2018-09-18 16:36:43 -07:00
|
|
|
{
|
2018-12-06 03:16:24 -08:00
|
|
|
_system.CriticalSection.Leave();
|
2018-09-18 16:36:43 -07:00
|
|
|
|
|
|
|
return MakeError(ErrorModule.Kernel, KernelErr.Timeout);
|
|
|
|
}
|
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
currentThread.MutexAddress = address;
|
|
|
|
currentThread.WaitingInArbitration = true;
|
2018-09-18 16:36:43 -07:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
InsertSortedByPriority(ArbiterThreads, currentThread);
|
2018-09-18 16:36:43 -07:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
currentThread.Reschedule(ThreadSchedState.Paused);
|
2018-09-18 16:36:43 -07:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
if (timeout > 0)
|
2018-09-18 16:36:43 -07:00
|
|
|
{
|
2018-12-06 03:16:24 -08:00
|
|
|
_system.TimeManager.ScheduleFutureInvocation(currentThread, timeout);
|
2018-09-18 16:36:43 -07:00
|
|
|
}
|
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
_system.CriticalSection.Leave();
|
2018-09-18 16:36:43 -07:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
if (timeout > 0)
|
2018-09-18 16:36:43 -07:00
|
|
|
{
|
2018-12-06 03:16:24 -08:00
|
|
|
_system.TimeManager.UnscheduleFutureInvocation(currentThread);
|
2018-09-18 16:36:43 -07:00
|
|
|
}
|
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
_system.CriticalSection.Enter();
|
2018-09-18 16:36:43 -07:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
if (currentThread.WaitingInArbitration)
|
2018-09-18 16:36:43 -07:00
|
|
|
{
|
2018-12-06 03:16:24 -08:00
|
|
|
ArbiterThreads.Remove(currentThread);
|
2018-09-18 16:36:43 -07:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
currentThread.WaitingInArbitration = false;
|
2018-09-18 16:36:43 -07:00
|
|
|
}
|
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
_system.CriticalSection.Leave();
|
2018-09-18 16:36:43 -07:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
return currentThread.ObjSyncResult;
|
2018-09-18 16:36:43 -07:00
|
|
|
}
|
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
_system.CriticalSection.Leave();
|
2018-09-18 16:36:43 -07:00
|
|
|
|
|
|
|
return MakeError(ErrorModule.Kernel, KernelErr.InvalidState);
|
|
|
|
}
|
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
public long WaitForAddressIfLessThan(long address, int value, bool shouldDecrement, long timeout)
|
2018-09-18 16:36:43 -07:00
|
|
|
{
|
2018-12-06 03:16:24 -08:00
|
|
|
KThread currentThread = _system.Scheduler.GetCurrentThread();
|
2018-09-18 16:36:43 -07:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
_system.CriticalSection.Enter();
|
2018-09-18 16:36:43 -07:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
if (currentThread.ShallBeTerminated ||
|
|
|
|
currentThread.SchedFlags == ThreadSchedState.TerminationPending)
|
2018-09-18 16:36:43 -07:00
|
|
|
{
|
2018-12-06 03:16:24 -08:00
|
|
|
_system.CriticalSection.Leave();
|
2018-09-18 16:36:43 -07:00
|
|
|
|
|
|
|
return MakeError(ErrorModule.Kernel, KernelErr.ThreadTerminating);
|
|
|
|
}
|
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
currentThread.SignaledObj = null;
|
|
|
|
currentThread.ObjSyncResult = (int)MakeError(ErrorModule.Kernel, KernelErr.Timeout);
|
2018-09-18 16:36:43 -07:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
KProcess currentProcess = _system.Scheduler.GetCurrentProcess();
|
2018-11-28 14:18:09 -08:00
|
|
|
|
2018-09-18 16:36:43 -07:00
|
|
|
//If ShouldDecrement is true, do atomic decrement of the value at Address.
|
2018-12-06 03:16:24 -08:00
|
|
|
currentProcess.CpuMemory.SetExclusive(0, address);
|
2018-09-18 16:36:43 -07:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
if (!KernelTransfer.UserToKernelInt32(_system, address, out int currentValue))
|
2018-09-18 16:36:43 -07:00
|
|
|
{
|
2018-12-06 03:16:24 -08:00
|
|
|
_system.CriticalSection.Leave();
|
2018-09-18 16:36:43 -07:00
|
|
|
|
|
|
|
return MakeError(ErrorModule.Kernel, KernelErr.NoAccessPerm);
|
|
|
|
}
|
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
if (shouldDecrement)
|
2018-09-18 16:36:43 -07:00
|
|
|
{
|
2018-12-06 03:16:24 -08:00
|
|
|
while (currentValue < value)
|
2018-09-18 16:36:43 -07:00
|
|
|
{
|
2018-12-06 03:16:24 -08:00
|
|
|
if (currentProcess.CpuMemory.TestExclusive(0, address))
|
2018-09-18 16:36:43 -07:00
|
|
|
{
|
2018-12-06 03:16:24 -08:00
|
|
|
currentProcess.CpuMemory.WriteInt32(address, currentValue - 1);
|
2018-09-18 16:36:43 -07:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
currentProcess.CpuMemory.ClearExclusiveForStore(0);
|
2018-09-18 16:36:43 -07:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
currentProcess.CpuMemory.SetExclusive(0, address);
|
2018-09-18 16:36:43 -07:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
currentValue = currentProcess.CpuMemory.ReadInt32(address);
|
2018-09-18 16:36:43 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
currentProcess.CpuMemory.ClearExclusive(0);
|
2018-09-18 16:36:43 -07:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
if (currentValue < value)
|
2018-09-18 16:36:43 -07:00
|
|
|
{
|
2018-12-06 03:16:24 -08:00
|
|
|
if (timeout == 0)
|
2018-09-18 16:36:43 -07:00
|
|
|
{
|
2018-12-06 03:16:24 -08:00
|
|
|
_system.CriticalSection.Leave();
|
2018-09-18 16:36:43 -07:00
|
|
|
|
|
|
|
return MakeError(ErrorModule.Kernel, KernelErr.Timeout);
|
|
|
|
}
|
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
currentThread.MutexAddress = address;
|
|
|
|
currentThread.WaitingInArbitration = true;
|
2018-09-18 16:36:43 -07:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
InsertSortedByPriority(ArbiterThreads, currentThread);
|
2018-09-18 16:36:43 -07:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
currentThread.Reschedule(ThreadSchedState.Paused);
|
2018-09-18 16:36:43 -07:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
if (timeout > 0)
|
2018-09-18 16:36:43 -07:00
|
|
|
{
|
2018-12-06 03:16:24 -08:00
|
|
|
_system.TimeManager.ScheduleFutureInvocation(currentThread, timeout);
|
2018-09-18 16:36:43 -07:00
|
|
|
}
|
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
_system.CriticalSection.Leave();
|
2018-09-18 16:36:43 -07:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
if (timeout > 0)
|
2018-09-18 16:36:43 -07:00
|
|
|
{
|
2018-12-06 03:16:24 -08:00
|
|
|
_system.TimeManager.UnscheduleFutureInvocation(currentThread);
|
2018-09-18 16:36:43 -07:00
|
|
|
}
|
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
_system.CriticalSection.Enter();
|
2018-09-18 16:36:43 -07:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
if (currentThread.WaitingInArbitration)
|
2018-09-18 16:36:43 -07:00
|
|
|
{
|
2018-12-06 03:16:24 -08:00
|
|
|
ArbiterThreads.Remove(currentThread);
|
2018-09-18 16:36:43 -07:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
currentThread.WaitingInArbitration = false;
|
2018-09-18 16:36:43 -07:00
|
|
|
}
|
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
_system.CriticalSection.Leave();
|
2018-09-18 16:36:43 -07:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
return currentThread.ObjSyncResult;
|
2018-09-18 16:36:43 -07:00
|
|
|
}
|
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
_system.CriticalSection.Leave();
|
2018-09-18 16:36:43 -07:00
|
|
|
|
|
|
|
return MakeError(ErrorModule.Kernel, KernelErr.InvalidState);
|
|
|
|
}
|
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
private void InsertSortedByPriority(List<KThread> threads, KThread thread)
|
2018-09-18 16:36:43 -07:00
|
|
|
{
|
2018-12-06 03:16:24 -08:00
|
|
|
int nextIndex = -1;
|
2018-09-18 16:36:43 -07:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
for (int index = 0; index < threads.Count; index++)
|
2018-09-18 16:36:43 -07:00
|
|
|
{
|
2018-12-06 03:16:24 -08:00
|
|
|
if (threads[index].DynamicPriority > thread.DynamicPriority)
|
2018-09-18 16:36:43 -07:00
|
|
|
{
|
2018-12-06 03:16:24 -08:00
|
|
|
nextIndex = index;
|
2018-09-18 16:36:43 -07:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
if (nextIndex != -1)
|
2018-09-18 16:36:43 -07:00
|
|
|
{
|
2018-12-06 03:16:24 -08:00
|
|
|
threads.Insert(nextIndex, thread);
|
2018-09-18 16:36:43 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-12-06 03:16:24 -08:00
|
|
|
threads.Add(thread);
|
2018-09-18 16:36:43 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
public long Signal(long address, int count)
|
2018-09-18 16:36:43 -07:00
|
|
|
{
|
2018-12-06 03:16:24 -08:00
|
|
|
_system.CriticalSection.Enter();
|
2018-09-18 16:36:43 -07:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
WakeArbiterThreads(address, count);
|
2018-09-18 16:36:43 -07:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
_system.CriticalSection.Leave();
|
2018-09-18 16:36:43 -07:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
public long SignalAndIncrementIfEqual(long address, int value, int count)
|
2018-09-18 16:36:43 -07:00
|
|
|
{
|
2018-12-06 03:16:24 -08:00
|
|
|
_system.CriticalSection.Enter();
|
2018-11-28 14:18:09 -08:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
KProcess currentProcess = _system.Scheduler.GetCurrentProcess();
|
2018-09-18 16:36:43 -07:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
currentProcess.CpuMemory.SetExclusive(0, address);
|
2018-09-18 16:36:43 -07:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
if (!KernelTransfer.UserToKernelInt32(_system, address, out int currentValue))
|
2018-09-18 16:36:43 -07:00
|
|
|
{
|
2018-12-06 03:16:24 -08:00
|
|
|
_system.CriticalSection.Leave();
|
2018-09-18 16:36:43 -07:00
|
|
|
|
|
|
|
return MakeError(ErrorModule.Kernel, KernelErr.NoAccessPerm);
|
|
|
|
}
|
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
while (currentValue == value)
|
2018-09-18 16:36:43 -07:00
|
|
|
{
|
2018-12-06 03:16:24 -08:00
|
|
|
if (currentProcess.CpuMemory.TestExclusive(0, address))
|
2018-09-18 16:36:43 -07:00
|
|
|
{
|
2018-12-06 03:16:24 -08:00
|
|
|
currentProcess.CpuMemory.WriteInt32(address, currentValue + 1);
|
2018-09-18 16:36:43 -07:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
currentProcess.CpuMemory.ClearExclusiveForStore(0);
|
2018-09-18 16:36:43 -07:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
currentProcess.CpuMemory.SetExclusive(0, address);
|
2018-09-18 16:36:43 -07:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
currentValue = currentProcess.CpuMemory.ReadInt32(address);
|
2018-09-18 16:36:43 -07:00
|
|
|
}
|
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
currentProcess.CpuMemory.ClearExclusive(0);
|
2018-09-18 16:36:43 -07:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
if (currentValue != value)
|
2018-09-18 16:36:43 -07:00
|
|
|
{
|
2018-12-06 03:16:24 -08:00
|
|
|
_system.CriticalSection.Leave();
|
2018-09-18 16:36:43 -07:00
|
|
|
|
|
|
|
return MakeError(ErrorModule.Kernel, KernelErr.InvalidState);
|
|
|
|
}
|
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
WakeArbiterThreads(address, count);
|
2018-09-18 16:36:43 -07:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
_system.CriticalSection.Leave();
|
2018-09-18 16:36:43 -07:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
public long SignalAndModifyIfEqual(long address, int value, int count)
|
2018-09-18 16:36:43 -07:00
|
|
|
{
|
2018-12-06 03:16:24 -08:00
|
|
|
_system.CriticalSection.Enter();
|
2018-09-18 16:36:43 -07:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
int offset;
|
2018-09-18 16:36:43 -07:00
|
|
|
|
|
|
|
//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.
|
2018-12-06 03:16:24 -08:00
|
|
|
int waitingCount = 0;
|
2018-09-18 16:36:43 -07:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
foreach (KThread thread in ArbiterThreads.Where(x => x.MutexAddress == address))
|
2018-09-18 16:36:43 -07:00
|
|
|
{
|
2018-12-06 03:16:24 -08:00
|
|
|
if (++waitingCount > count)
|
2018-09-18 16:36:43 -07:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
if (waitingCount > 0)
|
2018-09-18 16:36:43 -07:00
|
|
|
{
|
2018-12-06 03:16:24 -08:00
|
|
|
offset = waitingCount <= count || count <= 0 ? -1 : 0;
|
2018-09-18 16:36:43 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-12-06 03:16:24 -08:00
|
|
|
offset = 1;
|
2018-09-18 16:36:43 -07:00
|
|
|
}
|
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
KProcess currentProcess = _system.Scheduler.GetCurrentProcess();
|
2018-09-18 16:36:43 -07:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
currentProcess.CpuMemory.SetExclusive(0, address);
|
2018-11-28 14:18:09 -08:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
if (!KernelTransfer.UserToKernelInt32(_system, address, out int currentValue))
|
2018-09-18 16:36:43 -07:00
|
|
|
{
|
2018-12-06 03:16:24 -08:00
|
|
|
_system.CriticalSection.Leave();
|
2018-09-18 16:36:43 -07:00
|
|
|
|
|
|
|
return MakeError(ErrorModule.Kernel, KernelErr.NoAccessPerm);
|
|
|
|
}
|
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
while (currentValue == value)
|
2018-09-18 16:36:43 -07:00
|
|
|
{
|
2018-12-06 03:16:24 -08:00
|
|
|
if (currentProcess.CpuMemory.TestExclusive(0, address))
|
2018-09-18 16:36:43 -07:00
|
|
|
{
|
2018-12-06 03:16:24 -08:00
|
|
|
currentProcess.CpuMemory.WriteInt32(address, currentValue + offset);
|
2018-09-18 16:36:43 -07:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
currentProcess.CpuMemory.ClearExclusiveForStore(0);
|
2018-09-18 16:36:43 -07:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
currentProcess.CpuMemory.SetExclusive(0, address);
|
2018-09-18 16:36:43 -07:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
currentValue = currentProcess.CpuMemory.ReadInt32(address);
|
2018-09-18 16:36:43 -07:00
|
|
|
}
|
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
currentProcess.CpuMemory.ClearExclusive(0);
|
2018-09-18 16:36:43 -07:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
if (currentValue != value)
|
2018-09-18 16:36:43 -07:00
|
|
|
{
|
2018-12-06 03:16:24 -08:00
|
|
|
_system.CriticalSection.Leave();
|
2018-09-18 16:36:43 -07:00
|
|
|
|
|
|
|
return MakeError(ErrorModule.Kernel, KernelErr.InvalidState);
|
|
|
|
}
|
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
WakeArbiterThreads(address, count);
|
2018-09-18 16:36:43 -07:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
_system.CriticalSection.Leave();
|
2018-09-18 16:36:43 -07:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
private void WakeArbiterThreads(long address, int count)
|
2018-09-18 16:36:43 -07:00
|
|
|
{
|
2018-12-06 03:16:24 -08:00
|
|
|
Queue<KThread> signaledThreads = new Queue<KThread>();
|
2018-09-18 16:36:43 -07:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
foreach (KThread thread in ArbiterThreads.Where(x => x.MutexAddress == address))
|
2018-09-18 16:36:43 -07:00
|
|
|
{
|
2018-12-06 03:16:24 -08:00
|
|
|
signaledThreads.Enqueue(thread);
|
2018-09-18 16:36:43 -07:00
|
|
|
|
|
|
|
//If the count is <= 0, we should signal all threads waiting.
|
2018-12-06 03:16:24 -08:00
|
|
|
if (count >= 1 && --count == 0)
|
2018-09-18 16:36:43 -07:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
while (signaledThreads.TryDequeue(out KThread thread))
|
2018-09-18 16:36:43 -07:00
|
|
|
{
|
2018-12-06 03:16:24 -08:00
|
|
|
thread.SignaledObj = null;
|
|
|
|
thread.ObjSyncResult = 0;
|
2018-09-18 16:36:43 -07:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
thread.ReleaseAndResume();
|
2018-09-18 16:36:43 -07:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
thread.WaitingInArbitration = false;
|
2018-09-18 16:36:43 -07:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
ArbiterThreads.Remove(thread);
|
2018-09-18 16:36:43 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|