diff options
author | bunnei <bunneidev@gmail.com> | 2018-01-06 20:19:28 +0100 |
---|---|---|
committer | bunnei <bunneidev@gmail.com> | 2018-01-06 20:19:28 +0100 |
commit | 57f42e588742dd5429d357ab9a12251abc557708 (patch) | |
tree | 5bc6f62b88c91f594bb5546ad7d43536bf90860d /src/core/hle | |
parent | lm: Improve Log() to format a useful string. (diff) | |
download | yuzu-57f42e588742dd5429d357ab9a12251abc557708.tar yuzu-57f42e588742dd5429d357ab9a12251abc557708.tar.gz yuzu-57f42e588742dd5429d357ab9a12251abc557708.tar.bz2 yuzu-57f42e588742dd5429d357ab9a12251abc557708.tar.lz yuzu-57f42e588742dd5429d357ab9a12251abc557708.tar.xz yuzu-57f42e588742dd5429d357ab9a12251abc557708.tar.zst yuzu-57f42e588742dd5429d357ab9a12251abc557708.zip |
Diffstat (limited to 'src/core/hle')
-rw-r--r-- | src/core/hle/kernel/svc.cpp | 58 |
1 files changed, 45 insertions, 13 deletions
diff --git a/src/core/hle/kernel/svc.cpp b/src/core/hle/kernel/svc.cpp index be9743d9f..a24641399 100644 --- a/src/core/hle/kernel/svc.cpp +++ b/src/core/hle/kernel/svc.cpp @@ -118,6 +118,50 @@ static ResultCode GetProcessId(u32* process_id, Handle process_handle) { return RESULT_SUCCESS; } +/// Default thread wakeup callback for WaitSynchronization +static void DefaultThreadWakeupCallback(ThreadWakeupReason reason, SharedPtr<Thread> thread, + SharedPtr<WaitObject> object) { + ASSERT(thread->status == THREADSTATUS_WAIT_SYNCH_ANY); + + if (reason == ThreadWakeupReason::Timeout) { + thread->SetWaitSynchronizationResult(RESULT_TIMEOUT); + return; + } + + ASSERT(reason == ThreadWakeupReason::Signal); + thread->SetWaitSynchronizationResult(RESULT_SUCCESS); +}; + +/// Wait for a kernel object to synchronize, timeout after the specified nanoseconds +static ResultCode WaitSynchronization1( + SharedPtr<WaitObject> object, Thread* thread, s64 nano_seconds = -1, + std::function<Thread::WakeupCallback> wakeup_callback = DefaultThreadWakeupCallback) { + + if (!object) { + return ERR_INVALID_HANDLE; + } + + if (object->ShouldWait(thread)) { + if (nano_seconds == 0) { + return RESULT_TIMEOUT; + } + + thread->wait_objects = {object}; + object->AddWaitingThread(thread); + thread->status = THREADSTATUS_WAIT_SYNCH_ANY; + + // Create an event to wake the thread up after the specified nanosecond delay has passed + thread->WakeAfterDelay(nano_seconds); + thread->wakeup_callback = wakeup_callback; + + Core::System::GetInstance().PrepareReschedule(); + } else { + object->Acquire(thread); + } + + return RESULT_SUCCESS; +} + /// Wait for the given handles to synchronize, timeout after the specified nanoseconds static ResultCode WaitSynchronization(VAddr handles_address, u64 handle_count, s64 nano_seconds) { LOG_WARNING(Kernel_SVC, @@ -147,19 +191,7 @@ static ResultCode LockMutex(Handle holding_thread_handle, VAddr mutex_addr, mutex->name = Common::StringFromFormat("mutex-%llx", mutex_addr); } - if (mutex->ShouldWait(requesting_thread.get())) { - // If we cannot lock the mutex, then put the thread too sleep and trigger a reschedule - requesting_thread->wait_objects = {mutex}; - mutex->AddWaitingThread(requesting_thread); - requesting_thread->status = THREADSTATUS_WAIT_SYNCH_ANY; - - Core::System::GetInstance().PrepareReschedule(); - } else { - // The mutex is available, lock it - mutex->Acquire(requesting_thread.get()); - } - - return RESULT_SUCCESS; + return WaitSynchronization1(mutex, requesting_thread.get()); } /// Unlock a mutex |