spin_lock.h 706 B

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