From 8634b8cb83755b6c6554faa11c0e488d2ad21f90 Mon Sep 17 00:00:00 2001 From: Subv Date: Sat, 3 Dec 2016 22:38:14 -0500 Subject: Threading: Reworked the way our scheduler works. Threads will now be awakened when the objects they're waiting on are signaled, instead of repeating the WaitSynchronization call every now and then. The scheduler is now called once after every SVC call, and once after a thread is awakened from sleep by its timeout callback. This new implementation is based off reverse-engineering of the real kernel. See https://gist.github.com/Subv/02f29bd9f1e5deb7aceea1e8f019c8f4 for a more detailed description of how the real kernel handles rescheduling. --- src/core/hle/kernel/thread.h | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) (limited to 'src/core/hle/kernel/thread.h') diff --git a/src/core/hle/kernel/thread.h b/src/core/hle/kernel/thread.h index e0ffcea8a..63b97b74f 100644 --- a/src/core/hle/kernel/thread.h +++ b/src/core/hle/kernel/thread.h @@ -5,6 +5,7 @@ #pragma once #include +#include #include #include #include "common/common_types.h" @@ -124,6 +125,16 @@ public: */ void SetWaitSynchronizationOutput(s32 output); + /** + * Retrieves the index that this particular object occupies in the list of objects + * that the thread passed to WaitSynchronizationN. + * It is used to set the output value of WaitSynchronizationN when the thread is awakened. + * @param object Object to query the index of. + */ + s32 GetWaitObjectIndex(WaitObject* object) { + return wait_objects_index[object->GetObjectId()]; + } + /** * Stops a thread, invalidating it from further use */ @@ -154,16 +165,16 @@ public: VAddr tls_address; ///< Virtual address of the Thread Local Storage of the thread - bool waitsynch_waited; ///< Set to true if the last svcWaitSynch call caused the thread to wait - /// Mutexes currently held by this thread, which will be released when it exits. boost::container::flat_set> held_mutexes; SharedPtr owner_process; ///< Process that owns this thread std::vector> wait_objects; ///< Objects that the thread is waiting on + std::unordered_map 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 - bool wait_all; ///< True if the thread is waiting on all objects before resuming - bool wait_set_output; ///< True if the output parameter should be set on thread wakeup + + bool wait_set_output; ///< True if the WaitSynchronizationN output parameter should be set on thread wakeup std::string name; @@ -215,10 +226,9 @@ void WaitCurrentThread_Sleep(); * @param wait_objects Kernel objects that we are waiting on * @param wait_set_output If true, set the output parameter on thread wakeup (for * WaitSynchronizationN only) - * @param wait_all If true, wait on all objects before resuming (for WaitSynchronizationN only) */ void WaitCurrentThread_WaitSynchronization(std::vector> wait_objects, - bool wait_set_output, bool wait_all); + bool wait_set_output); /** * Waits the current thread from an ArbitrateAddress call -- cgit v1.2.3 From bdad00c73f46106ba78995bdde1b50349e940b09 Mon Sep 17 00:00:00 2001 From: Subv Date: Sun, 4 Dec 2016 09:58:36 -0500 Subject: Threading: Added some utility functions and const correctness. --- src/core/hle/kernel/thread.h | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) (limited to 'src/core/hle/kernel/thread.h') diff --git a/src/core/hle/kernel/thread.h b/src/core/hle/kernel/thread.h index 63b97b74f..1b29fb3a3 100644 --- a/src/core/hle/kernel/thread.h +++ b/src/core/hle/kernel/thread.h @@ -131,8 +131,8 @@ public: * It is used to set the output value of WaitSynchronizationN when the thread is awakened. * @param object Object to query the index of. */ - s32 GetWaitObjectIndex(WaitObject* object) { - return wait_objects_index[object->GetObjectId()]; + s32 GetWaitObjectIndex(const WaitObject* object) const { + return wait_objects_index.at(object->GetObjectId()); } /** @@ -148,6 +148,15 @@ public: 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; u32 thread_id; @@ -169,7 +178,11 @@ public: boost::container::flat_set> held_mutexes; SharedPtr owner_process; ///< Process that owns this thread - std::vector> 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> wait_objects; + std::unordered_map 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 -- cgit v1.2.3 From 17b29d8865ea4d96c18f7e1671bd6d0f01eab95f Mon Sep 17 00:00:00 2001 From: Subv Date: Thu, 8 Dec 2016 10:34:53 -0500 Subject: WaitSynch: Removed unused variables and reduced SharedPtr copies. Define a variable with the value of the sync timeout error code. Use a boost::flat_map instead of an unordered_map to hold the equivalence of objects and wait indices in a WaitSynchN call. --- src/core/hle/kernel/thread.h | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'src/core/hle/kernel/thread.h') diff --git a/src/core/hle/kernel/thread.h b/src/core/hle/kernel/thread.h index 1b29fb3a3..4c254cb9d 100644 --- a/src/core/hle/kernel/thread.h +++ b/src/core/hle/kernel/thread.h @@ -7,6 +7,7 @@ #include #include #include +#include #include #include "common/common_types.h" #include "core/core.h" @@ -153,7 +154,7 @@ public: * its wait list to become ready, as a result of a WaitSynchronizationN call * with wait_all = true, or a ReplyAndReceive call. */ - bool IsWaitingAll() const { + bool IsSleepingOnWaitAll() const { return !wait_objects.empty(); } @@ -183,7 +184,7 @@ public: /// This is only populated when the thread should wait for all the objects to become ready. std::vector> wait_objects; - std::unordered_map wait_objects_index; ///< Mapping of Object ids to their position in the last waitlist that this object waited on. + boost::container::flat_map 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 -- cgit v1.2.3 From 5b1edc6ae70972d4a11eee1f1ff8fdff2122b5a2 Mon Sep 17 00:00:00 2001 From: Subv Date: Wed, 14 Dec 2016 12:13:02 -0500 Subject: Fixed the codestyle to match our clang-format rules. --- src/core/hle/kernel/thread.h | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) (limited to 'src/core/hle/kernel/thread.h') diff --git a/src/core/hle/kernel/thread.h b/src/core/hle/kernel/thread.h index 4c254cb9d..238359fc5 100644 --- a/src/core/hle/kernel/thread.h +++ b/src/core/hle/kernel/thread.h @@ -178,17 +178,19 @@ public: /// Mutexes currently held by this thread, which will be released when it exits. boost::container::flat_set> held_mutexes; - SharedPtr owner_process; ///< Process that owns this thread + SharedPtr owner_process; ///< Process that owns this thread /// 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> wait_objects; - boost::container::flat_map wait_objects_index; ///< Mapping of Object ids to their position in the last waitlist that this object waited on. + /// Mapping of Object ids to their position in the last waitlist that this object waited on. + boost::container::flat_map wait_objects_index; - 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 - bool wait_set_output; ///< True if the WaitSynchronizationN output parameter should be set on thread wakeup + /// True if the WaitSynchronizationN output parameter should be set on thread wakeup. + bool wait_set_output; std::string name; -- cgit v1.2.3