summaryrefslogtreecommitdiffstats
path: root/src/core/hle/kernel/k_resource_limit.h
blob: 4542317d0c059eba299b32037d4401d186a0bc7e (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
// Copyright 2020 yuzu Emulator Project
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.

#pragma once

#include <array>
#include "common/common_types.h"
#include "core/hle/kernel/k_light_condition_variable.h"
#include "core/hle/kernel/k_light_lock.h"
#include "core/hle/kernel/object.h"

union ResultCode;

namespace Core::Timing {
class CoreTiming;
}

namespace Kernel {
class KernelCore;
enum class LimitableResource : u32 {
    PhysicalMemory = 0,
    Threads = 1,
    Events = 2,
    TransferMemory = 3,
    Sessions = 4,

    Count,
};

constexpr bool IsValidResourceType(LimitableResource type) {
    return type < LimitableResource::Count;
}

class KResourceLimit final : public Object {
public:
    explicit KResourceLimit(KernelCore& kernel, const Core::Timing::CoreTiming& core_timing_);
    ~KResourceLimit();

    s64 GetLimitValue(LimitableResource which) const;
    s64 GetCurrentValue(LimitableResource which) const;
    s64 GetPeakValue(LimitableResource which) const;
    s64 GetFreeValue(LimitableResource which) const;

    ResultCode SetLimitValue(LimitableResource which, s64 value);

    bool Reserve(LimitableResource which, s64 value);
    bool Reserve(LimitableResource which, s64 value, s64 timeout);
    void Release(LimitableResource which, s64 value);
    void Release(LimitableResource which, s64 value, s64 hint);

    std::string GetTypeName() const override {
        return "KResourceLimit";
    }
    std::string GetName() const override {
        return GetTypeName();
    }

    static constexpr HandleType HANDLE_TYPE = HandleType::ResourceLimit;
    HandleType GetHandleType() const override {
        return HANDLE_TYPE;
    }

    virtual void Finalize() override {}

private:
    using ResourceArray = std::array<s64, static_cast<std::size_t>(LimitableResource::Count)>;
    ResourceArray limit_values{};
    ResourceArray current_values{};
    ResourceArray current_hints{};
    ResourceArray peak_values{};
    mutable KLightLock lock;
    s32 waiter_count{};
    KLightConditionVariable cond_var;
    const Core::Timing::CoreTiming& core_timing;
};
} // namespace Kernel