Пример #1
0
/* Called by the currently running thread in order to give up the CPU.
	The scheduler selects the next thread from the ready queue to load onto 
	the CPU, and calls the dispatcher function defined in 'threads.h' to
	do the context switch. */
void Scheduler::yield() {
	//check if disk's last read/write is ready, the blocked thread needs to be resumed then from the disk's queue
	if(this->disk != NULL && this->disk->is_ready()) {
		this->disk->resume_from_queue();
	}

	if(this->head == NULL) {
		Console::puts("Error: Ready queue is NULL.\n");
	} else {
		//disable interrupts
		if(machine_interrupts_enabled())
			machine_disable_interrupts();
		
		Thread *the_one;
		the_one = this->head->thread_ptr;
		this->head = this->head->next;
		Thread::dispatch_to(the_one);
		
		//disable interrupts
		if(!machine_interrupts_enabled())
			machine_enable_interrupts();
	}
}
void machine_disable_interrupts() {
    assert(machine_interrupts_enabled());
    __asm__ __volatile__ ("cli");
}
void machine_enable_interrupts() {
    assert(!machine_interrupts_enabled());
    __asm__ __volatile__ ("sti");
}