2018-11-28 23:18:09 +01:00
|
|
|
using Ryujinx.Common;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
|
|
namespace Ryujinx.HLE.HOS.Kernel
|
|
|
|
{
|
|
|
|
class KResourceLimit
|
|
|
|
{
|
|
|
|
private const int Time10SecondsMs = 10000;
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
private long[] Current;
|
|
|
|
private long[] Limit;
|
|
|
|
private long[] Available;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
private object LockObj;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
private LinkedList<KThread> WaitingThreads;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
private int WaitingThreadsCount;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
private Horizon System;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
public KResourceLimit(Horizon System)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
Current = new long[(int)LimitableResource.Count];
|
|
|
|
Limit = new long[(int)LimitableResource.Count];
|
|
|
|
Available = new long[(int)LimitableResource.Count];
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
LockObj = new object();
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
WaitingThreads = new LinkedList<KThread>();
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
this.System = System;
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
public bool Reserve(LimitableResource Resource, ulong Amount)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
return Reserve(Resource, (long)Amount);
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
public bool Reserve(LimitableResource Resource, long Amount)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
return Reserve(Resource, Amount, KTimeManager.ConvertMillisecondsToNanoseconds(Time10SecondsMs));
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
public bool Reserve(LimitableResource Resource, long Amount, long Timeout)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
long EndTimePoint = KTimeManager.ConvertNanosecondsToMilliseconds(Timeout);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
EndTimePoint += PerformanceCounter.ElapsedMilliseconds;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
bool Success = false;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
int Index = GetIndex(Resource);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
lock (LockObj)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
long NewCurrent = Current[Index] + Amount;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
while (NewCurrent > Limit[Index] && Available[Index] + Amount <= Limit[Index])
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
WaitingThreadsCount++;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
KConditionVariable.Wait(System, WaitingThreads, LockObj, Timeout);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
WaitingThreadsCount--;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
NewCurrent = Current[Index] + Amount;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
if (Timeout >= 0 && PerformanceCounter.ElapsedMilliseconds > EndTimePoint)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
if (NewCurrent <= Limit[Index])
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
Current[Index] = NewCurrent;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
Success = true;
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
return Success;
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
public void Release(LimitableResource Resource, ulong Amount)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
Release(Resource, (long)Amount);
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
public void Release(LimitableResource Resource, long Amount)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
Release(Resource, Amount, Amount);
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
private void Release(LimitableResource Resource, long UsedAmount, long AvailableAmount)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
int Index = GetIndex(Resource);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
lock (LockObj)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
Current [Index] -= UsedAmount;
|
|
|
|
Available[Index] -= AvailableAmount;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
if (WaitingThreadsCount > 0)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
KConditionVariable.NotifyAll(System, WaitingThreads);
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
public long GetRemainingValue(LimitableResource Resource)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
int Index = GetIndex(Resource);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
lock (LockObj)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
return Limit[Index] - Current[Index];
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
public KernelResult SetLimitValue(LimitableResource Resource, long Limit)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
int Index = GetIndex(Resource);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
lock (LockObj)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
if (Current[Index] <= Limit)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
this.Limit[Index] = Limit;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
|
|
|
return KernelResult.Success;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return KernelResult.InvalidState;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
private static int GetIndex(LimitableResource Resource)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
return (int)Resource;
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|