Roomba Tank
CSC 460 Project 3
queue.c
Go to the documentation of this file.
1 #include "queue.h"
2 
3 /*
4  * Checks if queue is full
5  */
6 volatile int isFull(volatile int *QCount) {
7  return *QCount == MAXTHREAD - 1;
8 }
9 
10 /*
11  * Checks if queue is empty, READY QUEUE SHOULD NEVER BE EMPTY
12  */
13 volatile int isEmpty(volatile int *QCount) {
14  return *QCount == 0;
15 }
16 
17 void enqueueWQ(volatile PD **p, volatile PD **Queue, volatile int *QCount) {
18  if(isFull(QCount)) {
19  return;
20  }
21 
22  int i = (*QCount) - 1;
23 
24  while(i >= 0) {
25  Queue[i+1] = Queue[i];
26  i--;
27  }
28 
29  Queue[i+1] = *p;
30  (*QCount)++;
31 }
32 
33 void enqueueSQ(volatile PD **p, volatile PD **Queue, volatile int *QCount) {
34  if(isFull(QCount)) {
35  return;
36  }
37 
38  int i = (*QCount) - 1;
39 
40  volatile PD *new = *p;
41 
42  volatile PD *temp = Queue[i];
43 
44  while(i >= 0 && ((new->wakeTickOverflow > temp->wakeTickOverflow) || ((new->wakeTickOverflow >= temp->wakeTickOverflow) && (new->wakeTick >= temp->wakeTick)))) {
45  Queue[i+1] = Queue[i];
46  i--;
47  temp = Queue[i];
48  }
49 
50  Queue[i+1] = *p;
51  (*QCount)++;
52 }
53 
54 /*
55  * Insert into the queue sorted by priority
56  */
57 void enqueueRQ(volatile PD **p, volatile PD **Queue, volatile int *QCount) {
58  if(isFull(QCount)) {
59  return;
60  }
61 
62  int i = (*QCount) - 1;
63 
64  volatile PD *new = *p;
65 
66  volatile PD *temp = Queue[i];
67 
68  while(i >= 0 && (new->inheritedPy >= temp->inheritedPy)) {
69  Queue[i+1] = Queue[i];
70  i--;
71  temp = Queue[i];
72  }
73 
74  Queue[i+1] = *p;
75  (*QCount)++;
76 }
77 
78 /*
79  * Return the first element of the queue with the correct MUTEX m
80  */
81 volatile PD *dequeueWQ(volatile PD **Queue, volatile int *QCount, MUTEX m) {
82 
83  if(isEmpty(QCount)) {
84  return NULL;
85  }
86 
87  int i,j;
88  volatile PD* result = NULL;
89  for (i = (*QCount)-1; i>=0; i--) {
90  if(Queue[i]->m == m){
91  result = Queue[i];
92  break;
93  }
94  }
95  if(result != NULL) {
96  while(i<(*QCount)-1) {
97  Queue[i] = Queue[i+1];
98  i++;
99  }
100  (*QCount)--;
101  }
102 
103  return result;
104 }
105 
106 /*
107  * Return the first element of the Ready Queue
108  */
109 volatile PD *dequeueRQ(volatile PD **Queue, volatile int *QCount) {
110 
111  if(isEmpty(QCount)) {
112  return NULL;
113  }
114 
115  int i,j;
116  volatile PD* result = NULL;
117  for (i = (*QCount)-1; i >= 0; i--) {
118  if((Queue[i]->suspended == 0) && (Queue[i]->state == READY)) {
119  result = Queue[i];
120  break;
121  }
122  }
123  if(result != NULL) {
124  while(i < (*QCount)-1) {
125  Queue[i] = Queue[i+1];
126  i++;
127  }
128  (*QCount)--;
129  }
130 
131  return result;
132 }
133 
134 /*
135  * Return the first element of the queue
136  */
137 volatile PD *dequeue(volatile PD **Queue, volatile int *QCount) {
138 
139  if(isEmpty(QCount)) {
140  return;
141  }
142 
143  volatile PD *result = (Queue[(*QCount)-1]);
144  (*QCount)--;
145 
146  return result;
147 }
volatile int isFull(volatile int *QCount)
Definition: queue.c:6
Definition: os.h:32
volatile PD * dequeue(volatile PD **Queue, volatile int *QCount)
Definition: queue.c:137
TICK wakeTick
Definition: os.h:115
void enqueueRQ(volatile PD **p, volatile PD **Queue, volatile int *QCount)
Definition: queue.c:57
volatile int isEmpty(volatile int *QCount)
Definition: queue.c:13
PRIORITY inheritedPy
Definition: os.h:109
volatile PD * dequeueRQ(volatile PD **Queue, volatile int *QCount)
Definition: queue.c:109
TICK wakeTickOverflow
Definition: os.h:114
unsigned int MUTEX
Definition: os.h:22
#define MAXTHREAD
Definition: os.h:4
#define NULL
Definition: os.h:13
void enqueueSQ(volatile PD **p, volatile PD **Queue, volatile int *QCount)
Definition: queue.c:33
volatile PD * dequeueWQ(volatile PD **Queue, volatile int *QCount, MUTEX m)
Definition: queue.c:81
void enqueueWQ(volatile PD **p, volatile PD **Queue, volatile int *QCount)
Definition: queue.c:17