Seregon/ShadPKG

A tool for deriving PKG packet encryption keys for ps4 written in c++

C++/47.3 KB/No license
common/spin_lock.cpp
ShadPKG / common / spin_lock.cpp
1// SPDX-FileCopyrightText: Copyright 2020 yuzu Emulator Project
2// SPDX-License-Identifier: GPL-2.0-or-later
3 
4#include "common/spin_lock.h"
5 
6#if _MSC_VER
7#include <intrin.h>
8#if _M_AMD64
9#define __x86_64__ 1
10#endif
11#if _M_ARM64
12#define __aarch64__ 1
13#endif
14#else
15#if __x86_64__
16#include <xmmintrin.h>
17#endif
18#endif
19 
20namespace {
21 
22void ThreadPause() {
23#if __x86_64__
24 _mm_pause();
25#elif __aarch64__ && _MSC_VER
26 __yield();
27#elif __aarch64__
28 asm("yield");
29#endif
30}
31 
32} // Anonymous namespace
33 
34namespace Common {
35 
36void SpinLock::lock() {
37 while (lck.test_and_set(std::memory_order_acquire)) {
38 ThreadPause();
39 }
40}
41 
42void SpinLock::unlock() {
43 lck.clear(std::memory_order_release);
44}
45 
46bool SpinLock::try_lock() {
47 if (lck.test_and_set(std::memory_order_acquire)) {
48 return false;
49 }
50 return true;
51}
52 
53} // namespace Common
54