2018-11-28 14:18:09 -08:00
|
|
|
using Ryujinx.Common;
|
2018-12-17 21:33:36 -08:00
|
|
|
using Ryujinx.HLE.HOS.Kernel.Threading;
|
2018-11-28 14:18:09 -08:00
|
|
|
using System.Collections.Generic;
|
|
|
|
|
2018-12-17 21:33:36 -08:00
|
|
|
namespace Ryujinx.HLE.HOS.Kernel.Common
|
2018-11-28 14:18:09 -08:00
|
|
|
{
|
2019-01-18 14:26:39 -08:00
|
|
|
class KResourceLimit : KAutoObject
|
2018-11-28 14:18:09 -08:00
|
|
|
{
|
|
|
|
private const int Time10SecondsMs = 10000;
|
|
|
|
|
2020-05-03 20:41:29 -07:00
|
|
|
private readonly long[] _current;
|
|
|
|
private readonly long[] _limit;
|
|
|
|
private readonly long[] _available;
|
2018-11-28 14:18:09 -08:00
|
|
|
|
2020-05-03 20:41:29 -07:00
|
|
|
private readonly object _lockObj;
|
2018-11-28 14:18:09 -08:00
|
|
|
|
2020-05-03 20:41:29 -07:00
|
|
|
private readonly LinkedList<KThread> _waitingThreads;
|
2018-11-28 14:18:09 -08:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
private int _waitingThreadsCount;
|
2018-11-28 14:18:09 -08:00
|
|
|
|
2020-05-03 20:41:29 -07:00
|
|
|
public KResourceLimit(KernelContext context) : base(context)
|
2018-11-28 14:18:09 -08:00
|
|
|
{
|
2018-12-06 03:16:24 -08:00
|
|
|
_current = new long[(int)LimitableResource.Count];
|
|
|
|
_limit = new long[(int)LimitableResource.Count];
|
|
|
|
_available = new long[(int)LimitableResource.Count];
|
2018-11-28 14:18:09 -08:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
_lockObj = new object();
|
2018-11-28 14:18:09 -08:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
_waitingThreads = new LinkedList<KThread>();
|
2018-11-28 14:18:09 -08:00
|
|
|
}
|
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
public bool Reserve(LimitableResource resource, ulong amount)
|
2018-11-28 14:18:09 -08:00
|
|
|
{
|
2018-12-06 03:16:24 -08:00
|
|
|
return Reserve(resource, (long)amount);
|
2018-11-28 14:18:09 -08:00
|
|
|
}
|
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
public bool Reserve(LimitableResource resource, long amount)
|
2018-11-28 14:18:09 -08:00
|
|
|
{
|
2018-12-06 03:16:24 -08:00
|
|
|
return Reserve(resource, amount, KTimeManager.ConvertMillisecondsToNanoseconds(Time10SecondsMs));
|
2018-11-28 14:18:09 -08:00
|
|
|
}
|
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
public bool Reserve(LimitableResource resource, long amount, long timeout)
|
2018-11-28 14:18:09 -08:00
|
|
|
{
|
2018-12-06 03:16:24 -08:00
|
|
|
long endTimePoint = KTimeManager.ConvertNanosecondsToMilliseconds(timeout);
|
2018-11-28 14:18:09 -08:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
endTimePoint += PerformanceCounter.ElapsedMilliseconds;
|
2018-11-28 14:18:09 -08:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
bool success = false;
|
2018-11-28 14:18:09 -08:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
int index = GetIndex(resource);
|
2018-11-28 14:18:09 -08:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
lock (_lockObj)
|
2018-11-28 14:18:09 -08:00
|
|
|
{
|
2018-12-06 03:16:24 -08:00
|
|
|
long newCurrent = _current[index] + amount;
|
2018-11-28 14:18:09 -08:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
while (newCurrent > _limit[index] && _available[index] + amount <= _limit[index])
|
2018-11-28 14:18:09 -08:00
|
|
|
{
|
2018-12-06 03:16:24 -08:00
|
|
|
_waitingThreadsCount++;
|
2018-11-28 14:18:09 -08:00
|
|
|
|
2020-05-03 20:41:29 -07:00
|
|
|
KConditionVariable.Wait(KernelContext, _waitingThreads, _lockObj, timeout);
|
2018-11-28 14:18:09 -08:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
_waitingThreadsCount--;
|
2018-11-28 14:18:09 -08:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
newCurrent = _current[index] + amount;
|
2018-11-28 14:18:09 -08:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
if (timeout >= 0 && PerformanceCounter.ElapsedMilliseconds > endTimePoint)
|
2018-11-28 14:18:09 -08:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
if (newCurrent <= _limit[index])
|
2018-11-28 14:18:09 -08:00
|
|
|
{
|
2018-12-06 03:16:24 -08:00
|
|
|
_current[index] = newCurrent;
|
2018-11-28 14:18:09 -08:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
success = true;
|
2018-11-28 14:18:09 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
return success;
|
2018-11-28 14:18:09 -08:00
|
|
|
}
|
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
public void Release(LimitableResource resource, ulong amount)
|
2018-11-28 14:18:09 -08:00
|
|
|
{
|
2018-12-06 03:16:24 -08:00
|
|
|
Release(resource, (long)amount);
|
2018-11-28 14:18:09 -08:00
|
|
|
}
|
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
public void Release(LimitableResource resource, long amount)
|
2018-11-28 14:18:09 -08:00
|
|
|
{
|
2018-12-06 03:16:24 -08:00
|
|
|
Release(resource, amount, amount);
|
2018-11-28 14:18:09 -08:00
|
|
|
}
|
|
|
|
|
2019-01-18 14:26:39 -08:00
|
|
|
public void Release(LimitableResource resource, long usedAmount, long availableAmount)
|
2018-11-28 14:18:09 -08:00
|
|
|
{
|
2018-12-06 03:16:24 -08:00
|
|
|
int index = GetIndex(resource);
|
2018-11-28 14:18:09 -08:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
lock (_lockObj)
|
2018-11-28 14:18:09 -08:00
|
|
|
{
|
2018-12-06 03:16:24 -08:00
|
|
|
_current [index] -= usedAmount;
|
|
|
|
_available[index] -= availableAmount;
|
2018-11-28 14:18:09 -08:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
if (_waitingThreadsCount > 0)
|
2018-11-28 14:18:09 -08:00
|
|
|
{
|
2020-05-03 20:41:29 -07:00
|
|
|
KConditionVariable.NotifyAll(KernelContext, _waitingThreads);
|
2018-11-28 14:18:09 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
public long GetRemainingValue(LimitableResource resource)
|
2018-11-28 14:18:09 -08:00
|
|
|
{
|
2018-12-06 03:16:24 -08:00
|
|
|
int index = GetIndex(resource);
|
2018-11-28 14:18:09 -08:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
lock (_lockObj)
|
2018-11-28 14:18:09 -08:00
|
|
|
{
|
2018-12-06 03:16:24 -08:00
|
|
|
return _limit[index] - _current[index];
|
2018-11-28 14:18:09 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
public KernelResult SetLimitValue(LimitableResource resource, long limit)
|
2018-11-28 14:18:09 -08:00
|
|
|
{
|
2018-12-06 03:16:24 -08:00
|
|
|
int index = GetIndex(resource);
|
2018-11-28 14:18:09 -08:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
lock (_lockObj)
|
2018-11-28 14:18:09 -08:00
|
|
|
{
|
2018-12-06 03:16:24 -08:00
|
|
|
if (_current[index] <= limit)
|
2018-11-28 14:18:09 -08:00
|
|
|
{
|
2018-12-06 03:16:24 -08:00
|
|
|
_limit[index] = limit;
|
2018-11-28 14:18:09 -08:00
|
|
|
|
|
|
|
return KernelResult.Success;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return KernelResult.InvalidState;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
private static int GetIndex(LimitableResource resource)
|
2018-11-28 14:18:09 -08:00
|
|
|
{
|
2018-12-06 03:16:24 -08:00
|
|
|
return (int)resource;
|
2018-11-28 14:18:09 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|