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