KResourceLimit.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. using Ryujinx.Common;
  2. using System.Collections.Generic;
  3. namespace Ryujinx.HLE.HOS.Kernel
  4. {
  5. class KResourceLimit
  6. {
  7. private const int Time10SecondsMs = 10000;
  8. private long[] _current;
  9. private long[] _limit;
  10. private long[] _available;
  11. private object _lockObj;
  12. private LinkedList<KThread> _waitingThreads;
  13. private int _waitingThreadsCount;
  14. private Horizon _system;
  15. public KResourceLimit(Horizon system)
  16. {
  17. _current = new long[(int)LimitableResource.Count];
  18. _limit = new long[(int)LimitableResource.Count];
  19. _available = new long[(int)LimitableResource.Count];
  20. _lockObj = new object();
  21. _waitingThreads = new LinkedList<KThread>();
  22. _system = system;
  23. }
  24. public bool Reserve(LimitableResource resource, ulong amount)
  25. {
  26. return Reserve(resource, (long)amount);
  27. }
  28. public bool Reserve(LimitableResource resource, long amount)
  29. {
  30. return Reserve(resource, amount, KTimeManager.ConvertMillisecondsToNanoseconds(Time10SecondsMs));
  31. }
  32. public bool Reserve(LimitableResource resource, long amount, long timeout)
  33. {
  34. long endTimePoint = KTimeManager.ConvertNanosecondsToMilliseconds(timeout);
  35. endTimePoint += PerformanceCounter.ElapsedMilliseconds;
  36. bool success = false;
  37. int index = GetIndex(resource);
  38. lock (_lockObj)
  39. {
  40. long newCurrent = _current[index] + amount;
  41. while (newCurrent > _limit[index] && _available[index] + amount <= _limit[index])
  42. {
  43. _waitingThreadsCount++;
  44. KConditionVariable.Wait(_system, _waitingThreads, _lockObj, timeout);
  45. _waitingThreadsCount--;
  46. newCurrent = _current[index] + amount;
  47. if (timeout >= 0 && PerformanceCounter.ElapsedMilliseconds > endTimePoint)
  48. {
  49. break;
  50. }
  51. }
  52. if (newCurrent <= _limit[index])
  53. {
  54. _current[index] = newCurrent;
  55. success = true;
  56. }
  57. }
  58. return success;
  59. }
  60. public void Release(LimitableResource resource, ulong amount)
  61. {
  62. Release(resource, (long)amount);
  63. }
  64. public void Release(LimitableResource resource, long amount)
  65. {
  66. Release(resource, amount, amount);
  67. }
  68. private void Release(LimitableResource resource, long usedAmount, long availableAmount)
  69. {
  70. int index = GetIndex(resource);
  71. lock (_lockObj)
  72. {
  73. _current [index] -= usedAmount;
  74. _available[index] -= availableAmount;
  75. if (_waitingThreadsCount > 0)
  76. {
  77. KConditionVariable.NotifyAll(_system, _waitingThreads);
  78. }
  79. }
  80. }
  81. public long GetRemainingValue(LimitableResource resource)
  82. {
  83. int index = GetIndex(resource);
  84. lock (_lockObj)
  85. {
  86. return _limit[index] - _current[index];
  87. }
  88. }
  89. public KernelResult SetLimitValue(LimitableResource resource, long limit)
  90. {
  91. int index = GetIndex(resource);
  92. lock (_lockObj)
  93. {
  94. if (_current[index] <= limit)
  95. {
  96. _limit[index] = limit;
  97. return KernelResult.Success;
  98. }
  99. else
  100. {
  101. return KernelResult.InvalidState;
  102. }
  103. }
  104. }
  105. private static int GetIndex(LimitableResource resource)
  106. {
  107. return (int)resource;
  108. }
  109. }
  110. }