2
1
Fork 0
mirror of https://github.com/yuzu-emu/yuzu.git synced 2024-07-04 23:31:19 +01:00

Threading: Added some utility functions and const correctness.

This commit is contained in:
Subv 2016-12-04 09:58:36 -05:00
parent 8634b8cb83
commit bdad00c73f
4 changed files with 36 additions and 16 deletions

View file

@ -230,7 +230,7 @@ std::vector<std::unique_ptr<WaitTreeItem>> WaitTreeThread::GetChildren() const {
list.push_back(std::make_unique<WaitTreeMutexList>(thread.held_mutexes)); list.push_back(std::make_unique<WaitTreeMutexList>(thread.held_mutexes));
} }
if (thread.status == THREADSTATUS_WAIT_SYNCH) { if (thread.status == THREADSTATUS_WAIT_SYNCH) {
list.push_back(std::make_unique<WaitTreeObjectList>(thread.wait_objects, !thread.wait_objects.empty())); list.push_back(std::make_unique<WaitTreeObjectList>(thread.wait_objects, thread.IsWaitingAll()));
} }
return list; return list;

View file

@ -33,7 +33,7 @@ void WaitObject::RemoveWaitingThread(Thread* thread) {
SharedPtr<Thread> WaitObject::GetHighestPriorityReadyThread() { SharedPtr<Thread> WaitObject::GetHighestPriorityReadyThread() {
// Remove the threads that are ready or already running from our waitlist // Remove the threads that are ready or already running from our waitlist
waiting_threads.erase(std::remove_if(waiting_threads.begin(), waiting_threads.end(), [](SharedPtr<Thread> thread) -> bool { waiting_threads.erase(std::remove_if(waiting_threads.begin(), waiting_threads.end(), [](const SharedPtr<Thread>& thread) -> bool {
return thread->status == THREADSTATUS_RUNNING || thread->status == THREADSTATUS_READY; return thread->status == THREADSTATUS_RUNNING || thread->status == THREADSTATUS_READY;
}), waiting_threads.end()); }), waiting_threads.end());
@ -42,12 +42,11 @@ SharedPtr<Thread> WaitObject::GetHighestPriorityReadyThread() {
auto candidate_threads = waiting_threads; auto candidate_threads = waiting_threads;
// Eliminate all threads that are waiting on more than one object, and not all of them are ready // Eliminate all threads that are waiting on more than one object, and not all of said objects are ready
candidate_threads.erase(std::remove_if(candidate_threads.begin(), candidate_threads.end(), [](SharedPtr<Thread> thread) -> bool { candidate_threads.erase(std::remove_if(candidate_threads.begin(), candidate_threads.end(), [](const SharedPtr<Thread>& thread) -> bool {
for (auto object : thread->wait_objects) return std::any_of(thread->wait_objects.begin(), thread->wait_objects.end(), [](const SharedPtr<WaitObject>& object) -> bool {
if (object->ShouldWait()) return object->ShouldWait();
return true; });
return false;
}), candidate_threads.end()); }), candidate_threads.end());
// Return the thread with the lowest priority value (The one with the highest priority) // Return the thread with the lowest priority value (The one with the highest priority)

View file

@ -131,8 +131,8 @@ public:
* It is used to set the output value of WaitSynchronizationN when the thread is awakened. * It is used to set the output value of WaitSynchronizationN when the thread is awakened.
* @param object Object to query the index of. * @param object Object to query the index of.
*/ */
s32 GetWaitObjectIndex(WaitObject* object) { s32 GetWaitObjectIndex(const WaitObject* object) const {
return wait_objects_index[object->GetObjectId()]; return wait_objects_index.at(object->GetObjectId());
} }
/** /**
@ -148,6 +148,15 @@ public:
return tls_address; return tls_address;
} }
/**
* Returns whether this thread is waiting for all the objects in
* its wait list to become ready, as a result of a WaitSynchronizationN call
* with wait_all = true, or a ReplyAndReceive call.
*/
bool IsWaitingAll() const {
return !wait_objects.empty();
}
Core::ThreadContext context; Core::ThreadContext context;
u32 thread_id; u32 thread_id;
@ -169,7 +178,11 @@ public:
boost::container::flat_set<SharedPtr<Mutex>> held_mutexes; boost::container::flat_set<SharedPtr<Mutex>> held_mutexes;
SharedPtr<Process> owner_process; ///< Process that owns this thread SharedPtr<Process> owner_process; ///< Process that owns this thread
std::vector<SharedPtr<WaitObject>> wait_objects; ///< Objects that the thread is waiting on
/// Objects that the thread is waiting on.
/// This is only populated when the thread should wait for all the objects to become ready.
std::vector<SharedPtr<WaitObject>> wait_objects;
std::unordered_map<int, s32> wait_objects_index; ///< Mapping of Object ids to their position in the last waitlist that this object waited on. std::unordered_map<int, s32> wait_objects_index; ///< Mapping of Object ids to their position in the last waitlist that this object waited on.
VAddr wait_address; ///< If waiting on an AddressArbiter, this is the arbitration address VAddr wait_address; ///< If waiting on an AddressArbiter, this is the arbitration address

View file

@ -257,18 +257,21 @@ static ResultCode WaitSynchronization1(Handle handle, s64 nano_seconds) {
if (object->ShouldWait()) { if (object->ShouldWait()) {
if (nano_seconds == 0) if (nano_seconds == 0) {
return ResultCode(ErrorDescription::Timeout, ErrorModule::OS, return ResultCode(ErrorDescription::Timeout, ErrorModule::OS,
ErrorSummary::StatusChanged, ErrorSummary::StatusChanged,
ErrorLevel::Info); ErrorLevel::Info);
}
object->AddWaitingThread(thread); object->AddWaitingThread(thread);
// TODO(Subv): Perform things like update the mutex lock owner's priority to prevent priority inversion.
// Currently this is done in Mutex::ShouldWait, but it should be moved to a function that is called from here.
thread->status = THREADSTATUS_WAIT_SYNCH; thread->status = THREADSTATUS_WAIT_SYNCH;
// Create an event to wake the thread up after the specified nanosecond delay has passed // Create an event to wake the thread up after the specified nanosecond delay has passed
thread->WakeAfterDelay(nano_seconds); thread->WakeAfterDelay(nano_seconds);
// Note: The output of this SVC will be set to RESULT_SUCCESS if the thread resumes due to a signal in one of its wait objects. // Note: The output of this SVC will be set to RESULT_SUCCESS if the thread resumes due to a signal in its wait objects.
// Otherwise we retain the default value of timeout. // Otherwise we retain the default value of timeout.
return ResultCode(ErrorDescription::Timeout, ErrorModule::OS, return ResultCode(ErrorDescription::Timeout, ErrorModule::OS,
ErrorSummary::StatusChanged, ErrorSummary::StatusChanged,
@ -312,7 +315,9 @@ static ResultCode WaitSynchronizationN(s32* out, Handle* handles, s32 handle_cou
objects[i] = object; objects[i] = object;
} }
// Clear the mapping of wait object indices // Clear the mapping of wait object indices.
// We don't want any lingering state in this map.
// It will be repopulated later in the wait_all = false case.
thread->wait_objects_index.clear(); thread->wait_objects_index.clear();
if (!wait_all) { if (!wait_all) {
@ -345,12 +350,13 @@ static ResultCode WaitSynchronizationN(s32* out, Handle* handles, s32 handle_cou
thread->wait_objects.clear(); thread->wait_objects.clear();
// Add the thread to each of the objects' waiting threads. // Add the thread to each of the objects' waiting threads.
for (int i = 0; i < objects.size(); ++i) { for (size_t i = 0; i < objects.size(); ++i) {
ObjectPtr object = objects[i]; ObjectPtr object = objects[i];
// Set the index of this object in the mapping of Objects -> index for this thread. // Set the index of this object in the mapping of Objects -> index for this thread.
thread->wait_objects_index[object->GetObjectId()] = i; thread->wait_objects_index[object->GetObjectId()] = static_cast<int>(i);
object->AddWaitingThread(thread); object->AddWaitingThread(thread);
// TODO(Subv): Perform things like update the mutex lock owner's priority to prevent priority inversion. // TODO(Subv): Perform things like update the mutex lock owner's priority to prevent priority inversion.
// Currently this is done in Mutex::ShouldWait, but it should be moved to a function that is called from here.
} }
// Note: If no handles and no timeout were given, then the thread will deadlock, this is consistent with hardware behavior. // Note: If no handles and no timeout were given, then the thread will deadlock, this is consistent with hardware behavior.
@ -396,6 +402,7 @@ static ResultCode WaitSynchronizationN(s32* out, Handle* handles, s32 handle_cou
for (auto object : objects) { for (auto object : objects) {
object->AddWaitingThread(thread); object->AddWaitingThread(thread);
// TODO(Subv): Perform things like update the mutex lock owner's priority to prevent priority inversion. // TODO(Subv): Perform things like update the mutex lock owner's priority to prevent priority inversion.
// Currently this is done in Mutex::ShouldWait, but it should be moved to a function that is called from here.
} }
// Create an event to wake the thread up after the specified nanosecond delay has passed // Create an event to wake the thread up after the specified nanosecond delay has passed
@ -1172,6 +1179,7 @@ void CallSVC(u32 immediate) {
if (info) { if (info) {
if (info->func) { if (info->func) {
info->func(); info->func();
// TODO(Subv): Not all service functions should cause a reschedule in all cases.
HLE::Reschedule(__func__); HLE::Reschedule(__func__);
} else { } else {
LOG_ERROR(Kernel_SVC, "unimplemented SVC function %s(..)", info->name); LOG_ERROR(Kernel_SVC, "unimplemented SVC function %s(..)", info->name);