2018-02-04 23:08:20 +00:00
|
|
|
using ChocolArm64.Memory;
|
2018-02-14 02:43:08 +00:00
|
|
|
using Ryujinx.OsHle.Handles;
|
|
|
|
using System.Collections.Generic;
|
2018-02-04 23:08:20 +00:00
|
|
|
|
|
|
|
namespace Ryujinx.OsHle
|
|
|
|
{
|
|
|
|
class CondVar
|
|
|
|
{
|
2018-02-14 02:43:08 +00:00
|
|
|
private Process Process;
|
2018-02-04 23:08:20 +00:00
|
|
|
|
|
|
|
private long CondVarAddress;
|
|
|
|
private long Timeout;
|
|
|
|
|
2018-02-14 02:43:08 +00:00
|
|
|
private List<HThread> WaitingThreads;
|
2018-02-04 23:08:20 +00:00
|
|
|
|
2018-02-14 02:43:08 +00:00
|
|
|
public CondVar(Process Process, long CondVarAddress, long Timeout)
|
2018-02-04 23:08:20 +00:00
|
|
|
{
|
2018-02-14 02:43:08 +00:00
|
|
|
this.Process = Process;
|
2018-02-04 23:08:20 +00:00
|
|
|
this.CondVarAddress = CondVarAddress;
|
|
|
|
this.Timeout = Timeout;
|
|
|
|
|
2018-02-14 02:43:08 +00:00
|
|
|
WaitingThreads = new List<HThread>();
|
2018-02-04 23:08:20 +00:00
|
|
|
}
|
|
|
|
|
2018-02-14 02:43:08 +00:00
|
|
|
public void WaitForSignal(HThread Thread)
|
2018-02-04 23:08:20 +00:00
|
|
|
{
|
2018-02-14 02:43:08 +00:00
|
|
|
int Count = ReadCondVarValue();
|
2018-02-04 23:08:20 +00:00
|
|
|
|
|
|
|
if (Count <= 0)
|
|
|
|
{
|
2018-02-14 02:43:08 +00:00
|
|
|
//FIXME: We shouldn't need to do that?
|
|
|
|
Process.Scheduler.Yield(Thread);
|
|
|
|
|
2018-02-04 23:08:20 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-02-14 02:43:08 +00:00
|
|
|
WriteCondVarValue(Count - 1);
|
2018-02-04 23:08:20 +00:00
|
|
|
|
2018-02-14 02:43:08 +00:00
|
|
|
lock (WaitingThreads)
|
|
|
|
{
|
|
|
|
WaitingThreads.Add(Thread);
|
|
|
|
}
|
2018-02-04 23:08:20 +00:00
|
|
|
|
|
|
|
if (Timeout != -1)
|
|
|
|
{
|
2018-02-14 02:43:08 +00:00
|
|
|
Process.Scheduler.WaitForSignal(Thread, (int)(Timeout / 1000000));
|
2018-02-04 23:08:20 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-02-14 02:43:08 +00:00
|
|
|
Process.Scheduler.WaitForSignal(Thread);
|
2018-02-04 23:08:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void SetSignal(int Count)
|
|
|
|
{
|
2018-02-14 02:43:08 +00:00
|
|
|
lock (WaitingThreads)
|
2018-02-04 23:08:20 +00:00
|
|
|
{
|
2018-02-14 02:43:08 +00:00
|
|
|
if (Count == -1)
|
2018-02-04 23:08:20 +00:00
|
|
|
{
|
2018-02-14 02:43:08 +00:00
|
|
|
Process.Scheduler.Signal(WaitingThreads.ToArray());
|
2018-02-04 23:08:20 +00:00
|
|
|
|
2018-02-14 02:43:08 +00:00
|
|
|
WriteCondVarValue(WaitingThreads.Count);
|
|
|
|
|
|
|
|
WaitingThreads.Clear();
|
2018-02-04 23:08:20 +00:00
|
|
|
}
|
2018-02-14 02:43:08 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
if (WaitingThreads.Count > 0)
|
|
|
|
{
|
|
|
|
int HighestPriority = WaitingThreads[0].Priority;
|
|
|
|
int HighestPrioIndex = 0;
|
|
|
|
|
|
|
|
for (int Index = 1; Index < WaitingThreads.Count; Index++)
|
|
|
|
{
|
|
|
|
if (HighestPriority > WaitingThreads[Index].Priority)
|
|
|
|
{
|
|
|
|
HighestPriority = WaitingThreads[Index].Priority;
|
|
|
|
|
|
|
|
HighestPrioIndex = Index;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Process.Scheduler.Signal(WaitingThreads[HighestPrioIndex]);
|
|
|
|
|
|
|
|
WaitingThreads.RemoveAt(HighestPrioIndex);
|
|
|
|
}
|
2018-02-04 23:08:20 +00:00
|
|
|
|
2018-02-14 02:43:08 +00:00
|
|
|
WriteCondVarValue(Count);
|
|
|
|
}
|
2018-02-04 23:08:20 +00:00
|
|
|
}
|
|
|
|
}
|
2018-02-14 02:43:08 +00:00
|
|
|
|
|
|
|
private int ReadCondVarValue()
|
|
|
|
{
|
|
|
|
return AMemoryHelper.ReadInt32Exclusive(Process.Memory, CondVarAddress);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void WriteCondVarValue(int Value)
|
|
|
|
{
|
|
|
|
AMemoryHelper.WriteInt32Exclusive(Process.Memory, CondVarAddress, Value);
|
|
|
|
}
|
2018-02-04 23:08:20 +00:00
|
|
|
}
|
|
|
|
}
|