diff options
| author | 2021-02-08 18:02:36 -0800 | |
|---|---|---|
| committer | 2021-02-18 16:16:24 -0800 | |
| commit | 6da91da08e54d02a4087eaddcbe192bd53eeaa9f (patch) | |
| tree | a78c199089f4589bed446a9994eba216e0622e0c /src/core/hle/kernel | |
| parent | core: memory: Add templated GetPointer methods. (diff) | |
| download | yuzu-6da91da08e54d02a4087eaddcbe192bd53eeaa9f.tar.gz yuzu-6da91da08e54d02a4087eaddcbe192bd53eeaa9f.tar.xz yuzu-6da91da08e54d02a4087eaddcbe192bd53eeaa9f.zip | |
hle: kernel: Add KSpinLock implementation.
Diffstat (limited to 'src/core/hle/kernel')
| -rw-r--r-- | src/core/hle/kernel/k_spin_lock.cpp | 54 | ||||
| -rw-r--r-- | src/core/hle/kernel/k_spin_lock.h | 33 |
2 files changed, 87 insertions, 0 deletions
diff --git a/src/core/hle/kernel/k_spin_lock.cpp b/src/core/hle/kernel/k_spin_lock.cpp new file mode 100644 index 000000000..4412aa4bb --- /dev/null +++ b/src/core/hle/kernel/k_spin_lock.cpp | |||
| @@ -0,0 +1,54 @@ | |||
| 1 | // Copyright 2021 yuzu Emulator Project | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #include "core/hle/kernel/k_spin_lock.h" | ||
| 6 | |||
| 7 | #if _MSC_VER | ||
| 8 | #include <intrin.h> | ||
| 9 | #if _M_AMD64 | ||
| 10 | #define __x86_64__ 1 | ||
| 11 | #endif | ||
| 12 | #if _M_ARM64 | ||
| 13 | #define __aarch64__ 1 | ||
| 14 | #endif | ||
| 15 | #else | ||
| 16 | #if __x86_64__ | ||
| 17 | #include <xmmintrin.h> | ||
| 18 | #endif | ||
| 19 | #endif | ||
| 20 | |||
| 21 | namespace { | ||
| 22 | |||
| 23 | void ThreadPause() { | ||
| 24 | #if __x86_64__ | ||
| 25 | _mm_pause(); | ||
| 26 | #elif __aarch64__ && _MSC_VER | ||
| 27 | __yield(); | ||
| 28 | #elif __aarch64__ | ||
| 29 | asm("yield"); | ||
| 30 | #endif | ||
| 31 | } | ||
| 32 | |||
| 33 | } // namespace | ||
| 34 | |||
| 35 | namespace Kernel { | ||
| 36 | |||
| 37 | void KSpinLock::Lock() { | ||
| 38 | while (lck.test_and_set(std::memory_order_acquire)) { | ||
| 39 | ThreadPause(); | ||
| 40 | } | ||
| 41 | } | ||
| 42 | |||
| 43 | void KSpinLock::Unlock() { | ||
| 44 | lck.clear(std::memory_order_release); | ||
| 45 | } | ||
| 46 | |||
| 47 | bool KSpinLock::TryLock() { | ||
| 48 | if (lck.test_and_set(std::memory_order_acquire)) { | ||
| 49 | return false; | ||
| 50 | } | ||
| 51 | return true; | ||
| 52 | } | ||
| 53 | |||
| 54 | } // namespace Kernel | ||
diff --git a/src/core/hle/kernel/k_spin_lock.h b/src/core/hle/kernel/k_spin_lock.h new file mode 100644 index 000000000..12c4b2e88 --- /dev/null +++ b/src/core/hle/kernel/k_spin_lock.h | |||
| @@ -0,0 +1,33 @@ | |||
| 1 | // Copyright 2021 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 | #include "core/hle/kernel/k_scoped_lock.h" | ||
| 10 | |||
| 11 | namespace Kernel { | ||
| 12 | |||
| 13 | class KSpinLock { | ||
| 14 | public: | ||
| 15 | KSpinLock() = default; | ||
| 16 | |||
| 17 | KSpinLock(const KSpinLock&) = delete; | ||
| 18 | KSpinLock& operator=(const KSpinLock&) = delete; | ||
| 19 | |||
| 20 | KSpinLock(KSpinLock&&) = delete; | ||
| 21 | KSpinLock& operator=(KSpinLock&&) = delete; | ||
| 22 | |||
| 23 | void Lock(); | ||
| 24 | void Unlock(); | ||
| 25 | [[nodiscard]] bool TryLock(); | ||
| 26 | |||
| 27 | private: | ||
| 28 | std::atomic_flag lck = ATOMIC_FLAG_INIT; | ||
| 29 | }; | ||
| 30 | |||
| 31 | using KScopedSpinLock = KScopedLock<KSpinLock>; | ||
| 32 | |||
| 33 | } // namespace Kernel | ||