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