2018-02-20 21:09:23 +01:00
|
|
|
using Ryujinx.Core.OsHle.Handles;
|
2018-02-14 03:43:08 +01:00
|
|
|
using System.Collections.Generic;
|
2018-02-19 20:37:13 +01:00
|
|
|
using System.Threading;
|
2018-02-05 00:08:20 +01:00
|
|
|
|
2018-02-20 21:09:23 +01:00
|
|
|
namespace Ryujinx.Core.OsHle
|
2018-02-05 00:08:20 +01:00
|
|
|
{
|
2018-03-12 05:04:52 +01:00
|
|
|
class CondVar
|
2018-02-05 00:08:20 +01:00
|
|
|
{
|
2018-02-14 03:43:08 +01:00
|
|
|
private Process Process;
|
2018-02-05 00:08:20 +01:00
|
|
|
|
|
|
|
private long CondVarAddress;
|
|
|
|
private long Timeout;
|
|
|
|
|
2018-02-19 20:37:13 +01:00
|
|
|
private bool OwnsCondVarValue;
|
|
|
|
|
2018-02-14 03:43:08 +01:00
|
|
|
private List<HThread> WaitingThreads;
|
2018-02-05 00:08:20 +01:00
|
|
|
|
2018-02-14 03:43:08 +01:00
|
|
|
public CondVar(Process Process, long CondVarAddress, long Timeout)
|
2018-02-05 00:08:20 +01:00
|
|
|
{
|
2018-02-14 03:43:08 +01:00
|
|
|
this.Process = Process;
|
2018-02-05 00:08:20 +01:00
|
|
|
this.CondVarAddress = CondVarAddress;
|
|
|
|
this.Timeout = Timeout;
|
|
|
|
|
2018-02-14 03:43:08 +01:00
|
|
|
WaitingThreads = new List<HThread>();
|
2018-02-05 00:08:20 +01:00
|
|
|
}
|
|
|
|
|
2018-03-05 00:32:18 +01:00
|
|
|
public bool WaitForSignal(HThread Thread)
|
2018-02-05 00:08:20 +01:00
|
|
|
{
|
2018-02-19 20:37:13 +01:00
|
|
|
int Count = Process.Memory.ReadInt32(CondVarAddress);
|
2018-02-05 00:08:20 +01:00
|
|
|
|
|
|
|
if (Count <= 0)
|
|
|
|
{
|
2018-02-19 20:37:13 +01:00
|
|
|
lock (WaitingThreads)
|
|
|
|
{
|
|
|
|
WaitingThreads.Add(Thread);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Timeout == -1)
|
|
|
|
{
|
|
|
|
Process.Scheduler.WaitForSignal(Thread);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-03-05 00:32:18 +01:00
|
|
|
bool Result = Process.Scheduler.WaitForSignal(Thread, (int)(Timeout / 1000000));
|
2018-02-14 03:43:08 +01:00
|
|
|
|
2018-02-19 20:37:13 +01:00
|
|
|
lock (WaitingThreads)
|
|
|
|
{
|
|
|
|
WaitingThreads.Remove(Thread);
|
|
|
|
}
|
2018-03-05 00:32:18 +01:00
|
|
|
|
|
|
|
return Result;
|
2018-02-19 20:37:13 +01:00
|
|
|
}
|
2018-02-05 00:08:20 +01:00
|
|
|
}
|
|
|
|
|
2018-02-19 20:37:13 +01:00
|
|
|
AcquireCondVarValue();
|
2018-02-05 00:08:20 +01:00
|
|
|
|
2018-02-19 20:37:13 +01:00
|
|
|
Count = Process.Memory.ReadInt32(CondVarAddress);
|
2018-02-05 00:08:20 +01:00
|
|
|
|
2018-02-19 20:37:13 +01:00
|
|
|
if (Count > 0)
|
2018-02-05 00:08:20 +01:00
|
|
|
{
|
2018-02-19 20:37:13 +01:00
|
|
|
Process.Memory.WriteInt32(CondVarAddress, Count - 1);
|
2018-02-05 00:08:20 +01:00
|
|
|
}
|
2018-02-19 20:37:13 +01:00
|
|
|
|
|
|
|
ReleaseCondVarValue();
|
2018-03-05 00:32:18 +01:00
|
|
|
|
|
|
|
return true;
|
2018-02-05 00:08:20 +01:00
|
|
|
}
|
|
|
|
|
2018-02-19 20:37:13 +01:00
|
|
|
public void SetSignal(HThread Thread, int Count)
|
2018-02-05 00:08:20 +01:00
|
|
|
{
|
2018-02-14 03:43:08 +01:00
|
|
|
lock (WaitingThreads)
|
2018-02-05 00:08:20 +01:00
|
|
|
{
|
2018-02-14 03:43:08 +01:00
|
|
|
if (Count == -1)
|
2018-02-05 00:08:20 +01:00
|
|
|
{
|
2018-02-14 03:43:08 +01:00
|
|
|
Process.Scheduler.Signal(WaitingThreads.ToArray());
|
2018-02-05 00:08:20 +01:00
|
|
|
|
2018-02-19 20:37:13 +01:00
|
|
|
AcquireCondVarValue();
|
|
|
|
|
|
|
|
Process.Memory.WriteInt32(CondVarAddress, WaitingThreads.Count);
|
|
|
|
|
|
|
|
ReleaseCondVarValue();
|
2018-02-14 03:43:08 +01:00
|
|
|
|
|
|
|
WaitingThreads.Clear();
|
2018-02-05 00:08:20 +01:00
|
|
|
}
|
2018-02-14 03:43:08 +01: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-05 00:08:20 +01:00
|
|
|
|
2018-02-19 20:37:13 +01:00
|
|
|
AcquireCondVarValue();
|
|
|
|
|
|
|
|
Process.Memory.WriteInt32(CondVarAddress, Count);
|
|
|
|
|
|
|
|
ReleaseCondVarValue();
|
2018-02-14 03:43:08 +01:00
|
|
|
}
|
2018-02-05 00:08:20 +01:00
|
|
|
}
|
2018-02-19 20:37:13 +01:00
|
|
|
|
|
|
|
Process.Scheduler.Suspend(Thread.ProcessorId);
|
|
|
|
Process.Scheduler.Resume(Thread);
|
2018-02-05 00:08:20 +01:00
|
|
|
}
|
2018-02-14 03:43:08 +01:00
|
|
|
|
2018-02-19 20:37:13 +01:00
|
|
|
private void AcquireCondVarValue()
|
2018-02-14 03:43:08 +01:00
|
|
|
{
|
2018-02-19 20:37:13 +01:00
|
|
|
if (!OwnsCondVarValue)
|
|
|
|
{
|
|
|
|
while (!Process.Memory.AcquireAddress(CondVarAddress))
|
|
|
|
{
|
|
|
|
Thread.Yield();
|
|
|
|
}
|
|
|
|
|
|
|
|
OwnsCondVarValue = true;
|
|
|
|
}
|
2018-02-14 03:43:08 +01:00
|
|
|
}
|
|
|
|
|
2018-02-19 20:37:13 +01:00
|
|
|
private void ReleaseCondVarValue()
|
2018-02-14 03:43:08 +01:00
|
|
|
{
|
2018-02-19 20:37:13 +01:00
|
|
|
if (OwnsCondVarValue)
|
|
|
|
{
|
|
|
|
OwnsCondVarValue = false;
|
|
|
|
|
|
|
|
Process.Memory.ReleaseAddress(CondVarAddress);
|
|
|
|
}
|
2018-02-14 03:43:08 +01:00
|
|
|
}
|
2018-02-05 00:08:20 +01:00
|
|
|
}
|
|
|
|
}
|