| 
						
						
							
								
							
						
						
					 | 
					 | 
					@ -19,7 +19,7 @@
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					#include "core/hle/kernel/kernel.h"
 | 
					 | 
					 | 
					 | 
					#include "core/hle/kernel/kernel.h"
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					#include "core/hle/kernel/thread.h"
 | 
					 | 
					 | 
					 | 
					#include "core/hle/kernel/thread.h"
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					
 | 
					 | 
					 | 
					 | 
					
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					// Enums
 | 
					 | 
					 | 
					 | 
					namespace Kernel {
 | 
				
			
			
				
				
			
		
	
		
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					
 | 
					 | 
					 | 
					 | 
					
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					enum ThreadStatus {
 | 
					 | 
					 | 
					 | 
					enum ThreadStatus {
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					    THREADSTATUS_RUNNING        = 1,
 | 
					 | 
					 | 
					 | 
					    THREADSTATUS_RUNNING        = 1,
 | 
				
			
			
		
	
	
		
		
			
				
					| 
						
							
								
							
						
						
							
								
							
						
						
					 | 
					 | 
					@ -81,33 +81,32 @@ std::vector<Handle> g_thread_queue;
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					Common::ThreadQueueList<Handle> g_thread_ready_queue;
 | 
					 | 
					 | 
					 | 
					Common::ThreadQueueList<Handle> g_thread_ready_queue;
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					
 | 
					 | 
					 | 
					 | 
					
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					Handle g_current_thread_handle;
 | 
					 | 
					 | 
					 | 
					Handle g_current_thread_handle;
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					
 | 
					 | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					Thread* g_current_thread;
 | 
					 | 
					 | 
					 | 
					Thread* g_current_thread;
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					
 | 
					 | 
					 | 
					 | 
					
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					
 | 
					 | 
					 | 
					 | 
					
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					/// Gets the current thread
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					inline Thread* __GetCurrentThread() {
 | 
					 | 
					 | 
					 | 
					inline Thread* __GetCurrentThread() {
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					    return g_current_thread;
 | 
					 | 
					 | 
					 | 
					    return g_current_thread;
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					}
 | 
					 | 
					 | 
					 | 
					}
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					
 | 
					 | 
					 | 
					 | 
					
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					/// Sets the current thread
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					inline void __SetCurrentThread(Thread* t) {
 | 
					 | 
					 | 
					 | 
					inline void __SetCurrentThread(Thread* t) {
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					    g_current_thread = t;
 | 
					 | 
					 | 
					 | 
					    g_current_thread = t;
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					    g_current_thread_handle = t->GetHandle();
 | 
					 | 
					 | 
					 | 
					    g_current_thread_handle = t->GetHandle();
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					}
 | 
					 | 
					 | 
					 | 
					}
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					
 | 
					 | 
					 | 
					 | 
					
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					////////////////////////////////////////////////////////////////////////////////////////////////////
 | 
					 | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					
 | 
					 | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					/// Saves the current CPU context
 | 
					 | 
					 | 
					 | 
					/// Saves the current CPU context
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					void __KernelSaveContext(ThreadContext& ctx) {
 | 
					 | 
					 | 
					 | 
					void __SaveContext(ThreadContext& ctx) {
 | 
				
			
			
				
				
			
		
	
		
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					    Core::g_app_core->SaveContext(ctx);
 | 
					 | 
					 | 
					 | 
					    Core::g_app_core->SaveContext(ctx);
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					}
 | 
					 | 
					 | 
					 | 
					}
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					
 | 
					 | 
					 | 
					 | 
					
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					/// Loads a CPU context
 | 
					 | 
					 | 
					 | 
					/// Loads a CPU context
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					void __KernelLoadContext(const ThreadContext& ctx) {
 | 
					 | 
					 | 
					 | 
					void __LoadContext(const ThreadContext& ctx) {
 | 
				
			
			
				
				
			
		
	
		
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					    Core::g_app_core->LoadContext(ctx);
 | 
					 | 
					 | 
					 | 
					    Core::g_app_core->LoadContext(ctx);
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					}
 | 
					 | 
					 | 
					 | 
					}
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					
 | 
					 | 
					 | 
					 | 
					
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					/// Resets a thread
 | 
					 | 
					 | 
					 | 
					/// Resets a thread
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					void __KernelResetThread(Thread* t, s32 lowest_priority) {
 | 
					 | 
					 | 
					 | 
					void __ResetThread(Thread* t, s32 lowest_priority) {
 | 
				
			
			
				
				
			
		
	
		
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					    memset(&t->context, 0, sizeof(ThreadContext));
 | 
					 | 
					 | 
					 | 
					    memset(&t->context, 0, sizeof(ThreadContext));
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					
 | 
					 | 
					 | 
					 | 
					
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					    t->context.pc = t->entry_point;
 | 
					 | 
					 | 
					 | 
					    t->context.pc = t->entry_point;
 | 
				
			
			
		
	
	
		
		
			
				
					| 
						
						
						
							
								
							
						
					 | 
					 | 
					@ -121,7 +120,7 @@ void __KernelResetThread(Thread* t, s32 lowest_priority) {
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					}
 | 
					 | 
					 | 
					 | 
					}
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					
 | 
					 | 
					 | 
					 | 
					
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					/// Change a thread to "ready" state
 | 
					 | 
					 | 
					 | 
					/// Change a thread to "ready" state
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					void __KernelChangeReadyState(Thread* t, bool ready) {
 | 
					 | 
					 | 
					 | 
					void __ChangeReadyState(Thread* t, bool ready) {
 | 
				
			
			
				
				
			
		
	
		
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					    Handle handle = t->GetHandle();
 | 
					 | 
					 | 
					 | 
					    Handle handle = t->GetHandle();
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					    if (t->IsReady()) {
 | 
					 | 
					 | 
					 | 
					    if (t->IsReady()) {
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					        if (!ready) {
 | 
					 | 
					 | 
					 | 
					        if (!ready) {
 | 
				
			
			
		
	
	
		
		
			
				
					| 
						
						
						
							
								
							
						
					 | 
					 | 
					@ -138,11 +137,11 @@ void __KernelChangeReadyState(Thread* t, bool ready) {
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					}
 | 
					 | 
					 | 
					 | 
					}
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					
 | 
					 | 
					 | 
					 | 
					
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					/// Changes a threads state
 | 
					 | 
					 | 
					 | 
					/// Changes a threads state
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					void __KernelChangeThreadState(Thread* t, ThreadStatus new_status) {
 | 
					 | 
					 | 
					 | 
					void __ChangeThreadState(Thread* t, ThreadStatus new_status) {
 | 
				
			
			
				
				
			
		
	
		
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					    if (!t || t->status == new_status) {
 | 
					 | 
					 | 
					 | 
					    if (!t || t->status == new_status) {
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					        return;
 | 
					 | 
					 | 
					 | 
					        return;
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					    }
 | 
					 | 
					 | 
					 | 
					    }
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					    __KernelChangeReadyState(t, (new_status & THREADSTATUS_READY) != 0);
 | 
					 | 
					 | 
					 | 
					    __ChangeReadyState(t, (new_status & THREADSTATUS_READY) != 0);
 | 
				
			
			
				
				
			
		
	
		
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					    t->status = new_status;
 | 
					 | 
					 | 
					 | 
					    t->status = new_status;
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					    
 | 
					 | 
					 | 
					 | 
					    
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					    if (new_status == THREADSTATUS_WAIT) {
 | 
					 | 
					 | 
					 | 
					    if (new_status == THREADSTATUS_WAIT) {
 | 
				
			
			
		
	
	
		
		
			
				
					| 
						
						
						
							
								
							
						
					 | 
					 | 
					@ -153,16 +152,75 @@ void __KernelChangeThreadState(Thread* t, ThreadStatus new_status) {
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					}
 | 
					 | 
					 | 
					 | 
					}
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					
 | 
					 | 
					 | 
					 | 
					
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					/// Calls a thread by marking it as "ready" (note: will not actually execute until current thread yields)
 | 
					 | 
					 | 
					 | 
					/// Calls a thread by marking it as "ready" (note: will not actually execute until current thread yields)
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					void __KernelCallThread(Thread* t) {
 | 
					 | 
					 | 
					 | 
					void __CallThread(Thread* t) {
 | 
				
			
			
				
				
			
		
	
		
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					    // Stop waiting
 | 
					 | 
					 | 
					 | 
					    // Stop waiting
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					    if (t->wait_type != WAITTYPE_NONE) {
 | 
					 | 
					 | 
					 | 
					    if (t->wait_type != WAITTYPE_NONE) {
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					        t->wait_type = WAITTYPE_NONE;
 | 
					 | 
					 | 
					 | 
					        t->wait_type = WAITTYPE_NONE;
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					    }
 | 
					 | 
					 | 
					 | 
					    }
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					    __KernelChangeThreadState(t, THREADSTATUS_READY);
 | 
					 | 
					 | 
					 | 
					    __ChangeThreadState(t, THREADSTATUS_READY);
 | 
				
			
			
				
				
			
		
	
		
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					}
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					/// Switches CPU context to that of the specified thread
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					void __SwitchContext(Thread* t, const char* reason) {
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					    Thread* cur = __GetCurrentThread();
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					    
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					    // Save context for current thread
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					    if (cur) {
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					        __SaveContext(cur->context);
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					        
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					        if (cur->IsRunning()) {
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					            __ChangeReadyState(cur, true);
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					        }
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					    }
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					    // Load context of new thread
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					    if (t) {
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					        __SetCurrentThread(t);
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					        __ChangeReadyState(t, false);
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					        t->status = (t->status | THREADSTATUS_RUNNING) & ~THREADSTATUS_READY;
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					        t->wait_type = WAITTYPE_NONE;
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					        __LoadContext(t->context);
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					    } else {
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					        __SetCurrentThread(NULL);
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					    }
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					}
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					/// Gets the next thread that is ready to be run by priority
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					Thread* __NextThread() {
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					    Handle next;
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					    Thread* cur = __GetCurrentThread();
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					    
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					    if (cur && cur->IsRunning()) {
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					        next = g_thread_ready_queue.pop_first_better(cur->current_priority);
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					    } else  {
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					        next = g_thread_ready_queue.pop_first();
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					    }
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					    if (next < 0) {
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					        return NULL;
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					    }
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					    return Kernel::g_object_pool.GetFast<Thread>(next);
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					}
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					/// Resumes a thread from waiting by marking it as "ready"
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					void __ResumeThreadFromWait(Handle handle) {
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					    u32 error;
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					    Thread* t = Kernel::g_object_pool.Get<Thread>(handle, error);
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					    if (t) {
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					        t->status &= ~THREADSTATUS_WAIT;
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					        if (!(t->status & (THREADSTATUS_WAITSUSPEND | THREADSTATUS_DORMANT | THREADSTATUS_DEAD))) {
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					            __ChangeReadyState(t, true);
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					        }
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					    }
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					}
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					/// Puts a thread in the wait state for the given type/reason
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					void __WaitCurThread(WaitType wait_type, const char* reason) {
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					    Thread* t = __GetCurrentThread();
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					    t->wait_type = wait_type;
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					    __ChangeThreadState(t, ThreadStatus(THREADSTATUS_WAIT | (t->status & THREADSTATUS_SUSPEND)));
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					}
 | 
					 | 
					 | 
					 | 
					}
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					
 | 
					 | 
					 | 
					 | 
					
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					/// Creates a new thread
 | 
					 | 
					 | 
					 | 
					/// Creates a new thread
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					Thread* __KernelCreateThread(Handle& handle, const char* name, u32 entry_point, s32 priority,
 | 
					 | 
					 | 
					 | 
					Thread* CreateThread(Handle& handle, const char* name, u32 entry_point, s32 priority,
 | 
				
			
			
				
				
			
		
	
		
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					    s32 processor_id, u32 stack_top, int stack_size) {
 | 
					 | 
					 | 
					 | 
					    s32 processor_id, u32 stack_top, int stack_size) {
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					
 | 
					 | 
					 | 
					 | 
					
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					    Thread* t = new Thread;
 | 
					 | 
					 | 
					 | 
					    Thread* t = new Thread;
 | 
				
			
			
		
	
	
		
		
			
				
					| 
						
						
						
							
								
							
						
					 | 
					 | 
					@ -187,31 +245,31 @@ Thread* __KernelCreateThread(Handle& handle, const char* name, u32 entry_point,
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					}
 | 
					 | 
					 | 
					 | 
					}
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					
 | 
					 | 
					 | 
					 | 
					
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					/// Creates a new thread - wrapper for external user
 | 
					 | 
					 | 
					 | 
					/// Creates a new thread - wrapper for external user
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					Handle __KernelCreateThread(const char* name, u32 entry_point, s32 priority, s32 processor_id,
 | 
					 | 
					 | 
					 | 
					Handle CreateThread(const char* name, u32 entry_point, s32 priority, s32 processor_id,
 | 
				
			
			
				
				
			
		
	
		
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					    u32 stack_top, int stack_size) {
 | 
					 | 
					 | 
					 | 
					    u32 stack_top, int stack_size) {
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					    if (name == NULL) {
 | 
					 | 
					 | 
					 | 
					    if (name == NULL) {
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					        ERROR_LOG(KERNEL, "__KernelCreateThread(): NULL name");
 | 
					 | 
					 | 
					 | 
					        ERROR_LOG(KERNEL, "CreateThread(): NULL name");
 | 
				
			
			
				
				
			
		
	
		
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					        return -1;
 | 
					 | 
					 | 
					 | 
					        return -1;
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					    }
 | 
					 | 
					 | 
					 | 
					    }
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					    if ((u32)stack_size < 0x200) {
 | 
					 | 
					 | 
					 | 
					    if ((u32)stack_size < 0x200) {
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					        ERROR_LOG(KERNEL, "__KernelCreateThread(name=%s): invalid stack_size=0x%08X", name, 
 | 
					 | 
					 | 
					 | 
					        ERROR_LOG(KERNEL, "CreateThread(name=%s): invalid stack_size=0x%08X", name, 
 | 
				
			
			
				
				
			
		
	
		
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					            stack_size);
 | 
					 | 
					 | 
					 | 
					            stack_size);
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					        return -1;
 | 
					 | 
					 | 
					 | 
					        return -1;
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					    }
 | 
					 | 
					 | 
					 | 
					    }
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					    if (priority < THREADPRIO_HIGHEST || priority > THREADPRIO_LOWEST) {
 | 
					 | 
					 | 
					 | 
					    if (priority < THREADPRIO_HIGHEST || priority > THREADPRIO_LOWEST) {
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					        s32 new_priority = CLAMP(priority, THREADPRIO_HIGHEST, THREADPRIO_LOWEST);
 | 
					 | 
					 | 
					 | 
					        s32 new_priority = CLAMP(priority, THREADPRIO_HIGHEST, THREADPRIO_LOWEST);
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					        WARN_LOG(KERNEL, "__KernelCreateThread(name=%s): invalid priority=0x%08X, clamping to %08X",
 | 
					 | 
					 | 
					 | 
					        WARN_LOG(KERNEL, "CreateThread(name=%s): invalid priority=0x%08X, clamping to %08X",
 | 
				
			
			
				
				
			
		
	
		
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					            name, priority, new_priority);
 | 
					 | 
					 | 
					 | 
					            name, priority, new_priority);
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					        // TODO(bunnei): Clamping to a valid priority is not necessarily correct behavior... Confirm
 | 
					 | 
					 | 
					 | 
					        // TODO(bunnei): Clamping to a valid priority is not necessarily correct behavior... Confirm
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					        // validity of this
 | 
					 | 
					 | 
					 | 
					        // validity of this
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					        priority = new_priority;
 | 
					 | 
					 | 
					 | 
					        priority = new_priority;
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					    }
 | 
					 | 
					 | 
					 | 
					    }
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					    if (!Memory::GetPointer(entry_point)) {
 | 
					 | 
					 | 
					 | 
					    if (!Memory::GetPointer(entry_point)) {
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					        ERROR_LOG(KERNEL, "__KernelCreateThread(name=%s): invalid entry %08x", name, entry_point);
 | 
					 | 
					 | 
					 | 
					        ERROR_LOG(KERNEL, "CreateThread(name=%s): invalid entry %08x", name, entry_point);
 | 
				
			
			
				
				
			
		
	
		
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					        return -1;
 | 
					 | 
					 | 
					 | 
					        return -1;
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					    }
 | 
					 | 
					 | 
					 | 
					    }
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					    Handle handle;
 | 
					 | 
					 | 
					 | 
					    Handle handle;
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					    Thread* t = __KernelCreateThread(handle, name, entry_point, priority, processor_id, stack_top, 
 | 
					 | 
					 | 
					 | 
					    Thread* t = CreateThread(handle, name, entry_point, priority, processor_id, stack_top, 
 | 
				
			
			
				
				
			
		
	
		
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					        stack_size);
 | 
					 | 
					 | 
					 | 
					        stack_size);
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					
 | 
					 | 
					 | 
					 | 
					
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					    HLE::EatCycles(32000);
 | 
					 | 
					 | 
					 | 
					    HLE::EatCycles(32000);
 | 
				
			
			
		
	
	
		
		
			
				
					| 
						
						
						
							
								
							
						
					 | 
					 | 
					@ -220,114 +278,62 @@ Handle __KernelCreateThread(const char* name, u32 entry_point, s32 priority, s32
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					    // Technically, this should not eat all at once, and reschedule in the middle, but that's hard.
 | 
					 | 
					 | 
					 | 
					    // Technically, this should not eat all at once, and reschedule in the middle, but that's hard.
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					    HLE::ReSchedule("thread created");
 | 
					 | 
					 | 
					 | 
					    HLE::ReSchedule("thread created");
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					
 | 
					 | 
					 | 
					 | 
					
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					    __KernelCallThread(t);
 | 
					 | 
					 | 
					 | 
					    __CallThread(t);
 | 
				
			
			
				
				
			
		
	
		
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					    
 | 
					 | 
					 | 
					 | 
					    
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					    return handle;
 | 
					 | 
					 | 
					 | 
					    return handle;
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					}
 | 
					 | 
					 | 
					 | 
					}
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					
 | 
					 | 
					 | 
					 | 
					
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					/// Switches CPU context to that of the specified thread
 | 
					 | 
					 | 
					 | 
					/// Gets the current thread
 | 
				
			
			
				
				
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					void __KernelSwitchContext(Thread* t, const char* reason) {
 | 
					 | 
					 | 
					 | 
					Handle GetCurrentThread() {
 | 
				
			
			
				
				
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					    Thread* cur = __GetCurrentThread();
 | 
					 | 
					 | 
					 | 
					    return __GetCurrentThread()->GetHandle();
 | 
				
			
			
				
				
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					    
 | 
					 | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					    // Save context for current thread
 | 
					 | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					    if (cur) {
 | 
					 | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					        __KernelSaveContext(cur->context);
 | 
					 | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					        
 | 
					 | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					        if (cur->IsRunning()) {
 | 
					 | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					            __KernelChangeReadyState(cur, true);
 | 
					 | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					        }
 | 
					 | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					    }
 | 
					 | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					    // Load context of new thread
 | 
					 | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					    if (t) {
 | 
					 | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					        __SetCurrentThread(t);
 | 
					 | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					        __KernelChangeReadyState(t, false);
 | 
					 | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					        t->status = (t->status | THREADSTATUS_RUNNING) & ~THREADSTATUS_READY;
 | 
					 | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					        t->wait_type = WAITTYPE_NONE;
 | 
					 | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					        __KernelLoadContext(t->context);
 | 
					 | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					    } else {
 | 
					 | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					        __SetCurrentThread(NULL);
 | 
					 | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					    }
 | 
					 | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					}
 | 
					 | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					
 | 
					 | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					/// Gets the next thread that is ready to be run by priority
 | 
					 | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					Thread* __KernelNextThread() {
 | 
					 | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					    Handle next;
 | 
					 | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					    Thread* cur = __GetCurrentThread();
 | 
					 | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					    
 | 
					 | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					    if (cur && cur->IsRunning()) {
 | 
					 | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					        next = g_thread_ready_queue.pop_first_better(cur->current_priority);
 | 
					 | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					    } else  {
 | 
					 | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					        next = g_thread_ready_queue.pop_first();
 | 
					 | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					    }
 | 
					 | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					    if (next < 0) {
 | 
					 | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					        return NULL;
 | 
					 | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					    }
 | 
					 | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					    return Kernel::g_object_pool.GetFast<Thread>(next);
 | 
					 | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
	
		
		
	
		
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					}
 | 
					 | 
					 | 
					 | 
					}
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					
 | 
					 | 
					 | 
					 | 
					
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					/// Sets up the primary application thread
 | 
					 | 
					 | 
					 | 
					/// Sets up the primary application thread
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					Handle __KernelSetupMainThread(s32 priority, int stack_size) {
 | 
					 | 
					 | 
					 | 
					Handle SetupMainThread(s32 priority, int stack_size) {
 | 
				
			
			
				
				
			
		
	
		
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					    Handle handle;
 | 
					 | 
					 | 
					 | 
					    Handle handle;
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					    
 | 
					 | 
					 | 
					 | 
					    
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					    // Initialize new "main" thread
 | 
					 | 
					 | 
					 | 
					    // Initialize new "main" thread
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					    Thread* t = __KernelCreateThread(handle, "main", Core::g_app_core->GetPC(), priority, 
 | 
					 | 
					 | 
					 | 
					    Thread* t = CreateThread(handle, "main", Core::g_app_core->GetPC(), priority, 
 | 
				
			
			
				
				
			
		
	
		
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					        THREADPROCESSORID_0, Memory::SCRATCHPAD_VADDR_END, stack_size);
 | 
					 | 
					 | 
					 | 
					        THREADPROCESSORID_0, Memory::SCRATCHPAD_VADDR_END, stack_size);
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					    
 | 
					 | 
					 | 
					 | 
					    
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					    __KernelResetThread(t, 0);
 | 
					 | 
					 | 
					 | 
					    __ResetThread(t, 0);
 | 
				
			
			
				
				
			
		
	
		
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					    
 | 
					 | 
					 | 
					 | 
					    
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					    // If running another thread already, set it to "ready" state
 | 
					 | 
					 | 
					 | 
					    // If running another thread already, set it to "ready" state
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					    Thread* cur = __GetCurrentThread();
 | 
					 | 
					 | 
					 | 
					    Thread* cur = __GetCurrentThread();
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					    if (cur && cur->IsRunning()) {
 | 
					 | 
					 | 
					 | 
					    if (cur && cur->IsRunning()) {
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					        __KernelChangeReadyState(cur, true);
 | 
					 | 
					 | 
					 | 
					        __ChangeReadyState(cur, true);
 | 
				
			
			
				
				
			
		
	
		
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					    }
 | 
					 | 
					 | 
					 | 
					    }
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					    
 | 
					 | 
					 | 
					 | 
					    
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					    // Run new "main" thread
 | 
					 | 
					 | 
					 | 
					    // Run new "main" thread
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					    __SetCurrentThread(t);
 | 
					 | 
					 | 
					 | 
					    __SetCurrentThread(t);
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					    t->status = THREADSTATUS_RUNNING;
 | 
					 | 
					 | 
					 | 
					    t->status = THREADSTATUS_RUNNING;
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					    __KernelLoadContext(t->context);
 | 
					 | 
					 | 
					 | 
					    __LoadContext(t->context);
 | 
				
			
			
				
				
			
		
	
		
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					
 | 
					 | 
					 | 
					 | 
					
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					    return handle;
 | 
					 | 
					 | 
					 | 
					    return handle;
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					}
 | 
					 | 
					 | 
					 | 
					}
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					
 | 
					 | 
					 | 
					 | 
					
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					/// Resumes a thread from waiting by marking it as "ready"
 | 
					 | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					void __KernelResumeThreadFromWait(Handle handle) {
 | 
					 | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					    u32 error;
 | 
					 | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					    Thread* t = Kernel::g_object_pool.Get<Thread>(handle, error);
 | 
					 | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					    if (t) {
 | 
					 | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					        t->status &= ~THREADSTATUS_WAIT;
 | 
					 | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					        if (!(t->status & (THREADSTATUS_WAITSUSPEND | THREADSTATUS_DORMANT | THREADSTATUS_DEAD))) {
 | 
					 | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					            __KernelChangeReadyState(t, true);
 | 
					 | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					        }
 | 
					 | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					    }
 | 
					 | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					}
 | 
					 | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					
 | 
					 | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					/// Puts a thread in the wait state for the given type/reason
 | 
					 | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					void __KernelWaitCurThread(WaitType wait_type, const char* reason) {
 | 
					 | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					    Thread* t = __GetCurrentThread();
 | 
					 | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					    t->wait_type = wait_type;
 | 
					 | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					    __KernelChangeThreadState(t, ThreadStatus(THREADSTATUS_WAIT | (t->status & THREADSTATUS_SUSPEND)));
 | 
					 | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					}
 | 
					 | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					
 | 
					 | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					/// Reschedules to the next available thread (call after current thread is suspended)
 | 
					 | 
					 | 
					 | 
					/// Reschedules to the next available thread (call after current thread is suspended)
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					void __KernelReschedule(const char* reason) {
 | 
					 | 
					 | 
					 | 
					void Reschedule(const char* reason) {
 | 
				
			
			
				
				
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					    Thread* next = __KernelNextThread();
 | 
					 | 
					 | 
					 | 
					    Thread* next = __NextThread();
 | 
				
			
			
				
				
			
		
	
		
		
	
		
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					    if (next > 0) {
 | 
					 | 
					 | 
					 | 
					    if (next > 0) {
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					        __KernelSwitchContext(next, reason);
 | 
					 | 
					 | 
					 | 
					        __SwitchContext(next, reason);
 | 
				
			
			
				
				
			
		
	
		
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					    }
 | 
					 | 
					 | 
					 | 
					    }
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					}
 | 
					 | 
					 | 
					 | 
					}
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					
 | 
					 | 
					 | 
					 | 
					
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					////////////////////////////////////////////////////////////////////////////////////////////////////
 | 
					 | 
					 | 
					 | 
					////////////////////////////////////////////////////////////////////////////////////////////////////
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					
 | 
					 | 
					 | 
					 | 
					
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					/// Put current thread in a wait state - on WaitSynchronization
 | 
					 | 
					 | 
					 | 
					/// Put current thread in a wait state - on WaitSynchronization
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					void __KernelWaitThread_Synchronization() {
 | 
					 | 
					 | 
					 | 
					void WaitThread_Synchronization() {
 | 
				
			
			
				
				
			
		
	
		
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					    // TODO(bunnei): Just a placeholder function for now... FixMe
 | 
					 | 
					 | 
					 | 
					    // TODO(bunnei): Just a placeholder function for now... FixMe
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					    __KernelWaitCurThread(WAITTYPE_SYNCH, "waitSynchronization called");
 | 
					 | 
					 | 
					 | 
					    __WaitCurThread(WAITTYPE_SYNCH, "waitSynchronization called");
 | 
				
			
			
				
				
			
		
	
		
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					}
 | 
					 | 
					 | 
					 | 
					}
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					
 | 
					 | 
					 | 
					 | 
					
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					////////////////////////////////////////////////////////////////////////////////////////////////////
 | 
					 | 
					 | 
					 | 
					////////////////////////////////////////////////////////////////////////////////////////////////////
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					
 | 
					 | 
					 | 
					 | 
					
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					void __KernelThreadingInit() {
 | 
					 | 
					 | 
					 | 
					void ThreadingInit() {
 | 
				
			
			
				
				
			
		
	
		
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					}
 | 
					 | 
					 | 
					 | 
					}
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					
 | 
					 | 
					 | 
					 | 
					
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					void __KernelThreadingShutdown() {
 | 
					 | 
					 | 
					 | 
					void ThreadingShutdown() {
 | 
				
			
			
				
				
			
		
	
		
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					}
 | 
					 | 
					 | 
					 | 
					}
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					} // namespace
 | 
				
			
			
		
	
	
		
		
			
				
					| 
						
						
						
					 | 
					 | 
					
 
 |