From d73d782ba7ea6f3f2dd9c4f70d34c1004397dacb Mon Sep 17 00:00:00 2001 From: bunnei Date: Mon, 26 May 2014 21:01:27 -0400 Subject: kernel: add a SyncRequest method to KernelObject for use with svcSendSyncRequest --- src/core/hle/kernel/thread.cpp | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'src/core/hle/kernel/thread.cpp') diff --git a/src/core/hle/kernel/thread.cpp b/src/core/hle/kernel/thread.cpp index bf4c8353c..b9dd9fac4 100644 --- a/src/core/hle/kernel/thread.cpp +++ b/src/core/hle/kernel/thread.cpp @@ -36,6 +36,11 @@ public: inline bool IsWaiting() const { return (status & THREADSTATUS_WAIT) != 0; } inline bool IsSuspended() const { return (status & THREADSTATUS_SUSPEND) != 0; } + /// Synchronize kernel object + Result SyncRequest() { + return 0; + } + ThreadContext context; u32 status; -- cgit v1.2.3 From 58a3adcdd2eed9d31cd441186af872a0a8924e73 Mon Sep 17 00:00:00 2001 From: bunnei Date: Mon, 26 May 2014 22:12:46 -0400 Subject: kernel: updated SyncRequest to take boolean thread wait result as a parameter --- src/core/hle/kernel/thread.cpp | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'src/core/hle/kernel/thread.cpp') diff --git a/src/core/hle/kernel/thread.cpp b/src/core/hle/kernel/thread.cpp index b9dd9fac4..56c7755cf 100644 --- a/src/core/hle/kernel/thread.cpp +++ b/src/core/hle/kernel/thread.cpp @@ -36,8 +36,12 @@ public: inline bool IsWaiting() const { return (status & THREADSTATUS_WAIT) != 0; } inline bool IsSuspended() const { return (status & THREADSTATUS_SUSPEND) != 0; } - /// Synchronize kernel object - Result SyncRequest() { + /** + * Synchronize kernel object + * @param wait Boolean wait set if current thread should wait as a result of sync operation + * @return Result of operation, 0 on success, otherwise error code + */ + Result SyncRequest(bool* wait) { return 0; } -- cgit v1.2.3 From a432dc8f39a866b7b523235d6d94531f93bb4aa1 Mon Sep 17 00:00:00 2001 From: bunnei Date: Mon, 26 May 2014 22:17:49 -0400 Subject: kernel: added WaitSynchronization method to Kernel::Object --- src/core/hle/kernel/thread.cpp | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'src/core/hle/kernel/thread.cpp') diff --git a/src/core/hle/kernel/thread.cpp b/src/core/hle/kernel/thread.cpp index 56c7755cf..6e8b53eb1 100644 --- a/src/core/hle/kernel/thread.cpp +++ b/src/core/hle/kernel/thread.cpp @@ -42,6 +42,17 @@ public: * @return Result of operation, 0 on success, otherwise error code */ Result SyncRequest(bool* wait) { + // TODO(bunnei): ImplementMe + return 0; + } + + /** + * Wait for kernel object to synchronize + * @param wait Boolean wait set if current thread should wait as a result of sync operation + * @return Result of operation, 0 on success, otherwise error code + */ + Result WaitSynchronization(bool* wait) { + // TODO(bunnei): ImplementMe return 0; } -- cgit v1.2.3 From ba98e25e979b1aedaa9b917a7db8f710c83c073c Mon Sep 17 00:00:00 2001 From: bunnei Date: Sun, 1 Jun 2014 10:35:42 -0400 Subject: thread: updated Reschedule to sit at a synchronization barrier when no other threads are ready for execution --- src/core/hle/kernel/thread.cpp | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to 'src/core/hle/kernel/thread.cpp') diff --git a/src/core/hle/kernel/thread.cpp b/src/core/hle/kernel/thread.cpp index 6e8b53eb1..d1e13c949 100644 --- a/src/core/hle/kernel/thread.cpp +++ b/src/core/hle/kernel/thread.cpp @@ -323,11 +323,29 @@ void Reschedule() { Thread* prev = GetCurrentThread(); Thread* next = NextThread(); if (next > 0) { + INFO_LOG(KERNEL, "context switch 0x%08X -> 0x%08X", prev->GetHandle(), next->GetHandle()); + SwitchContext(next); // Hack - automatically change previous thread (which would have been in "wait" state) to // "ready" state, so that we can immediately resume to it when new thread yields. FixMe to // actually wait for whatever event it is supposed to be waiting on. + + ChangeReadyState(prev, true); + } else { + INFO_LOG(KERNEL, "no ready threads, staying on 0x%08X", prev->GetHandle()); + + // Hack - no other threads are available, so decrement current PC to the last instruction, + // and then resume current thread. This should always be called on a blocking instruction + // (e.g. svcWaitSynchronization), and the result should be that the instruction is repeated + // until it no longer blocks. + + // TODO(bunnei): A better solution: Have the CPU switch to an idle thread + + ThreadContext ctx; + SaveContext(ctx); + ctx.pc -= 4; + LoadContext(ctx); ChangeReadyState(prev, true); } } -- cgit v1.2.3 From 10447d1f4831b495d7bef7711681ddd548f847a6 Mon Sep 17 00:00:00 2001 From: bunnei Date: Sun, 1 Jun 2014 21:42:50 -0400 Subject: kernel: changed main thread priority to default, updated Kernel::Reschedule to use PrepareReschedule --- src/core/hle/kernel/thread.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'src/core/hle/kernel/thread.cpp') diff --git a/src/core/hle/kernel/thread.cpp b/src/core/hle/kernel/thread.cpp index d1e13c949..f2094f7a7 100644 --- a/src/core/hle/kernel/thread.cpp +++ b/src/core/hle/kernel/thread.cpp @@ -285,11 +285,11 @@ Handle CreateThread(const char* name, u32 entry_point, s32 priority, u32 arg, s3 HLE::EatCycles(32000); + CallThread(t); + // This won't schedule to the new thread, but it may to one woken from eating cycles. // Technically, this should not eat all at once, and reschedule in the middle, but that's hard. - HLE::ReSchedule("thread created"); - - CallThread(t); + //HLE::Reschedule("thread created"); return handle; } -- cgit v1.2.3 From 3fb31fbc57fd1d537db79af898ef26c92b0e0867 Mon Sep 17 00:00:00 2001 From: bunnei Date: Sun, 1 Jun 2014 22:12:54 -0400 Subject: svc: added GetThreadPriority and SetThreadPriority, added (incomplete) DuplicateHandle support --- src/core/hle/kernel/thread.cpp | 45 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) (limited to 'src/core/hle/kernel/thread.cpp') diff --git a/src/core/hle/kernel/thread.cpp b/src/core/hle/kernel/thread.cpp index f2094f7a7..c84fdf91d 100644 --- a/src/core/hle/kernel/thread.cpp +++ b/src/core/hle/kernel/thread.cpp @@ -294,6 +294,51 @@ Handle CreateThread(const char* name, u32 entry_point, s32 priority, u32 arg, s3 return handle; } +/// Get the priority of the thread specified by handle +u32 GetThreadPriority(const Handle handle) { + Thread* thread = g_object_pool.GetFast(handle); + _assert_msg_(KERNEL, thread, "called, but thread is NULL!"); + return thread->current_priority; +} + +/// Set the priority of the thread specified by handle +Result SetThreadPriority(Handle handle, s32 priority) { + Thread* thread = NULL; + if (!handle) { + thread = GetCurrentThread(); // TODO(bunnei): Is this correct behavior? + } else { + thread = g_object_pool.GetFast(handle); + } + _assert_msg_(KERNEL, thread, "called, but thread is NULL!"); + + // If priority is invalid, clamp to valid range + if (priority < THREADPRIO_HIGHEST || priority > THREADPRIO_LOWEST) { + s32 new_priority = CLAMP(priority, THREADPRIO_HIGHEST, THREADPRIO_LOWEST); + WARN_LOG(KERNEL, "invalid priority=0x%08X, clamping to %08X", priority, new_priority); + // TODO(bunnei): Clamping to a valid priority is not necessarily correct behavior... Confirm + // validity of this + priority = new_priority; + } + + // Change thread priority + s32 old = thread->current_priority; + g_thread_ready_queue.remove(old, handle); + thread->current_priority = priority; + g_thread_ready_queue.prepare(thread->current_priority); + + // Change thread status to "ready" and push to ready queue + if (thread->IsRunning()) { + thread->status = (thread->status & ~THREADSTATUS_RUNNING) | THREADSTATUS_READY; + } + if (thread->IsReady()) { + g_thread_ready_queue.push_back(thread->current_priority, handle); + } + + HLE::EatCycles(450); + + return 0; +} + /// Sets up the primary application thread Handle SetupMainThread(s32 priority, int stack_size) { Handle handle; -- cgit v1.2.3 From f5c7c1543434e25a215286e6db5e71c055ba48cf Mon Sep 17 00:00:00 2001 From: bunnei Date: Thu, 5 Jun 2014 22:35:36 -0400 Subject: Kernel: Added real support for thread and event blocking - SVC: Added ExitThread support - SVC: Added SignalEvent support - Thread: Added WAITTYPE_EVENT for waiting threads for event signals - Thread: Added support for blocking on other threads to finish (e.g. Thread::Join) - Thread: Added debug function for printing current threads ready for execution - Thread: Removed hack/broken thread ready state code from Kernel::Reschedule - Mutex: Moved WaitCurrentThread from SVC to Mutex::WaitSynchronization - Event: Added support for blocking threads on event signalling Kernel: Added missing algorithm #include for use of std::find on non-Windows platforms. --- src/core/hle/kernel/thread.cpp | 121 ++++++++++++++++++++++++++++++----------- 1 file changed, 88 insertions(+), 33 deletions(-) (limited to 'src/core/hle/kernel/thread.cpp') diff --git a/src/core/hle/kernel/thread.cpp b/src/core/hle/kernel/thread.cpp index c84fdf91d..d372df709 100644 --- a/src/core/hle/kernel/thread.cpp +++ b/src/core/hle/kernel/thread.cpp @@ -5,6 +5,7 @@ #include #include +#include #include #include #include @@ -52,7 +53,14 @@ public: * @return Result of operation, 0 on success, otherwise error code */ Result WaitSynchronization(bool* wait) { - // TODO(bunnei): ImplementMe + if (status != THREADSTATUS_DORMANT) { + Handle thread = GetCurrentThreadHandle(); + if (std::find(waiting_threads.begin(), waiting_threads.end(), thread) == waiting_threads.end()) { + waiting_threads.push_back(thread); + } + WaitCurrentThread(WAITTYPE_THREADEND, this->GetHandle()); + *wait = true; + } return 0; } @@ -69,6 +77,9 @@ public: s32 processor_id; WaitType wait_type; + Handle wait_handle; + + std::vector waiting_threads; char name[Kernel::MAX_NAME_LENGTH + 1]; }; @@ -82,7 +93,6 @@ Common::ThreadQueueList g_thread_ready_queue; Handle g_current_thread_handle; Thread* g_current_thread; - /// Gets the current thread inline Thread* GetCurrentThread() { return g_current_thread; @@ -114,15 +124,15 @@ void ResetThread(Thread* t, u32 arg, s32 lowest_priority) { memset(&t->context, 0, sizeof(ThreadContext)); t->context.cpu_registers[0] = arg; - t->context.pc = t->entry_point; + t->context.pc = t->context.cpu_registers[15] = t->entry_point; t->context.sp = t->stack_top; t->context.cpsr = 0x1F; // Usermode if (t->current_priority < lowest_priority) { t->current_priority = t->initial_priority; } - t->wait_type = WAITTYPE_NONE; + t->wait_handle = 0; } /// Change a thread to "ready" state @@ -142,6 +152,43 @@ void ChangeReadyState(Thread* t, bool ready) { } } +/// Verify that a thread has not been released from waiting +inline bool VerifyWait(const Handle& thread, WaitType type, Handle handle) { + Handle wait_id = 0; + Thread *t = g_object_pool.GetFast(thread); + if (t) { + if (type == t->wait_type && handle == t->wait_handle) { + return true; + } + } else { + ERROR_LOG(KERNEL, "thread 0x%08X does not exist", thread); + } + return false; +} + +/// Stops the current thread +void StopThread(Handle thread, const char* reason) { + u32 error; + Thread *t = g_object_pool.Get(thread, error); + if (t) { + ChangeReadyState(t, false); + t->status = THREADSTATUS_DORMANT; + for (size_t i = 0; i < t->waiting_threads.size(); ++i) { + const Handle waiting_thread = t->waiting_threads[i]; + if (VerifyWait(waiting_thread, WAITTYPE_THREADEND, thread)) { + ResumeThreadFromWait(waiting_thread); + } + } + t->waiting_threads.clear(); + + // Stopped threads are never waiting. + t->wait_type = WAITTYPE_NONE; + t->wait_handle = 0; + } else { + ERROR_LOG(KERNEL, "thread 0x%08X does not exist", thread); + } +} + /// Changes a threads state void ChangeThreadState(Thread* t, ThreadStatus new_status) { if (!t || t->status == new_status) { @@ -152,7 +199,7 @@ void ChangeThreadState(Thread* t, ThreadStatus new_status) { if (new_status == THREADSTATUS_WAIT) { if (t->wait_type == WAITTYPE_NONE) { - printf("ERROR: Waittype none not allowed here\n"); + ERROR_LOG(KERNEL, "Waittype none not allowed"); } } } @@ -207,9 +254,10 @@ Thread* NextThread() { } /// Puts the current thread in the wait state for the given type -void WaitCurrentThread(WaitType wait_type) { +void WaitCurrentThread(WaitType wait_type, Handle wait_handle) { Thread* t = GetCurrentThread(); t->wait_type = wait_type; + t->wait_handle = wait_handle; ChangeThreadState(t, ThreadStatus(THREADSTATUS_WAIT | (t->status & THREADSTATUS_SUSPEND))); } @@ -225,6 +273,22 @@ void ResumeThreadFromWait(Handle handle) { } } +/// Prints the thread queue for debugging purposes +void DebugThreadQueue() { + Thread* thread = GetCurrentThread(); + if (!thread) { + return; + } + INFO_LOG(KERNEL, "0x%02X 0x%08X (current)", thread->current_priority, GetCurrentThreadHandle()); + for (u32 i = 0; i < g_thread_queue.size(); i++) { + Handle handle = g_thread_queue[i]; + s32 priority = g_thread_ready_queue.contains(handle); + if (priority != -1) { + INFO_LOG(KERNEL, "0x%02X 0x%08X", priority, handle); + } + } +} + /// Creates a new thread Thread* CreateThread(Handle& handle, const char* name, u32 entry_point, s32 priority, s32 processor_id, u32 stack_top, int stack_size) { @@ -233,12 +297,12 @@ Thread* CreateThread(Handle& handle, const char* name, u32 entry_point, s32 prio "CreateThread priority=%d, outside of allowable range!", priority) Thread* t = new Thread; - + handle = Kernel::g_object_pool.Create(t); - + g_thread_queue.push_back(handle); g_thread_ready_queue.prepare(priority); - + t->status = THREADSTATUS_DORMANT; t->entry_point = entry_point; t->stack_top = stack_top; @@ -246,16 +310,18 @@ Thread* CreateThread(Handle& handle, const char* name, u32 entry_point, s32 prio t->initial_priority = t->current_priority = priority; t->processor_id = processor_id; t->wait_type = WAITTYPE_NONE; - + t->wait_handle = 0; + strncpy(t->name, name, Kernel::MAX_NAME_LENGTH); t->name[Kernel::MAX_NAME_LENGTH] = '\0'; - + return t; } /// Creates a new thread - wrapper for external user Handle CreateThread(const char* name, u32 entry_point, s32 priority, u32 arg, s32 processor_id, u32 stack_top, int stack_size) { + if (name == NULL) { ERROR_LOG(KERNEL, "CreateThread(): NULL name"); return -1; @@ -289,7 +355,7 @@ Handle CreateThread(const char* name, u32 entry_point, s32 priority, u32 arg, s3 // This won't schedule to the new thread, but it may to one woken from eating cycles. // Technically, this should not eat all at once, and reschedule in the middle, but that's hard. - //HLE::Reschedule("thread created"); + //HLE::Reschedule(__func__); return handle; } @@ -363,35 +429,24 @@ Handle SetupMainThread(s32 priority, int stack_size) { return handle; } + /// Reschedules to the next available thread (call after current thread is suspended) void Reschedule() { Thread* prev = GetCurrentThread(); Thread* next = NextThread(); + HLE::g_reschedule = false; if (next > 0) { INFO_LOG(KERNEL, "context switch 0x%08X -> 0x%08X", prev->GetHandle(), next->GetHandle()); - + SwitchContext(next); - // Hack - automatically change previous thread (which would have been in "wait" state) to - // "ready" state, so that we can immediately resume to it when new thread yields. FixMe to - // actually wait for whatever event it is supposed to be waiting on. - - ChangeReadyState(prev, true); - } else { - INFO_LOG(KERNEL, "no ready threads, staying on 0x%08X", prev->GetHandle()); - - // Hack - no other threads are available, so decrement current PC to the last instruction, - // and then resume current thread. This should always be called on a blocking instruction - // (e.g. svcWaitSynchronization), and the result should be that the instruction is repeated - // until it no longer blocks. - - // TODO(bunnei): A better solution: Have the CPU switch to an idle thread - - ThreadContext ctx; - SaveContext(ctx); - ctx.pc -= 4; - LoadContext(ctx); - ChangeReadyState(prev, true); + // Hack - There is no mechanism yet to waken the primary thread if it has been put to sleep + // by a simulated VBLANK thread switch. So, we'll just immediately set it to "ready" again. + // This results in the current thread yielding on a VBLANK once, and then it will be + // immediately placed back in the queue for execution. + if (prev->wait_type == WAITTYPE_VBLANK) { + ResumeThreadFromWait(prev->GetHandle()); + } } } -- cgit v1.2.3 From aae9fcf4a4071a408af10ca1c72180cdc04687b8 Mon Sep 17 00:00:00 2001 From: bunnei Date: Thu, 5 Jun 2014 23:13:28 -0400 Subject: Kernel: Made SyncRequest not pure virtual, with a default implementation of error (as this is not required for all kernel objects) --- src/core/hle/kernel/thread.cpp | 10 ---------- 1 file changed, 10 deletions(-) (limited to 'src/core/hle/kernel/thread.cpp') diff --git a/src/core/hle/kernel/thread.cpp b/src/core/hle/kernel/thread.cpp index d372df709..180c14928 100644 --- a/src/core/hle/kernel/thread.cpp +++ b/src/core/hle/kernel/thread.cpp @@ -37,16 +37,6 @@ public: inline bool IsWaiting() const { return (status & THREADSTATUS_WAIT) != 0; } inline bool IsSuspended() const { return (status & THREADSTATUS_SUSPEND) != 0; } - /** - * Synchronize kernel object - * @param wait Boolean wait set if current thread should wait as a result of sync operation - * @return Result of operation, 0 on success, otherwise error code - */ - Result SyncRequest(bool* wait) { - // TODO(bunnei): ImplementMe - return 0; - } - /** * Wait for kernel object to synchronize * @param wait Boolean wait set if current thread should wait as a result of sync operation -- cgit v1.2.3 From b774b8b04e50ad709381506a80e881a1a1471b0f Mon Sep 17 00:00:00 2001 From: bunnei Date: Thu, 5 Jun 2014 23:19:55 -0400 Subject: Thread: Fixed bug with ResetThread where cpu_registers[15] was being incorrectly set --- src/core/hle/kernel/thread.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/core/hle/kernel/thread.cpp') diff --git a/src/core/hle/kernel/thread.cpp b/src/core/hle/kernel/thread.cpp index 180c14928..6196c352c 100644 --- a/src/core/hle/kernel/thread.cpp +++ b/src/core/hle/kernel/thread.cpp @@ -114,7 +114,7 @@ void ResetThread(Thread* t, u32 arg, s32 lowest_priority) { memset(&t->context, 0, sizeof(ThreadContext)); t->context.cpu_registers[0] = arg; - t->context.pc = t->context.cpu_registers[15] = t->entry_point; + t->context.pc = t->context.reg_15 = t->entry_point; t->context.sp = t->stack_top; t->context.cpsr = 0x1F; // Usermode -- cgit v1.2.3 From 8cac527c943253a9471849d17b1520f4762fbb5c Mon Sep 17 00:00:00 2001 From: bunnei Date: Fri, 6 Jun 2014 00:10:50 -0400 Subject: Kernel: Updated several member functions to be const --- src/core/hle/kernel/thread.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'src/core/hle/kernel/thread.cpp') diff --git a/src/core/hle/kernel/thread.cpp b/src/core/hle/kernel/thread.cpp index 6196c352c..da93e006c 100644 --- a/src/core/hle/kernel/thread.cpp +++ b/src/core/hle/kernel/thread.cpp @@ -25,10 +25,10 @@ namespace Kernel { class Thread : public Kernel::Object { public: - const char* GetName() { return name; } - const char* GetTypeName() { return "Thread"; } + const char* GetName() const { return name; } + const char* GetTypeName() const { return "Thread"; } - static Kernel::HandleType GetStaticHandleType() { return Kernel::HandleType::Thread; } + static Kernel::HandleType GetStaticHandleType() { return Kernel::HandleType::Thread; } Kernel::HandleType GetHandleType() const { return Kernel::HandleType::Thread; } inline bool IsRunning() const { return (status & THREADSTATUS_RUNNING) != 0; } -- cgit v1.2.3 From d7363322c79d6e7598e0d80cf1af9c05b652cecb Mon Sep 17 00:00:00 2001 From: bunnei Date: Fri, 6 Jun 2014 00:19:40 -0400 Subject: HLE: Updated various handle debug assertions to be more clear. --- src/core/hle/kernel/thread.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/core/hle/kernel/thread.cpp') diff --git a/src/core/hle/kernel/thread.cpp b/src/core/hle/kernel/thread.cpp index da93e006c..5fdb4fbe6 100644 --- a/src/core/hle/kernel/thread.cpp +++ b/src/core/hle/kernel/thread.cpp @@ -353,7 +353,7 @@ Handle CreateThread(const char* name, u32 entry_point, s32 priority, u32 arg, s3 /// Get the priority of the thread specified by handle u32 GetThreadPriority(const Handle handle) { Thread* thread = g_object_pool.GetFast(handle); - _assert_msg_(KERNEL, thread, "called, but thread is NULL!"); + _assert_msg_(KERNEL, (thread != NULL), "called, but thread is NULL!"); return thread->current_priority; } @@ -365,7 +365,7 @@ Result SetThreadPriority(Handle handle, s32 priority) { } else { thread = g_object_pool.GetFast(handle); } - _assert_msg_(KERNEL, thread, "called, but thread is NULL!"); + _assert_msg_(KERNEL, (thread != NULL), "called, but thread is NULL!"); // If priority is invalid, clamp to valid range if (priority < THREADPRIO_HIGHEST || priority > THREADPRIO_LOWEST) { -- cgit v1.2.3 From c95972275e276abe3afcac79d956ea29a0879c8e Mon Sep 17 00:00:00 2001 From: bunnei Date: Fri, 6 Jun 2014 00:35:49 -0400 Subject: HLE: Updated all uses of NULL to nullptr (to be C++11 compliant) --- src/core/hle/kernel/thread.cpp | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'src/core/hle/kernel/thread.cpp') diff --git a/src/core/hle/kernel/thread.cpp b/src/core/hle/kernel/thread.cpp index 5fdb4fbe6..700f4ea7c 100644 --- a/src/core/hle/kernel/thread.cpp +++ b/src/core/hle/kernel/thread.cpp @@ -223,7 +223,7 @@ void SwitchContext(Thread* t) { t->wait_type = WAITTYPE_NONE; LoadContext(t->context); } else { - SetCurrentThread(NULL); + SetCurrentThread(nullptr); } } @@ -238,7 +238,7 @@ Thread* NextThread() { next = g_thread_ready_queue.pop_first(); } if (next == 0) { - return NULL; + return nullptr; } return Kernel::g_object_pool.GetFast(next); } @@ -312,8 +312,8 @@ Thread* CreateThread(Handle& handle, const char* name, u32 entry_point, s32 prio Handle CreateThread(const char* name, u32 entry_point, s32 priority, u32 arg, s32 processor_id, u32 stack_top, int stack_size) { - if (name == NULL) { - ERROR_LOG(KERNEL, "CreateThread(): NULL name"); + if (name == nullptr) { + ERROR_LOG(KERNEL, "CreateThread(): nullptr name"); return -1; } if ((u32)stack_size < 0x200) { @@ -353,19 +353,19 @@ Handle CreateThread(const char* name, u32 entry_point, s32 priority, u32 arg, s3 /// Get the priority of the thread specified by handle u32 GetThreadPriority(const Handle handle) { Thread* thread = g_object_pool.GetFast(handle); - _assert_msg_(KERNEL, (thread != NULL), "called, but thread is NULL!"); + _assert_msg_(KERNEL, (thread != nullptr), "called, but thread is nullptr!"); return thread->current_priority; } /// Set the priority of the thread specified by handle Result SetThreadPriority(Handle handle, s32 priority) { - Thread* thread = NULL; + Thread* thread = nullptr; if (!handle) { thread = GetCurrentThread(); // TODO(bunnei): Is this correct behavior? } else { thread = g_object_pool.GetFast(handle); } - _assert_msg_(KERNEL, (thread != NULL), "called, but thread is NULL!"); + _assert_msg_(KERNEL, (thread != nullptr), "called, but thread is nullptr!"); // If priority is invalid, clamp to valid range if (priority < THREADPRIO_HIGHEST || priority > THREADPRIO_LOWEST) { -- cgit v1.2.3 From 12e2a59565520382e76c641896b594fdafebd244 Mon Sep 17 00:00:00 2001 From: bunnei Date: Fri, 6 Jun 2014 17:57:52 -0400 Subject: Thread: Updated VerifyWait to be more readable (but functionally the same). --- src/core/hle/kernel/thread.cpp | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) (limited to 'src/core/hle/kernel/thread.cpp') diff --git a/src/core/hle/kernel/thread.cpp b/src/core/hle/kernel/thread.cpp index 700f4ea7c..0385a64fd 100644 --- a/src/core/hle/kernel/thread.cpp +++ b/src/core/hle/kernel/thread.cpp @@ -146,12 +146,11 @@ void ChangeReadyState(Thread* t, bool ready) { inline bool VerifyWait(const Handle& thread, WaitType type, Handle handle) { Handle wait_id = 0; Thread *t = g_object_pool.GetFast(thread); - if (t) { - if (type == t->wait_type && handle == t->wait_handle) { - return true; - } + if (t != nullptr && type == t->wait_type && handle == t->wait_handle) { + return true; } else { ERROR_LOG(KERNEL, "thread 0x%08X does not exist", thread); + return false; } return false; } -- cgit v1.2.3 From bfdd874b1f3a7af9ec540105ed1d52edc0d72472 Mon Sep 17 00:00:00 2001 From: bunnei Date: Fri, 6 Jun 2014 20:05:16 -0400 Subject: Thread: Moved position of * in arguments. --- src/core/hle/kernel/thread.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/core/hle/kernel/thread.cpp') diff --git a/src/core/hle/kernel/thread.cpp b/src/core/hle/kernel/thread.cpp index 0385a64fd..cb5b89379 100644 --- a/src/core/hle/kernel/thread.cpp +++ b/src/core/hle/kernel/thread.cpp @@ -145,7 +145,7 @@ void ChangeReadyState(Thread* t, bool ready) { /// Verify that a thread has not been released from waiting inline bool VerifyWait(const Handle& thread, WaitType type, Handle handle) { Handle wait_id = 0; - Thread *t = g_object_pool.GetFast(thread); + Thread* t = g_object_pool.GetFast(thread); if (t != nullptr && type == t->wait_type && handle == t->wait_handle) { return true; } else { @@ -158,7 +158,7 @@ inline bool VerifyWait(const Handle& thread, WaitType type, Handle handle) { /// Stops the current thread void StopThread(Handle thread, const char* reason) { u32 error; - Thread *t = g_object_pool.Get(thread, error); + Thread* t = g_object_pool.Get(thread, error); if (t) { ChangeReadyState(t, false); t->status = THREADSTATUS_DORMANT; -- cgit v1.2.3 From 4620e2a741735a18f3a331037f86b3aae0f9fc6d Mon Sep 17 00:00:00 2001 From: bunnei Date: Fri, 6 Jun 2014 23:34:49 -0400 Subject: HLE: Removed usnused EatCycles function. --- src/core/hle/kernel/thread.cpp | 9 --------- 1 file changed, 9 deletions(-) (limited to 'src/core/hle/kernel/thread.cpp') diff --git a/src/core/hle/kernel/thread.cpp b/src/core/hle/kernel/thread.cpp index cb5b89379..ebe308a93 100644 --- a/src/core/hle/kernel/thread.cpp +++ b/src/core/hle/kernel/thread.cpp @@ -337,15 +337,8 @@ Handle CreateThread(const char* name, u32 entry_point, s32 priority, u32 arg, s3 stack_size); ResetThread(t, arg, 0); - - HLE::EatCycles(32000); - CallThread(t); - // This won't schedule to the new thread, but it may to one woken from eating cycles. - // Technically, this should not eat all at once, and reschedule in the middle, but that's hard. - //HLE::Reschedule(__func__); - return handle; } @@ -389,8 +382,6 @@ Result SetThreadPriority(Handle handle, s32 priority) { g_thread_ready_queue.push_back(thread->current_priority, handle); } - HLE::EatCycles(450); - return 0; } -- cgit v1.2.3 From 5b7cf50a7760ea1d4202ac3890cefc8934ac841f Mon Sep 17 00:00:00 2001 From: bunnei Date: Mon, 9 Jun 2014 22:08:49 -0400 Subject: Thread: Cleaned up VerifyWait, fixed issue where nullptr msg could unnecessarily be logged. --- src/core/hle/kernel/thread.cpp | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) (limited to 'src/core/hle/kernel/thread.cpp') diff --git a/src/core/hle/kernel/thread.cpp b/src/core/hle/kernel/thread.cpp index ebe308a93..baa9687cb 100644 --- a/src/core/hle/kernel/thread.cpp +++ b/src/core/hle/kernel/thread.cpp @@ -143,16 +143,14 @@ void ChangeReadyState(Thread* t, bool ready) { } /// Verify that a thread has not been released from waiting -inline bool VerifyWait(const Handle& thread, WaitType type, Handle handle) { - Handle wait_id = 0; - Thread* t = g_object_pool.GetFast(thread); - if (t != nullptr && type == t->wait_type && handle == t->wait_handle) { - return true; - } else { - ERROR_LOG(KERNEL, "thread 0x%08X does not exist", thread); +inline bool VerifyWait(const Handle& handle, WaitType type, Handle wait_handle) { + Thread* thread = g_object_pool.GetFast(handle); + _assert_msg_(KERNEL, (thread != nullptr), "called, but thread is nullptr!"); + + if (type != thread->wait_type || wait_handle != thread->wait_handle) return false; - } - return false; + + return true; } /// Stops the current thread -- cgit v1.2.3 From b62ef4bbd2f415b6a193ce53e87decb56bacb3e1 Mon Sep 17 00:00:00 2001 From: bunnei Date: Mon, 9 Jun 2014 22:14:03 -0400 Subject: Thread: Renamed occurrences of "t" to "thread" to improve readability. --- src/core/hle/kernel/thread.cpp | 93 ++++++++++++++++++++---------------------- 1 file changed, 45 insertions(+), 48 deletions(-) (limited to 'src/core/hle/kernel/thread.cpp') diff --git a/src/core/hle/kernel/thread.cpp b/src/core/hle/kernel/thread.cpp index baa9687cb..ab5a5559e 100644 --- a/src/core/hle/kernel/thread.cpp +++ b/src/core/hle/kernel/thread.cpp @@ -154,26 +154,23 @@ inline bool VerifyWait(const Handle& handle, WaitType type, Handle wait_handle) } /// Stops the current thread -void StopThread(Handle thread, const char* reason) { - u32 error; - Thread* t = g_object_pool.Get(thread, error); - if (t) { - ChangeReadyState(t, false); - t->status = THREADSTATUS_DORMANT; - for (size_t i = 0; i < t->waiting_threads.size(); ++i) { - const Handle waiting_thread = t->waiting_threads[i]; - if (VerifyWait(waiting_thread, WAITTYPE_THREADEND, thread)) { - ResumeThreadFromWait(waiting_thread); - } +void StopThread(Handle handle, const char* reason) { + Thread* thread = g_object_pool.GetFast(handle); + _assert_msg_(KERNEL, (thread != nullptr), "called, but thread is nullptr!"); + + ChangeReadyState(thread, false); + thread->status = THREADSTATUS_DORMANT; + for (size_t i = 0; i < thread->waiting_threads.size(); ++i) { + const Handle waiting_thread = thread->waiting_threads[i]; + if (VerifyWait(waiting_thread, WAITTYPE_THREADEND, handle)) { + ResumeThreadFromWait(waiting_thread); } - t->waiting_threads.clear(); - - // Stopped threads are never waiting. - t->wait_type = WAITTYPE_NONE; - t->wait_handle = 0; - } else { - ERROR_LOG(KERNEL, "thread 0x%08X does not exist", thread); } + thread->waiting_threads.clear(); + + // Stopped threads are never waiting. + thread->wait_type = WAITTYPE_NONE; + thread->wait_handle = 0; } /// Changes a threads state @@ -242,20 +239,20 @@ Thread* NextThread() { /// Puts the current thread in the wait state for the given type void WaitCurrentThread(WaitType wait_type, Handle wait_handle) { - Thread* t = GetCurrentThread(); - t->wait_type = wait_type; - t->wait_handle = wait_handle; - ChangeThreadState(t, ThreadStatus(THREADSTATUS_WAIT | (t->status & THREADSTATUS_SUSPEND))); + Thread* thread = GetCurrentThread(); + thread->wait_type = wait_type; + thread->wait_handle = wait_handle; + ChangeThreadState(thread, ThreadStatus(THREADSTATUS_WAIT | (thread->status & THREADSTATUS_SUSPEND))); } /// Resumes a thread from waiting by marking it as "ready" void ResumeThreadFromWait(Handle handle) { u32 error; - Thread* t = Kernel::g_object_pool.Get(handle, error); - if (t) { - t->status &= ~THREADSTATUS_WAIT; - if (!(t->status & (THREADSTATUS_WAITSUSPEND | THREADSTATUS_DORMANT | THREADSTATUS_DEAD))) { - ChangeReadyState(t, true); + Thread* thread = Kernel::g_object_pool.Get(handle, error); + if (thread) { + thread->status &= ~THREADSTATUS_WAIT; + if (!(thread->status & (THREADSTATUS_WAITSUSPEND | THREADSTATUS_DORMANT | THREADSTATUS_DEAD))) { + ChangeReadyState(thread, true); } } } @@ -283,26 +280,26 @@ Thread* CreateThread(Handle& handle, const char* name, u32 entry_point, s32 prio _assert_msg_(KERNEL, (priority >= THREADPRIO_HIGHEST && priority <= THREADPRIO_LOWEST), "CreateThread priority=%d, outside of allowable range!", priority) - Thread* t = new Thread; + Thread* thread = new Thread; - handle = Kernel::g_object_pool.Create(t); + handle = Kernel::g_object_pool.Create(thread); g_thread_queue.push_back(handle); g_thread_ready_queue.prepare(priority); - t->status = THREADSTATUS_DORMANT; - t->entry_point = entry_point; - t->stack_top = stack_top; - t->stack_size = stack_size; - t->initial_priority = t->current_priority = priority; - t->processor_id = processor_id; - t->wait_type = WAITTYPE_NONE; - t->wait_handle = 0; + thread->status = THREADSTATUS_DORMANT; + thread->entry_point = entry_point; + thread->stack_top = stack_top; + thread->stack_size = stack_size; + thread->initial_priority = thread->current_priority = priority; + thread->processor_id = processor_id; + thread->wait_type = WAITTYPE_NONE; + thread->wait_handle = 0; - strncpy(t->name, name, Kernel::MAX_NAME_LENGTH); - t->name[Kernel::MAX_NAME_LENGTH] = '\0'; + strncpy(thread->name, name, Kernel::MAX_NAME_LENGTH); + thread->name[Kernel::MAX_NAME_LENGTH] = '\0'; - return t; + return thread; } /// Creates a new thread - wrapper for external user @@ -331,11 +328,11 @@ Handle CreateThread(const char* name, u32 entry_point, s32 priority, u32 arg, s3 return -1; } Handle handle; - Thread* t = CreateThread(handle, name, entry_point, priority, processor_id, stack_top, + Thread* thread = CreateThread(handle, name, entry_point, priority, processor_id, stack_top, stack_size); - ResetThread(t, arg, 0); - CallThread(t); + ResetThread(thread, arg, 0); + CallThread(thread); return handle; } @@ -388,10 +385,10 @@ Handle SetupMainThread(s32 priority, int stack_size) { Handle handle; // Initialize new "main" thread - Thread* t = CreateThread(handle, "main", Core::g_app_core->GetPC(), priority, + Thread* thread = CreateThread(handle, "main", Core::g_app_core->GetPC(), priority, THREADPROCESSORID_0, Memory::SCRATCHPAD_VADDR_END, stack_size); - ResetThread(t, 0, 0); + ResetThread(thread, 0, 0); // If running another thread already, set it to "ready" state Thread* cur = GetCurrentThread(); @@ -400,9 +397,9 @@ Handle SetupMainThread(s32 priority, int stack_size) { } // Run new "main" thread - SetCurrentThread(t); - t->status = THREADSTATUS_RUNNING; - LoadContext(t->context); + SetCurrentThread(thread); + thread->status = THREADSTATUS_RUNNING; + LoadContext(thread->context); return handle; } -- cgit v1.2.3