diff options
Diffstat (limited to 'src/common/spin_lock.h')
| -rw-r--r-- | src/common/spin_lock.h | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/src/common/spin_lock.h b/src/common/spin_lock.h new file mode 100644 index 000000000..06ac2f5bb --- /dev/null +++ b/src/common/spin_lock.h | |||
| @@ -0,0 +1,34 @@ | |||
| 1 | // Copyright 2020 yuzu Emulator Project | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #pragma once | ||
| 6 | |||
| 7 | #include <atomic> | ||
| 8 | |||
| 9 | namespace Common { | ||
| 10 | |||
| 11 | /** | ||
| 12 | * SpinLock class | ||
| 13 | * a lock similar to mutex that forces a thread to spin wait instead calling the | ||
| 14 | * supervisor. Should be used on short sequences of code. | ||
| 15 | */ | ||
| 16 | class SpinLock { | ||
| 17 | public: | ||
| 18 | SpinLock() = default; | ||
| 19 | |||
| 20 | SpinLock(const SpinLock&) = delete; | ||
| 21 | SpinLock& operator=(const SpinLock&) = delete; | ||
| 22 | |||
| 23 | SpinLock(SpinLock&&) = delete; | ||
| 24 | SpinLock& operator=(SpinLock&&) = delete; | ||
| 25 | |||
| 26 | void lock(); | ||
| 27 | void unlock(); | ||
| 28 | [[nodiscard]] bool try_lock(); | ||
| 29 | |||
| 30 | private: | ||
| 31 | std::atomic_flag lck = ATOMIC_FLAG_INIT; | ||
| 32 | }; | ||
| 33 | |||
| 34 | } // namespace Common | ||