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