-
Notifications
You must be signed in to change notification settings - Fork 0
/
chan.c
156 lines (133 loc) · 2.52 KB
/
chan.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#include <stdlib.h>
#include <string.h>
#include "runtime.h"
static bool trysend(Chan *, void *);
static bool tryrecv(Chan *, void *);
static void push(WaitQ *, G *);
static G *pop(WaitQ *);
void *at(Chan *cp, int i) { return cp->ptr + i * cp->elsize; }
bool full(Chan *cp) {
if (cp->cap == 0)
return cp->recvq.head == nil;
return cp->cap == cp->len;
}
bool empty(Chan *cp) {
if (cp->cap == 0)
return cp->sendq.head == nil;
return cp->len == 0;
}
Chan *makechan(int cap, int elsize) {
Chan *cp;
if (!(cp = malloc(cap * elsize + sizeof *cp)))
return nil;
memset(cp, 0, sizeof *cp);
cp->cap = cap;
cp->elsize = elsize;
cp->ptr = cp + 1;
return cp;
}
bool chansend(Chan *cp, void *p, bool block) {
G *gp;
if (!cp)
return false;
if (trysend(cp, p))
return true;
if (!block)
return false;
gp = getg();
gp->p = p;
push(&cp->sendq, gp);
yield();
return true;
}
bool chanrecv(Chan *cp, void *p, bool block) {
G *gp;
if (!cp)
return false;
if (tryrecv(cp, p))
return true;
if (!block)
return false;
gp = getg();
gp->p = p;
push(&cp->recvq, gp);
yield();
return true;
}
void chanclose(Chan *cp) {
G *gp;
if (cp->closed)
throw("chanclose: close of closed channel");
cp->closed = true;
for (;;) {
gp = pop(&cp->recvq);
if (!gp)
break;
ready(gp);
}
for (;;) {
gp = pop(&cp->sendq);
if (!gp)
break;
ready(gp);
}
}
int selectgo(Case *cas, int n, bool block) {
/* TODO */
throw("selectgo: not implemented");
return -1;
}
bool trysend(Chan *cp, void *p) {
G *gp;
void *q;
if (cp->closed)
throw("trysend: send on closed channel");
if (full(cp))
return false;
gp = pop(&cp->recvq);
if (gp) {
memcpy(gp->p, p, cp->elsize);
ready(gp);
return true;
}
q = at(cp, cp->sendx);
memcpy(q, p, cp->elsize);
cp->sendx = (cp->sendx + 1) % cp->cap;
cp->len++;
return true;
}
bool tryrecv(Chan *cp, void *p) {
G *gp;
void *q;
if (cp->closed || empty(cp))
return false;
gp = pop(&cp->sendq);
if (gp) {
memcpy(p, gp->p, cp->elsize);
ready(gp);
return true;
}
q = at(cp, cp->recvx);
memcpy(p, q, cp->elsize);
cp->recvx = (cp->recvx + 1) % cp->cap;
cp->len--;
return true;
}
static void push(WaitQ *q, G *gp) {
if (q->tail)
q->tail->next = gp;
else
q->head = gp;
q->tail = gp;
}
static G *pop(WaitQ *q) {
G *gp;
gp = q->head;
if (gp) {
q->head = gp->next;
gp->next = nil;
}
if (gp == q->tail)
q->tail = nil;
return gp;
}