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