BarryServer : Git

All the code for all my projects
// BarryServer : Git / Nucleus / blob / 3a1c5d910225135fd7316c50f5cd7f97b18b62f9 / task / scheduler.c

// Related

Nucleus

Barry Priority-based round-robin scheduler 3a1c5d9 (3 years, 3 months ago)
/*
 * This file contains the scheduler.  It implements a basic task switching
 * routine, as well as the schedule() function.  The scheduler can be called
 * from anywhere, and will switch to the next task decided by the scheduler
 * rules.  If it cannot find a task to schedule, it just idles until one becomes
 * available.  This avoids the need for an idle task.
 */

#include <nucleus/panic.h>
#include <nucleus/task.h>

#define PRIORITY_COUNT 6

TaskQueue *readyQueue[PRIORITY_COUNT];

/* Read the EIP */
static uintptr_t
read_eip(void)
{
	uintptr_t eip;
	asm volatile("movl 4(%%ebp), %0" : "=r" (eip));
	return eip;
}

/* Switch to a task */
static void
switch_to_task(Task *task)
{
	uintptr_t esp, ebp, eip;
	asm volatile("mov %%esp, %0" : "=r" (esp));
	asm volatile("mov %%ebp, %0" : "=r" (ebp));
	eip = (uintptr_t) &&end;

	current->esp = esp;
	current->ebp = ebp;
	current->eip = eip;
	put(current);
	current = task; /* Use the passed reference */
	esp = current->esp;
	ebp = current->ebp;
	eip = current->eip;

	asm volatile (
		"cli;"
		"movl %0, %%ecx;"
		"movl %1, %%esp;"
		"movl %2, %%ebp;"
		"movl %3, %%cr3;"
		"sti;"
		"jmp *%%ecx"
		:: "g" (eip), "g" (esp), "g" (ebp), "g" (current->pageDir)
	);
end:
}

/* Find the next schedulable ready queue */
static TaskQueue *
highest_priority_queue(void)
{
	enum Priority p;
	for (p = PRIORITY_COUNT - 1; p > 0; p--) {
		if (readyQueue[p]->start)
			return readyQueue[p];
	}
	return NULL;
}

/* Schedule the next task */
void
schedule(void)
{
	Task *task = current;
	TaskQueue *queue = highest_priority_queue();

	/* Next schedulable task */
	if (queue) {
		task = pop_from_queue(queue);
		task->state = RUNNING;
		if (current->state == RUNNING) {
			current->state = READY;
			add_to_queue(readyQueue[current->priority], current);
		}
		switch_to_task(task);
	/* Idle */
	} else if (current->state != RUNNING) {
		current = NULL;
		asm volatile("sti");
		while (!(queue = highest_priority_queue()))
			asm volatile("hlt");
		asm volatile("cli");
		current = task;
		task = pop_from_queue(queue);
		task->state = RUNNING;
		switch_to_task(task);
	}
}

/* Initialise the scheduler */
void
init_scheduler(void)
{
	enum Priority p;
	for (p = 0; p < PRIORITY_COUNT; p++)
		readyQueue[p] = new(&taskQueueType);
}