1
0
Fork 0
mirror of https://github.com/Ryujinx/Ryujinx.git synced 2024-12-01 00:52:19 +00:00
Ryujinx/Ryujinx.HLE/HOS/Kernel/KCriticalSection.cs

93 lines
2.7 KiB
C#
Raw Normal View History

using ChocolArm64;
using System.Threading;
namespace Ryujinx.HLE.HOS.Kernel
{
class KCriticalSection
{
2018-12-01 20:01:59 +00:00
private Horizon _system;
public object LockObj { get; private set; }
2018-12-01 20:01:59 +00:00
private int _recursionCount;
2018-12-01 20:01:59 +00:00
public KCriticalSection(Horizon system)
{
2018-12-01 20:24:37 +00:00
_system = system;
LockObj = new object();
}
public void Enter()
{
Monitor.Enter(LockObj);
2018-12-01 20:01:59 +00:00
_recursionCount++;
}
public void Leave()
{
2018-12-01 20:01:59 +00:00
if (_recursionCount == 0)
{
return;
}
2018-12-01 20:01:59 +00:00
bool doContextSwitch = false;
2018-12-01 20:01:59 +00:00
if (--_recursionCount == 0)
{
2018-12-01 20:01:59 +00:00
if (_system.Scheduler.ThreadReselectionRequested)
{
2018-12-01 20:01:59 +00:00
_system.Scheduler.SelectThreads();
}
Monitor.Exit(LockObj);
2018-12-01 20:01:59 +00:00
if (_system.Scheduler.MultiCoreScheduling)
{
2018-12-01 20:01:59 +00:00
lock (_system.Scheduler.CoreContexts)
{
2018-12-01 20:01:59 +00:00
for (int core = 0; core < KScheduler.CpuCoresCount; core++)
{
2018-12-01 20:01:59 +00:00
KCoreContext coreContext = _system.Scheduler.CoreContexts[core];
2018-12-01 20:01:59 +00:00
if (coreContext.ContextSwitchNeeded)
{
2018-12-01 20:01:59 +00:00
CpuThread currentHleThread = coreContext.CurrentThread?.Context;
2018-12-01 20:01:59 +00:00
if (currentHleThread == null)
{
//Nothing is running, we can perform the context switch immediately.
2018-12-01 20:01:59 +00:00
coreContext.ContextSwitch();
}
2018-12-01 20:01:59 +00:00
else if (currentHleThread.IsCurrentThread())
{
//Thread running on the current core, context switch will block.
2018-12-01 20:01:59 +00:00
doContextSwitch = true;
}
else
{
//Thread running on another core, request a interrupt.
2018-12-01 20:01:59 +00:00
currentHleThread.RequestInterrupt();
}
}
}
}
}
else
{
2018-12-01 20:01:59 +00:00
doContextSwitch = true;
}
}
else
{
Monitor.Exit(LockObj);
}
2018-12-01 20:01:59 +00:00
if (doContextSwitch)
{
2018-12-01 20:01:59 +00:00
_system.Scheduler.ContextSwitch();
}
}
}
}