Ryujinx/Ryujinx.HLE/HOS/Kernel/KResourceLimit.cs

146 lines
3.9 KiB
C#
Raw Normal View History

using Ryujinx.Common;
using System.Collections.Generic;
namespace Ryujinx.HLE.HOS.Kernel
{
class KResourceLimit
{
private const int Time10SecondsMs = 10000;
2018-12-01 21:01:59 +01:00
private long[] _current;
private long[] _limit;
private long[] _available;
2018-12-01 21:01:59 +01:00
private object _lockObj;
2018-12-01 21:01:59 +01:00
private LinkedList<KThread> _waitingThreads;
2018-12-01 21:01:59 +01:00
private int _waitingThreadsCount;
2018-12-01 21:01:59 +01:00
private Horizon _system;
2018-12-01 21:01:59 +01:00
public KResourceLimit(Horizon system)
{
2018-12-01 21:01:59 +01:00
_current = new long[(int)LimitableResource.Count];
_limit = new long[(int)LimitableResource.Count];
_available = new long[(int)LimitableResource.Count];
2018-12-01 21:01:59 +01:00
_lockObj = new object();
2018-12-01 21:01:59 +01:00
_waitingThreads = new LinkedList<KThread>();
2018-12-01 21:24:37 +01:00
_system = system;
}
2018-12-01 21:01:59 +01:00
public bool Reserve(LimitableResource resource, ulong amount)
{
2018-12-01 21:01:59 +01:00
return Reserve(resource, (long)amount);
}
2018-12-01 21:01:59 +01:00
public bool Reserve(LimitableResource resource, long amount)
{
2018-12-01 21:01:59 +01:00
return Reserve(resource, amount, KTimeManager.ConvertMillisecondsToNanoseconds(Time10SecondsMs));
}
2018-12-01 21:01:59 +01:00
public bool Reserve(LimitableResource resource, long amount, long timeout)
{
2018-12-01 21:01:59 +01:00
long endTimePoint = KTimeManager.ConvertNanosecondsToMilliseconds(timeout);
2018-12-01 21:01:59 +01:00
endTimePoint += PerformanceCounter.ElapsedMilliseconds;
2018-12-01 21:01:59 +01:00
bool success = false;
2018-12-01 21:01:59 +01:00
int index = GetIndex(resource);
2018-12-01 21:01:59 +01:00
lock (_lockObj)
{
2018-12-01 21:01:59 +01:00
long newCurrent = _current[index] + amount;
2018-12-01 21:01:59 +01:00
while (newCurrent > _limit[index] && _available[index] + amount <= _limit[index])
{
2018-12-01 21:01:59 +01:00
_waitingThreadsCount++;
2018-12-01 21:01:59 +01:00
KConditionVariable.Wait(_system, _waitingThreads, _lockObj, timeout);
2018-12-01 21:01:59 +01:00
_waitingThreadsCount--;
2018-12-01 21:01:59 +01:00
newCurrent = _current[index] + amount;
2018-12-01 21:01:59 +01:00
if (timeout >= 0 && PerformanceCounter.ElapsedMilliseconds > endTimePoint)
{
break;
}
}
2018-12-01 21:01:59 +01:00
if (newCurrent <= _limit[index])
{
2018-12-01 21:01:59 +01:00
_current[index] = newCurrent;
2018-12-01 21:01:59 +01:00
success = true;
}
}
2018-12-01 21:01:59 +01:00
return success;
}
2018-12-01 21:01:59 +01:00
public void Release(LimitableResource resource, ulong amount)
{
2018-12-01 21:01:59 +01:00
Release(resource, (long)amount);
}
2018-12-01 21:01:59 +01:00
public void Release(LimitableResource resource, long amount)
{
2018-12-01 21:01:59 +01:00
Release(resource, amount, amount);
}
2018-12-01 21:01:59 +01:00
private void Release(LimitableResource resource, long usedAmount, long availableAmount)
{
2018-12-01 21:01:59 +01:00
int index = GetIndex(resource);
2018-12-01 21:01:59 +01:00
lock (_lockObj)
{
2018-12-01 21:01:59 +01:00
_current [index] -= usedAmount;
_available[index] -= availableAmount;
2018-12-01 21:01:59 +01:00
if (_waitingThreadsCount > 0)
{
2018-12-01 21:01:59 +01:00
KConditionVariable.NotifyAll(_system, _waitingThreads);
}
}
}
2018-12-01 21:01:59 +01:00
public long GetRemainingValue(LimitableResource resource)
{
2018-12-01 21:01:59 +01:00
int index = GetIndex(resource);
2018-12-01 21:01:59 +01:00
lock (_lockObj)
{
2018-12-01 21:01:59 +01:00
return _limit[index] - _current[index];
}
}
2018-12-01 21:01:59 +01:00
public KernelResult SetLimitValue(LimitableResource resource, long limit)
{
2018-12-01 21:01:59 +01:00
int index = GetIndex(resource);
2018-12-01 21:01:59 +01:00
lock (_lockObj)
{
2018-12-01 21:01:59 +01:00
if (_current[index] <= limit)
{
2018-12-01 21:24:37 +01:00
_limit[index] = limit;
return KernelResult.Success;
}
else
{
return KernelResult.InvalidState;
}
}
}
2018-12-01 21:01:59 +01:00
private static int GetIndex(LimitableResource resource)
{
2018-12-01 21:01:59 +01:00
return (int)resource;
}
}
}