summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
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 9f7166ca4..a811db392 100644
--- a/src/core/hle/kernel/mutex.cpp
+++ b/src/core/hle/kernel/mutex.cpp
@@ -21,7 +21,7 @@ namespace Kernel {
21 */ 21 */
22static void ResumeWaitingThread(Mutex* mutex) { 22static void ResumeWaitingThread(Mutex* mutex) {
23 // Reset mutex lock thread handle, nothing is waiting 23 // Reset mutex lock thread handle, nothing is waiting
24 mutex->locked = false; 24 mutex->lock_count = 0;
25 mutex->holding_thread = nullptr; 25 mutex->holding_thread = nullptr;
26 26
27 // Find the next waiting thread for the mutex... 27 // Find the next waiting thread for the mutex...
@@ -44,8 +44,7 @@ Mutex::~Mutex() {}
44SharedPtr<Mutex> Mutex::Create(bool initial_locked, std::string name) { 44SharedPtr<Mutex> Mutex::Create(bool initial_locked, std::string name) {
45 SharedPtr<Mutex> mutex(new Mutex); 45 SharedPtr<Mutex> mutex(new Mutex);
46 46
47 mutex->initial_locked = initial_locked; 47 mutex->lock_count = 0;
48 mutex->locked = false;
49 mutex->name = std::move(name); 48 mutex->name = std::move(name);
50 mutex->holding_thread = nullptr; 49 mutex->holding_thread = nullptr;
51 50
@@ -57,7 +56,7 @@ SharedPtr<Mutex> Mutex::Create(bool initial_locked, std::string name) {
57} 56}
58 57
59bool Mutex::ShouldWait() { 58bool Mutex::ShouldWait() {
60 return locked && holding_thread != GetCurrentThread(); 59 return lock_count > 0 && holding_thread != GetCurrentThread();;
61} 60}
62 61
63void Mutex::Acquire() { 62void Mutex::Acquire() {
@@ -66,21 +65,27 @@ void Mutex::Acquire() {
66 65
67void Mutex::Acquire(SharedPtr<Thread> thread) { 66void Mutex::Acquire(SharedPtr<Thread> thread) {
68 _assert_msg_(Kernel, !ShouldWait(), "object unavailable!"); 67 _assert_msg_(Kernel, !ShouldWait(), "object unavailable!");
69 if (locked)
70 return;
71 68
72 locked = true; 69 // Actually "acquire" the mutex only if we don't already have it...
70 if (lock_count == 0) {
71 thread->held_mutexes.insert(this);
72 holding_thread = std::move(thread);
73 }
73 74
74 thread->held_mutexes.insert(this); 75 lock_count++;
75 holding_thread = std::move(thread);
76} 76}
77 77
78void Mutex::Release() { 78void Mutex::Release() {
79 if (!locked) 79 // Only release if the mutex is held...
80 return; 80 if (lock_count > 0) {
81 81 lock_count--;
82 holding_thread->held_mutexes.erase(this); 82
83 ResumeWaitingThread(this); 83 // Yield to the next thread only if we've fully released the mutex...
84 if (lock_count == 0) {
85 holding_thread->held_mutexes.erase(this);
86 ResumeWaitingThread(this);
87 }
88 }
84} 89}
85 90
86} // namespace 91} // namespace
diff --git a/src/core/hle/kernel/mutex.h b/src/core/hle/kernel/mutex.h
index 548403614..d6d5328be 100644
--- a/src/core/hle/kernel/mutex.h
+++ b/src/core/hle/kernel/mutex.h
@@ -30,8 +30,7 @@ public:
30 static const HandleType HANDLE_TYPE = HandleType::Mutex; 30 static const HandleType HANDLE_TYPE = HandleType::Mutex;
31 HandleType GetHandleType() const override { return HANDLE_TYPE; } 31 HandleType GetHandleType() const override { return HANDLE_TYPE; }
32 32
33 bool initial_locked; ///< Initial lock state when mutex was created 33 int lock_count; ///< Number of times the mutex has been acquired
34 bool locked; ///< Current locked state
35 std::string name; ///< Name of mutex (optional) 34 std::string name; ///< Name of mutex (optional)
36 SharedPtr<Thread> holding_thread; ///< Thread that has acquired the mutex 35 SharedPtr<Thread> holding_thread; ///< Thread that has acquired the mutex
37 36
diff --git a/src/core/hle/svc.cpp b/src/core/hle/svc.cpp
index 34a27917f..c4866fcce 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) {
144 LOG_TRACE(Kernel_SVC, "called handle=0x%08X(%s:%s), nanoseconds=%lld", handle, 144 LOG_TRACE(Kernel_SVC, "called handle=0x%08X(%s:%s), nanoseconds=%lld", handle,
145 object->GetTypeName().c_str(), object->GetName().c_str(), nano_seconds); 145 object->GetTypeName().c_str(), object->GetName().c_str(), nano_seconds);
146 146
147 HLE::Reschedule(__func__);
148
147 // Check for next thread to schedule 149 // Check for next thread to schedule
148 if (object->ShouldWait()) { 150 if (object->ShouldWait()) {
149 151
@@ -153,8 +155,6 @@ static ResultCode WaitSynchronization1(Handle handle, s64 nano_seconds) {
153 // Create an event to wake the thread up after the specified nanosecond delay has passed 155 // Create an event to wake the thread up after the specified nanosecond delay has passed
154 Kernel::GetCurrentThread()->WakeAfterDelay(nano_seconds); 156 Kernel::GetCurrentThread()->WakeAfterDelay(nano_seconds);
155 157
156 HLE::Reschedule(__func__);
157
158 // NOTE: output of this SVC will be set later depending on how the thread resumes 158 // NOTE: output of this SVC will be set later depending on how the thread resumes
159 return RESULT_INVALID; 159 return RESULT_INVALID;
160 } 160 }
@@ -216,6 +216,8 @@ static ResultCode WaitSynchronizationN(s32* out, Handle* handles, s32 handle_cou
216 } 216 }
217 } 217 }
218 218
219 HLE::Reschedule(__func__);
220
219 // If thread should wait, then set its state to waiting and then reschedule... 221 // If thread should wait, then set its state to waiting and then reschedule...
220 if (wait_thread) { 222 if (wait_thread) {
221 223
@@ -229,8 +231,6 @@ static ResultCode WaitSynchronizationN(s32* out, Handle* handles, s32 handle_cou
229 // Create an event to wake the thread up after the specified nanosecond delay has passed 231 // Create an event to wake the thread up after the specified nanosecond delay has passed
230 Kernel::GetCurrentThread()->WakeAfterDelay(nano_seconds); 232 Kernel::GetCurrentThread()->WakeAfterDelay(nano_seconds);
231 233
232 HLE::Reschedule(__func__);
233
234 // NOTE: output of this SVC will be set later depending on how the thread resumes 234 // NOTE: output of this SVC will be set later depending on how the thread resumes
235 return RESULT_INVALID; 235 return RESULT_INVALID;
236 } 236 }