Ryujinx/Ryujinx.HLE/HOS/Kernel/KSynchronization.cs
gdkchan 00579927e4
Better process implementation (#491)
* Initial implementation of KProcess

* Some improvements to the memory manager, implement back guest stack trace printing

* Better GetInfo implementation, improve checking in some places with information from process capabilities

* Allow the cpu to read/write from the correct memory locations for accesses crossing a page boundary

* Change long -> ulong for address/size on memory related methods to avoid unnecessary casts

* Attempt at implementing ldr:ro with new KProcess

* Allow BSS with size 0 on ldr:ro

* Add checking for memory block slab heap usage, return errors if full, exit gracefully

* Use KMemoryBlockSize const from KMemoryManager

* Allow all methods to read from non-contiguous locations

* Fix for TransactParcelAuto

* Address PR feedback, additionally fix some small issues related to the KIP loader and implement SVCs GetProcessId, GetProcessList, GetSystemInfo, CreatePort and ManageNamedPort

* Fix wrong check for source pages count from page list on MapPhysicalMemory

* Fix some issues with UnloadNro on ldr:ro
2018-11-28 20:18:09 -02:00

135 lines
No EOL
3.8 KiB
C#

using System.Collections.Generic;
using static Ryujinx.HLE.HOS.ErrorCode;
namespace Ryujinx.HLE.HOS.Kernel
{
class KSynchronization
{
private Horizon System;
public KSynchronization(Horizon System)
{
this.System = System;
}
public long WaitFor(KSynchronizationObject[] SyncObjs, long Timeout, ref int HndIndex)
{
long Result = MakeError(ErrorModule.Kernel, KernelErr.Timeout);
System.CriticalSection.Enter();
//Check if objects are already signaled before waiting.
for (int Index = 0; Index < SyncObjs.Length; Index++)
{
if (!SyncObjs[Index].IsSignaled())
{
continue;
}
HndIndex = Index;
System.CriticalSection.Leave();
return 0;
}
if (Timeout == 0)
{
System.CriticalSection.Leave();
return Result;
}
KThread CurrentThread = System.Scheduler.GetCurrentThread();
if (CurrentThread.ShallBeTerminated ||
CurrentThread.SchedFlags == ThreadSchedState.TerminationPending)
{
Result = MakeError(ErrorModule.Kernel, KernelErr.ThreadTerminating);
}
else if (CurrentThread.SyncCancelled)
{
CurrentThread.SyncCancelled = false;
Result = MakeError(ErrorModule.Kernel, KernelErr.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 = (int)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 = (uint)CurrentThread.ObjSyncResult;
HndIndex = -1;
for (int Index = 0; Index < SyncObjs.Length; Index++)
{
SyncObjs[Index].RemoveWaitingThread(SyncNodes[Index]);
if (SyncObjs[Index] == CurrentThread.SignaledObj)
{
HndIndex = 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 = 0;
Thread.Reschedule(ThreadSchedState.Running);
}
Node = Node.Next;
}
}
System.CriticalSection.Leave();
}
}
}