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