From e5a9f1c64483e01b7856c581ae5685d0c5ad88dc Mon Sep 17 00:00:00 2001 From: bunnei Date: Sun, 18 Jan 2015 13:25:51 -0500 Subject: Kernel: Get rid of WaitTypes and simplify lots of code, removing hacks. --- src/core/hle/kernel/event.cpp | 9 ++---- src/core/hle/kernel/kernel.cpp | 6 ++-- src/core/hle/kernel/kernel.h | 5 ++- src/core/hle/kernel/mutex.cpp | 9 ++---- src/core/hle/kernel/semaphore.cpp | 11 ++----- src/core/hle/kernel/thread.cpp | 65 ++++++++++++--------------------------- src/core/hle/kernel/thread.h | 41 ++++++------------------ src/core/hle/kernel/timer.cpp | 9 ++---- 8 files changed, 43 insertions(+), 112 deletions(-) (limited to 'src/core/hle/kernel') diff --git a/src/core/hle/kernel/event.cpp b/src/core/hle/kernel/event.cpp index ae9b06b84..399730cb1 100644 --- a/src/core/hle/kernel/event.cpp +++ b/src/core/hle/kernel/event.cpp @@ -28,13 +28,8 @@ public: bool signaled; ///< Whether the event has already been signaled std::string name; ///< Name of event (optional) - ResultVal Wait(bool wait_thread) override { - bool wait = !signaled; - if (wait && wait_thread) { - AddWaitingThread(GetCurrentThread()); - Kernel::WaitCurrentThread_WaitSynchronization(WAITTYPE_EVENT, this); - } - return MakeResult(wait); + ResultVal Wait() override { + return MakeResult(!signaled); } ResultVal Acquire() override { diff --git a/src/core/hle/kernel/kernel.cpp b/src/core/hle/kernel/kernel.cpp index be3495412..57e0e8df7 100644 --- a/src/core/hle/kernel/kernel.cpp +++ b/src/core/hle/kernel/kernel.cpp @@ -42,13 +42,15 @@ Thread* WaitObject::ReleaseNextThread() { return next_thread.get(); } -void WaitObject::ReleaseAllWaitingThreads() { +void WaitObject::WakeupAllWaitingThreads() { auto waiting_threads_copy = waiting_threads; + // We use a copy because ReleaseWaitObject will remove the thread from this object's + // waiting_threads list for (auto thread : waiting_threads_copy) thread->ReleaseWaitObject(this); - waiting_threads.clear(); + _assert_msg_(Kernel, waiting_threads.empty(), "failed to awaken all waiting threads!"); } HandleTable::HandleTable() { diff --git a/src/core/hle/kernel/kernel.h b/src/core/hle/kernel/kernel.h index cfaf0c901..5bf9a2bfc 100644 --- a/src/core/hle/kernel/kernel.h +++ b/src/core/hle/kernel/kernel.h @@ -65,11 +65,10 @@ public: virtual Kernel::HandleType GetHandleType() const = 0; /** - * Check if this object is available, (optionally) wait the current thread if not - * @param wait_thread If true, wait the current thread if this object is unavailable + * Check if this object is available * @return True if the current thread should wait due to this object being unavailable */ - virtual ResultVal Wait(bool wait_thread) { + virtual ResultVal Wait() { LOG_ERROR(Kernel, "(UNIMPLEMENTED)"); return UnimplementedFunction(ErrorModule::Kernel); } diff --git a/src/core/hle/kernel/mutex.cpp b/src/core/hle/kernel/mutex.cpp index f97c69a78..4a1eaca37 100644 --- a/src/core/hle/kernel/mutex.cpp +++ b/src/core/hle/kernel/mutex.cpp @@ -26,7 +26,7 @@ public: Handle lock_thread; ///< Handle to thread that currently has mutex std::string name; ///< Name of mutex (optional) - ResultVal Wait(bool wait_thread) override; + ResultVal Wait() override; ResultVal Acquire() override; }; @@ -156,12 +156,7 @@ Handle CreateMutex(bool initial_locked, const std::string& name) { return handle; } -ResultVal Mutex::Wait(bool wait_thread) { - if (locked && wait_thread) { - AddWaitingThread(GetCurrentThread()); - Kernel::WaitCurrentThread_WaitSynchronization(WAITTYPE_MUTEX, this); - } - +ResultVal Mutex::Wait() { return MakeResult(locked); } diff --git a/src/core/hle/kernel/semaphore.cpp b/src/core/hle/kernel/semaphore.cpp index 42b5cf704..6ccdb2a8f 100644 --- a/src/core/hle/kernel/semaphore.cpp +++ b/src/core/hle/kernel/semaphore.cpp @@ -32,15 +32,8 @@ public: return available_count > 0; } - ResultVal Wait(bool wait_thread) override { - bool wait = !IsAvailable(); - - if (wait && wait_thread) { - Kernel::WaitCurrentThread_WaitSynchronization(WAITTYPE_SEMA, this); - AddWaitingThread(GetCurrentThread()); - } - - return MakeResult(wait); + ResultVal Wait() override { + return MakeResult(!IsAvailable()); } ResultVal Acquire() override { diff --git a/src/core/hle/kernel/thread.cpp b/src/core/hle/kernel/thread.cpp index 62b85f56a..601e0eb20 100644 --- a/src/core/hle/kernel/thread.cpp +++ b/src/core/hle/kernel/thread.cpp @@ -22,14 +22,8 @@ namespace Kernel { -ResultVal Thread::Wait(bool wait_thread) { - const bool wait = status != THREADSTATUS_DORMANT; - if (wait && wait_thread) { - AddWaitingThread(GetCurrentThread()); - WaitCurrentThread_WaitSynchronization(WAITTYPE_THREADEND, this); - } - - return MakeResult(wait); +ResultVal Thread::Wait() { + return MakeResult(status != THREADSTATUS_DORMANT); } ResultVal Thread::Acquire() { @@ -68,7 +62,7 @@ static void ResetThread(Thread* t, u32 arg, s32 lowest_priority) { if (t->current_priority < lowest_priority) { t->current_priority = t->initial_priority; } - t->wait_type = WAITTYPE_NONE; + t->wait_objects.clear(); t->wait_address = 0; } @@ -89,23 +83,18 @@ static void ChangeReadyState(Thread* t, bool ready) { } } -/// Check if a thread is blocking on a specified wait type -static bool CheckWaitType(const Thread* thread, WaitType type) { - return (type == thread->wait_type) && (thread->IsWaiting()); -} - -/// Check if a thread is blocking on a specified wait type with a specified handle -static bool CheckWaitType(const Thread* thread, WaitType type, Object* wait_object) { +/// Check if a thread is blocking on a the specified object +static bool CheckWaitType(const Thread* thread, Object* wait_object) { for (auto itr = thread->wait_objects.begin(); itr != thread->wait_objects.end(); ++itr) { if (*itr == wait_object) - return CheckWaitType(thread, type); + return (thread->IsWaiting()); } return false; } -/// Check if a thread is blocking on a specified wait type with a specified handle and address -static bool CheckWaitType(const Thread* thread, WaitType type, Object* wait_object, VAddr wait_address) { - return CheckWaitType(thread, type, wait_object) && (wait_address == thread->wait_address); +/// Check if a thread is blocking on a the specified object and an address +static bool CheckWaitType(const Thread* thread, Object* wait_object, VAddr wait_address) { + return CheckWaitType(thread, wait_object) && (wait_address == thread->wait_address); } /// Stops the current thread @@ -118,7 +107,6 @@ void Thread::Stop(const char* reason) { ReleaseAllWaitingThreads(); // Stopped threads are never waiting. - wait_type = WAITTYPE_NONE; wait_objects.clear(); wait_address = 0; } @@ -130,12 +118,6 @@ static void ChangeThreadState(Thread* t, ThreadStatus new_status) { } ChangeReadyState(t, (new_status & THREADSTATUS_READY) != 0); t->status = new_status; - - if (new_status == THREADSTATUS_WAIT) { - if (t->wait_type == WAITTYPE_NONE) { - LOG_ERROR(Kernel, "Waittype none not allowed"); - } - } } /// Arbitrate the highest priority thread that is waiting @@ -145,7 +127,7 @@ Thread* ArbitrateHighestPriorityThread(WaitObject* arbiter, u32 address) { // Iterate through threads, find highest priority thread that is waiting to be arbitrated... for (auto& thread : thread_list) { - if (!CheckWaitType(thread.get(), WAITTYPE_ARB, arbiter, address)) + if (!CheckWaitType(thread.get(), arbiter, address)) continue; if (thread == nullptr) @@ -170,7 +152,7 @@ void ArbitrateAllThreads(WaitObject* arbiter, u32 address) { // Iterate through threads, find highest priority thread that is waiting to be arbitrated... for (auto& thread : thread_list) { - if (CheckWaitType(thread.get(), WAITTYPE_ARB, arbiter, address)) + if (CheckWaitType(thread.get(), arbiter, address)) thread->ReleaseFromWait(arbiter); } } @@ -178,9 +160,6 @@ void ArbitrateAllThreads(WaitObject* arbiter, u32 address) { /// Calls a thread by marking it as "ready" (note: will not actually execute until current thread yields) static void CallThread(Thread* t) { // Stop waiting - if (t->wait_type != WAITTYPE_NONE) { - t->wait_type = WAITTYPE_NONE; - } ChangeThreadState(t, THREADSTATUS_READY); } @@ -201,7 +180,6 @@ static void SwitchContext(Thread* t) { current_thread = t; ChangeReadyState(t, false); t->status = (t->status | THREADSTATUS_RUNNING) & ~THREADSTATUS_READY; - t->wait_type = WAITTYPE_NONE; Core::g_app_core->LoadContext(t->context); } else { current_thread = nullptr; @@ -224,23 +202,20 @@ static Thread* NextThread() { return next; } -void WaitCurrentThread(WaitType wait_type) { +void WaitCurrentThread() { Thread* thread = GetCurrentThread(); - thread->wait_type = wait_type; ChangeThreadState(thread, ThreadStatus(THREADSTATUS_WAIT | (thread->status & THREADSTATUS_SUSPEND))); } -void WaitCurrentThread_WaitSynchronization(WaitType wait_type, WaitObject* wait_object, unsigned index) { +void WaitCurrentThread_WaitSynchronization(WaitObject* wait_object, bool wait_all) { Thread* thread = GetCurrentThread(); - thread->wait_type = wait_type; - + thread->wait_all = wait_all; thread->wait_objects.push_back(wait_object); - ChangeThreadState(thread, ThreadStatus(THREADSTATUS_WAIT | (thread->status & THREADSTATUS_SUSPEND))); } void WaitCurrentThread_ArbitrateAddress(WaitObject* wait_object, VAddr wait_address) { - WaitCurrentThread_WaitSynchronization(WaitType::WAITTYPE_ARB, wait_object, 0); + WaitCurrentThread_WaitSynchronization(wait_object); GetCurrentThread()->wait_address = wait_address; } @@ -287,7 +262,7 @@ void Thread::ReleaseFromWait(WaitObject* wait_object) { // Iterate through all waiting objects to check availability... for (auto itr = wait_objects.begin(); itr != wait_objects.end(); ++itr) { - auto res = (*itr)->Wait(false); + auto res = (*itr)->Wait(); if (*res && res.Succeeded()) wait_all_failed = true; @@ -322,9 +297,8 @@ void Thread::ResumeFromWait() { wait_object->RemoveWaitingThread(this); wait_objects.clear(); - - wait_type = WAITTYPE_NONE; wait_all = false; + if (!(status & (THREADSTATUS_WAITSUSPEND | THREADSTATUS_DORMANT | THREADSTATUS_DEAD))) { ChangeReadyState(this, true); } @@ -390,7 +364,6 @@ ResultVal> Thread::Create(std::string name, VAddr entry_point, thread->stack_size = stack_size; thread->initial_priority = thread->current_priority = priority; thread->processor_id = processor_id; - thread->wait_type = WAITTYPE_NONE; thread->wait_all = false; thread->wait_objects.clear(); thread->wait_address = 0; @@ -476,8 +449,8 @@ void Reschedule() { LOG_TRACE(Kernel, "cannot context switch from 0x%08X, no higher priority thread!", prev->GetHandle()); for (auto& thread : thread_list) { - LOG_TRACE(Kernel, "\thandle=0x%08X prio=0x%02X, status=0x%08X wait_type=0x%08X", - thread->GetHandle(), thread->current_priority, thread->status, thread->wait_type); + LOG_TRACE(Kernel, "\thandle=0x%08X prio=0x%02X, status=0x%08X", thread->GetHandle(), + thread->current_priority, thread->status); } } } diff --git a/src/core/hle/kernel/thread.h b/src/core/hle/kernel/thread.h index dff6bbaec..cb48fcadc 100644 --- a/src/core/hle/kernel/thread.h +++ b/src/core/hle/kernel/thread.h @@ -38,18 +38,6 @@ enum ThreadStatus { THREADSTATUS_WAITSUSPEND = THREADSTATUS_WAIT | THREADSTATUS_SUSPEND }; -enum WaitType { - WAITTYPE_NONE, - WAITTYPE_SLEEP, - WAITTYPE_SEMA, - WAITTYPE_EVENT, - WAITTYPE_THREADEND, - WAITTYPE_MUTEX, - WAITTYPE_SYNCH, - WAITTYPE_ARB, - WAITTYPE_TIMER, -}; - namespace Kernel { class Thread : public WaitObject { @@ -70,7 +58,7 @@ public: inline bool IsSuspended() const { return (status & THREADSTATUS_SUSPEND) != 0; } inline bool IsIdle() const { return idle; } - ResultVal Wait(bool wait_thread) override; + ResultVal Wait() override; ResultVal Acquire() override; s32 GetPriority() const { return current_priority; } @@ -89,12 +77,6 @@ public: /// Resumes a thread from waiting by marking it as "ready" void ResumeFromWait(); - /** - * Sets the waiting mode of the thread - * @param wait_all If true, wait for all objects, otherwise just wait for the first one - */ - void SetWaitAll(bool wait_all) { this->wait_all = wait_all; } - /** * Sets the output values after the thread awakens from WaitSynchronization * @param return_val Value returned @@ -116,9 +98,10 @@ public: s32 processor_id; - WaitType wait_type; - std::vector> wait_objects; - VAddr wait_address; + std::vector> wait_objects; ///< Objects that the thread is waiting 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 std::string name; @@ -126,7 +109,6 @@ public: bool idle = false; private: - bool wait_all = false; Thread() = default; }; @@ -146,19 +128,15 @@ void ArbitrateAllThreads(WaitObject* arbiter, u32 address); /// Gets the current thread Thread* GetCurrentThread(); -/** - * Waits the current thread for the given type - * @param wait_type Type of wait - */ -void WaitCurrentThread(WaitType wait_type); +/// Waits the current thread +void WaitCurrentThread(); /** * Waits the current thread from a WaitSynchronization call - * @param wait_type Type of wait * @param wait_object Kernel object that we are waiting on - * @param index Index of calling object (for WaitSynchronizationN only) + * @param wait_all If true, wait on all objects before resuming (for WaitSynchronizationN only) */ -void WaitCurrentThread_WaitSynchronization(WaitType wait_type, WaitObject* wait_object, unsigned index=0); +void WaitCurrentThread_WaitSynchronization(WaitObject* wait_object, bool wait_all=false); /** * Waits the current thread from an ArbitrateAddress call @@ -181,6 +159,7 @@ void WakeThreadAfterDelay(Thread* thread, s64 nanoseconds); * @returns The handle of the idle thread */ Handle SetupIdleThread(); + /// Initialize threading void ThreadingInit(); diff --git a/src/core/hle/kernel/timer.cpp b/src/core/hle/kernel/timer.cpp index 9f0fbafe2..62bdf07c7 100644 --- a/src/core/hle/kernel/timer.cpp +++ b/src/core/hle/kernel/timer.cpp @@ -29,13 +29,8 @@ public: u64 initial_delay; ///< The delay until the timer fires for the first time u64 interval_delay; ///< The delay until the timer fires after the first time - ResultVal Wait(bool wait_thread) override { - bool wait = !signaled; - if (wait && wait_thread) { - AddWaitingThread(GetCurrentThread()); - Kernel::WaitCurrentThread_WaitSynchronization(WAITTYPE_TIMER, this); - } - return MakeResult(wait); + ResultVal Wait() override { + return MakeResult(!signaled); } ResultVal Acquire() override { -- cgit v1.2.3