summaryrefslogtreecommitdiffstats
path: root/src/core/hle/kernel/k_resource_limit.cpp
blob: f91cb65dca41b5c419788af3d5a4fa5b4b23b749 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// Copyright 2020 yuzu Emulator Project
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.

#include "common/assert.h"
#include "core/core_timing.h"
#include "core/hle/kernel/k_resource_limit.h"
#include "core/hle/kernel/svc_results.h"

namespace Kernel {
constexpr s64 DefaultTimeout = 10000000000; // 10 seconds

KResourceLimit::KResourceLimit(KernelCore& kernel_)
    : KAutoObjectWithSlabHeapAndContainer{kernel_}, lock{kernel_}, cond_var{kernel_} {}
KResourceLimit::~KResourceLimit() = default;

void KResourceLimit::Initialize(const Core::Timing::CoreTiming* core_timing_) {
    core_timing = core_timing_;
}

void KResourceLimit::Finalize() {}

s64 KResourceLimit::GetLimitValue(LimitableResource which) const {
    const auto index = static_cast<std::size_t>(which);
    s64 value{};
    {
        KScopedLightLock lk{lock};
        value = limit_values[index];
        ASSERT(value >= 0);
        ASSERT(current_values[index] <= limit_values[index]);
        ASSERT(current_hints[index] <= current_values[index]);
    }
    return value;
}

s64 KResourceLimit::GetCurrentValue(LimitableResource which) const {
    const auto index = static_cast<std::size_t>(which);
    s64 value{};
    {
        KScopedLightLock lk{lock};
        value = current_values[index];
        ASSERT(value >= 0);
        ASSERT(current_values[index] <= limit_values[index]);
        ASSERT(current_hints[index] <= current_values[index]);
    }
    return value;
}

s64 KResourceLimit::GetPeakValue(LimitableResource which) const {
    const auto index = static_cast<std::size_t>(which);
    s64 value{};
    {
        KScopedLightLock lk{lock};
        value = peak_values[index];
        ASSERT(value >= 0);
        ASSERT(current_values[index] <= limit_values[index]);
        ASSERT(current_hints[index] <= current_values[index]);
    }
    return value;
}

s64 KResourceLimit::GetFreeValue(LimitableResource which) const {
    const auto index = static_cast<std::size_t>(which);
    s64 value{};
    {
        KScopedLightLock lk(lock);
        ASSERT(current_values[index] >= 0);
        ASSERT(current_values[index] <= limit_values[index]);
        ASSERT(current_hints[index] <= current_values[index]);
        value = limit_values[index] - current_values[index];
    }

    return value;
}

ResultCode KResourceLimit::SetLimitValue(LimitableResource which, s64 value) {
    const auto index = static_cast<std::size_t>(which);
    KScopedLightLock lk(lock);
    R_UNLESS(current_values[index] <= value, ResultInvalidState);

    limit_values[index] = value;

    return ResultSuccess;
}

bool KResourceLimit::Reserve(LimitableResource which, s64 value) {
    return Reserve(which, value, core_timing->GetGlobalTimeNs().count() + DefaultTimeout);
}

bool KResourceLimit::Reserve(LimitableResource which, s64 value, s64 timeout) {
    ASSERT(value >= 0);
    const auto index = static_cast<std::size_t>(which);
    KScopedLightLock lk(lock);

    ASSERT(current_hints[index] <= current_values[index]);
    if (current_hints[index] >= limit_values[index]) {
        return false;
    }

    // Loop until we reserve or run out of time.
    while (true) {
        ASSERT(current_values[index] <= limit_values[index]);
        ASSERT(current_hints[index] <= current_values[index]);

        // If we would overflow, don't allow to succeed.
        if (current_values[index] + value <= current_values[index]) {
            break;
        }

        if (current_values[index] + value <= limit_values[index]) {
            current_values[index] += value;
            current_hints[index] += value;
            peak_values[index] = std::max(peak_values[index], current_values[index]);
            return true;
        }

        if (current_hints[index] + value <= limit_values[index] &&
            (timeout < 0 || core_timing->GetGlobalTimeNs().count() < timeout)) {
            waiter_count++;
            cond_var.Wait(&lock, timeout);
            waiter_count--;
        } else {
            break;
        }
    }

    return false;
}

void KResourceLimit::Release(LimitableResource which, s64 value) {
    Release(which, value, value);
}

void KResourceLimit::Release(LimitableResource which, s64 value, s64 hint) {
    ASSERT(value >= 0);
    ASSERT(hint >= 0);

    const auto index = static_cast<std::size_t>(which);
    KScopedLightLock lk(lock);
    ASSERT(current_values[index] <= limit_values[index]);
    ASSERT(current_hints[index] <= current_values[index]);
    ASSERT(value <= current_values[index]);
    ASSERT(hint <= current_hints[index]);

    current_values[index] -= value;
    current_hints[index] -= hint;

    if (waiter_count != 0) {
        cond_var.Broadcast();
    }
}

} // namespace Kernel