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. this.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. this.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. }