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