#include "queue.h"
/*
enqueue
puts pointer into readyQueue based on priority
"higher" priority is numerically larger...
have to think about what this does
since -1 is the initial priority of the mainProc
*/
int enqueue(PROC* ptr)
{
PROC *curr, *nxt;
/*
insert if nothing in readyQueue or if the first thing
in the readyQueue has a lower priority than the
new thing
*/
if(0 == readyQueue || (readyQueue->priority < ptr->priority))
{
ptr->next = readyQueue;
readyQueue = ptr;
return 0;
}/*end if null or this has better priority*/
curr = readyQueue;
nxt = readyQueue;
/*loop until nxt points to first new proc
whose priority is < the new one's priority*/
while( (nxt) && (nxt->priority >= ptr->priority) )
{
curr = nxt;
nxt = nxt->next;
}/*end while still have a current and it's pri >= new one*/
/*make order curr->ptr->nxt*/
ptr->next = nxt;
curr->next = ptr;
return 0;
}/*end enqueue*/
/*
dequeue
get first thing of highest priority
don't call on empty readyQueue
*/
PROC* dequeue()
{
PROC* ptr;
ptr = readyQueue;
readyQueue = readyQueue->next;
return ptr;
}/*end PROC* dequeue*/
/*print contents of the readyQueue*/
int printReadyQueue()
{
PROC* ptr = readyQueue;
myprintf("readyQueue: ");
if(0==ptr)
{
myprintf("<empty>\n\r");
return 0;
}/*end if empty*/
while(ptr)
{
myprintf(" -> (proc [%d], pri [%d])", ptr->pid, ptr->priority);
ptr = ptr->next;
}/*end while ptr not null*/
myprintf("\n\r");
return 0;
}/*end printQueue*/
int readyQueueNumItems()
{
int ret = 0;
PROC* ptr = readyQueue;
while(ptr)
{
ret++;
ptr = ptr->next;
}/*end while ptr not null*/
myprintf("\n\rreadyQueue has %d items\n\r",ret);
return ret;
}/*end readyQueueNumItems*/
int hasPid(int pidToFind)
{
PROC* ptr = readyQueue;
while(ptr)
{
if(pidToFind == ptr->pid)
{
return 1;
}/*end if*/
ptr = ptr->next;
}/*end for*/
return 0;
}/*end hasPid(int pidToFind)*/