2018-09-18 23:36:43 +00:00
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
|
|
using static Ryujinx.HLE.HOS.ErrorCode;
|
|
|
|
|
|
|
|
namespace Ryujinx.HLE.HOS.Kernel
|
|
|
|
{
|
|
|
|
class KSynchronization
|
|
|
|
{
|
|
|
|
private Horizon System;
|
|
|
|
|
|
|
|
public KSynchronization(Horizon System)
|
|
|
|
{
|
|
|
|
this.System = System;
|
|
|
|
}
|
|
|
|
|
|
|
|
public long WaitFor(KSynchronizationObject[] SyncObjs, long Timeout, ref int HndIndex)
|
|
|
|
{
|
|
|
|
long Result = MakeError(ErrorModule.Kernel, KernelErr.Timeout);
|
|
|
|
|
2018-11-28 22:18:09 +00:00
|
|
|
System.CriticalSection.Enter();
|
2018-09-18 23:36:43 +00:00
|
|
|
|
|
|
|
//Check if objects are already signaled before waiting.
|
|
|
|
for (int Index = 0; Index < SyncObjs.Length; Index++)
|
|
|
|
{
|
|
|
|
if (!SyncObjs[Index].IsSignaled())
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
HndIndex = Index;
|
|
|
|
|
2018-11-28 22:18:09 +00:00
|
|
|
System.CriticalSection.Leave();
|
2018-09-18 23:36:43 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Timeout == 0)
|
|
|
|
{
|
2018-11-28 22:18:09 +00:00
|
|
|
System.CriticalSection.Leave();
|
2018-09-18 23:36:43 +00:00
|
|
|
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
|
|
|
KThread CurrentThread = System.Scheduler.GetCurrentThread();
|
|
|
|
|
|
|
|
if (CurrentThread.ShallBeTerminated ||
|
|
|
|
CurrentThread.SchedFlags == ThreadSchedState.TerminationPending)
|
|
|
|
{
|
|
|
|
Result = MakeError(ErrorModule.Kernel, KernelErr.ThreadTerminating);
|
|
|
|
}
|
|
|
|
else if (CurrentThread.SyncCancelled)
|
|
|
|
{
|
|
|
|
CurrentThread.SyncCancelled = false;
|
|
|
|
|
|
|
|
Result = MakeError(ErrorModule.Kernel, KernelErr.Cancelled);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
LinkedListNode<KThread>[] SyncNodes = new LinkedListNode<KThread>[SyncObjs.Length];
|
|
|
|
|
|
|
|
for (int Index = 0; Index < SyncObjs.Length; Index++)
|
|
|
|
{
|
|
|
|
SyncNodes[Index] = SyncObjs[Index].AddWaitingThread(CurrentThread);
|
|
|
|
}
|
|
|
|
|
|
|
|
CurrentThread.WaitingSync = true;
|
|
|
|
CurrentThread.SignaledObj = null;
|
|
|
|
CurrentThread.ObjSyncResult = (int)Result;
|
|
|
|
|
|
|
|
CurrentThread.Reschedule(ThreadSchedState.Paused);
|
|
|
|
|
|
|
|
if (Timeout > 0)
|
|
|
|
{
|
|
|
|
System.TimeManager.ScheduleFutureInvocation(CurrentThread, Timeout);
|
|
|
|
}
|
|
|
|
|
2018-11-28 22:18:09 +00:00
|
|
|
System.CriticalSection.Leave();
|
2018-09-18 23:36:43 +00:00
|
|
|
|
|
|
|
CurrentThread.WaitingSync = false;
|
|
|
|
|
|
|
|
if (Timeout > 0)
|
|
|
|
{
|
|
|
|
System.TimeManager.UnscheduleFutureInvocation(CurrentThread);
|
|
|
|
}
|
|
|
|
|
2018-11-28 22:18:09 +00:00
|
|
|
System.CriticalSection.Enter();
|
2018-09-18 23:36:43 +00:00
|
|
|
|
|
|
|
Result = (uint)CurrentThread.ObjSyncResult;
|
|
|
|
|
|
|
|
HndIndex = -1;
|
|
|
|
|
|
|
|
for (int Index = 0; Index < SyncObjs.Length; Index++)
|
|
|
|
{
|
|
|
|
SyncObjs[Index].RemoveWaitingThread(SyncNodes[Index]);
|
|
|
|
|
|
|
|
if (SyncObjs[Index] == CurrentThread.SignaledObj)
|
|
|
|
{
|
|
|
|
HndIndex = Index;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-28 22:18:09 +00:00
|
|
|
System.CriticalSection.Leave();
|
2018-09-18 23:36:43 +00:00
|
|
|
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void SignalObject(KSynchronizationObject SyncObj)
|
|
|
|
{
|
2018-11-28 22:18:09 +00:00
|
|
|
System.CriticalSection.Enter();
|
2018-09-18 23:36:43 +00:00
|
|
|
|
|
|
|
if (SyncObj.IsSignaled())
|
|
|
|
{
|
|
|
|
LinkedListNode<KThread> Node = SyncObj.WaitingThreads.First;
|
|
|
|
|
|
|
|
while (Node != null)
|
|
|
|
{
|
|
|
|
KThread Thread = Node.Value;
|
|
|
|
|
2018-11-28 22:18:09 +00:00
|
|
|
if ((Thread.SchedFlags & ThreadSchedState.LowMask) == ThreadSchedState.Paused)
|
2018-09-18 23:36:43 +00:00
|
|
|
{
|
|
|
|
Thread.SignaledObj = SyncObj;
|
|
|
|
Thread.ObjSyncResult = 0;
|
|
|
|
|
|
|
|
Thread.Reschedule(ThreadSchedState.Running);
|
|
|
|
}
|
|
|
|
|
|
|
|
Node = Node.Next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-28 22:18:09 +00:00
|
|
|
System.CriticalSection.Leave();
|
2018-09-18 23:36:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|