2018-09-18 23:36:43 +00:00
|
|
|
using ChocolArm64;
|
|
|
|
using System.Threading;
|
|
|
|
|
|
|
|
namespace Ryujinx.HLE.HOS.Kernel
|
|
|
|
{
|
2018-11-28 22:18:09 +00:00
|
|
|
class KCriticalSection
|
2018-09-18 23:36:43 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +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-05 00:52:39 +00:00
|
|
|
private int RecursionCount;
|
2018-09-18 23:36:43 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
public KCriticalSection(Horizon System)
|
2018-09-18 23:36:43 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
this.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-05 00:52:39 +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-05 00:52:39 +00:00
|
|
|
if (RecursionCount == 0)
|
2018-09-18 23:36:43 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
bool DoContextSwitch = false;
|
2018-09-18 23:36:43 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
if (--RecursionCount == 0)
|
2018-09-18 23:36:43 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
if (System.Scheduler.ThreadReselectionRequested)
|
2018-09-18 23:36:43 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
System.Scheduler.SelectThreads();
|
2018-09-18 23:36:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Monitor.Exit(LockObj);
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
if (System.Scheduler.MultiCoreScheduling)
|
2018-09-18 23:36:43 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
lock (System.Scheduler.CoreContexts)
|
2018-09-18 23:36:43 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
for (int Core = 0; Core < KScheduler.CpuCoresCount; Core++)
|
2018-09-18 23:36:43 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
KCoreContext CoreContext = System.Scheduler.CoreContexts[Core];
|
2018-09-18 23:36:43 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
if (CoreContext.ContextSwitchNeeded)
|
2018-09-18 23:36:43 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
CpuThread CurrentHleThread = CoreContext.CurrentThread?.Context;
|
2018-09-18 23:36:43 +00:00
|
|
|
|
2018-12-05 00:52:39 +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-05 00:52:39 +00:00
|
|
|
CoreContext.ContextSwitch();
|
2018-09-18 23:36:43 +00:00
|
|
|
}
|
2018-12-05 00:52:39 +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-05 00:52:39 +00:00
|
|
|
DoContextSwitch = true;
|
2018-09-18 23:36:43 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
//Thread running on another core, request a interrupt.
|
2018-12-05 00:52:39 +00:00
|
|
|
CurrentHleThread.RequestInterrupt();
|
2018-09-18 23:36:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
DoContextSwitch = true;
|
2018-09-18 23:36:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Monitor.Exit(LockObj);
|
|
|
|
}
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
if (DoContextSwitch)
|
2018-09-18 23:36:43 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
System.Scheduler.ContextSwitch();
|
2018-09-18 23:36:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|