forked from amescon/raspicomm-module
-
Notifications
You must be signed in to change notification settings - Fork 1
/
queue.c
47 lines (41 loc) · 1002 Bytes
/
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
#include <linux/kernel.h>
#include "module.h"
#include "queue.h"
int queue_get_room(queue_t* queue)
{
/* cache read / write for thread safety as they might be modified during the evaluation */
int read, write;
read = queue->read;
write = queue->write;
return (read > write) ? (read - write - 1) : (QUEUE_SIZE - 1 - write + read);
}
int queue_is_full(queue_t* queue)
{
return (((queue->write + 1) % QUEUE_SIZE) == queue->read);
}
int queue_is_empty(queue_t* queue)
{
return (queue->write == queue->read);
}
int queue_enqueue(queue_t* queue, int item)
{
if (queue_is_full(queue)) {
return 0;
}
else {
queue->arr[queue->write] = item;
queue->write = ((queue->write + 1) == QUEUE_SIZE) ? 0 : queue->write + 1;
return 1;
}
}
int queue_dequeue(queue_t* queue, int* item)
{
if (queue_is_empty(queue)) {
return 0;
}
else {
*item = queue->arr[queue->read];
queue->read = ((queue->read + 1) == QUEUE_SIZE) ? 0 : queue->read + 1;
return 1;
}
}