-
Notifications
You must be signed in to change notification settings - Fork 161
/
chan.c
348 lines (319 loc) · 12.5 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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/*
Copyright (c) 2018 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 "ctx.h"
#include "list.h"
#include "utils.h"
#define DILL_DISABLE_RAW_NAMES
#include "libdillimpl.h"
struct dill_halfchan {
/* Table of virtual functions. */
struct dill_hvfs vfs;
/* List of clauses wanting to receive from the inbound halfchannel. */
struct dill_list in;
/* List of clauses wanting to send to the inbound halfchannel. */
struct dill_list out;
/* Whether this is the fist or the second half-channel of the channel. */
unsigned int index : 1;
/* 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;
/* 1 if hclose() was already called for this half-channel. */
unsigned int closed : 1;
};
/* Channel clause. */
struct dill_chanclause {
struct dill_clause cl;
/* An item in either the dill_halfchan::in or dill_halfchan::out list. */
struct dill_list item;
/* The object being passed via the channel. */
void *val;
size_t len;
};
DILL_CT_ASSERT(sizeof(struct dill_chstorage) >=
sizeof(struct dill_halfchan) * 2);
/******************************************************************************/
/* Handle implementation. */
/******************************************************************************/
static const int dill_halfchan_type_placeholder = 0;
const void *dill_halfchan_type = &dill_halfchan_type_placeholder;
static void *dill_halfchan_query(struct dill_hvfs *vfs, const void *type);
static void dill_halfchan_close(struct dill_hvfs *vfs);
/******************************************************************************/
/* Helpers. */
/******************************************************************************/
/* Return the other half-channel within the same channel. */
#define dill_halfchan_other(self) (self->index ? self - 1 : self + 1)
/******************************************************************************/
/* Channel creation and deallocation. */
/******************************************************************************/
static void dill_halfchan_init(struct dill_halfchan *ch, int index) {
ch->vfs.query = dill_halfchan_query;
ch->vfs.close = dill_halfchan_close;
dill_list_init(&ch->in);
dill_list_init(&ch->out);
ch->index = index;
ch->done = 0;
ch->mem = 1;
ch->closed = 0;
}
int dill_chmake_mem(struct dill_chstorage *mem, int chv[2]) {
int err;
if(dill_slow(!mem)) {err = EINVAL; goto error1;}
struct dill_halfchan *ch = (struct dill_halfchan*)mem;
dill_halfchan_init(&ch[0], 0);
dill_halfchan_init(&ch[1], 1);
chv[0] = dill_hmake(&ch[0].vfs);
if(dill_slow(chv[0] < 0)) {err = errno; goto error1;}
chv[1] = dill_hmake(&ch[1].vfs);
if(dill_slow(chv[1] < 0)) {err = errno; goto error2;}
return 0;
error2:
/* This closes the handle but leaves everything else alone given
that the second handle wasn't event created. */
dill_hclose(chv[0]);
error1:
errno = err;
return -1;
}
int dill_chmake(int chv[2]) {
int err;
struct dill_chstorage *ch = malloc(sizeof(struct dill_chstorage));
if(dill_slow(!ch)) {err = ENOMEM; goto error1;}
int h = dill_chmake_mem(ch, chv);
if(dill_slow(h < 0)) {err = errno; goto error2;}
((struct dill_halfchan*)ch)[0].mem = 0;
((struct dill_halfchan*)ch)[1].mem = 0;
return h;
error2:
free(ch);
error1:
errno = err;
return -1;
}
static void *dill_halfchan_query(struct dill_hvfs *vfs, const void *type) {
if(dill_fast(type == dill_halfchan_type)) return vfs;
errno = ENOTSUP;
return NULL;
}
static void dill_halfchan_term(struct dill_halfchan *ch) {
/* Resume any remaining senders and receivers on the channel
with the EPIPE error. */
while(!dill_list_empty(&ch->in)) {
struct dill_chanclause *chcl = dill_cont(dill_list_next(&ch->in),
struct dill_chanclause, item);
dill_trigger(&chcl->cl, EPIPE);
}
while(!dill_list_empty(&ch->out)) {
struct dill_chanclause *chcl = dill_cont(dill_list_next(&ch->out),
struct dill_chanclause, item);
dill_trigger(&chcl->cl, EPIPE);
}
}
static void dill_halfchan_close(struct dill_hvfs *vfs) {
struct dill_halfchan *ch = (struct dill_halfchan*)vfs;
dill_assert(ch && !ch->closed);
/* If the other half of the channel is still open do nothing. */
if(!dill_halfchan_other(ch)->closed) {
ch->closed = 1;
return;
}
if(ch->index) ch = dill_halfchan_other(ch);
dill_halfchan_term(&ch[0]);
dill_halfchan_term(&ch[1]);
if(!ch->mem) free(ch);
}
/******************************************************************************/
/* Sending and receiving. */
/******************************************************************************/
static void dill_chcancel(struct dill_clause *cl) {
struct dill_chanclause *chcl = dill_cont(cl, struct dill_chanclause, cl);
dill_list_erase(&chcl->item);
}
int dill_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_halfchan *ch = dill_hquery(h, dill_halfchan_type);
if(dill_slow(!ch)) return -1;
/* Sending is always done to the opposite side of the channel. */
ch = dill_halfchan_other(ch);
/* 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_chanclause *chcl = dill_cont(dill_list_next(&ch->in),
struct dill_chanclause, item);
if(dill_slow(len != chcl->len)) {
dill_trigger(&chcl->cl, EMSGSIZE);
errno = EMSGSIZE;
return -1;
}
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_chanclause chcl;
dill_list_insert(&chcl.item, &ch->out);
chcl.val = (void*)val;
chcl.len = len;
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 dill_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_halfchan *ch = dill_hquery(h, dill_halfchan_type);
if(dill_slow(!ch)) 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_chanclause *chcl = dill_cont(dill_list_next(&ch->out),
struct dill_chanclause, item);
if(dill_slow(len != chcl->len)) {
dill_trigger(&chcl->cl, EMSGSIZE);
errno = EMSGSIZE;
return -1;
}
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_chanclause chcl;
dill_list_insert(&chcl.item, &ch->in);
chcl.val = val;
chcl.len = len;
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 dill_chdone(int h) {
struct dill_halfchan *ch = dill_hquery(h, dill_halfchan_type);
if(dill_slow(!ch)) return -1;
/* Done is always done to the opposite side of the channel. */
ch = dill_halfchan_other(ch);
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_chanclause *chcl = dill_cont(dill_list_next(&ch->in),
struct dill_chanclause, item);
dill_trigger(&chcl->cl, EPIPE);
}
while(!dill_list_empty(&ch->out)) {
struct dill_chanclause *chcl = dill_cont(dill_list_next(&ch->out),
struct dill_chanclause, item);
dill_trigger(&chcl->cl, EPIPE);
}
return 0;
}
int dill_choose(struct dill_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 dill_chclause *cl = &clauses[i];
struct dill_halfchan *ch = dill_hquery(cl->ch, dill_halfchan_type);
if(dill_slow(!ch)) return i;
if(dill_slow(cl->len > 0 && !cl->val)) {errno = EINVAL; return i;}
struct dill_chanclause *chcl;
switch(cl->op) {
case DILL_CHSEND:
ch = dill_halfchan_other(ch);
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_chanclause, item);
if(dill_slow(cl->len != chcl->len)) {
dill_trigger(&chcl->cl, EMSGSIZE);
errno = EMSGSIZE;
return i;
}
memcpy(chcl->val, cl->val, cl->len);
dill_trigger(&chcl->cl, 0);
errno = 0;
return i;
case DILL_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_chanclause, item);
if(dill_slow(cl->len != chcl->len)) {
dill_trigger(&chcl->cl, EMSGSIZE);
errno = EMSGSIZE;
return i;
}
memcpy(cl->val, chcl->val, cl->len);
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_chanclause chcls[nclauses];
for(i = 0; i != nclauses; ++i) {
struct dill_halfchan *ch = dill_hquery(clauses[i].ch,
dill_halfchan_type);
dill_assert(ch);
dill_list_insert(&chcls[i].item, clauses[i].op == DILL_CHRECV ?
&ch->in : &dill_halfchan_other(ch)->out);
chcls[i].val = clauses[i].val;
chcls[i].len = clauses[i].len;
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;
}