k_scoped_lock.h 977 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  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() }
  12. ->std::same_as<void>;
  13. { t.Unlock() }
  14. ->std::same_as<void>;
  15. };
  16. template <typename T>
  17. requires KLockable<T> class KScopedLock {
  18. public:
  19. explicit KScopedLock(T* l) : lock_ptr(l) {
  20. this->lock_ptr->Lock();
  21. }
  22. explicit KScopedLock(T& l) : KScopedLock(std::addressof(l)) { /* ... */
  23. }
  24. ~KScopedLock() {
  25. this->lock_ptr->Unlock();
  26. }
  27. KScopedLock(const KScopedLock&) = delete;
  28. KScopedLock(KScopedLock&&) = delete;
  29. private:
  30. T* lock_ptr;
  31. };
  32. } // namespace Kernel