summaryrefslogtreecommitdiff
path: root/src/common/spin_lock.cpp
diff options
context:
space:
mode:
authorGravatar David2020-06-28 01:34:07 +1000
committerGravatar GitHub2020-06-28 01:34:07 +1000
commit0ea4a8bcc4bca14bb7c65b248ed1899d2e7167cf (patch)
treea83acb1e779b98d31fa54389bae4be5669573a41 /src/common/spin_lock.cpp
parentMerge pull request #4097 from kevinxucs/kevinxucs/device-pixel-scaling-float (diff)
parentCommon: Fix non-conan build (diff)
downloadyuzu-0ea4a8bcc4bca14bb7c65b248ed1899d2e7167cf.tar.gz
yuzu-0ea4a8bcc4bca14bb7c65b248ed1899d2e7167cf.tar.xz
yuzu-0ea4a8bcc4bca14bb7c65b248ed1899d2e7167cf.zip
Merge pull request #3396 from FernandoS27/prometheus-1
Implement SpinLocks, Fibers and a Host Timer
Diffstat (limited to 'src/common/spin_lock.cpp')
-rw-r--r--src/common/spin_lock.cpp54
1 files changed, 54 insertions, 0 deletions
diff --git a/src/common/spin_lock.cpp b/src/common/spin_lock.cpp
new file mode 100644
index 000000000..c7b46aac6
--- /dev/null
+++ b/src/common/spin_lock.cpp
@@ -0,0 +1,54 @@
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
21namespace {
22
23void 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
35namespace Common {
36
37void SpinLock::lock() {
38 while (lck.test_and_set(std::memory_order_acquire)) {
39 thread_pause();
40 }
41}
42
43void SpinLock::unlock() {
44 lck.clear(std::memory_order_release);
45}
46
47bool SpinLock::try_lock() {
48 if (lck.test_and_set(std::memory_order_acquire)) {
49 return false;
50 }
51 return true;
52}
53
54} // namespace Common