summaryrefslogtreecommitdiff
path: root/src/core/hle/kernel/mutex.cpp
diff options
context:
space:
mode:
authorGravatar bunnei2015-01-14 19:22:50 -0500
committerGravatar bunnei2015-01-21 18:41:00 -0500
commitc22bac6398ff1705992fc44b2c29775c84cff662 (patch)
treee20da7e6e1824c19b7ced73f43815397749ffae7 /src/core/hle/kernel/mutex.cpp
parentMerge pull request #491 from archshift/hidspvr (diff)
downloadyuzu-c22bac6398ff1705992fc44b2c29775c84cff662.tar.gz
yuzu-c22bac6398ff1705992fc44b2c29775c84cff662.tar.xz
yuzu-c22bac6398ff1705992fc44b2c29775c84cff662.zip
Kernel: Added WaitObject and changed "waitable" objects inherit from it.
Diffstat (limited to 'src/core/hle/kernel/mutex.cpp')
-rw-r--r--src/core/hle/kernel/mutex.cpp29
1 files changed, 6 insertions, 23 deletions
diff --git a/src/core/hle/kernel/mutex.cpp b/src/core/hle/kernel/mutex.cpp
index 853a5dd74..35d829606 100644
--- a/src/core/hle/kernel/mutex.cpp
+++ b/src/core/hle/kernel/mutex.cpp
@@ -13,7 +13,7 @@
13 13
14namespace Kernel { 14namespace Kernel {
15 15
16class Mutex : public Object { 16class Mutex : public WaitObject {
17public: 17public:
18 std::string GetTypeName() const override { return "Mutex"; } 18 std::string GetTypeName() const override { return "Mutex"; }
19 std::string GetName() const override { return name; } 19 std::string GetName() const override { return name; }
@@ -24,7 +24,6 @@ public:
24 bool initial_locked; ///< Initial lock state when mutex was created 24 bool initial_locked; ///< Initial lock state when mutex was created
25 bool locked; ///< Current locked state 25 bool locked; ///< Current locked state
26 Handle lock_thread; ///< Handle to thread that currently has mutex 26 Handle lock_thread; ///< Handle to thread that currently has mutex
27 std::vector<Handle> waiting_threads; ///< Threads that are waiting for the mutex
28 std::string name; ///< Name of mutex (optional) 27 std::string name; ///< Name of mutex (optional)
29 28
30 ResultVal<bool> WaitSynchronization() override; 29 ResultVal<bool> WaitSynchronization() override;
@@ -45,36 +44,20 @@ void MutexAcquireLock(Mutex* mutex, Handle thread = GetCurrentThread()->GetHandl
45 mutex->lock_thread = thread; 44 mutex->lock_thread = thread;
46} 45}
47 46
48bool ReleaseMutexForThread(Mutex* mutex, Handle thread_handle) {
49 MutexAcquireLock(mutex, thread_handle);
50
51 Thread* thread = Kernel::g_handle_table.Get<Thread>(thread_handle).get();
52 if (thread == nullptr) {
53 LOG_ERROR(Kernel, "Called with invalid handle: %08X", thread_handle);
54 return false;
55 }
56
57 thread->ResumeFromWait();
58 return true;
59}
60
61/** 47/**
62 * Resumes a thread waiting for the specified mutex 48 * Resumes a thread waiting for the specified mutex
63 * @param mutex The mutex that some thread is waiting on 49 * @param mutex The mutex that some thread is waiting on
64 */ 50 */
65void ResumeWaitingThread(Mutex* mutex) { 51void ResumeWaitingThread(Mutex* mutex) {
66 // Find the next waiting thread for the mutex... 52 // Find the next waiting thread for the mutex...
67 if (mutex->waiting_threads.empty()) { 53 auto next_thread = mutex->ResumeNextThread();
54 if (next_thread != nullptr) {
55 MutexAcquireLock(mutex, next_thread->GetHandle());
56 } else {
68 // Reset mutex lock thread handle, nothing is waiting 57 // Reset mutex lock thread handle, nothing is waiting
69 mutex->locked = false; 58 mutex->locked = false;
70 mutex->lock_thread = -1; 59 mutex->lock_thread = -1;
71 } 60 }
72 else {
73 // Resume the next waiting thread and re-lock the mutex
74 std::vector<Handle>::iterator iter = mutex->waiting_threads.begin();
75 ReleaseMutexForThread(mutex, *iter);
76 mutex->waiting_threads.erase(iter);
77 }
78} 61}
79 62
80void MutexEraseLock(Mutex* mutex) { 63void MutexEraseLock(Mutex* mutex) {
@@ -175,7 +158,7 @@ Handle CreateMutex(bool initial_locked, const std::string& name) {
175ResultVal<bool> Mutex::WaitSynchronization() { 158ResultVal<bool> Mutex::WaitSynchronization() {
176 bool wait = locked; 159 bool wait = locked;
177 if (locked) { 160 if (locked) {
178 waiting_threads.push_back(GetCurrentThread()->GetHandle()); 161 AddWaitingThread(GetCurrentThread());
179 Kernel::WaitCurrentThread(WAITTYPE_MUTEX, this); 162 Kernel::WaitCurrentThread(WAITTYPE_MUTEX, this);
180 } else { 163 } else {
181 // Lock the mutex when the first thread accesses it 164 // Lock the mutex when the first thread accesses it