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