diff options
| author | 2020-02-04 11:23:12 -0400 | |
|---|---|---|
| committer | 2020-06-18 16:29:13 -0400 | |
| commit | 13ed9438fb47d62663fb1ef367baac1a567b25b3 (patch) | |
| tree | 6b342b85f39bde149532d7af8a06186560221f31 /src | |
| parent | Merge pull request #4108 from ReinUsesLisp/a32-implicit-cast (diff) | |
| download | yuzu-13ed9438fb47d62663fb1ef367baac1a567b25b3.tar.gz yuzu-13ed9438fb47d62663fb1ef367baac1a567b25b3.tar.xz yuzu-13ed9438fb47d62663fb1ef367baac1a567b25b3.zip | |
Common: Implement a basic SpinLock class
Diffstat (limited to 'src')
| -rw-r--r-- | src/common/CMakeLists.txt | 2 | ||||
| -rw-r--r-- | src/common/spin_lock.cpp | 46 | ||||
| -rw-r--r-- | src/common/spin_lock.h | 20 |
3 files changed, 68 insertions, 0 deletions
diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt index 0a3e2f4d1..c8bf80372 100644 --- a/src/common/CMakeLists.txt +++ b/src/common/CMakeLists.txt | |||
| @@ -143,6 +143,8 @@ add_library(common STATIC | |||
| 143 | scm_rev.cpp | 143 | scm_rev.cpp |
| 144 | scm_rev.h | 144 | scm_rev.h |
| 145 | scope_exit.h | 145 | scope_exit.h |
| 146 | spin_lock.cpp | ||
| 147 | spin_lock.h | ||
| 146 | string_util.cpp | 148 | string_util.cpp |
| 147 | string_util.h | 149 | string_util.h |
| 148 | swap.h | 150 | swap.h |
diff --git a/src/common/spin_lock.cpp b/src/common/spin_lock.cpp new file mode 100644 index 000000000..8077b78d2 --- /dev/null +++ b/src/common/spin_lock.cpp | |||
| @@ -0,0 +1,46 @@ | |||
| 1 | // Copyright 2020 yuzu Emulator Project | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #include "common/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 thread_pause() { | ||
| 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 Common { | ||
| 36 | |||
| 37 | void SpinLock::lock() { | ||
| 38 | while (lck.test_and_set(std::memory_order_acquire)) | ||
| 39 | thread_pause(); | ||
| 40 | } | ||
| 41 | |||
| 42 | void SpinLock::unlock() { | ||
| 43 | lck.clear(std::memory_order_release); | ||
| 44 | } | ||
| 45 | |||
| 46 | } // namespace Common | ||
diff --git a/src/common/spin_lock.h b/src/common/spin_lock.h new file mode 100644 index 000000000..cbc67b6c8 --- /dev/null +++ b/src/common/spin_lock.h | |||
| @@ -0,0 +1,20 @@ | |||
| 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 | class SpinLock { | ||
| 12 | public: | ||
| 13 | void lock(); | ||
| 14 | void unlock(); | ||
| 15 | |||
| 16 | private: | ||
| 17 | std::atomic_flag lck = ATOMIC_FLAG_INIT; | ||
| 18 | }; | ||
| 19 | |||
| 20 | } // namespace Common | ||