ryujinx/Ryujinx.HLE/HOS/Kernel/Threading/KSynchronization.cs
gdkchan 22bacc6188
Improve kernel IPC implementation (#550)
* Implement some IPC related kernel SVCs properly

* Fix BLZ decompression when the segment also has a uncompressed chunck

* Set default cpu core on process start from ProgramLoader, remove debug message

* Load process capabilities properly on KIPs

* Fix a copy/paste error in UnmapPhysicalMemory64

* Implement smarter switching between old and new IPC system to support the old HLE services implementation without the manual switch

* Implement RegisterService on sm and AcceptSession (partial)

* Misc fixes and improvements on new IPC methods

* Move IPC related SVCs into a separate file, and logging on RegisterService (sm)

* Some small fixes related to receive list buffers and error cases

* Load NSOs using the correct pool partition

* Fix corner case on GetMaskFromMinMax where range is 64, doesn't happen in pratice however

* Fix send static buffer copy

* Session release, implement closing requests on client disconnect

* Implement ConnectToPort SVC

* KLightSession init
2019-01-18 20:26:39 -02:00

136 lines
3.8 KiB
C#

using Ryujinx.HLE.HOS.Kernel.Common;
using System.Collections.Generic;
namespace Ryujinx.HLE.HOS.Kernel.Threading
{
class KSynchronization
{
private Horizon _system;
public KSynchronization(Horizon system)
{
_system = system;
}
public KernelResult WaitFor(KSynchronizationObject[] syncObjs, long timeout, out int handleIndex)
{
handleIndex = 0;
KernelResult result = KernelResult.TimedOut;
_system.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;
_system.CriticalSection.Leave();
return KernelResult.Success;
}
if (timeout == 0)
{
_system.CriticalSection.Leave();
return result;
}
KThread currentThread = _system.Scheduler.GetCurrentThread();
if (currentThread.ShallBeTerminated ||
currentThread.SchedFlags == ThreadSchedState.TerminationPending)
{
result = KernelResult.ThreadTerminating;
}
else if (currentThread.SyncCancelled)
{
currentThread.SyncCancelled = false;
result = KernelResult.Cancelled;
}
else
{
LinkedListNode<KThread>[] syncNodes = new LinkedListNode<KThread>[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)
{
_system.TimeManager.ScheduleFutureInvocation(currentThread, timeout);
}
_system.CriticalSection.Leave();
currentThread.WaitingSync = false;
if (timeout > 0)
{
_system.TimeManager.UnscheduleFutureInvocation(currentThread);
}
_system.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;
}
}
}
_system.CriticalSection.Leave();
return result;
}
public void SignalObject(KSynchronizationObject syncObj)
{
_system.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 = KernelResult.Success;
thread.Reschedule(ThreadSchedState.Running);
}
node = node.Next;
}
}
_system.CriticalSection.Leave();
}
}
}