aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/core/hle/kernel/mutex.cpp
diff options
context:
space:
mode:
authorGravatar bunnei <bunneidev@gmail.com>2015-01-14 19:22:50 -0500
committerGravatar bunnei <bunneidev@gmail.com>2015-01-21 18:41:00 -0500
commitc22bac6398ff1705992fc44b2c29775c84cff662 (patch)
treee20da7e6e1824c19b7ced73f43815397749ffae7 /src/core/hle/kernel/mutex.cpp
parent0c7498545f7f9beeb7e8070e1a1955a8d6f40bb6 (diff)
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 853a5dd7..35d82960 100644
--- a/src/core/hle/kernel/mutex.cpp
+++ b/src/core/hle/kernel/mutex.cpp
@@ -13,7 +13,7 @@
namespace Kernel {
-class Mutex : public Object {
+class Mutex : public WaitObject {
public:
std::string GetTypeName() const override { return "Mutex"; }
std::string GetName() const override { return name; }
@@ -24,7 +24,6 @@ public:
bool initial_locked; ///< Initial lock state when mutex was created
bool locked; ///< Current locked state
Handle lock_thread; ///< Handle to thread that currently has mutex
- std::vector<Handle> waiting_threads; ///< Threads that are waiting for the mutex
std::string name; ///< Name of mutex (optional)
ResultVal<bool> WaitSynchronization() override;
@@ -45,36 +44,20 @@ void MutexAcquireLock(Mutex* mutex, Handle thread = GetCurrentThread()->GetHandl
mutex->lock_thread = thread;
}
-bool ReleaseMutexForThread(Mutex* mutex, Handle thread_handle) {
- MutexAcquireLock(mutex, thread_handle);
-
- Thread* thread = Kernel::g_handle_table.Get<Thread>(thread_handle).get();
- if (thread == nullptr) {
- LOG_ERROR(Kernel, "Called with invalid handle: %08X", thread_handle);
- return false;
- }
-
- thread->ResumeFromWait();
- return true;
-}
-
/**
* Resumes a thread waiting for the specified mutex
* @param mutex The mutex that some thread is waiting on
*/
void ResumeWaitingThread(Mutex* mutex) {
// Find the next waiting thread for the mutex...
- if (mutex->waiting_threads.empty()) {
+ auto next_thread = mutex->ResumeNextThread();
+ if (next_thread != nullptr) {
+ MutexAcquireLock(mutex, next_thread->GetHandle());
+ } else {
// Reset mutex lock thread handle, nothing is waiting
mutex->locked = false;
mutex->lock_thread = -1;
}
- else {
- // Resume the next waiting thread and re-lock the mutex
- std::vector<Handle>::iterator iter = mutex->waiting_threads.begin();
- ReleaseMutexForThread(mutex, *iter);
- mutex->waiting_threads.erase(iter);
- }
}
void MutexEraseLock(Mutex* mutex) {
@@ -175,7 +158,7 @@ Handle CreateMutex(bool initial_locked, const std::string& name) {
ResultVal<bool> Mutex::WaitSynchronization() {
bool wait = locked;
if (locked) {
- waiting_threads.push_back(GetCurrentThread()->GetHandle());
+ AddWaitingThread(GetCurrentThread());
Kernel::WaitCurrentThread(WAITTYPE_MUTEX, this);
} else {
// Lock the mutex when the first thread accesses it