diff options
author | ameerj <52414509+ameerj@users.noreply.github.com> | 2021-02-05 02:06:54 +0100 |
---|---|---|
committer | ameerj <52414509+ameerj@users.noreply.github.com> | 2021-02-13 00:57:34 +0100 |
commit | 5fa6b15215d2c15a1601c88ac1125a28c55797fc (patch) | |
tree | 4a25c58180aecd1eb9bcc3560a1a3dfa18c67761 /src/core/hle/kernel/k_scoped_resource_reservation.h | |
parent | kernel: Unify result codes (#5890) (diff) | |
download | yuzu-5fa6b15215d2c15a1601c88ac1125a28c55797fc.tar yuzu-5fa6b15215d2c15a1601c88ac1125a28c55797fc.tar.gz yuzu-5fa6b15215d2c15a1601c88ac1125a28c55797fc.tar.bz2 yuzu-5fa6b15215d2c15a1601c88ac1125a28c55797fc.tar.lz yuzu-5fa6b15215d2c15a1601c88ac1125a28c55797fc.tar.xz yuzu-5fa6b15215d2c15a1601c88ac1125a28c55797fc.tar.zst yuzu-5fa6b15215d2c15a1601c88ac1125a28c55797fc.zip |
Diffstat (limited to '')
-rw-r--r-- | src/core/hle/kernel/k_scoped_resource_reservation.h | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/src/core/hle/kernel/k_scoped_resource_reservation.h b/src/core/hle/kernel/k_scoped_resource_reservation.h new file mode 100644 index 000000000..c5deca00b --- /dev/null +++ b/src/core/hle/kernel/k_scoped_resource_reservation.h @@ -0,0 +1,67 @@ +// Copyright 2021 yuzu Emulator Project +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +// This file references various implementation details from Atmosphere, an open-source firmware for +// the Nintendo Switch. Copyright 2018-2020 Atmosphere-NX. + +#pragma once + +#include "common/common_types.h" +#include "core/hle/kernel/k_resource_limit.h" +#include "core/hle/kernel/process.h" + +namespace Kernel { + +class KScopedResourceReservation { +public: + explicit KScopedResourceReservation(std::shared_ptr<KResourceLimit> l, LimitableResource r, + s64 v, s64 timeout) + : resource_limit(std::move(l)), value(v), resource(r) { + if (resource_limit && value) { + success = resource_limit->Reserve(resource, value, timeout); + } else { + success = true; + } + } + + explicit KScopedResourceReservation(std::shared_ptr<KResourceLimit> l, LimitableResource r, + s64 v = 1) + : resource_limit(std::move(l)), value(v), resource(r) { + if (resource_limit && value) { + success = resource_limit->Reserve(resource, value); + } else { + success = true; + } + } + + explicit KScopedResourceReservation(const Process* p, LimitableResource r, s64 v, s64 t) + : KScopedResourceReservation(p->GetResourceLimit(), r, v, t) {} + + explicit KScopedResourceReservation(const Process* p, LimitableResource r, s64 v = 1) + : KScopedResourceReservation(p->GetResourceLimit(), r, v) {} + + ~KScopedResourceReservation() noexcept { + if (resource_limit && value && success) { + // resource was not committed, release the reservation. + resource_limit->Release(resource, value); + } + } + + /// Commit the resource reservation, destruction of this object does not release the resource + void Commit() { + resource_limit = nullptr; + } + + [[nodiscard]] bool Succeeded() const { + return success; + } + +private: + std::shared_ptr<KResourceLimit> resource_limit; + s64 value; + LimitableResource resource; + bool success; +}; + +} // namespace Kernel |