forked from sustrik/libdill
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chan.c
281 lines (257 loc) · 10.1 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/*
Copyright (c) 2016 Martin Sustrik
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom
the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
*/
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include "cr.h"
#include "libdill.h"
#include "list.h"
#include "utils.h"
struct dill_chan {
/* Table of virtual functions. */
struct hvfs vfs;
/* The size of one element stored in the channel, in bytes. */
size_t sz;
/* List of clauses wanting to receive from the channel. */
struct dill_list in;
/* List of clauses wanting to send to the channel. */
struct dill_list out;
/* 1 if chdone() has been called on this channel. 0 otherwise. */
unsigned int done : 1;
/* 1 if the object was created with chmake_mem(). */
unsigned int mem : 1;
};
/* Channel clause. */
struct dill_chclause {
struct dill_clause cl;
/* An item in either the dill_chan::in or dill_chan::out list. */
struct dill_list item;
void *val;
};
DILL_CT_ASSERT(sizeof(struct chmem) >= sizeof(struct dill_chan));
/******************************************************************************/
/* Handle implementation. */
/******************************************************************************/
static const int dill_chan_type_placeholder = 0;
static const void *dill_chan_type = &dill_chan_type_placeholder;
static void *dill_chan_query(struct hvfs *vfs, const void *type);
static void dill_chan_close(struct hvfs *vfs);
/******************************************************************************/
/* Channel creation and deallocation. */
/******************************************************************************/
int chmake_mem(size_t itemsz, struct chmem *mem) {
if(dill_slow(!mem)) {errno = EINVAL; return -1;}
/* Returns ECANCELED if the coroutine is shutting down. */
int rc = dill_canblock();
if(dill_slow(rc < 0)) return -1;
struct dill_chan *ch = (struct dill_chan*)mem;
ch->vfs.query = dill_chan_query;
ch->vfs.close = dill_chan_close;
ch->sz = itemsz;
dill_list_init(&ch->in);
dill_list_init(&ch->out);
ch->done = 0;
ch->mem = 1;
/* Allocate a handle to point to the channel. */
return hmake(&ch->vfs);
}
int chmake(size_t itemsz) {
struct dill_chan *ch = malloc(sizeof(struct dill_chan));
if(dill_slow(!ch)) {errno = ENOMEM; return -1;}
int h = chmake_mem(itemsz, (struct chmem*)ch);
if(dill_slow(h < 0)) {
int err = errno;
free(ch);
errno = err;
return -1;
}
ch->mem = 0;
return h;
}
static void *dill_chan_query(struct hvfs *vfs, const void *type) {
if(dill_fast(type == dill_chan_type)) return vfs;
errno = ENOTSUP;
return NULL;
}
static void dill_chan_close(struct hvfs *vfs) {
struct dill_chan *ch = (struct dill_chan*)vfs;
dill_assert(ch);
/* Resume any remaining senders and receivers on the channel
with the EPIPE error. */
while(!dill_list_empty(&ch->in)) {
struct dill_chclause *chcl = dill_cont(dill_list_next(&ch->in),
struct dill_chclause, item);
dill_trigger(&chcl->cl, EPIPE);
}
while(!dill_list_empty(&ch->out)) {
struct dill_chclause *chcl = dill_cont(dill_list_next(&ch->out),
struct dill_chclause, item);
dill_trigger(&chcl->cl, EPIPE);
}
if(!ch->mem) free(ch);
}
/******************************************************************************/
/* Sending and receiving. */
/******************************************************************************/
static void dill_chcancel(struct dill_clause *cl) {
struct dill_chclause *chcl = dill_cont(cl, struct dill_chclause, cl);
dill_list_erase(&chcl->item);
}
int chsend(int h, const void *val, size_t len, int64_t deadline) {
int rc = dill_canblock();
if(dill_slow(rc < 0)) return -1;
/* Get the channel interface. */
struct dill_chan *ch = hquery(h, dill_chan_type);
if(dill_slow(!ch)) return -1;
/* Check that the length provided matches the channel length */
if(dill_slow(len != ch->sz)) {errno = EINVAL; return -1;}
/* Check if the channel is done. */
if(dill_slow(ch->done)) {errno = EPIPE; return -1;}
/* Copy the message directly to the waiting receiver, if any. */
if(!dill_list_empty(&ch->in)) {
struct dill_chclause *chcl = dill_cont(dill_list_next(&ch->in),
struct dill_chclause, item);
memcpy(chcl->val, val, len);
dill_trigger(&chcl->cl, 0);
return 0;
}
/* The clause is not available immediately. */
if(dill_slow(deadline == 0)) {errno = ETIMEDOUT; return -1;}
/* Let's wait. */
struct dill_chclause chcl;
dill_list_insert(&chcl.item, &ch->out);
chcl.val = (void*)val;
dill_waitfor(&chcl.cl, 0, dill_chcancel);
struct dill_tmclause tmcl;
dill_timer(&tmcl, 1, deadline);
int id = dill_wait();
if(dill_slow(id < 0)) return -1;
if(dill_slow(id == 1)) {errno = ETIMEDOUT; return -1;}
if(dill_slow(errno != 0)) return -1;
return 0;
}
int chrecv(int h, void *val, size_t len, int64_t deadline) {
int rc = dill_canblock();
if(dill_slow(rc < 0)) return -1;
/* Get the channel interface. */
struct dill_chan *ch = hquery(h, dill_chan_type);
if(dill_slow(!ch)) return -1;
/* Check that the length provided matches the channel length */
if(dill_slow(len != ch->sz)) {errno = EINVAL; return -1;}
/* Check whether the channel is done. */
if(dill_slow(ch->done)) {errno = EPIPE; return -1;}
/* If there's a sender waiting, copy the message directly from the sender. */
if(!dill_list_empty(&ch->out)) {
struct dill_chclause *chcl = dill_cont(dill_list_next(&ch->out),
struct dill_chclause, item);
memcpy(val, chcl->val, len);
dill_trigger(&chcl->cl, 0);
return 0;
}
/* The clause is not immediately available. */
if(dill_slow(deadline == 0)) {errno = ETIMEDOUT; return -1;}
/* Let's wait. */
struct dill_chclause chcl;
dill_list_insert(&chcl.item, &ch->in);
chcl.val = val;
dill_waitfor(&chcl.cl, 0, dill_chcancel);
struct dill_tmclause tmcl;
dill_timer(&tmcl, 1, deadline);
int id = dill_wait();
if(dill_slow(id < 0)) return -1;
if(dill_slow(id == 1)) {errno = ETIMEDOUT; return -1;}
if(dill_slow(errno != 0)) return -1;
return 0;
}
int chdone(int h) {
struct dill_chan *ch = hquery(h, dill_chan_type);
if(dill_slow(!ch)) return -1;
if(ch->done) {errno = EPIPE; return -1;}
ch->done = 1;
/* Resume any remaining senders and receivers on the channel
with the EPIPE error. */
while(!dill_list_empty(&ch->in)) {
struct dill_chclause *chcl = dill_cont(dill_list_next(&ch->in),
struct dill_chclause, item);
dill_trigger(&chcl->cl, EPIPE);
}
while(!dill_list_empty(&ch->out)) {
struct dill_chclause *chcl = dill_cont(dill_list_next(&ch->out),
struct dill_chclause, item);
dill_trigger(&chcl->cl, EPIPE);
}
return 0;
}
int choose(struct chclause *clauses, int nclauses, int64_t deadline) {
int rc = dill_canblock();
if(dill_slow(rc < 0)) return -1;
if(dill_slow(nclauses < 0 || (nclauses != 0 && !clauses))) {
errno = EINVAL; return -1;}
int i;
for(i = 0; i != nclauses; ++i) {
struct chclause *cl = &clauses[i];
struct dill_chan *ch = hquery(cl->ch, dill_chan_type);
if(dill_slow(!ch)) return i;
if(dill_slow(cl->len != ch->sz || (cl->len > 0 && !cl->val))) {
errno = EINVAL; return i;}
struct dill_chclause *chcl;
switch(cl->op) {
case CHSEND:
if(dill_slow(ch->done)) {errno = EPIPE; return i;}
if(dill_list_empty(&ch->in)) break;
chcl = dill_cont(dill_list_next(&ch->in),
struct dill_chclause, item);
memcpy(chcl->val, cl->val, cl->len);
dill_trigger(&chcl->cl, 0);
errno = 0;
return i;
case CHRECV:
if(dill_slow(ch->done)) {errno = EPIPE; return i;}
if(dill_list_empty(&ch->out)) break;
chcl = dill_cont(dill_list_next(&ch->out),
struct dill_chclause, item);
memcpy(cl->val, chcl->val, ch->sz);
dill_trigger(&chcl->cl, 0);
errno = 0;
return i;
default:
errno = EINVAL;
return i;
}
}
/* There are no clauses immediately available. */
if(dill_slow(deadline == 0)) {errno = ETIMEDOUT; return -1;}
/* Let's wait. */
struct dill_chclause chcls[nclauses];
for(i = 0; i != nclauses; ++i) {
struct dill_chan *ch = hquery(clauses[i].ch, dill_chan_type);
dill_assert(ch);
dill_list_insert(&chcls[i].item,
clauses[i].op == CHRECV ? &ch->in : &ch->out);
chcls[i].val = clauses[i].val;
dill_waitfor(&chcls[i].cl, i, dill_chcancel);
}
struct dill_tmclause tmcl;
dill_timer(&tmcl, nclauses, deadline);
int id = dill_wait();
if(dill_slow(id < 0)) return -1;
if(dill_slow(id == nclauses)) {errno = ETIMEDOUT; return -1;}
return id;
}