summaryrefslogtreecommitdiff
path: root/src/core/hle/kernel
diff options
context:
space:
mode:
authorGravatar bunnei2021-11-09 20:51:47 -0800
committerGravatar bunnei2021-12-06 16:39:17 -0800
commit423acf53b740ce96ca37988aad79ddf5013645ef (patch)
tree8e0674b69def392ef7e634e24d0027b9e0f213ad /src/core/hle/kernel
parenthle: kernel: KAddressArbiter: Migrate to updated KThreadQueue. (diff)
downloadyuzu-423acf53b740ce96ca37988aad79ddf5013645ef.tar.gz
yuzu-423acf53b740ce96ca37988aad79ddf5013645ef.tar.xz
yuzu-423acf53b740ce96ca37988aad79ddf5013645ef.zip
hle: kernel: KLightLock: Migrate to updated KThreadQueue.
Diffstat (limited to 'src/core/hle/kernel')
-rw-r--r--src/core/hle/kernel/k_light_lock.cpp69
-rw-r--r--src/core/hle/kernel/k_light_lock.h2
2 files changed, 36 insertions, 35 deletions
diff --git a/src/core/hle/kernel/k_light_lock.cpp b/src/core/hle/kernel/k_light_lock.cpp
index 0896e705f..5e8f1a510 100644
--- a/src/core/hle/kernel/k_light_lock.cpp
+++ b/src/core/hle/kernel/k_light_lock.cpp
@@ -5,44 +5,54 @@
5#include "core/hle/kernel/k_light_lock.h" 5#include "core/hle/kernel/k_light_lock.h"
6#include "core/hle/kernel/k_scheduler.h" 6#include "core/hle/kernel/k_scheduler.h"
7#include "core/hle/kernel/k_thread.h" 7#include "core/hle/kernel/k_thread.h"
8#include "core/hle/kernel/k_thread_queue.h"
8#include "core/hle/kernel/kernel.h" 9#include "core/hle/kernel/kernel.h"
9 10
10namespace Kernel { 11namespace Kernel {
11 12
13namespace {
14
15class ThreadQueueImplForKLightLock final : public KThreadQueue {
16public:
17 explicit ThreadQueueImplForKLightLock(KernelCore& kernel_) : KThreadQueue(kernel_) {}
18
19 virtual void CancelWait([[maybe_unused]] KThread* waiting_thread,
20 [[maybe_unused]] ResultCode wait_result,
21 [[maybe_unused]] bool cancel_timer_task) override {
22 // Do nothing, waiting to acquire a light lock cannot be canceled.
23 }
24};
25
26} // namespace
27
12void KLightLock::Lock() { 28void KLightLock::Lock() {
13 const uintptr_t cur_thread = reinterpret_cast<uintptr_t>(GetCurrentThreadPointer(kernel)); 29 const uintptr_t cur_thread = reinterpret_cast<uintptr_t>(GetCurrentThreadPointer(kernel));
14 const uintptr_t cur_thread_tag = (cur_thread | 1);
15 30
16 while (true) { 31 while (true) {
17 uintptr_t old_tag = tag.load(std::memory_order_relaxed); 32 uintptr_t old_tag = tag.load(std::memory_order_relaxed);
18 33
19 while (!tag.compare_exchange_weak(old_tag, (old_tag == 0) ? cur_thread : old_tag | 1, 34 while (!tag.compare_exchange_weak(old_tag, (old_tag == 0) ? cur_thread : (old_tag | 1),
20 std::memory_order_acquire)) { 35 std::memory_order_acquire)) {
21 if ((old_tag | 1) == cur_thread_tag) {
22 return;
23 }
24 } 36 }
25 37
26 if ((old_tag == 0) || ((old_tag | 1) == cur_thread_tag)) { 38 if (old_tag == 0 || this->LockSlowPath(old_tag | 1, cur_thread)) {
27 break; 39 break;
28 } 40 }
29
30 LockSlowPath(old_tag | 1, cur_thread);
31 } 41 }
32} 42}
33 43
34void KLightLock::Unlock() { 44void KLightLock::Unlock() {
35 const uintptr_t cur_thread = reinterpret_cast<uintptr_t>(GetCurrentThreadPointer(kernel)); 45 const uintptr_t cur_thread = reinterpret_cast<uintptr_t>(GetCurrentThreadPointer(kernel));
46
36 uintptr_t expected = cur_thread; 47 uintptr_t expected = cur_thread;
37 do { 48 if (!tag.compare_exchange_strong(expected, 0, std::memory_order_release)) {
38 if (expected != cur_thread) { 49 this->UnlockSlowPath(cur_thread);
39 return UnlockSlowPath(cur_thread); 50 }
40 }
41 } while (!tag.compare_exchange_weak(expected, 0, std::memory_order_release));
42} 51}
43 52
44void KLightLock::LockSlowPath(uintptr_t _owner, uintptr_t _cur_thread) { 53bool KLightLock::LockSlowPath(uintptr_t _owner, uintptr_t _cur_thread) {
45 KThread* cur_thread = reinterpret_cast<KThread*>(_cur_thread); 54 KThread* cur_thread = reinterpret_cast<KThread*>(_cur_thread);
55 ThreadQueueImplForKLightLock wait_queue(kernel);
46 56
47 // Pend the current thread waiting on the owner thread. 57 // Pend the current thread waiting on the owner thread.
48 { 58 {
@@ -50,30 +60,23 @@ void KLightLock::LockSlowPath(uintptr_t _owner, uintptr_t _cur_thread) {
50 60
51 // Ensure we actually have locking to do. 61 // Ensure we actually have locking to do.
52 if (tag.load(std::memory_order_relaxed) != _owner) { 62 if (tag.load(std::memory_order_relaxed) != _owner) {
53 return; 63 return false;
54 } 64 }
55 65
56 // Add the current thread as a waiter on the owner. 66 // Add the current thread as a waiter on the owner.
57 KThread* owner_thread = reinterpret_cast<KThread*>(_owner & ~1ULL); 67 KThread* owner_thread = reinterpret_cast<KThread*>(_owner & ~1ul);
58 cur_thread->SetAddressKey(reinterpret_cast<uintptr_t>(std::addressof(tag))); 68 cur_thread->SetAddressKey(reinterpret_cast<uintptr_t>(std::addressof(tag)));
59 owner_thread->AddWaiter(cur_thread); 69 owner_thread->AddWaiter(cur_thread);
60 70
61 // Set thread states. 71 // Begin waiting to hold the lock.
62 cur_thread->SetState(ThreadState::Waiting); 72 cur_thread->BeginWait(std::addressof(wait_queue));
63 73
64 if (owner_thread->IsSuspended()) { 74 if (owner_thread->IsSuspended()) {
65 owner_thread->ContinueIfHasKernelWaiters(); 75 owner_thread->ContinueIfHasKernelWaiters();
66 } 76 }
67 } 77 }
68 78
69 // We're no longer waiting on the lock owner. 79 return true;
70 {
71 KScopedSchedulerLock sl{kernel};
72
73 if (KThread* owner_thread = cur_thread->GetLockOwner(); owner_thread != nullptr) {
74 owner_thread->RemoveWaiter(cur_thread);
75 }
76 }
77} 80}
78 81
79void KLightLock::UnlockSlowPath(uintptr_t _cur_thread) { 82void KLightLock::UnlockSlowPath(uintptr_t _cur_thread) {
@@ -81,22 +84,20 @@ void KLightLock::UnlockSlowPath(uintptr_t _cur_thread) {
81 84
82 // Unlock. 85 // Unlock.
83 { 86 {
84 KScopedSchedulerLock sl{kernel}; 87 KScopedSchedulerLock sl(kernel);
85 88
86 // Get the next owner. 89 // Get the next owner.
87 s32 num_waiters = 0; 90 s32 num_waiters;
88 KThread* next_owner = owner_thread->RemoveWaiterByKey( 91 KThread* next_owner = owner_thread->RemoveWaiterByKey(
89 std::addressof(num_waiters), reinterpret_cast<uintptr_t>(std::addressof(tag))); 92 std::addressof(num_waiters), reinterpret_cast<uintptr_t>(std::addressof(tag)));
90 93
91 // Pass the lock to the next owner. 94 // Pass the lock to the next owner.
92 uintptr_t next_tag = 0; 95 uintptr_t next_tag = 0;
93 if (next_owner != nullptr) { 96 if (next_owner != nullptr) {
94 next_tag = reinterpret_cast<uintptr_t>(next_owner); 97 next_tag =
95 if (num_waiters > 1) { 98 reinterpret_cast<uintptr_t>(next_owner) | static_cast<uintptr_t>(num_waiters > 1);
96 next_tag |= 0x1;
97 }
98 99
99 next_owner->SetState(ThreadState::Runnable); 100 next_owner->EndWait(ResultSuccess);
100 101
101 if (next_owner->IsSuspended()) { 102 if (next_owner->IsSuspended()) {
102 next_owner->ContinueIfHasKernelWaiters(); 103 next_owner->ContinueIfHasKernelWaiters();
@@ -110,7 +111,7 @@ void KLightLock::UnlockSlowPath(uintptr_t _cur_thread) {
110 } 111 }
111 112
112 // Write the new tag value. 113 // Write the new tag value.
113 tag.store(next_tag); 114 tag.store(next_tag, std::memory_order_release);
114 } 115 }
115} 116}
116 117
diff --git a/src/core/hle/kernel/k_light_lock.h b/src/core/hle/kernel/k_light_lock.h
index ad853661d..4163b8a85 100644
--- a/src/core/hle/kernel/k_light_lock.h
+++ b/src/core/hle/kernel/k_light_lock.h
@@ -20,7 +20,7 @@ public:
20 20
21 void Unlock(); 21 void Unlock();
22 22
23 void LockSlowPath(uintptr_t owner, uintptr_t cur_thread); 23 bool LockSlowPath(uintptr_t owner, uintptr_t cur_thread);
24 24
25 void UnlockSlowPath(uintptr_t cur_thread); 25 void UnlockSlowPath(uintptr_t cur_thread);
26 26