2018-12-18 00:33:36 -05:00
|
|
|
using Ryujinx.HLE.HOS.Kernel.Common;
|
2023-01-04 17:15:45 -05:00
|
|
|
using Ryujinx.Horizon.Common;
|
2020-07-17 00:22:13 -04:00
|
|
|
using System;
|
2018-09-18 19:36:43 -04:00
|
|
|
using System.Collections.Generic;
|
|
|
|
|
2018-12-18 00:33:36 -05:00
|
|
|
namespace Ryujinx.HLE.HOS.Kernel.Threading
|
2018-09-18 19:36:43 -04:00
|
|
|
{
|
|
|
|
class KSynchronization
|
|
|
|
{
|
2020-05-03 23:41:29 -04:00
|
|
|
private KernelContext _context;
|
2018-09-18 19:36:43 -04:00
|
|
|
|
2020-05-03 23:41:29 -04:00
|
|
|
public KSynchronization(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
|
|
|
}
|
|
|
|
|
2023-01-04 17:15:45 -05:00
|
|
|
public Result WaitFor(Span<KSynchronizationObject> syncObjs, long timeout, out int handleIndex)
|
2018-09-18 19:36:43 -04:00
|
|
|
{
|
2018-12-18 00:33:36 -05:00
|
|
|
handleIndex = 0;
|
|
|
|
|
2023-01-04 17:15:45 -05:00
|
|
|
Result result = KernelResult.TimedOut;
|
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
|
|
|
|
2019-07-01 22:39:22 -04:00
|
|
|
// Check if objects are already signaled before waiting.
|
2018-12-06 06:16:24 -05:00
|
|
|
for (int index = 0; index < syncObjs.Length; index++)
|
2018-09-18 19:36:43 -04:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
if (!syncObjs[index].IsSignaled())
|
2018-09-18 19:36:43 -04:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-12-18 00:33:36 -05:00
|
|
|
handleIndex = index;
|
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
|
|
|
|
2023-01-04 17:15:45 -05:00
|
|
|
return Result.Success;
|
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-06 06:16:24 -05:00
|
|
|
return result;
|
2018-09-18 19:36:43 -04:00
|
|
|
}
|
|
|
|
|
2020-12-09 17:20:05 -05:00
|
|
|
KThread currentThread = KernelStatic.GetCurrentThread();
|
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
|
|
|
{
|
2018-12-18 00:33:36 -05:00
|
|
|
result = KernelResult.ThreadTerminating;
|
2018-09-18 19:36:43 -04:00
|
|
|
}
|
2018-12-06 06:16:24 -05:00
|
|
|
else if (currentThread.SyncCancelled)
|
2018-09-18 19:36:43 -04:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
currentThread.SyncCancelled = false;
|
2018-09-18 19:36:43 -04:00
|
|
|
|
2018-12-18 00:33:36 -05:00
|
|
|
result = KernelResult.Cancelled;
|
2018-09-18 19:36:43 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-03-17 08:14:50 -04:00
|
|
|
LinkedListNode<KThread>[] syncNodes = syncObjs.Length == 0 ? Array.Empty<LinkedListNode<KThread>>() : new LinkedListNode<KThread>[syncObjs.Length];
|
2018-09-18 19:36:43 -04:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
for (int index = 0; index < syncObjs.Length; index++)
|
2018-09-18 19:36:43 -04:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
syncNodes[index] = syncObjs[index].AddWaitingThread(currentThread);
|
2018-09-18 19:36:43 -04:00
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
currentThread.WaitingSync = true;
|
|
|
|
currentThread.SignaledObj = null;
|
2018-12-18 00:33:36 -05:00
|
|
|
currentThread.ObjSyncResult = result;
|
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
|
|
|
currentThread.WaitingSync = false;
|
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-18 00:33:36 -05:00
|
|
|
result = currentThread.ObjSyncResult;
|
2018-09-18 19:36:43 -04:00
|
|
|
|
2018-12-18 00:33:36 -05:00
|
|
|
handleIndex = -1;
|
2018-09-18 19:36:43 -04:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
for (int index = 0; index < syncObjs.Length; index++)
|
2018-09-18 19:36:43 -04:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
syncObjs[index].RemoveWaitingThread(syncNodes[index]);
|
2018-09-18 19:36:43 -04:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
if (syncObjs[index] == currentThread.SignaledObj)
|
2018-09-18 19:36:43 -04:00
|
|
|
{
|
2018-12-18 00:33:36 -05:00
|
|
|
handleIndex = index;
|
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
|
|
|
public void SignalObject(KSynchronizationObject syncObj)
|
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 (syncObj.IsSignaled())
|
2018-09-18 19:36:43 -04:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
LinkedListNode<KThread> node = syncObj.WaitingThreads.First;
|
2018-09-18 19:36:43 -04:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
while (node != null)
|
2018-09-18 19:36:43 -04:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
KThread thread = node.Value;
|
2018-09-18 19:36:43 -04:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
if ((thread.SchedFlags & ThreadSchedState.LowMask) == ThreadSchedState.Paused)
|
2018-09-18 19:36:43 -04:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
thread.SignaledObj = syncObj;
|
2023-01-04 17:15:45 -05:00
|
|
|
thread.ObjSyncResult = Result.Success;
|
2018-09-18 19:36:43 -04:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
thread.Reschedule(ThreadSchedState.Running);
|
2018-09-18 19:36:43 -04:00
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
node = node.Next;
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|