All pastes #1528513 Raw Edit

richard

public text v1 · immutable
#1528513 ·published 2009-08-13 20:29 UTC
rendered paste body
#include "sched.h"#include "terminal.h"#include "task.h"#include "interrupts.h"Scheduler scheduler;void Scheduler::do_it(registers *regs) {	schedule(regs);}int Scheduler::addTask(Task* t) {	if(t==0) {		PANIC("Added bad task");	}	if(!t->page_dir.is_set()) {		con->putval("invalid page directory for task",ntasks+1);		con->putval("Set as: ",t->page_dir.physical_address);		PANIC("INVALID PAGE DIRECTORY");	}	t->pid=ntasks++;	t->setNext(head);	head=t;	return t->pid;}void Scheduler::schedule(registers *regs) {	if(currTask==0) {		con->print("\n***First scheduler invocation!\n ");		currTask=head;	} else {		currTask->store_regs(regs);	}	if(currTask->_state==Task::RUNNING)		currTask->setState(Task::RUNNABLE);	if(currTask==currTask->getNext())		PANIC("Circular list in scheduler...");	Task* prev=currTask;	Task* next=currTask->_next;	// go through the list looking for the next runnable task.	for(;;) {		if(next==0) { // make the list wrap around.			next=head;			prev=0;		}		if(next->_state==Task::RUNNABLE) {			// we are looking for a runnable task.			break;		} else if(next->_state==Task::EXITED) {			// remove dead tasks from the list.			Task *n=next->getNext();			if(prev==0) {				head=n;			} else {				prev->setNext(n);			}			next->page_dir.switch_to_this();			next->page_dir.destroy();			delete next;			next=n;			continue;		} else {			// advance the iterator.			prev=next;			next=next->getNext();		}	}	next->restore_regs(regs);	currTask=next;	if(currTask->_state!=Task::RUNNABLE) {		PANIC("Task to run is not runnable!");	}	currTask->setState(Task::RUNNING);	currTask->page_dir.switch_to_this();    return;}