Ryujinx-uplift/Ryujinx.HLE/HOS/Kernel/KCriticalSection.cs
Alex Barney 85dbb9559a Adjust naming conventions and general refactoring in HLE Project (#490)
* Rename enum fields

* Naming conventions

* Remove unneeded ".this"

* Remove unneeded semicolons

* Remove unused Usings

* Don't use var

* Remove unneeded enum underlying types

* Explicitly label class visibility

* Remove unneeded @ prefixes

* Remove unneeded commas

* Remove unneeded if expressions

* Method doesn't use unsafe code

* Remove unneeded casts

* Initialized objects don't need an empty constructor

* Remove settings from DotSettings

* Revert "Explicitly label class visibility"

This reverts commit ad5eb5787cc5b27a4631cd46ef5f551c4ae95e51.

* Small changes

* Revert external enum renaming

* Changes from feedback

* Remove unneeded property setters
2018-12-04 18:23:37 -02:00

93 lines
2.7 KiB
C#

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