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