+void Proc_DumpThreadCPUState(tThread *Thread)
+{
+ Log(" At %04x:%016llx", Thread->SavedState.UserCS, Thread->SavedState.UserRIP);
+}
+
+void Proc_Reschedule(void)
+{
+ tThread *nextthread, *curthread;
+ int cpu = GetCPUNum();
+
+ // TODO: Wait for it?
+ if(IS_LOCKED(&glThreadListLock)) return;
+
+ curthread = gaCPUs[cpu].Current;
+
+ nextthread = Threads_GetNextToRun(cpu, curthread);
+
+ if(nextthread == curthread) return ;
+ if(!nextthread)
+ nextthread = gaCPUs[cpu].IdleThread;
+ if(!nextthread)
+ return ;
+
+ #if DEBUG_TRACE_SWITCH
+ LogF("\nSwitching to task CR3 = 0x%x, RIP = %p, RSP = %p - %i (%s)\n",
+ nextthread->MemState.CR3,
+ nextthread->SavedState.RIP,
+ nextthread->SavedState.RSP,
+ nextthread->TID,
+ nextthread->ThreadName
+ );
+ #endif
+
+ // Update CPU state
+ gaCPUs[cpu].Current = nextthread;
+ gTSSs[cpu].RSP0 = nextthread->KernelStack-4;
+ __asm__ __volatile__ ("mov %0, %%db0" : : "r" (nextthread));
+
+ SwitchTasks(
+ nextthread->SavedState.RSP, &curthread->SavedState.RSP,
+ nextthread->SavedState.RIP, &curthread->SavedState.RIP,
+ nextthread->MemState.CR3
+ );
+ return ;
+}
+