-
Notifications
You must be signed in to change notification settings - Fork 0
/
queue.c
78 lines (63 loc) · 1.18 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
69
70
71
72
73
74
75
76
77
78
// SYS86 project
// Lock-free byte queue for single reader / single writer
#include "queue.h"
#include "heap.h"
void queue_init (queue_s * q, word_t s)
{
q->wr = 0;
q->rd = 0;
q->sz = s;
q->buf = heap_alloc (s, HEAP_TAG_QUEUE);
}
int queue_not_empty (queue_s * q)
{
if (q->rd == q->wr) return 0;
return 1;
}
byte_t queue_get (queue_s * q)
{
byte_t c = q->buf [q->rd];
q->rd = (q->rd + 1) & (q->sz - 1);
return c;
}
int queue_not_full (queue_s * q)
{
if (((q->wr + 1) & (q->sz - 1)) == q->rd) return 0;
return 1;
}
void queue_put (queue_s * q, byte_t c)
{
q->buf [q->wr] = c;
q->wr = (q->wr + 1) & (q->sz - 1);
}
word_t queue_len (queue_s * q)
{
return (q->wr - q->rd) & (q->sz - 1);
}
word_t queue_read (queue_s * q, byte_t * buf, word_t c1)
{
word_t c2 = 0;
word_t r1 = q->rd;
while (c2 < c1)
{
if (r1 == q->wr) break;
buf [c2++] = q->buf [r1];
r1 = (r1 + 1) & (q->sz - 1);
}
q->rd = r1;
return c2;
}
word_t queue_write (queue_s * q, byte_t * buf, word_t c1)
{
word_t c2 = 0;
word_t w1 = q->wr;
while (c2 < c1)
{
word_t w2 = (w1 + 1) & (q->sz - 1);
if (w2 == q->rd) break;
q->buf [w1] = buf [c2++];
w1 = w2;
}
q->wr = w1;
return c2;
}