From fab3dd98fec042e9c6a7ccfbd549ea2d6c41cfae Mon Sep 17 00:00:00 2001 From: Subv Date: Sat, 19 May 2018 16:56:33 -0500 Subject: SVC: Removed unused WaitSynchronization1 function --- src/core/hle/kernel/svc.cpp | 30 ------------------------------ 1 file changed, 30 deletions(-) (limited to 'src') diff --git a/src/core/hle/kernel/svc.cpp b/src/core/hle/kernel/svc.cpp index 1ae530c90..1eb1b8fbf 100644 --- a/src/core/hle/kernel/svc.cpp +++ b/src/core/hle/kernel/svc.cpp @@ -145,36 +145,6 @@ static bool DefaultThreadWakeupCallback(ThreadWakeupReason reason, SharedPtr object, Thread* thread, s64 nano_seconds = -1, - std::function 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(Handle* index, VAddr handles_address, u64 handle_count, s64 nano_seconds) { -- cgit v1.2.3 From c74f2555b61fb411b4c3a0a54afecfee7f58b63d Mon Sep 17 00:00:00 2001 From: Subv Date: Sat, 19 May 2018 16:57:44 -0500 Subject: Kernel/Threads: Reschedule the proper core when operating on that core's threads. --- src/core/hle/kernel/svc.cpp | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/core/hle/kernel/svc.cpp b/src/core/hle/kernel/svc.cpp index 1eb1b8fbf..810ef66aa 100644 --- a/src/core/hle/kernel/svc.cpp +++ b/src/core/hle/kernel/svc.cpp @@ -202,7 +202,7 @@ static ResultCode WaitSynchronization(Handle* index, VAddr handles_address, u64 thread->WakeAfterDelay(nano_seconds); thread->wakeup_callback = DefaultThreadWakeupCallback; - Core::System::GetInstance().PrepareReschedule(); + Core::System::GetInstance().CpuCore(thread->processor_id).PrepareReschedule(); return RESULT_TIMEOUT; } @@ -365,7 +365,7 @@ static ResultCode SetThreadPriority(Handle handle, u32 priority) { thread->SetPriority(priority); - Core::System::GetInstance().PrepareReschedule(); + Core::System::GetInstance().CpuCore(thread->processor_id).PrepareReschedule(); return RESULT_SUCCESS; } @@ -522,6 +522,7 @@ static ResultCode CreateThread(Handle* out_handle, VAddr entry_point, u64 arg, V *out_handle = thread->guest_handle; Core::System::GetInstance().PrepareReschedule(); + Core::System::GetInstance().CpuCore(thread->processor_id).PrepareReschedule(); NGLOG_TRACE(Kernel_SVC, "called entrypoint=0x{:08X} ({}), arg=0x{:08X}, stacktop=0x{:08X}, " @@ -540,7 +541,10 @@ static ResultCode StartThread(Handle thread_handle) { return ERR_INVALID_HANDLE; } + ASSERT(thread->status == THREADSTATUS_DORMANT); + thread->ResumeFromWait(); + Core::System::GetInstance().CpuCore(thread->processor_id).PrepareReschedule(); return RESULT_SUCCESS; } -- cgit v1.2.3 From 2a35a3625195087b4e7b0e420b91637e31ef3bca Mon Sep 17 00:00:00 2001 From: Subv Date: Sat, 19 May 2018 16:58:30 -0500 Subject: Kernel/SVC: Signal the highest priority threads first in svcSignalProcessWideKey. --- src/core/hle/kernel/svc.cpp | 119 +++++++++++++++++++++++++------------------- 1 file changed, 68 insertions(+), 51 deletions(-) (limited to 'src') diff --git a/src/core/hle/kernel/svc.cpp b/src/core/hle/kernel/svc.cpp index 810ef66aa..0811a16b8 100644 --- a/src/core/hle/kernel/svc.cpp +++ b/src/core/hle/kernel/svc.cpp @@ -608,61 +608,78 @@ static ResultCode SignalProcessWideKey(VAddr condition_variable_addr, s32 target NGLOG_TRACE(Kernel_SVC, "called, condition_variable_addr=0x{:X}, target=0x{:08X}", condition_variable_addr, target); - u32 processed = 0; + auto RetrieveWaitingThreads = + [](size_t core_index, std::vector>& waiting_threads, VAddr condvar_addr) { + const auto& scheduler = Core::System::GetInstance().Scheduler(core_index); + auto& thread_list = scheduler->GetThreadList(); + + for (auto& thread : thread_list) { + if (thread->condvar_wait_address == condvar_addr) + waiting_threads.push_back(thread); + } + }; + + // Retrieve a list of all threads that are waiting for this condition variable. + std::vector> waiting_threads; + RetrieveWaitingThreads(0, waiting_threads, condition_variable_addr); + RetrieveWaitingThreads(1, waiting_threads, condition_variable_addr); + RetrieveWaitingThreads(2, waiting_threads, condition_variable_addr); + RetrieveWaitingThreads(3, waiting_threads, condition_variable_addr); + // Sort them by priority, such that the highest priority ones come first. + std::sort(waiting_threads.begin(), waiting_threads.end(), + [](const SharedPtr& lhs, const SharedPtr& rhs) { + return lhs->current_priority < rhs->current_priority; + }); + + // Only process up to 'target' threads, unless 'target' is -1, in which case process + // them all. + size_t last = waiting_threads.size(); + if (target != -1) + last = target; + + // If there are no threads waiting on this condition variable, just exit + if (last > waiting_threads.size()) + return RESULT_SUCCESS; - auto signal_process_wide_key = [&](size_t core_index) { - const auto& scheduler = Core::System::GetInstance().Scheduler(core_index); - for (auto& thread : scheduler->GetThreadList()) { - if (thread->condvar_wait_address != condition_variable_addr) - continue; + for (size_t index = 0; index < last; ++index) { + auto& thread = waiting_threads[index]; - // Only process up to 'target' threads, unless 'target' is -1, in which case process - // them all. - if (target != -1 && processed >= target) - break; - - // If the mutex is not yet acquired, acquire it. - u32 mutex_val = Memory::Read32(thread->mutex_wait_address); - - if (mutex_val == 0) { - // We were able to acquire the mutex, resume this thread. - Memory::Write32(thread->mutex_wait_address, thread->wait_handle); - ASSERT(thread->status == THREADSTATUS_WAIT_MUTEX); - thread->ResumeFromWait(); - - auto lock_owner = thread->lock_owner; - if (lock_owner) - lock_owner->RemoveMutexWaiter(thread); - - thread->lock_owner = nullptr; - thread->mutex_wait_address = 0; - thread->condvar_wait_address = 0; - thread->wait_handle = 0; - } else { - // Couldn't acquire the mutex, block the thread. - Handle owner_handle = static_cast(mutex_val & Mutex::MutexOwnerMask); - auto owner = g_handle_table.Get(owner_handle); - ASSERT(owner); - ASSERT(thread->status != THREADSTATUS_RUNNING); - thread->status = THREADSTATUS_WAIT_MUTEX; - thread->wakeup_callback = nullptr; - - // Signal that the mutex now has a waiting thread. - Memory::Write32(thread->mutex_wait_address, mutex_val | Mutex::MutexHasWaitersFlag); - - owner->AddMutexWaiter(thread); - - Core::System::GetInstance().CpuCore(thread->processor_id).PrepareReschedule(); - } + ASSERT(thread->condvar_wait_address == condition_variable_addr); - ++processed; - } - }; + // If the mutex is not yet acquired, acquire it. + u32 mutex_val = Memory::Read32(thread->mutex_wait_address); + + if (mutex_val == 0) { + // We were able to acquire the mutex, resume this thread. + Memory::Write32(thread->mutex_wait_address, thread->wait_handle); + ASSERT(thread->status == THREADSTATUS_WAIT_MUTEX); + thread->ResumeFromWait(); - signal_process_wide_key(0); - signal_process_wide_key(1); - signal_process_wide_key(2); - signal_process_wide_key(3); + auto lock_owner = thread->lock_owner; + if (lock_owner) + lock_owner->RemoveMutexWaiter(thread); + + thread->lock_owner = nullptr; + thread->mutex_wait_address = 0; + thread->condvar_wait_address = 0; + thread->wait_handle = 0; + } else { + // Couldn't acquire the mutex, block the thread. + Handle owner_handle = static_cast(mutex_val & Mutex::MutexOwnerMask); + auto owner = g_handle_table.Get(owner_handle); + ASSERT(owner); + ASSERT(thread->status != THREADSTATUS_RUNNING); + thread->status = THREADSTATUS_WAIT_MUTEX; + thread->wakeup_callback = nullptr; + + // Signal that the mutex now has a waiting thread. + Memory::Write32(thread->mutex_wait_address, mutex_val | Mutex::MutexHasWaitersFlag); + + owner->AddMutexWaiter(thread); + + Core::System::GetInstance().CpuCore(thread->processor_id).PrepareReschedule(); + } + } return RESULT_SUCCESS; } -- cgit v1.2.3