666e05f5cb
* use ArrayPool, avoid 6000-7000 allocs/sec of runtime * use ArrayPool, avoid ~7k allocs/second during game execution * use ArrayPool, avoid ~3000 allocs/sec during game execution * use MemoryPool, reduce 0.5 MB/sec of new allocations during game execution * avoid over-allocation by setting List<> Capacity when known * remove LINQ in KTimeManager.UnscheduleFutureInvocation * KTimeManager - avoid spinning one more time when the time has arrived * KTimeManager - let SpinWait decide when to Thread.Yield(), and don't SpinOnce() immediately after Thread.Yield() * use MemoryPool, reduce ~175k bytes/sec allocation during game execution * IpcService - call commands via dynamic methods instead of reflection .Invoke(). Faster to call and with fewer allocations because parameters can be passed directly instead of as an array * Make ButtonMappingEntry a record struct to avoid allocations. Set the List<ButtonMappingEntry> capacity according to use. * add MemoryBuffer type for working with MemoryPool<byte> * update changes to use MemoryBuffer * make parameter ReadOnlySpan instead of Span * whitespace fix * Revert "IpcService - call commands via dynamic methods instead of reflection .Invoke(). Faster to call and with fewer allocations because parameters can be passed directly instead of as an array" This reverts commit f2c698bdf65f049e8481c9f2ec7138d9b9a8261d. * tweak KTimeManager spin behavior * replace MemoryBuffer with ByteMemoryPool modeled after System.Buffers.ArrayMemoryPool<T> * make ByteMemoryPoolBuffer responsible for renting memory
143 lines
No EOL
4.1 KiB
C#
143 lines
No EOL
4.1 KiB
C#
using Ryujinx.HLE.HOS.Kernel.Common;
|
|
using Ryujinx.Horizon.Common;
|
|
using System;
|
|
using System.Buffers;
|
|
using System.Collections.Generic;
|
|
|
|
namespace Ryujinx.HLE.HOS.Kernel.Threading
|
|
{
|
|
class KSynchronization
|
|
{
|
|
private KernelContext _context;
|
|
|
|
public KSynchronization(KernelContext context)
|
|
{
|
|
_context = context;
|
|
}
|
|
|
|
public Result WaitFor(Span<KSynchronizationObject> syncObjs, long timeout, out int handleIndex)
|
|
{
|
|
handleIndex = 0;
|
|
|
|
Result result = KernelResult.TimedOut;
|
|
|
|
_context.CriticalSection.Enter();
|
|
|
|
// Check if objects are already signaled before waiting.
|
|
for (int index = 0; index < syncObjs.Length; index++)
|
|
{
|
|
if (!syncObjs[index].IsSignaled())
|
|
{
|
|
continue;
|
|
}
|
|
|
|
handleIndex = index;
|
|
|
|
_context.CriticalSection.Leave();
|
|
|
|
return Result.Success;
|
|
}
|
|
|
|
if (timeout == 0)
|
|
{
|
|
_context.CriticalSection.Leave();
|
|
|
|
return result;
|
|
}
|
|
|
|
KThread currentThread = KernelStatic.GetCurrentThread();
|
|
|
|
if (currentThread.ShallBeTerminated ||
|
|
currentThread.SchedFlags == ThreadSchedState.TerminationPending)
|
|
{
|
|
result = KernelResult.ThreadTerminating;
|
|
}
|
|
else if (currentThread.SyncCancelled)
|
|
{
|
|
currentThread.SyncCancelled = false;
|
|
|
|
result = KernelResult.Cancelled;
|
|
}
|
|
else
|
|
{
|
|
LinkedListNode<KThread>[] syncNodesArray = ArrayPool<LinkedListNode<KThread>>.Shared.Rent(syncObjs.Length);
|
|
|
|
Span<LinkedListNode<KThread>> syncNodes = syncNodesArray.AsSpan(0, syncObjs.Length);
|
|
|
|
for (int index = 0; index < syncObjs.Length; index++)
|
|
{
|
|
syncNodes[index] = syncObjs[index].AddWaitingThread(currentThread);
|
|
}
|
|
|
|
currentThread.WaitingSync = true;
|
|
currentThread.SignaledObj = null;
|
|
currentThread.ObjSyncResult = result;
|
|
|
|
currentThread.Reschedule(ThreadSchedState.Paused);
|
|
|
|
if (timeout > 0)
|
|
{
|
|
_context.TimeManager.ScheduleFutureInvocation(currentThread, timeout);
|
|
}
|
|
|
|
_context.CriticalSection.Leave();
|
|
|
|
currentThread.WaitingSync = false;
|
|
|
|
if (timeout > 0)
|
|
{
|
|
_context.TimeManager.UnscheduleFutureInvocation(currentThread);
|
|
}
|
|
|
|
_context.CriticalSection.Enter();
|
|
|
|
result = currentThread.ObjSyncResult;
|
|
|
|
handleIndex = -1;
|
|
|
|
for (int index = 0; index < syncObjs.Length; index++)
|
|
{
|
|
syncObjs[index].RemoveWaitingThread(syncNodes[index]);
|
|
|
|
if (syncObjs[index] == currentThread.SignaledObj)
|
|
{
|
|
handleIndex = index;
|
|
}
|
|
}
|
|
|
|
ArrayPool<LinkedListNode<KThread>>.Shared.Return(syncNodesArray);
|
|
}
|
|
|
|
_context.CriticalSection.Leave();
|
|
|
|
return result;
|
|
}
|
|
|
|
public void SignalObject(KSynchronizationObject syncObj)
|
|
{
|
|
_context.CriticalSection.Enter();
|
|
|
|
if (syncObj.IsSignaled())
|
|
{
|
|
LinkedListNode<KThread> node = syncObj.WaitingThreads.First;
|
|
|
|
while (node != null)
|
|
{
|
|
KThread thread = node.Value;
|
|
|
|
if ((thread.SchedFlags & ThreadSchedState.LowMask) == ThreadSchedState.Paused)
|
|
{
|
|
thread.SignaledObj = syncObj;
|
|
thread.ObjSyncResult = Result.Success;
|
|
|
|
thread.Reschedule(ThreadSchedState.Running);
|
|
}
|
|
|
|
node = node.Next;
|
|
}
|
|
}
|
|
|
|
_context.CriticalSection.Leave();
|
|
}
|
|
}
|
|
} |