Skip to content

Commit

Permalink
adds comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Sail338 committed Feb 4, 2018
1 parent cff58ff commit 4c2bee7
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 21 deletions.
48 changes: 29 additions & 19 deletions Asst1/util.c
Expand Up @@ -4,12 +4,18 @@
*
**/
#include "util.h"
/**
*Enqueues a Node into the multi level queue. We assume the node has already been malloced the apprpirate level is already set
*@param threadNode * Node : A threadNode that has been malloced
*
**/

void enqueue(threadNode *Node){
//check if the head if the queue is null
//check if the head if the queue is null and initialize relvant values
threadQ* threadq = scheduler->tq[Node->qlevel];
if(threadq == NULL){
threadq = malloc(sizeof(threadQ));
_thread_q_init(Node,threadq);
thread_q_init(Node,threadq);
threadq ->size =1;
threadq -> min = scheduler->tq[Node->qlevel-1]->max;
threadq->max = threadq->min + ( MULTIPLIER * Node->qlevel * 25000 );
Expand All @@ -18,7 +24,7 @@ void enqueue(threadNode *Node){
}
//front is empty similar if the list has not been malloced yet
else if (threadq -> front == NULL) {
_thread_q_init(Node,threadq);
thread_q_init(Node,threadq);
threadq ->size ++;
}
// add to the end of the Linked list
Expand All @@ -32,12 +38,12 @@ void enqueue(threadNode *Node){
}


threadNode* dequeue ()
{
threadNode* dequeue () {


int curr = 0;
threadQ* threadq = NULL;
threadq = _scan_non_empty(&curr)
threadq = _scan_non_empty(&curr);
if (threadq == NULL)
{
return NULL;
Expand Down Expand Up @@ -65,30 +71,26 @@ threadNode* dequeue ()
threadq -> front = threadq ->front->next;
return tNode;
}

/**
* Scans for non empty bucket in the multi level prioirty queue
* @param int * curr takes a pointer to an iterator which is the current index in our Array of queues
*
*/
threadQ * _scan_non_empty(int * curr){
threadQ* threadq = NULL;
do
{
threadq = scheduler->tq[*curr];
*curr++;
*curr = *curr +1;
if(*curr >= LEVELS){
return NULL;
}
}
while(threadq == NULL || (threadq!=NULL && threadq->front == NULL);
return threadq
return threadq;

}

void _thread_q_init(threadNode * tNode,threadQ* threadq){
threadq -> front = (threadNode *)malloc(sizeof(threadNode));
threadq ->rear = (threadNode *)malloc(sizeof(threadNode));
threadq ->front = tNode;
threadq ->rear = tNode;
threadq ->front ->next = NULL;
threadq -> rear ->next = NULL;
}



Expand All @@ -97,13 +99,13 @@ void mutex_enqueue(threadNode * tNode, my_pthread_mutex_t * mutex)
if (mutex->waitQ == NULL)
{
mutex->waitQ = (threadQ *)malloc(sizeof(threadQ));
_thread_q_init(tNode, mutex->waitQ);
thread_q_init(tNode, mutex->waitQ);
mutex ->waitQ -> size = 1;
}

else if (mutex->waitQ->front == NULL)
{
_thread_q_init(tNode, mutex->waitQ);
thread_q_init(tNode, mutex->waitQ);
mutex ->waitQ ->size =1;
}
else
Expand Down Expand Up @@ -131,3 +133,11 @@ threadNode * mutex_dequeue(my_pthread_mutex_t *mutex){



void thread_q_init(threadNode * tNode,threadQ* threadq){
threadq -> front = (threadNode *)malloc(sizeof(threadNode));
threadq ->rear = (threadNode *)malloc(sizeof(threadNode));
threadq ->front = tNode;
threadq ->rear = tNode;
threadq ->front ->next = NULL;
threadq -> rear ->next = NULL;
}
4 changes: 2 additions & 2 deletions Asst1/util.h
Expand Up @@ -60,6 +60,6 @@ Scheduler* scheduler;
void enqueue(threadNode *);
threadQ* _scan_non_empty(int*curr);
threadNode* dequeue();
threadNode* mutex_dequeue(my_pthread_mutex_t *):
void _thread_q_init(threadNode *,threadQ* );
threadNode* mutex_dequeue(my_pthread_mutex_t *);
void thread_q_init(threadNode *,threadQ*);
#endif

0 comments on commit 4c2bee7

Please sign in to comment.