Archived
1
0
Fork 0
forked from Mirror/Ryujinx
This repository has been archived on 2024-10-11. You can view files and clone it, but cannot push or open issues or pull requests.
jinx/Ryujinx/OsHle/CondVar.cs

103 lines
2.8 KiB
C#
Raw Normal View History

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