-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathchannel.cc
406 lines (373 loc) · 9 KB
/
channel.cc
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
#define ICI_CORE
#include "channel.h"
#include "array.h"
#include "cfunc.h"
#include "forall.h"
#include "fwd.h"
#include "null.h"
#include "str.h"
/*
* TODO: add close() support
*/
/*
* Inter-thread link object
*
* A channel is a communications link between threads. Channels
* allows threads to send objects to one another and to synchronize
* their actions.
*
* Channels support two fundamental operations, "get" and "put". To
* send an object along a channel the "put" operation is used,
* receieving an object uses the "get" operation. Any type of object
* may be sent via channels including channels themselves. There is no
* defined "end of communications" object however nullptr is typically
* used to indicate that no more communications will occur along a
* channel.
*
* Channels have a defined "capacity", the number of objects that may
* be "in" the channel at any one time. The capacity of a channel is
* defined when the channel is created and defaults to zero. Channels
* with a capacity of zero are "unbuffered" and input and output
* operations block until some nother thread performs the
* complementary operation. This provides for synchronization between
* threads.
*
* Channels with capacity's greater than one are "buffered" - to the
* degree of the channel's capacity. Objects may be "put" without
* blocking until the channel is "full".
*
* Channels perform synchronization between threads. If there are no
* objects when a thread attempts to "get" from a channel the thread
* is blocked until an object is available. Correspondingly, when a
* thread attempts to "put" an object into a channel and there is no
* room, the thread is blocked until room becomes available. This
* behaviour forms the basis for channels' synchronization mechanism.
*
* The size, or capacity, of a channel is defined when the channel is
* created and, currently, may not be changed*. By default channels
* have a capacity of one, i.e., only a single object may be in the
* channel at the one time and the channel is an unbuffered synchronous
* communications mechanism. Sizes greater than one allow the channel
* to buffer that many objects and to decouple the communicating threads
* to that degree.
*
* * It is feasible that channel capacity may be made variable and
* facilities added to support this however given the limited experience
* with channels within ICI any decision on this matter is being
* delayed until experience shows it is desirable.
*
* This --intro-- and --synopsis-- are part of --ici-channel-- documentation.
*
*/
#include "channel.h"
namespace ici
{
// ----------------------------------------------------------------
channel *new_channel(size_t capacity)
{
channel *chan = ici_talloc(channel);
if (chan == nullptr)
{
return nullptr;
}
set_tfnz(chan, TC_CHANNEL, 0, 1, 0);
if ((chan->c_q = new_array(capacity)) == nullptr)
{
ici_tfree(chan, channel);
return nullptr;
}
chan->c_capacity = capacity;
chan->c_altobj = nullptr;
rego(chan);
return chan;
}
object *get(channel *c)
{
auto q = channelof(c)->c_q;
while (q->len() < 1)
{
if (c->hasflag(ICI_CHANNEL_CLOSED))
{
return nullptr;
}
if (waitfor(q))
{
return nullptr;
}
}
auto o = q->pop_front();
wakeup(q);
if (c->c_altobj)
{
wakeup(c->c_altobj);
}
return o;
}
int put(channel *c, object *o)
{
if (c->hasflag(ICI_CHANNEL_CLOSED))
{
return null_ret();
}
auto q = channelof(c)->c_q;
// unbuffered
if (channelof(c)->c_capacity == 0)
{
while (q->len() > 0)
{
if (waitfor(q))
{
return 1;
}
}
}
else
{
while (q->len() >= channelof(c)->c_capacity)
{
if (waitfor(q))
{
return 1;
}
}
}
q->push_back(o);
wakeup(q);
if (channelof(c)->c_altobj != nullptr)
{
wakeup(channelof(c)->c_altobj);
}
return 0;
}
int close_channel(channel *c)
{
if (c->hasflag(ICI_CHANNEL_CLOSED))
{
set_error("attempt to close a channel that is already closed");
return 1;
}
c->set(ICI_CHANNEL_CLOSED);
return 0;
}
// ----------------------------------------------------------------
size_t channel_type::mark(object *o)
{
auto mem = type::mark(o);
mem += ici_mark(channelof(o)->c_q);
mem += mark_optional(channelof(o)->c_altobj);
return mem;
}
int channel_type::forall(object *o)
{
auto fa = forallof(o);
auto chan = channelof(fa->fa_aggr);
auto val = get(chan);
if (val == nullptr)
{
return -1;
}
if (fa->fa_kaggr == null)
{
if (fa->fa_vaggr != null)
{
if (ici_assign(fa->fa_vaggr, fa->fa_vkey, val))
{
return 1;
}
}
}
else
{
if (fa->fa_vaggr != null)
{
if (ici_assign(fa->fa_vaggr, fa->fa_vkey, val))
{
return 1;
}
}
if (ici_assign(fa->fa_kaggr, fa->fa_kkey, val))
{
return 1;
}
}
return 0;
}
int channel_type::save(archiver *ar, object *o)
{
return type::save(ar, o);
}
object *channel_type::restore(archiver *ar)
{
return type::restore(ar);
}
int64_t channel_type::len(object *o)
{
return channelof(o)->c_capacity;
}
/*
* channel = channel([capacity])
*
* Create a new channel with the given capacity. If the capacity
* is not given it defaults to one, if it is given it must be a
* positive integer (i.e, a value greater than zero).
*
* This --topic-- forms part of the --ici-channel-- documentation.
*/
static int f_channel()
{
size_t capacity = 0;
if (NARGS() != 0)
{
long val;
if (typecheck("i", &val))
{
return 1;
}
if (val < 0)
{
set_error("channel capacity must be non-negative");
return 1;
}
capacity = size_t(val);
}
if (capacity == 0)
{
capacity = 1;
}
if (auto chan = new_channel(capacity))
{
return ret_with_decref(chan);
}
return 1;
}
/*
* any = get(channel)
*
* Return the next object from the channel. If there are no objects
* in the channel the caller is blocked until an object is available.
* If the channel is closed NULL is returned.
*/
static int f_get()
{
object *c;
if (typecheck("o", &c))
{
return 1;
}
if (!ischannel(c))
{
return argerror(0);
}
if (auto chan = get(channelof(c)))
{
return ret_no_decref(chan);
}
return null_ret();
}
/*
* put(channel, any)
*
* Write an object to a channel. If the channel has space for the
* object the object is placed in the channel and the caller continues
* execution. If there is no space in the channel for the object the
* caller is blocked until space is made available through another
* threads calls to channel:get().
*
* This --topic-- forms part of the --ici-channel-- documentation.
*/
static int f_put()
{
object *c;
object *o;
if (typecheck("oo", &c, &o))
{
return 1;
}
if (!ischannel(c))
{
return 1;
}
if (put(channelof(c), o))
{
return 1;
}
return null_ret();
}
//================================================================
static int alt_setup(array *alts, object *obj)
{
size_t n = alts->len();
size_t i;
for (i = 0; i < n; ++i)
{
object *o = alts->get(i);
channel *chan;
if (ischannel(o))
{
chan = channelof(o);
chan->c_altobj = obj;
}
else if (!isnull(o))
{
set_error("bad object in array passed to channel.alt");
return 1;
}
}
return 0;
}
static int alt(array *alts)
{
int idx = -1;
int n = alts->len();
int i;
for (i = 0; i < n && idx == -1; ++i)
{
object *o = alts->get(i);
if (ischannel(o) && channelof(o)->c_q->len() > 0)
{
idx = i;
}
}
return idx;
}
/*
* int = alt(array)
*
* Determine which of a collection of channels is ready to perform I/O
* and returns an index to a channel within that collection which is
* ready.
*
* @todo return a set of ready channels
* @todo randomize selection to avoid livelock
*/
static int f_alt()
{
int idx;
array *alts;
if (typecheck("a", &alts))
{
return 1;
}
if (alt_setup(alts, alts))
{
return 1;
}
while ((idx = alt(alts)) == -1)
{
if (waitfor(alts))
{
return 1;
}
}
alt_setup(alts, nullptr);
return int_ret(idx);
}
ICI_DEFINE_CFUNCS(channel)
{
ICI_DEFINE_CFUNC(channel, f_channel),
ICI_DEFINE_CFUNC(get, f_get),
ICI_DEFINE_CFUNC(put, f_put),
ICI_DEFINE_CFUNC(alt, f_alt),
ICI_CFUNCS_END()
};
} // namespace ici