summaryrefslogblamecommitdiff
path: root/src/kernel/task/sched.cpp
blob: 5b3f0072bdb4f7ff90983a91154c76d2b5b794d6 (plain) (tree)
1
2
3
4
5
6
7
8
9
10


                     
                  
 
                           
 
                                                                

                                                                                        
                                                 
                          

                                           
                

                                          


         
                                                                                   
                                           
                                      
                                
                                                                                                     

                                           
                           



                                                                                
                                             
                                     


                                                              


                                                                                  

                         





                                          

                  





                                                                            

                                                                                                             




                                                                       

                                                                                                                             






                                                    
#include "sched.h"
#include <core/sys.h>
#include <mem/mem.h>
#include <ui/vt.h>

extern thread *idle_thread;

static thread *queue[PRIORITIES] = {0}, *last[PRIORITIES] = {0};

/*	For internal use only. Enqueues specified thread in specified priority queue. */
static void sched_enqueueIn(thread *t, int qid) {
	t->queue_next = 0;
	if (queue[qid] == 0) {
		queue[qid] = last[qid] = t;
	} else {
		last[qid]->queue_next = t;
		last[qid] = t;
	}
}

/*	For internal use only. Pops a thread from specified queue, if available. */
static thread *sched_dequeueFrom(int qid) {
	if (queue[qid] == 0) return 0;
	thread *it = queue[qid];
	ASSERT((it->queue_next == 0 && it == last[qid]) || (it->queue_next != 0 && it != last[qid]));
	queue[qid] = it->queue_next;
	if (queue[qid] == 0) last[qid] = 0;
	it->queue_next = 0;
	return it;
}

/*	Used by task.c. Enqueus a thread in the corresponding priority queue. */
void sched_enqueue(thread *t, int priority) {
	if (t == idle_thread) return;
	if (priority >= PRIORITIES) priority = PRIORITIES - 1;
	if (priority < 0) return;
	sched_enqueueIn(t, priority);
}

/*	Used by task.c. Pops a thread from the lowest priority non-empty queue. */
thread *sched_dequeue() {
	thread *it = 0;
	int i;
	for (i = 0; i < PRIORITIES; i++) {
		it = sched_dequeueFrom(i);
		if (it != 0) break;
	}
	if (it == 0) return idle_thread;
	return it;
}

/*	Used by task.c. Removes specified thread from any queue it is in. */
void sched_remove(thread *t) {
	for (int i = 0; i < PRIORITIES; i++) {
		if (queue[i] == t) {
			queue[i] = t->queue_next;
			ASSERT((t->queue_next == 0 && last[i] == t) || (t->queue_next != 0 && last[i] != t));
			if (last[i] == t) last[i] = 0;
		} else if (queue[i] != 0) {
			thread *it = queue[i];
			while (it->queue_next != 0) {
				if (it->queue_next == t) {
					it->queue_next = t->queue_next;
					ASSERT((t->queue_next == 0 && last[i] == t) || (t->queue_next != 0 && last[i] != t));
					if (last[i] == t) last[i] = it;
					break;
				}
				it = it->queue_next;
			}
		}
	}
}