Revision 336
Updated wireless to use a circular buffer instead of a queue using malloc. Tested on both the computer and robots with a token ring, and was successful.
queue.c | ||
---|---|---|
37 | 37 |
|
38 | 38 |
#include <stdlib.h> |
39 | 39 |
#include <stdio.h> |
40 |
#ifndef ROBOT |
|
41 |
#include <pthread.h> |
|
42 |
#endif |
|
40 | 43 |
|
41 | 44 |
/** |
42 | 45 |
* @struct node_def |
... | ... | |
67 | 70 |
|
68 | 71 |
q->head = NULL; |
69 | 72 |
q->size = 0; |
73 |
|
|
74 |
#ifndef ROBOT |
|
75 |
pthread_mutex_init(&(q->lock), NULL); |
|
76 |
#endif |
|
77 |
|
|
70 | 78 |
return q; |
71 | 79 |
} |
72 | 80 |
|
... | ... | |
84 | 92 |
free(t); |
85 | 93 |
t = t2; |
86 | 94 |
} |
95 |
|
|
96 |
#ifndef ROBOT |
|
97 |
pthread_mutex_destroy(&(q->lock)); |
|
98 |
#endif |
|
99 |
|
|
87 | 100 |
free(q); |
88 | 101 |
} |
89 | 102 |
|
... | ... | |
93 | 106 |
* @param q the queue to add an element to |
94 | 107 |
* @param item the item to add to the queue |
95 | 108 |
**/ |
96 |
void queue_add(Queue* q, void* item)
|
|
109 |
int queue_add(Queue* q, void* item)
|
|
97 | 110 |
{ |
111 |
#ifndef ROBOT |
|
112 |
pthread_mutex_lock(&(q->lock)); |
|
113 |
#endif |
|
114 |
|
|
98 | 115 |
Node* n = (Node*)malloc(sizeof(Node)); |
116 |
if (n == NULL) |
|
117 |
{ |
|
118 |
#ifndef ROBOT |
|
119 |
pthread_mutex_unlock(&(q->lock)); |
|
120 |
#endif |
|
121 |
return -1; |
|
122 |
} |
|
123 |
|
|
99 | 124 |
n->val = item; |
100 | 125 |
n->next = NULL; |
101 | 126 |
|
... | ... | |
106 | 131 |
} |
107 | 132 |
else |
108 | 133 |
{ |
134 |
|
|
109 | 135 |
q->tail->next = n; |
110 | 136 |
q->tail = n; |
111 | 137 |
} |
112 | 138 |
|
113 | 139 |
q->size++; |
140 |
|
|
141 |
#ifndef ROBOT |
|
142 |
pthread_mutex_unlock(&(q->lock)); |
|
143 |
#endif |
|
144 |
|
|
145 |
return 0; |
|
114 | 146 |
} |
115 | 147 |
|
116 | 148 |
/** |
... | ... | |
122 | 154 |
**/ |
123 | 155 |
void* queue_remove(Queue* q) |
124 | 156 |
{ |
157 |
#ifndef ROBOT |
|
158 |
pthread_mutex_lock(&(q->lock)); |
|
159 |
#endif |
|
160 |
|
|
125 | 161 |
Node* first = q->head; |
126 | 162 |
if (first == NULL) |
127 | 163 |
{ |
128 | 164 |
WL_DEBUG_PRINT("Attempting to remove item \ |
129 | 165 |
from empty queue.\r\n"); |
130 | 166 |
WL_DEBUG_PRINT_INT(queue_size(q)); |
167 |
#ifndef ROBOT |
|
168 |
pthread_mutex_unlock(&(q->lock)); |
|
169 |
#endif |
|
170 |
return NULL; |
|
131 | 171 |
} |
132 | 172 |
void* ret = first->val; |
133 | 173 |
q->head = first->next; |
... | ... | |
135 | 175 |
q->tail = NULL; |
136 | 176 |
free(first); |
137 | 177 |
q->size--; |
178 |
|
|
179 |
#ifndef ROBOT |
|
180 |
pthread_mutex_unlock(&(q->lock)); |
|
181 |
#endif |
|
182 |
|
|
138 | 183 |
return ret; |
139 | 184 |
} |
140 | 185 |
|
... | ... | |
146 | 191 |
**/ |
147 | 192 |
void queue_remove_all(Queue* q, void* item) |
148 | 193 |
{ |
194 |
#ifndef ROBOT |
|
195 |
pthread_mutex_lock(&(q->lock)); |
|
196 |
#endif |
|
197 |
|
|
149 | 198 |
Node* curr = q->head; |
150 | 199 |
Node* prev = NULL; |
151 | 200 |
|
... | ... | |
167 | 216 |
prev = curr; |
168 | 217 |
curr = next; |
169 | 218 |
} |
219 |
|
|
220 |
#ifndef ROBOT |
|
221 |
pthread_mutex_unlock(&(q->lock)); |
|
222 |
#endif |
|
170 | 223 |
} |
171 | 224 |
|
172 | 225 |
/** |
... | ... | |
177 | 230 |
**/ |
178 | 231 |
int queue_size(Queue* q) |
179 | 232 |
{ |
180 |
return q->size; |
|
233 |
int size; |
|
234 |
|
|
235 |
#ifndef ROBOT |
|
236 |
pthread_mutex_lock(&(q->lock)); |
|
237 |
#endif |
|
238 |
|
|
239 |
size = q->size; |
|
240 |
|
|
241 |
#ifndef ROBOT |
|
242 |
pthread_mutex_unlock(&(q->lock)); |
|
243 |
#endif |
|
244 |
|
|
245 |
return size; |
|
181 | 246 |
} |
182 | 247 |
|
183 | 248 |
/** |
... | ... | |
188 | 253 |
**/ |
189 | 254 |
int queue_is_empty(Queue* q) |
190 | 255 |
{ |
191 |
return q->size == 0; |
|
256 |
int result; |
|
257 |
|
|
258 |
#ifndef ROBOT |
|
259 |
pthread_mutex_lock(&(q->lock)); |
|
260 |
#endif |
|
261 |
|
|
262 |
result = q->size == 0; |
|
263 |
|
|
264 |
#ifndef ROBOT |
|
265 |
pthread_mutex_unlock(&(q->lock)); |
|
266 |
#endif |
|
267 |
|
|
268 |
return result; |
|
192 | 269 |
} |
193 | 270 |
|
Also available in: Unified diff