spin_lock.h 725 B

12345678910111213141516171819202122232425262728293031323334
  1. // Copyright 2020 yuzu Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #pragma once
  5. #include <atomic>
  6. namespace Common {
  7. /**
  8. * SpinLock class
  9. * a lock similar to mutex that forces a thread to spin wait instead calling the
  10. * supervisor. Should be used on short sequences of code.
  11. */
  12. class SpinLock {
  13. public:
  14. SpinLock() = default;
  15. SpinLock(const SpinLock&) = delete;
  16. SpinLock& operator=(const SpinLock&) = delete;
  17. SpinLock(SpinLock&&) = delete;
  18. SpinLock& operator=(SpinLock&&) = delete;
  19. void lock();
  20. void unlock();
  21. [[nodiscard]] bool try_lock();
  22. private:
  23. std::atomic_flag lck = ATOMIC_FLAG_INIT;
  24. };
  25. } // namespace Common