-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueue.c
68 lines (61 loc) · 2.16 KB
/
queue.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include "queue.h"
/* Message Queue - an array of OS_message structures */
static OS_message * messageQueue[COMM_QUEUE_MAX_MESSAGES] = {0};
/* Index for the queue - the position of the next available spot */
static uint32_t index = 0;
/* Static Function */
static void dequeue(uint32_t messageIndex);
/* Initialises a message structure
* Takes a pointer to a message, returns nothing
* Sets the data and recipient fields of that structure to 0
*/
void OS_initMessage(OS_message * message) {
message->data = 0;
message->recipient = 0;
}
/* Adds a message to the queue - allows a task to 'send' messages.
* Takes a pointer to a message, returns nothing
* Checks that the queue is not full, and if not then adds the pointer
* to the queue at the next available point, and increments index.
*/
void OS_sendMessage(OS_message * message) {
if(index < COMM_QUEUE_MAX_MESSAGES) {
messageQueue[index] = message;
index += 1;
}
}
/* Returns a message to the current TCB - allows a task to 'read' messages.
* Takse nothing, returns a uint32_t.
* Creates a temporary variable initialised to 0 in case there is no data to return
* Loops through the queue checking if the current TCB is the recipient of
* any of the messages. If it is, then the message is dequeued and the data returned.
*/
uint32_t * OS_readMessage(void) {
uint32_t * returnData = 0;
for (int i = 0; i <= index; i++) {
if ((uint32_t) messageQueue[i]->recipient == (uint32_t) OS_currentTCB()) {
returnData = messageQueue[i]->data;
dequeue(i);
return returnData;
}
}
// If it gets to here than there is no messages in the queue for the task
return returnData;
}
/* Deqeues a given message from the queue
* Takes an integer as the element to dequeue, returns nothing
* Sets the index of the message to 0, and then shuffles every other
* element down the queue.
*/
static void dequeue(uint32_t messageIndex) {
messageQueue[messageIndex] = 0;
for (int i = messageIndex; i < COMM_QUEUE_MAX_MESSAGES - 1; ++i) {
if (messageQueue[i] == 0) {
index = i;
break;
} else {
messageQueue[i] = messageQueue[i + 1];
index = i;
}
}
}