mirror of
https://github.com/Ryujinx/Ryujinx.git
synced 2024-11-30 16:30:15 +00:00
15d1cc806b
* Move kernel state from Horizon to KernelContext * Merge syscalls partial classes, split 32 and 64-bit variants * Sort usings
92 lines
No EOL
2.7 KiB
C#
92 lines
No EOL
2.7 KiB
C#
using System.Threading;
|
|
|
|
namespace Ryujinx.HLE.HOS.Kernel.Threading
|
|
{
|
|
class KCriticalSection
|
|
{
|
|
private readonly KernelContext _context;
|
|
|
|
public object LockObj { get; private set; }
|
|
|
|
private int _recursionCount;
|
|
|
|
public KCriticalSection(KernelContext context)
|
|
{
|
|
_context = context;
|
|
|
|
LockObj = new object();
|
|
}
|
|
|
|
public void Enter()
|
|
{
|
|
Monitor.Enter(LockObj);
|
|
|
|
_recursionCount++;
|
|
}
|
|
|
|
public void Leave()
|
|
{
|
|
if (_recursionCount == 0)
|
|
{
|
|
return;
|
|
}
|
|
|
|
bool doContextSwitch = false;
|
|
|
|
if (--_recursionCount == 0)
|
|
{
|
|
if (_context.Scheduler.ThreadReselectionRequested)
|
|
{
|
|
_context.Scheduler.SelectThreads();
|
|
}
|
|
|
|
Monitor.Exit(LockObj);
|
|
|
|
if (_context.Scheduler.MultiCoreScheduling)
|
|
{
|
|
lock (_context.Scheduler.CoreContexts)
|
|
{
|
|
for (int core = 0; core < KScheduler.CpuCoresCount; core++)
|
|
{
|
|
KCoreContext coreContext = _context.Scheduler.CoreContexts[core];
|
|
|
|
if (coreContext.ContextSwitchNeeded)
|
|
{
|
|
KThread currentThread = coreContext.CurrentThread;
|
|
|
|
if (currentThread == null)
|
|
{
|
|
// Nothing is running, we can perform the context switch immediately.
|
|
coreContext.ContextSwitch();
|
|
}
|
|
else if (currentThread.IsCurrentHostThread())
|
|
{
|
|
// Thread running on the current core, context switch will block.
|
|
doContextSwitch = true;
|
|
}
|
|
else
|
|
{
|
|
// Thread running on another core, request a interrupt.
|
|
currentThread.Context.RequestInterrupt();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
doContextSwitch = true;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
Monitor.Exit(LockObj);
|
|
}
|
|
|
|
if (doContextSwitch)
|
|
{
|
|
_context.Scheduler.ContextSwitch();
|
|
}
|
|
}
|
|
}
|
|
} |