aboutsummaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
authorGravatar bunnei <bunneidev@gmail.com>2015-02-09 22:17:20 -0500
committerGravatar bunnei <bunneidev@gmail.com>2015-02-09 22:17:20 -0500
commit848795f3834c30695208e86c5bbebf6c71a57d96 (patch)
tree2cc5b3092cf41215bac17c6ddaf3bdec985b7adb /src
parent837439e7d57833973e6f3bdd04ce597aaaa9fd45 (diff)
parent1b0bf00cbcadfc4cca0a3d16efac8b85a4bd71e5 (diff)
Merge pull request #551 from bunnei/mutex-fixes
Mutex/synch fixes
Diffstat (limited to 'src')
-rw-r--r--src/core/hle/kernel/mutex.cpp33
-rw-r--r--src/core/hle/kernel/mutex.h3
-rw-r--r--src/core/hle/svc.cpp8
3 files changed, 24 insertions, 20 deletions
diff --git a/src/core/hle/kernel/mutex.cpp b/src/core/hle/kernel/mutex.cpp
index 9f7166ca..a811db39 100644
--- a/src/core/hle/kernel/mutex.cpp
+++ b/src/core/hle/kernel/mutex.cpp
@@ -21,7 +21,7 @@ namespace Kernel {
*/
static void ResumeWaitingThread(Mutex* mutex) {
// Reset mutex lock thread handle, nothing is waiting
- mutex->locked = false;
+ mutex->lock_count = 0;
mutex->holding_thread = nullptr;
// Find the next waiting thread for the mutex...
@@ -44,8 +44,7 @@ Mutex::~Mutex() {}
SharedPtr<Mutex> Mutex::Create(bool initial_locked, std::string name) {
SharedPtr<Mutex> mutex(new Mutex);
- mutex->initial_locked = initial_locked;
- mutex->locked = false;
+ mutex->lock_count = 0;
mutex->name = std::move(name);
mutex->holding_thread = nullptr;
@@ -57,7 +56,7 @@ SharedPtr<Mutex> Mutex::Create(bool initial_locked, std::string name) {
}
bool Mutex::ShouldWait() {
- return locked && holding_thread != GetCurrentThread();
+ return lock_count > 0 && holding_thread != GetCurrentThread();;
}
void Mutex::Acquire() {
@@ -66,21 +65,27 @@ void Mutex::Acquire() {
void Mutex::Acquire(SharedPtr<Thread> thread) {
_assert_msg_(Kernel, !ShouldWait(), "object unavailable!");
- if (locked)
- return;
- locked = true;
+ // Actually "acquire" the mutex only if we don't already have it...
+ if (lock_count == 0) {
+ thread->held_mutexes.insert(this);
+ holding_thread = std::move(thread);
+ }
- thread->held_mutexes.insert(this);
- holding_thread = std::move(thread);
+ lock_count++;
}
void Mutex::Release() {
- if (!locked)
- return;
-
- holding_thread->held_mutexes.erase(this);
- ResumeWaitingThread(this);
+ // Only release if the mutex is held...
+ if (lock_count > 0) {
+ lock_count--;
+
+ // Yield to the next thread only if we've fully released the mutex...
+ if (lock_count == 0) {
+ holding_thread->held_mutexes.erase(this);
+ ResumeWaitingThread(this);
+ }
+ }
}
} // namespace
diff --git a/src/core/hle/kernel/mutex.h b/src/core/hle/kernel/mutex.h
index 54840361..d6d5328b 100644
--- a/src/core/hle/kernel/mutex.h
+++ b/src/core/hle/kernel/mutex.h
@@ -30,8 +30,7 @@ public:
static const HandleType HANDLE_TYPE = HandleType::Mutex;
HandleType GetHandleType() const override { return HANDLE_TYPE; }
- bool initial_locked; ///< Initial lock state when mutex was created
- bool locked; ///< Current locked state
+ int lock_count; ///< Number of times the mutex has been acquired
std::string name; ///< Name of mutex (optional)
SharedPtr<Thread> holding_thread; ///< Thread that has acquired the mutex
diff --git a/src/core/hle/svc.cpp b/src/core/hle/svc.cpp
index 34a27917..c4866fcc 100644
--- a/src/core/hle/svc.cpp
+++ b/src/core/hle/svc.cpp
@@ -144,6 +144,8 @@ static ResultCode WaitSynchronization1(Handle handle, s64 nano_seconds) {
LOG_TRACE(Kernel_SVC, "called handle=0x%08X(%s:%s), nanoseconds=%lld", handle,
object->GetTypeName().c_str(), object->GetName().c_str(), nano_seconds);
+ HLE::Reschedule(__func__);
+
// Check for next thread to schedule
if (object->ShouldWait()) {
@@ -153,8 +155,6 @@ static ResultCode WaitSynchronization1(Handle handle, s64 nano_seconds) {
// Create an event to wake the thread up after the specified nanosecond delay has passed
Kernel::GetCurrentThread()->WakeAfterDelay(nano_seconds);
- HLE::Reschedule(__func__);
-
// NOTE: output of this SVC will be set later depending on how the thread resumes
return RESULT_INVALID;
}
@@ -216,6 +216,8 @@ static ResultCode WaitSynchronizationN(s32* out, Handle* handles, s32 handle_cou
}
}
+ HLE::Reschedule(__func__);
+
// If thread should wait, then set its state to waiting and then reschedule...
if (wait_thread) {
@@ -229,8 +231,6 @@ static ResultCode WaitSynchronizationN(s32* out, Handle* handles, s32 handle_cou
// Create an event to wake the thread up after the specified nanosecond delay has passed
Kernel::GetCurrentThread()->WakeAfterDelay(nano_seconds);
- HLE::Reschedule(__func__);
-
// NOTE: output of this SVC will be set later depending on how the thread resumes
return RESULT_INVALID;
}