/* * Create a new raw thread object. * Returns a null pointer if there isn't enough memory. */ static struct Kernel_Thread* Create_Thread(int priority, bool detached) { struct Kernel_Thread* kthread; void* stackPage = 0; /* * For now, just allocate one page each for the thread context * object and the thread's stack. */ kthread = Alloc_Page(); if (kthread != 0) stackPage = Alloc_Page(); /* Make sure that the memory allocations succeeded. */ if (kthread == 0) return 0; if (stackPage == 0) { Free_Page(kthread); return 0; } /*Print("New thread @ %x, stack @ %x\n", kthread, stackPage); */ /* * Initialize the stack pointer of the new thread * and accounting info */ Init_Thread(kthread, stackPage, priority, detached); /* Add to the list of all threads in the system. */ Add_To_Back_Of_All_Thread_List(&s_allThreadList, kthread); return kthread; }
void Init_Scheduler(void) { struct Kernel_Thread* mainThread = (struct Kernel_Thread *) KERNEL_THREAD_OBJECT; PrintBoth("Initializing Scheduler\n"); /* * Create initial kernel thread context object and stack, * and make them current. */ Init_Thread(mainThread, (void *) KERNEL_STACK, PRIORITY_NORMAL, true); g_currentThread = mainThread; Add_To_Back_Of_All_Thread_List(&s_allThreadList, mainThread); /* * Create the idle thread. */ /*Print("starting idle thread\n");*/ Start_Kernel_Thread(Idle, 0, PRIORITY_IDLE, true); /* * Create the reaper thread. */ /*Print("starting reaper thread\n");*/ Start_Kernel_Thread(Reaper, 0, PRIORITY_NORMAL, true); }