2022-04-23 10:59:50 +02:00
|
|
|
// SPDX-FileCopyrightText: Copyright 2020 yuzu Emulator Project
|
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2020-12-04 07:26:42 +01:00
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2021-11-04 02:31:09 +01:00
|
|
|
#include <concepts>
|
|
|
|
#include <type_traits>
|
|
|
|
|
2020-12-04 07:26:42 +01:00
|
|
|
namespace Kernel {
|
|
|
|
|
|
|
|
template <typename T>
|
2023-01-29 21:54:13 +01:00
|
|
|
concept KLockable = !
|
|
|
|
std::is_reference_v<T>&& requires(T& t) {
|
|
|
|
{ t.Lock() } -> std::same_as<void>;
|
|
|
|
{ t.Unlock() } -> std::same_as<void>;
|
|
|
|
};
|
2020-12-04 07:26:42 +01:00
|
|
|
|
|
|
|
template <typename T>
|
2023-01-29 21:54:13 +01:00
|
|
|
requires KLockable<T>
|
2021-09-24 07:21:07 +02:00
|
|
|
class [[nodiscard]] KScopedLock {
|
2020-12-04 07:26:42 +01:00
|
|
|
public:
|
2021-09-24 07:21:07 +02:00
|
|
|
explicit KScopedLock(T* l) : lock_ptr(l) {
|
2020-12-04 07:26:42 +01:00
|
|
|
this->lock_ptr->Lock();
|
|
|
|
}
|
2021-09-24 07:21:07 +02:00
|
|
|
explicit KScopedLock(T& l) : KScopedLock(std::addressof(l)) {}
|
2021-04-07 07:25:55 +02:00
|
|
|
|
2020-12-04 07:26:42 +01:00
|
|
|
~KScopedLock() {
|
|
|
|
this->lock_ptr->Unlock();
|
|
|
|
}
|
2020-12-05 09:07:00 +01:00
|
|
|
|
|
|
|
KScopedLock(const KScopedLock&) = delete;
|
2021-04-07 07:25:55 +02:00
|
|
|
KScopedLock& operator=(const KScopedLock&) = delete;
|
|
|
|
|
2021-09-24 07:21:07 +02:00
|
|
|
KScopedLock(KScopedLock&&) = delete;
|
2021-04-07 07:25:55 +02:00
|
|
|
KScopedLock& operator=(KScopedLock&&) = delete;
|
2020-12-05 09:07:00 +01:00
|
|
|
|
|
|
|
private:
|
|
|
|
T* lock_ptr;
|
2020-12-04 07:26:42 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace Kernel
|