1
0
Fork 0
mirror of https://github.com/Ryujinx/Ryujinx.git synced 2024-12-03 19:42:02 +00:00
Ryujinx/Ryujinx.HLE/HOS/Kernel/KSynchronization.cs

135 lines
3.8 KiB
C#
Raw Normal View History

using System.Collections.Generic;
using static Ryujinx.HLE.HOS.ErrorCode;
namespace Ryujinx.HLE.HOS.Kernel
{
class KSynchronization
{
2018-12-01 20:01:59 +00:00
private Horizon _system;
2018-12-01 20:01:59 +00:00
public KSynchronization(Horizon system)
{
2018-12-01 20:01:59 +00:00
this._system = system;
}
2018-12-01 20:01:59 +00:00
public long WaitFor(KSynchronizationObject[] syncObjs, long timeout, ref int hndIndex)
{
2018-12-01 20:01:59 +00:00
long result = MakeError(ErrorModule.Kernel, KernelErr.Timeout);
2018-12-01 20:01:59 +00:00
_system.CriticalSection.Enter();
//Check if objects are already signaled before waiting.
2018-12-01 20:01:59 +00:00
for (int index = 0; index < syncObjs.Length; index++)
{
2018-12-01 20:01:59 +00:00
if (!syncObjs[index].IsSignaled())
{
continue;
}
2018-12-01 20:01:59 +00:00
hndIndex = index;
2018-12-01 20:01:59 +00:00
_system.CriticalSection.Leave();
return 0;
}
2018-12-01 20:01:59 +00:00
if (timeout == 0)
{
2018-12-01 20:01:59 +00:00
_system.CriticalSection.Leave();
2018-12-01 20:01:59 +00:00
return result;
}
2018-12-01 20:01:59 +00:00
KThread currentThread = _system.Scheduler.GetCurrentThread();
2018-12-01 20:01:59 +00:00
if (currentThread.ShallBeTerminated ||
currentThread.SchedFlags == ThreadSchedState.TerminationPending)
{
2018-12-01 20:01:59 +00:00
result = MakeError(ErrorModule.Kernel, KernelErr.ThreadTerminating);
}
2018-12-01 20:01:59 +00:00
else if (currentThread.SyncCancelled)
{
2018-12-01 20:01:59 +00:00
currentThread.SyncCancelled = false;
2018-12-01 20:01:59 +00:00
result = MakeError(ErrorModule.Kernel, KernelErr.Cancelled);
}
else
{
2018-12-01 20:01:59 +00:00
LinkedListNode<KThread>[] syncNodes = new LinkedListNode<KThread>[syncObjs.Length];
2018-12-01 20:01:59 +00:00
for (int index = 0; index < syncObjs.Length; index++)
{
2018-12-01 20:01:59 +00:00
syncNodes[index] = syncObjs[index].AddWaitingThread(currentThread);
}
2018-12-01 20:01:59 +00:00
currentThread.WaitingSync = true;
currentThread.SignaledObj = null;
currentThread.ObjSyncResult = (int)result;
2018-12-01 20:01:59 +00:00
currentThread.Reschedule(ThreadSchedState.Paused);
2018-12-01 20:01:59 +00:00
if (timeout > 0)
{
2018-12-01 20:01:59 +00:00
_system.TimeManager.ScheduleFutureInvocation(currentThread, timeout);
}
2018-12-01 20:01:59 +00:00
_system.CriticalSection.Leave();
2018-12-01 20:01:59 +00:00
currentThread.WaitingSync = false;
2018-12-01 20:01:59 +00:00
if (timeout > 0)
{
2018-12-01 20:01:59 +00:00
_system.TimeManager.UnscheduleFutureInvocation(currentThread);
}
2018-12-01 20:01:59 +00:00
_system.CriticalSection.Enter();
2018-12-01 20:01:59 +00:00
result = (uint)currentThread.ObjSyncResult;
2018-12-01 20:01:59 +00:00
hndIndex = -1;
2018-12-01 20:01:59 +00:00
for (int index = 0; index < syncObjs.Length; index++)
{
2018-12-01 20:01:59 +00:00
syncObjs[index].RemoveWaitingThread(syncNodes[index]);
2018-12-01 20:01:59 +00:00
if (syncObjs[index] == currentThread.SignaledObj)
{
2018-12-01 20:01:59 +00:00
hndIndex = index;
}
}
}
2018-12-01 20:01:59 +00:00
_system.CriticalSection.Leave();
2018-12-01 20:01:59 +00:00
return result;
}
2018-12-01 20:01:59 +00:00
public void SignalObject(KSynchronizationObject syncObj)
{
2018-12-01 20:01:59 +00:00
_system.CriticalSection.Enter();
2018-12-01 20:01:59 +00:00
if (syncObj.IsSignaled())
{
2018-12-01 20:01:59 +00:00
LinkedListNode<KThread> node = syncObj.WaitingThreads.First;
2018-12-01 20:01:59 +00:00
while (node != null)
{
2018-12-01 20:01:59 +00:00
KThread thread = node.Value;
2018-12-01 20:01:59 +00:00
if ((thread.SchedFlags & ThreadSchedState.LowMask) == ThreadSchedState.Paused)
{
2018-12-01 20:01:59 +00:00
thread.SignaledObj = syncObj;
thread.ObjSyncResult = 0;
2018-12-01 20:01:59 +00:00
thread.Reschedule(ThreadSchedState.Running);
}
2018-12-01 20:01:59 +00:00
node = node.Next;
}
}
2018-12-01 20:01:59 +00:00
_system.CriticalSection.Leave();
}
}
}