k_resource_limit.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. // Copyright 2020 yuzu Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. // This file references various implementation details from Atmosphere, an open-source firmware for
  5. // the Nintendo Switch. Copyright 2018-2020 Atmosphere-NX.
  6. #include "common/assert.h"
  7. #include "core/core.h"
  8. #include "core/core_timing.h"
  9. #include "core/core_timing_util.h"
  10. #include "core/hle/kernel/k_resource_limit.h"
  11. #include "core/hle/kernel/svc_results.h"
  12. namespace Kernel {
  13. constexpr s64 DefaultTimeout = 10000000000; // 10 seconds
  14. KResourceLimit::KResourceLimit(KernelCore& kernel, Core::System& system)
  15. : Object{kernel}, lock{kernel}, cond_var{kernel}, kernel{kernel}, system(system) {}
  16. KResourceLimit::~KResourceLimit() = default;
  17. s64 KResourceLimit::GetLimitValue(LimitableResource which) const {
  18. const auto index = static_cast<std::size_t>(which);
  19. s64 value{};
  20. {
  21. KScopedLightLock lk{lock};
  22. value = limit_values[index];
  23. ASSERT(value >= 0);
  24. ASSERT(current_values[index] <= limit_values[index]);
  25. ASSERT(current_hints[index] <= current_values[index]);
  26. }
  27. return value;
  28. }
  29. s64 KResourceLimit::GetCurrentValue(LimitableResource which) const {
  30. const auto index = static_cast<std::size_t>(which);
  31. s64 value{};
  32. {
  33. KScopedLightLock lk{lock};
  34. value = current_values[index];
  35. ASSERT(value >= 0);
  36. ASSERT(current_values[index] <= limit_values[index]);
  37. ASSERT(current_hints[index] <= current_values[index]);
  38. }
  39. return value;
  40. }
  41. s64 KResourceLimit::GetPeakValue(LimitableResource which) const {
  42. const auto index = static_cast<std::size_t>(which);
  43. s64 value{};
  44. {
  45. KScopedLightLock lk{lock};
  46. value = peak_values[index];
  47. ASSERT(value >= 0);
  48. ASSERT(current_values[index] <= limit_values[index]);
  49. ASSERT(current_hints[index] <= current_values[index]);
  50. }
  51. return value;
  52. }
  53. s64 KResourceLimit::GetFreeValue(LimitableResource which) const {
  54. const auto index = static_cast<std::size_t>(which);
  55. s64 value{};
  56. {
  57. KScopedLightLock lk(lock);
  58. ASSERT(current_values[index] >= 0);
  59. ASSERT(current_values[index] <= limit_values[index]);
  60. ASSERT(current_hints[index] <= current_values[index]);
  61. value = limit_values[index] - current_values[index];
  62. }
  63. return value;
  64. }
  65. ResultCode KResourceLimit::SetLimitValue(LimitableResource which, s64 value) {
  66. const auto index = static_cast<std::size_t>(which);
  67. KScopedLightLock lk(lock);
  68. R_UNLESS(current_values[index] <= value, Svc::ResultInvalidState);
  69. limit_values[index] = value;
  70. return RESULT_SUCCESS;
  71. }
  72. bool KResourceLimit::Reserve(LimitableResource which, s64 value) {
  73. return Reserve(which, value, system.CoreTiming().GetGlobalTimeNs().count() + DefaultTimeout);
  74. }
  75. bool KResourceLimit::Reserve(LimitableResource which, s64 value, s64 timeout) {
  76. ASSERT(value >= 0);
  77. const auto index = static_cast<std::size_t>(which);
  78. KScopedLightLock lk(lock);
  79. ASSERT(current_hints[index] <= current_values[index]);
  80. if (current_hints[index] >= limit_values[index]) {
  81. return false;
  82. }
  83. // Loop until we reserve or run out of time.
  84. while (true) {
  85. ASSERT(current_values[index] <= limit_values[index]);
  86. ASSERT(current_hints[index] <= current_values[index]);
  87. // If we would overflow, don't allow to succeed.
  88. if (current_values[index] + value <= current_values[index]) {
  89. break;
  90. }
  91. if (current_values[index] + value <= limit_values[index]) {
  92. current_values[index] += value;
  93. current_hints[index] += value;
  94. peak_values[index] = std::max(peak_values[index], current_values[index]);
  95. return true;
  96. }
  97. if (current_hints[index] + value <= limit_values[index] &&
  98. (timeout < 0 || system.CoreTiming().GetGlobalTimeNs().count() < timeout)) {
  99. waiter_count++;
  100. cond_var.Wait(&lock, timeout);
  101. waiter_count--;
  102. } else {
  103. break;
  104. }
  105. }
  106. return false;
  107. }
  108. void KResourceLimit::Release(LimitableResource which, s64 value) {
  109. Release(which, value, value);
  110. }
  111. void KResourceLimit::Release(LimitableResource which, s64 value, s64 hint) {
  112. ASSERT(value >= 0);
  113. ASSERT(hint >= 0);
  114. const auto index = static_cast<std::size_t>(which);
  115. KScopedLightLock lk(lock);
  116. ASSERT(current_values[index] <= limit_values[index]);
  117. ASSERT(current_hints[index] <= current_values[index]);
  118. ASSERT(value <= current_values[index]);
  119. ASSERT(hint <= current_hints[index]);
  120. current_values[index] -= value;
  121. current_hints[index] -= hint;
  122. if (waiter_count != 0) {
  123. cond_var.Broadcast();
  124. }
  125. }
  126. } // namespace Kernel