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