-
Notifications
You must be signed in to change notification settings - Fork 0
/
link_fifo.c
58 lines (49 loc) · 1.2 KB
/
link_fifo.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
typedef struct {
BittideFrame frame;
double scheduledTime;
} LinkFrame;
typedef struct {
size_t head;
size_t tail;
size_t size;
uint16_t num_elements;
LinkFrame* data;
} LinkQueue;
LinkFrame link_queue_read(LinkQueue* q) {
LinkFrame handle = q->data[q->tail];
q->tail = (q->tail + 1) % q->size;
q->num_elements--;
return handle;
}
LinkFrame make_linkframe(BittideFrame data, double deadline) {
LinkFrame new_frame = {data, deadline};
return new_frame;
}
LinkFrame link_queue_peek(LinkQueue* q) {
LinkFrame handle = q->data[q->tail];
return handle;
}
double link_get_tail_time(LinkQueue* q){
if (q->num_elements == 0) return DBL_MAX;
LinkFrame handle = q->data[q->tail];
return handle.scheduledTime;
}
int link_queue_write(LinkQueue* q, LinkFrame handle) {
if (((q->head + 1) % q->size) == q->tail) {
return -1;
}
q->data[q->head] = handle;
q->head = (q->head + 1) % q->size;
q->num_elements++;
return 0;
}
static int link_count = 0;
LinkQueue link_make_queue(int size) {
LinkQueue q;
q.head=0;
q.tail=0;
q.size=size;
q.num_elements = 0;
q.data= malloc(sizeof(LinkFrame) * size);
return q;
}