2018-10-31 20:47:31 +00:00
|
|
|
#include <mesosphere/kresources/KResourceLimit.hpp>
|
|
|
|
|
|
|
|
namespace mesosphere
|
|
|
|
{
|
|
|
|
|
|
|
|
KResourceLimit KResourceLimit::defaultInstance{};
|
|
|
|
|
|
|
|
size_t KResourceLimit::GetCurrentValue(KResourceLimit::Category category) const
|
|
|
|
{
|
|
|
|
// Caller should check category
|
2018-11-05 13:12:38 +00:00
|
|
|
std::scoped_lock guard{condvar.mutex()};
|
2018-10-31 20:47:31 +00:00
|
|
|
return currentValues[(uint)category];
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t KResourceLimit::GetLimitValue(KResourceLimit::Category category) const
|
|
|
|
{
|
|
|
|
// Caller should check category
|
2018-11-05 13:12:38 +00:00
|
|
|
std::scoped_lock guard{condvar.mutex()};
|
2018-10-31 20:47:31 +00:00
|
|
|
return limitValues[(uint)category];
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t KResourceLimit::GetRemainingValue(KResourceLimit::Category category) const
|
|
|
|
{
|
|
|
|
// Caller should check category
|
2018-11-05 13:12:38 +00:00
|
|
|
std::scoped_lock guard{condvar.mutex()};
|
2018-10-31 20:47:31 +00:00
|
|
|
return limitValues[(uint)category] - currentValues[(uint)category];
|
|
|
|
}
|
|
|
|
|
|
|
|
bool KResourceLimit::SetLimitValue(KResourceLimit::Category category, size_t value)
|
|
|
|
{
|
2018-11-05 13:12:38 +00:00
|
|
|
std::scoped_lock guard{condvar.mutex()};
|
2018-10-31 20:47:31 +00:00
|
|
|
if ((long)value < 0 || currentValues[(uint)category] > value) {
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
limitValues[(uint)category] = value;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void KResourceLimit::Release(KResourceLimit::Category category, size_t count, size_t realCount)
|
|
|
|
{
|
|
|
|
// Caller should ensure parameters are correct
|
2018-11-05 13:12:38 +00:00
|
|
|
std::scoped_lock guard{condvar.mutex()};
|
2018-10-31 20:47:31 +00:00
|
|
|
currentValues[(uint)category] -= count;
|
|
|
|
realValues[(uint)category] -= realCount;
|
|
|
|
condvar.notify_all();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool KResourceLimit::ReserveDetail(KResourceLimit::Category category, size_t count, const KSystemClock::time_point &timeoutTime)
|
|
|
|
{
|
2018-11-05 13:12:38 +00:00
|
|
|
std::scoped_lock guard{condvar.mutex()};
|
2018-10-31 20:47:31 +00:00
|
|
|
if ((long)count <= 0 || realValues[(uint)category] >= limitValues[(uint)category]) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t newCur = currentValues[(uint)category] + count;
|
|
|
|
bool ok = false;
|
|
|
|
|
|
|
|
auto condition =
|
|
|
|
[=, &newCur] {
|
|
|
|
newCur = this->currentValues[(uint)category] + count;
|
|
|
|
size_t lval = this->limitValues[(uint)category];
|
|
|
|
return this->realValues[(uint)category] <= lval && newCur <= lval; // need to check here
|
|
|
|
};
|
|
|
|
|
|
|
|
if (timeoutTime <= KSystemClock::never) {
|
|
|
|
ok = true;
|
|
|
|
condvar.wait(condition);
|
|
|
|
} else {
|
|
|
|
ok = condvar.wait_until(timeoutTime, condition);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ok) {
|
|
|
|
currentValues[(uint)category] += count;
|
|
|
|
realValues[(uint)category] += count;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|