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