2018-12-18 06:33:36 +01:00
|
|
|
using Ryujinx.HLE.HOS.Kernel.Common;
|
2018-09-19 01:36:43 +02:00
|
|
|
using System.Collections.Generic;
|
|
|
|
|
2018-12-18 06:33:36 +01:00
|
|
|
namespace Ryujinx.HLE.HOS.Kernel.Threading
|
2018-09-19 01:36:43 +02:00
|
|
|
{
|
|
|
|
class KSynchronization
|
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
private Horizon _system;
|
2018-09-19 01:36:43 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
public KSynchronization(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
|
|
|
}
|
|
|
|
|
2018-12-18 06:33:36 +01:00
|
|
|
public KernelResult WaitFor(KSynchronizationObject[] syncObjs, long timeout, out int handleIndex)
|
2018-09-19 01:36:43 +02:00
|
|
|
{
|
2018-12-18 06:33:36 +01:00
|
|
|
handleIndex = 0;
|
|
|
|
|
|
|
|
KernelResult result = KernelResult.TimedOut;
|
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
|
|
|
|
|
|
|
//Check if objects are already signaled before waiting.
|
2018-12-06 12:16:24 +01:00
|
|
|
for (int index = 0; index < syncObjs.Length; index++)
|
2018-09-19 01:36:43 +02:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
if (!syncObjs[index].IsSignaled())
|
2018-09-19 01:36:43 +02:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-12-18 06:33:36 +01:00
|
|
|
handleIndex = index;
|
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
|
|
|
|
2019-01-18 23:26:39 +01:00
|
|
|
return KernelResult.Success;
|
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-06 12:16:24 +01:00
|
|
|
return result;
|
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
|
|
|
if (currentThread.ShallBeTerminated ||
|
|
|
|
currentThread.SchedFlags == ThreadSchedState.TerminationPending)
|
2018-09-19 01:36:43 +02:00
|
|
|
{
|
2018-12-18 06:33:36 +01:00
|
|
|
result = KernelResult.ThreadTerminating;
|
2018-09-19 01:36:43 +02:00
|
|
|
}
|
2018-12-06 12:16:24 +01:00
|
|
|
else if (currentThread.SyncCancelled)
|
2018-09-19 01:36:43 +02:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
currentThread.SyncCancelled = false;
|
2018-09-19 01:36:43 +02:00
|
|
|
|
2018-12-18 06:33:36 +01:00
|
|
|
result = KernelResult.Cancelled;
|
2018-09-19 01:36:43 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
LinkedListNode<KThread>[] syncNodes = new LinkedListNode<KThread>[syncObjs.Length];
|
2018-09-19 01:36:43 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
for (int index = 0; index < syncObjs.Length; index++)
|
2018-09-19 01:36:43 +02:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
syncNodes[index] = syncObjs[index].AddWaitingThread(currentThread);
|
2018-09-19 01:36:43 +02:00
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
currentThread.WaitingSync = true;
|
|
|
|
currentThread.SignaledObj = null;
|
2018-12-18 06:33:36 +01:00
|
|
|
currentThread.ObjSyncResult = result;
|
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
|
|
|
currentThread.WaitingSync = false;
|
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-18 06:33:36 +01:00
|
|
|
result = currentThread.ObjSyncResult;
|
2018-09-19 01:36:43 +02:00
|
|
|
|
2018-12-18 06:33:36 +01:00
|
|
|
handleIndex = -1;
|
2018-09-19 01:36:43 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
for (int index = 0; index < syncObjs.Length; index++)
|
2018-09-19 01:36:43 +02:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
syncObjs[index].RemoveWaitingThread(syncNodes[index]);
|
2018-09-19 01:36:43 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
if (syncObjs[index] == currentThread.SignaledObj)
|
2018-09-19 01:36:43 +02:00
|
|
|
{
|
2018-12-18 06:33:36 +01:00
|
|
|
handleIndex = index;
|
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
|
|
|
public void SignalObject(KSynchronizationObject syncObj)
|
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 (syncObj.IsSignaled())
|
2018-09-19 01:36:43 +02:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
LinkedListNode<KThread> node = syncObj.WaitingThreads.First;
|
2018-09-19 01:36:43 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
while (node != null)
|
2018-09-19 01:36:43 +02:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
KThread thread = node.Value;
|
2018-09-19 01:36:43 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
if ((thread.SchedFlags & ThreadSchedState.LowMask) == ThreadSchedState.Paused)
|
2018-09-19 01:36:43 +02:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
thread.SignaledObj = syncObj;
|
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.Reschedule(ThreadSchedState.Running);
|
2018-09-19 01:36:43 +02:00
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
node = node.Next;
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|