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