0
0
Fork 0
mirror of https://github.com/GreemDev/Ryujinx.git synced 2024-12-26 04:55:47 +00:00
Ryujinx/Ryujinx.Core/OsHle/Kernel/ConditionVariable.cs

137 lines
3.8 KiB
C#
Raw Normal View History

using Ryujinx.Core.OsHle.Handles;
using System.Collections.Generic;
2018-02-19 19:37:13 +00:00
using System.Threading;
2018-02-04 23:08:20 +00:00
namespace Ryujinx.Core.OsHle.Kernel
2018-02-04 23:08:20 +00:00
{
class ConditionVariable
2018-02-04 23:08:20 +00:00
{
private Process Process;
2018-02-04 23:08:20 +00:00
private long CondVarAddress;
2018-02-19 19:37:13 +00:00
private bool OwnsCondVarValue;
private List<(KThread Thread, AutoResetEvent WaitEvent)> WaitingThreads;
2018-02-04 23:08:20 +00:00
public ConditionVariable(Process Process, long CondVarAddress)
2018-02-04 23:08:20 +00:00
{
this.Process = Process;
2018-02-04 23:08:20 +00:00
this.CondVarAddress = CondVarAddress;
WaitingThreads = new List<(KThread, AutoResetEvent)>();
2018-02-04 23:08:20 +00:00
}
public bool WaitForSignal(KThread Thread, ulong Timeout)
2018-02-04 23:08:20 +00:00
{
bool Result = true;
2018-02-19 19:37:13 +00:00
int Count = Process.Memory.ReadInt32(CondVarAddress);
2018-02-04 23:08:20 +00:00
if (Count <= 0)
{
using (AutoResetEvent WaitEvent = new AutoResetEvent(false))
2018-02-19 19:37:13 +00:00
{
lock (WaitingThreads)
{
WaitingThreads.Add((Thread, WaitEvent));
}
2018-02-19 19:37:13 +00:00
if (Timeout == ulong.MaxValue)
2018-02-19 19:37:13 +00:00
{
Result = WaitEvent.WaitOne();
}
else
{
Result = WaitEvent.WaitOne(NsTimeConverter.GetTimeMs(Timeout));
lock (WaitingThreads)
{
WaitingThreads.Remove((Thread, WaitEvent));
}
2018-02-19 19:37:13 +00:00
}
}
2018-02-04 23:08:20 +00:00
}
2018-02-19 19:37:13 +00:00
AcquireCondVarValue();
2018-02-04 23:08:20 +00:00
2018-02-19 19:37:13 +00:00
Count = Process.Memory.ReadInt32(CondVarAddress);
2018-02-04 23:08:20 +00:00
2018-02-19 19:37:13 +00:00
if (Count > 0)
2018-02-04 23:08:20 +00:00
{
2018-02-19 19:37:13 +00:00
Process.Memory.WriteInt32(CondVarAddress, Count - 1);
2018-02-04 23:08:20 +00:00
}
2018-02-19 19:37:13 +00:00
ReleaseCondVarValue();
return Result;
2018-02-04 23:08:20 +00:00
}
public void SetSignal(KThread Thread, int Count)
2018-02-04 23:08:20 +00:00
{
lock (WaitingThreads)
2018-02-04 23:08:20 +00:00
{
if (Count < 0)
2018-02-04 23:08:20 +00:00
{
2018-02-19 19:37:13 +00:00
Process.Memory.WriteInt32(CondVarAddress, WaitingThreads.Count);
foreach ((_, AutoResetEvent WaitEvent) in WaitingThreads)
{
WaitEvent.Set();
}
WaitingThreads.Clear();
2018-02-04 23:08:20 +00:00
}
else
{
Process.Memory.WriteInt32(CondVarAddress, Count);
while (WaitingThreads.Count > 0 && Count-- > 0)
{
int HighestPriority = WaitingThreads[0].Thread.Priority;
int HighestPrioIndex = 0;
for (int Index = 1; Index < WaitingThreads.Count; Index++)
{
if (HighestPriority > WaitingThreads[Index].Thread.Priority)
{
HighestPriority = WaitingThreads[Index].Thread.Priority;
HighestPrioIndex = Index;
}
}
WaitingThreads[HighestPrioIndex].WaitEvent.Set();
WaitingThreads.RemoveAt(HighestPrioIndex);
}
}
2018-02-04 23:08:20 +00:00
}
Process.Scheduler.Yield(Thread);
2018-02-04 23:08:20 +00:00
}
2018-02-19 19:37:13 +00:00
private void AcquireCondVarValue()
{
2018-02-19 19:37:13 +00:00
if (!OwnsCondVarValue)
{
while (!Process.Memory.AcquireAddress(CondVarAddress))
{
Thread.Yield();
}
OwnsCondVarValue = true;
}
}
2018-02-19 19:37:13 +00:00
private void ReleaseCondVarValue()
{
2018-02-19 19:37:13 +00:00
if (OwnsCondVarValue)
{
OwnsCondVarValue = false;
Process.Memory.ReleaseAddress(CondVarAddress);
}
}
2018-02-04 23:08:20 +00:00
}
}