VERY experimental Nintendo Switch 2 emulator
C++
Kernel: Separate WaitSynchronization into Wait and Acquire methods.
bunneiaa01c57ae9d7
diff --git a/src/core/hle/kernel/event.cpp b/src/core/hle/kernel/event.cppindex 4173a980bd..41e1bd6c52 100644--- a/src/core/hle/kernel/event.cpp+++ b/src/core/hle/kernel/event.cpp@@ -28,7 +28,7 @@ public:bool signaled; ///< Whether the event has already been signaledstd::string name; ///< Name of event (optional)- ResultVal<bool> WaitSynchronization(unsigned index) override {+ ResultVal<bool> Wait(unsigned index) override {bool wait = !signaled;if (wait) {AddWaitingThread(GetCurrentThread());@@ -36,6 +36,10 @@ public:}return MakeResult<bool>(wait);}++ ResultVal<bool> Acquire() override {+ return MakeResult<bool>(true);+ }};ResultCode SignalEvent(const Handle handle) {diff --git a/src/core/hle/kernel/kernel.h b/src/core/hle/kernel/kernel.hindex af4e2f4439..d98fd0389e 100644--- a/src/core/hle/kernel/kernel.h+++ b/src/core/hle/kernel/kernel.h@@ -65,11 +65,20 @@ public:virtual Kernel::HandleType GetHandleType() const = 0;/**- * Wait for kernel object to synchronize.+ * Wait the current thread for kernel object to synchronize.* @param index Index of wait object (only applies to WaitSynchronizationN)* @return True if the current thread should wait as a result of the wait*/- virtual ResultVal<bool> WaitSynchronization(unsigned index=0) {+ virtual ResultVal<bool> Wait(unsigned index = 0) {+ LOG_ERROR(Kernel, "(UNIMPLEMENTED)");+ return UnimplementedFunction(ErrorModule::Kernel);+ }++ /**+ * Acquire/lock the kernel object if it is available+ * @return True if we were able to acquire the kernel object, otherwise false+ */+ virtual ResultVal<bool> Acquire() {LOG_ERROR(Kernel, "(UNIMPLEMENTED)");return UnimplementedFunction(ErrorModule::Kernel);}diff --git a/src/core/hle/kernel/mutex.cpp b/src/core/hle/kernel/mutex.cppindex 78063b8f18..37e7be4e77 100644--- a/src/core/hle/kernel/mutex.cpp+++ b/src/core/hle/kernel/mutex.cpp@@ -26,7 +26,8 @@ public:Handle lock_thread; ///< Handle to thread that currently has mutexstd::string name; ///< Name of mutex (optional)- ResultVal<bool> WaitSynchronization(unsigned index) override;+ ResultVal<bool> Wait(unsigned index) override;+ ResultVal<bool> Acquire() override;};////////////////////////////////////////////////////////////////////////////////////////////////////@@ -155,17 +156,25 @@ Handle CreateMutex(bool initial_locked, const std::string& name) {return handle;}-ResultVal<bool> Mutex::WaitSynchronization(unsigned index) {- bool wait = locked;+ResultVal<bool> Mutex::Wait(unsigned index) {if (locked) {AddWaitingThread(GetCurrentThread());Kernel::WaitCurrentThread_WaitSynchronization(WAITTYPE_MUTEX, this, index);- } else {+ }++ return MakeResult<bool>(locked);+}++ResultVal<bool> Mutex::Acquire() {+ bool res = false;++ if (!locked) {// Lock the mutex when the first thread accesses itlocked = true;+ res = true;MutexAcquireLock(this);}- return MakeResult<bool>(wait);+ return MakeResult<bool>(res);}} // namespacediff --git a/src/core/hle/kernel/semaphore.cpp b/src/core/hle/kernel/semaphore.cppindex 288928441e..6464b25809 100644--- a/src/core/hle/kernel/semaphore.cpp+++ b/src/core/hle/kernel/semaphore.cpp@@ -32,18 +32,27 @@ public:return available_count > 0;}- ResultVal<bool> WaitSynchronization(unsigned index) override {+ ResultVal<bool> Wait(unsigned index) override {bool wait = !IsAvailable();if (wait) {Kernel::WaitCurrentThread_WaitSynchronization(WAITTYPE_SEMA, this, index);AddWaitingThread(GetCurrentThread());- } else {- --available_count;}return MakeResult<bool>(wait);}++ ResultVal<bool> Acquire() override {+ bool res = false;++ if (IsAvailable()) {+ --available_count;+ res = true;+ }++ return MakeResult<bool>(res);+ }};////////////////////////////////////////////////////////////////////////////////////////////////////diff --git a/src/core/hle/kernel/thread.cpp b/src/core/hle/kernel/thread.cppindex 0c9ecc091a..6b0bdebb53 100644--- a/src/core/hle/kernel/thread.cpp+++ b/src/core/hle/kernel/thread.cpp@@ -22,7 +22,7 @@namespace Kernel {-ResultVal<bool> Thread::WaitSynchronization(unsigned index) {+ResultVal<bool> Thread::Wait(unsigned index) {const bool wait = status != THREADSTATUS_DORMANT;if (wait) {AddWaitingThread(GetCurrentThread());@@ -32,6 +32,10 @@ ResultVal<bool> Thread::WaitSynchronization(unsigned index) {return MakeResult<bool>(wait);}+ResultVal<bool> Thread::Acquire() {+ return MakeResult<bool>(true);+}+// Lists all thread ids that aren't deleted/etc.static std::vector<SharedPtr<Thread>> thread_list;diff --git a/src/core/hle/kernel/thread.h b/src/core/hle/kernel/thread.hindex f3dc4eec05..9faf89c15e 100644--- a/src/core/hle/kernel/thread.h+++ b/src/core/hle/kernel/thread.h@@ -70,7 +70,8 @@ public:inline bool IsSuspended() const { return (status & THREADSTATUS_SUSPEND) != 0; }inline bool IsIdle() const { return idle; }- ResultVal<bool> WaitSynchronization(unsigned index) override;+ ResultVal<bool> Wait(unsigned index) override;+ ResultVal<bool> Acquire() override;s32 GetPriority() const { return current_priority; }void SetPriority(s32 priority);diff --git a/src/core/hle/kernel/timer.cpp b/src/core/hle/kernel/timer.cppindex c97ae6c5cc..6497bb3493 100644--- a/src/core/hle/kernel/timer.cpp+++ b/src/core/hle/kernel/timer.cpp@@ -29,7 +29,7 @@ public:u64 initial_delay; ///< The delay until the timer fires for the first timeu64 interval_delay; ///< The delay until the timer fires after the first time- ResultVal<bool> WaitSynchronization(unsigned index) override {+ ResultVal<bool> Wait(unsigned index) override {bool wait = !signaled;if (wait) {AddWaitingThread(GetCurrentThread());@@ -37,6 +37,10 @@ public:}return MakeResult<bool>(wait);}++ ResultVal<bool> Acquire() override {+ return MakeResult<bool>(true);+ }};/**diff --git a/src/core/hle/svc.cpp b/src/core/hle/svc.cppindex 23885f1291..a27aa6269a 100644--- a/src/core/hle/svc.cpp+++ b/src/core/hle/svc.cpp@@ -127,7 +127,7 @@ static Result 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);- ResultVal<bool> wait = object->WaitSynchronization();+ ResultVal<bool> wait = object->Wait();// Check for next thread to scheduleif (wait.Succeeded() && *wait) {@@ -137,6 +137,8 @@ static Result WaitSynchronization1(Handle handle, s64 nano_seconds) {Kernel::GetCurrentThread()->SetWaitAll(false);HLE::Reschedule(__func__);+ } else {+ object->Acquire();}return wait.Code().raw;@@ -163,15 +165,14 @@ static Result WaitSynchronizationN(s32* out, Handle* handles, s32 handle_count,if (object == nullptr)return InvalidHandle(ErrorModule::Kernel).raw;- ResultVal<bool> wait = object->WaitSynchronization(handle_index);+ ResultVal<bool> wait = object->Wait(handle_index);wait_thread = (wait.Succeeded() && *wait);// If this object waited and we are waiting on all objects to synchronize- if (wait_thread && wait_all) {+ if (wait_thread && wait_all)// Enforce later on that this thread does not continuewait_all_succeeded = true;- }// If this object synchronized and we are not waiting on all objects to synchronizeif (!wait_thread && !wait_all)