2018-11-28 23:18:09 +01:00
|
|
|
using Ryujinx.Common;
|
2018-12-18 06:33:36 +01:00
|
|
|
using Ryujinx.HLE.HOS.Kernel.Threading;
|
2023-01-04 23:15:45 +01:00
|
|
|
using Ryujinx.Horizon.Common;
|
2018-11-28 23:18:09 +01:00
|
|
|
using System.Collections.Generic;
|
|
|
|
|
2018-12-18 06:33:36 +01:00
|
|
|
namespace Ryujinx.HLE.HOS.Kernel.Common
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2019-01-18 23:26:39 +01:00
|
|
|
class KResourceLimit : KAutoObject
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2020-07-17 06:18:31 +02:00
|
|
|
private const int DefaultTimeoutMs = 10000; // 10s
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2020-05-04 05:41:29 +02:00
|
|
|
private readonly long[] _current;
|
|
|
|
private readonly long[] _limit;
|
2020-07-17 06:18:31 +02:00
|
|
|
private readonly long[] _current2;
|
2021-10-24 01:40:13 +02:00
|
|
|
private readonly long[] _peak;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2020-07-17 06:18:31 +02:00
|
|
|
private readonly object _lock;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2020-05-04 05:41:29 +02:00
|
|
|
private readonly LinkedList<KThread> _waitingThreads;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
private int _waitingThreadsCount;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2020-05-04 05:41:29 +02:00
|
|
|
public KResourceLimit(KernelContext context) : base(context)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2020-07-17 06:18:31 +02:00
|
|
|
_current = new long[(int)LimitableResource.Count];
|
|
|
|
_limit = new long[(int)LimitableResource.Count];
|
|
|
|
_current2 = new long[(int)LimitableResource.Count];
|
2021-10-24 01:40:13 +02:00
|
|
|
_peak = new long[(int)LimitableResource.Count];
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2020-07-17 06:18:31 +02:00
|
|
|
_lock = new object();
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
_waitingThreads = new LinkedList<KThread>();
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
public bool Reserve(LimitableResource resource, ulong amount)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
return Reserve(resource, (long)amount);
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
public bool Reserve(LimitableResource resource, long amount)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2020-07-17 06:18:31 +02:00
|
|
|
return Reserve(resource, amount, KTimeManager.ConvertMillisecondsToNanoseconds(DefaultTimeoutMs));
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
public bool Reserve(LimitableResource resource, long amount, long timeout)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
long endTimePoint = KTimeManager.ConvertNanosecondsToMilliseconds(timeout);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
endTimePoint += PerformanceCounter.ElapsedMilliseconds;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
bool success = false;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
int index = GetIndex(resource);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2020-07-17 06:18:31 +02:00
|
|
|
lock (_lock)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2020-07-17 06:18:31 +02:00
|
|
|
if (_current2[index] >= _limit[index])
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
long newCurrent = _current[index] + amount;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2020-07-17 06:18:31 +02:00
|
|
|
while (newCurrent > _limit[index] && _current2[index] + amount <= _limit[index])
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
_waitingThreadsCount++;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2020-07-17 06:18:31 +02:00
|
|
|
KConditionVariable.Wait(KernelContext, _waitingThreads, _lock, timeout);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
_waitingThreadsCount--;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
newCurrent = _current[index] + amount;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
if (timeout >= 0 && PerformanceCounter.ElapsedMilliseconds > endTimePoint)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
if (newCurrent <= _limit[index])
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
_current[index] = newCurrent;
|
2020-07-17 06:18:31 +02:00
|
|
|
_current2[index] += amount;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2021-10-24 01:40:13 +02:00
|
|
|
if (_current[index] > _peak[index])
|
|
|
|
{
|
|
|
|
_peak[index] = _current[index];
|
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
success = true;
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
return success;
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
public void Release(LimitableResource resource, ulong amount)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
Release(resource, (long)amount);
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
public void Release(LimitableResource resource, long amount)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
Release(resource, amount, amount);
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
|
2020-07-17 06:18:31 +02:00
|
|
|
public void Release(LimitableResource resource, long amount, long amount2)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
int index = GetIndex(resource);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2020-07-17 06:18:31 +02:00
|
|
|
lock (_lock)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2020-07-17 06:18:31 +02:00
|
|
|
_current[index] -= amount;
|
|
|
|
_current2[index] -= amount2;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
if (_waitingThreadsCount > 0)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2020-05-04 05:41:29 +02:00
|
|
|
KConditionVariable.NotifyAll(KernelContext, _waitingThreads);
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
public long GetRemainingValue(LimitableResource resource)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
int index = GetIndex(resource);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2020-07-17 06:18:31 +02:00
|
|
|
lock (_lock)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
return _limit[index] - _current[index];
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-24 01:40:13 +02:00
|
|
|
public long GetCurrentValue(LimitableResource resource)
|
|
|
|
{
|
|
|
|
int index = GetIndex(resource);
|
|
|
|
|
|
|
|
lock (_lock)
|
|
|
|
{
|
|
|
|
return _current[index];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public long GetLimitValue(LimitableResource resource)
|
|
|
|
{
|
|
|
|
int index = GetIndex(resource);
|
|
|
|
|
|
|
|
lock (_lock)
|
|
|
|
{
|
|
|
|
return _limit[index];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public long GetPeakValue(LimitableResource resource)
|
|
|
|
{
|
|
|
|
int index = GetIndex(resource);
|
|
|
|
|
|
|
|
lock (_lock)
|
|
|
|
{
|
|
|
|
return _peak[index];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-04 23:15:45 +01:00
|
|
|
public Result SetLimitValue(LimitableResource resource, long limit)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
int index = GetIndex(resource);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2020-07-17 06:18:31 +02:00
|
|
|
lock (_lock)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
if (_current[index] <= limit)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
_limit[index] = limit;
|
2021-10-24 01:40:13 +02:00
|
|
|
_peak[index] = _current[index];
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2023-01-04 23:15:45 +01:00
|
|
|
return Result.Success;
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return KernelResult.InvalidState;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
private static int GetIndex(LimitableResource resource)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
return (int)resource;
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|