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 05:22:13 +01:00
|
|
|
using System;
|
2023-04-24 03:06:23 +01:00
|
|
|
using System.Buffers;
|
2018-09-19 00:36:43 +01:00
|
|
|
using System.Collections.Generic;
|
|
|
|
|
2018-12-18 05:33:36 +00:00
|
|
|
namespace Ryujinx.HLE.HOS.Kernel.Threading
|
2018-09-19 00:36:43 +01:00
|
|
|
{
|
|
|
|
class KSynchronization
|
|
|
|
{
|
2020-05-04 04:41:29 +01:00
|
|
|
private KernelContext _context;
|
2018-09-19 00:36:43 +01:00
|
|
|
|
2020-05-04 04:41:29 +01:00
|
|
|
public KSynchronization(KernelContext context)
|
2018-09-19 00:36:43 +01:00
|
|
|
{
|
2020-05-04 04:41:29 +01:00
|
|
|
_context = context;
|
2018-09-19 00:36:43 +01:00
|
|
|
}
|
|
|
|
|
2023-01-04 22:15:45 +00:00
|
|
|
public Result WaitFor(Span<KSynchronizationObject> syncObjs, long timeout, out int handleIndex)
|
2018-09-19 00:36:43 +01: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-19 00:36:43 +01:00
|
|
|
|
2020-05-04 04:41:29 +01:00
|
|
|
_context.CriticalSection.Enter();
|
2018-09-19 00:36:43 +01:00
|
|
|
|
2019-07-02 03:39:22 +01: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-19 00:36:43 +01:00
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
if (!syncObjs[index].IsSignaled())
|
2018-09-19 00:36:43 +01:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-12-18 05:33:36 +00:00
|
|
|
handleIndex = index;
|
2018-09-19 00:36:43 +01:00
|
|
|
|
2020-05-04 04:41:29 +01:00
|
|
|
_context.CriticalSection.Leave();
|
2018-09-19 00:36:43 +01:00
|
|
|
|
2023-01-04 22:15:45 +00:00
|
|
|
return Result.Success;
|
2018-09-19 00:36:43 +01:00
|
|
|
}
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
if (timeout == 0)
|
2018-09-19 00:36:43 +01:00
|
|
|
{
|
2020-05-04 04:41:29 +01:00
|
|
|
_context.CriticalSection.Leave();
|
2018-09-19 00:36:43 +01:00
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
return result;
|
2018-09-19 00:36:43 +01:00
|
|
|
}
|
|
|
|
|
2020-12-09 22:20:05 +00:00
|
|
|
KThread currentThread = KernelStatic.GetCurrentThread();
|
2018-09-19 00:36:43 +01:00
|
|
|
|
2023-04-25 23:33:14 +01:00
|
|
|
if (currentThread.TerminationRequested)
|
2018-09-19 00:36:43 +01:00
|
|
|
{
|
2018-12-18 05:33:36 +00:00
|
|
|
result = KernelResult.ThreadTerminating;
|
2018-09-19 00:36:43 +01:00
|
|
|
}
|
2018-12-06 11:16:24 +00:00
|
|
|
else if (currentThread.SyncCancelled)
|
2018-09-19 00:36:43 +01:00
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
currentThread.SyncCancelled = false;
|
2018-09-19 00:36:43 +01:00
|
|
|
|
2018-12-18 05:33:36 +00:00
|
|
|
result = KernelResult.Cancelled;
|
2018-09-19 00:36:43 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-04-24 03:06:23 +01:00
|
|
|
LinkedListNode<KThread>[] syncNodesArray = ArrayPool<LinkedListNode<KThread>>.Shared.Rent(syncObjs.Length);
|
2023-04-25 23:33:14 +01:00
|
|
|
|
2023-04-24 03:06:23 +01:00
|
|
|
Span<LinkedListNode<KThread>> syncNodes = syncNodesArray.AsSpan(0, syncObjs.Length);
|
2018-09-19 00:36:43 +01:00
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
for (int index = 0; index < syncObjs.Length; index++)
|
2018-09-19 00:36:43 +01:00
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
syncNodes[index] = syncObjs[index].AddWaitingThread(currentThread);
|
2018-09-19 00:36:43 +01: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-19 00:36:43 +01:00
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
currentThread.Reschedule(ThreadSchedState.Paused);
|
2018-09-19 00:36:43 +01:00
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
if (timeout > 0)
|
2018-09-19 00:36:43 +01:00
|
|
|
{
|
2020-05-04 04:41:29 +01:00
|
|
|
_context.TimeManager.ScheduleFutureInvocation(currentThread, timeout);
|
2018-09-19 00:36:43 +01:00
|
|
|
}
|
|
|
|
|
2020-05-04 04:41:29 +01:00
|
|
|
_context.CriticalSection.Leave();
|
2018-09-19 00:36:43 +01:00
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
currentThread.WaitingSync = false;
|
2018-09-19 00:36:43 +01:00
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
if (timeout > 0)
|
2018-09-19 00:36:43 +01:00
|
|
|
{
|
2020-05-04 04:41:29 +01:00
|
|
|
_context.TimeManager.UnscheduleFutureInvocation(currentThread);
|
2018-09-19 00:36:43 +01:00
|
|
|
}
|
|
|
|
|
2020-05-04 04:41:29 +01:00
|
|
|
_context.CriticalSection.Enter();
|
2018-09-19 00:36:43 +01:00
|
|
|
|
2018-12-18 05:33:36 +00:00
|
|
|
result = currentThread.ObjSyncResult;
|
2018-09-19 00:36:43 +01:00
|
|
|
|
2018-12-18 05:33:36 +00:00
|
|
|
handleIndex = -1;
|
2018-09-19 00:36:43 +01:00
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
for (int index = 0; index < syncObjs.Length; index++)
|
2018-09-19 00:36:43 +01:00
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
syncObjs[index].RemoveWaitingThread(syncNodes[index]);
|
2018-09-19 00:36:43 +01:00
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
if (syncObjs[index] == currentThread.SignaledObj)
|
2018-09-19 00:36:43 +01:00
|
|
|
{
|
2018-12-18 05:33:36 +00:00
|
|
|
handleIndex = index;
|
2018-09-19 00:36:43 +01:00
|
|
|
}
|
|
|
|
}
|
2023-04-24 03:06:23 +01:00
|
|
|
|
|
|
|
ArrayPool<LinkedListNode<KThread>>.Shared.Return(syncNodesArray);
|
2018-09-19 00:36:43 +01:00
|
|
|
}
|
|
|
|
|
2020-05-04 04:41:29 +01:00
|
|
|
_context.CriticalSection.Leave();
|
2018-09-19 00:36:43 +01:00
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
return result;
|
2018-09-19 00:36:43 +01:00
|
|
|
}
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
public void SignalObject(KSynchronizationObject syncObj)
|
2018-09-19 00:36:43 +01:00
|
|
|
{
|
2020-05-04 04:41:29 +01:00
|
|
|
_context.CriticalSection.Enter();
|
2018-09-19 00:36:43 +01:00
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
if (syncObj.IsSignaled())
|
2018-09-19 00:36:43 +01:00
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
LinkedListNode<KThread> node = syncObj.WaitingThreads.First;
|
2018-09-19 00:36:43 +01:00
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
while (node != null)
|
2018-09-19 00:36:43 +01:00
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
KThread thread = node.Value;
|
2018-09-19 00:36:43 +01:00
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
if ((thread.SchedFlags & ThreadSchedState.LowMask) == ThreadSchedState.Paused)
|
2018-09-19 00:36:43 +01: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-19 00:36:43 +01:00
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
thread.Reschedule(ThreadSchedState.Running);
|
2018-09-19 00:36:43 +01:00
|
|
|
}
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
node = node.Next;
|
2018-09-19 00:36:43 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-04 04:41:29 +01:00
|
|
|
_context.CriticalSection.Leave();
|
2018-09-19 00:36:43 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|