k_scoped_lock.h 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. // Copyright 2020 yuzu Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. // This file references various implementation details from Atmosphere, an open-source firmware for
  5. // the Nintendo Switch. Copyright 2018-2020 Atmosphere-NX.
  6. #pragma once
  7. #include "common/common_types.h"
  8. namespace Kernel {
  9. template <typename T>
  10. concept KLockable = !std::is_reference_v<T> && requires(T & t) {
  11. { t.Lock() } -> std::same_as<void>;
  12. { t.Unlock() } -> std::same_as<void>;
  13. };
  14. template <typename T>
  15. requires KLockable<T>
  16. class [[nodiscard]] KScopedLock {
  17. public:
  18. explicit KScopedLock(T* l) : lock_ptr(l) {
  19. this->lock_ptr->Lock();
  20. }
  21. explicit KScopedLock(T& l) : KScopedLock(std::addressof(l)) {}
  22. ~KScopedLock() {
  23. this->lock_ptr->Unlock();
  24. }
  25. KScopedLock(const KScopedLock&) = delete;
  26. KScopedLock& operator=(const KScopedLock&) = delete;
  27. KScopedLock(KScopedLock&&) = delete;
  28. KScopedLock& operator=(KScopedLock&&) = delete;
  29. private:
  30. T* lock_ptr;
  31. };
  32. } // namespace Kernel