about summary refs log tree commit diff stats
path: root/html/061channel.mu.html
blob: 286aaf220533b16924cf3d45dea052f73b14476a (plain) (blame)
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
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>Mu - 061channel.mu</title>
<meta name="Generator" content="Vim/7.4">
<meta name="plugin-version" content="vim7.4_v1">
<meta name="syntax" content="none">
<meta name="settings" content="use_css,pre_wrap,no_foldcolumn,expand_tabs,prevent_copy=">
<meta name="colorscheme" content="minimal">
<style type="text/css">
<!--
pre { white-space: pre-wrap; font-family: monospace; color: #d0d0d0; background-color: #000000; }
body { font-family: monospace; color: #d0d0d0; background-color: #000000; }
* { font-size: 1em; }
.Comment { color: #8080ff; }
.Delimiter { color: #c000c0; }
.Special { color: #ff6060; }
.Identifier { color: #008080; }
.SalientComment { color: #00ffff; }
-->
</style>

<script type='text/javascript'>
<!--

-->
</script>
</head>
<body>
<pre id='vimCodeElement'>
<span class="Comment"># Mu synchronizes using channels rather than locks, like Erlang and Go.</span>
<span class="Comment">#</span>
<span class="Comment"># The two ends of a channel will usually belong to different routines, but</span>
<span class="Comment"># each end should only be used by a single one. Don't try to read from or</span>
<span class="Comment"># write to it from multiple routines at once.</span>
<span class="Comment">#</span>
<span class="Comment"># The key property of channels is that writing to a full channel or reading</span>
<span class="Comment"># from an empty one will put the current routine in 'waiting' state until the</span>
<span class="Comment"># operation can be completed.</span>

scenario channel <span class="Delimiter">[</span>
  run <span class="Delimiter">[</span>
    1:address:channel<span class="Special"> &lt;- </span>init-channel 3:literal/capacity
    1:address:channel<span class="Special"> &lt;- </span>write 1:address:channel, 34:literal
    2:integer, 1:address:channel<span class="Special"> &lt;- </span>read 1:address:channel
  <span class="Delimiter">]</span>
  memory-should-contain <span class="Delimiter">[</span>
    2<span class="Special"> &lt;- </span>34
  <span class="Delimiter">]</span>
<span class="Delimiter">]</span>

container channel <span class="Delimiter">[</span>
  <span class="Comment"># To avoid locking, writer and reader will never write to the same location.</span>
  <span class="Comment"># So channels will include fields in pairs, one for the writer and one for the</span>
  <span class="Comment"># reader.</span>
  first-full:integer  <span class="Comment"># for write</span>
  first-free:integer  <span class="Comment"># for read</span>
  <span class="Comment"># A circular buffer contains values from index first-full up to (but not</span>
  <span class="Comment"># including) index first-empty. The reader always modifies it at first-full,</span>
  <span class="Comment"># while the writer always modifies it at first-empty.</span>
  data:address:array:location
<span class="Delimiter">]</span>

<span class="Comment"># result:address:channel &lt;- init-channel capacity:integer</span>
recipe init-channel <span class="Delimiter">[</span>
  default-space:address:array:location<span class="Special"> &lt;- </span>new location:type, 30:literal
  <span class="Comment"># result = new channel</span>
  result:address:channel<span class="Special"> &lt;- </span>new channel:type
  <span class="Comment"># result.first-full = 0</span>
  full:address:integer<span class="Special"> &lt;- </span>get-address result:address:channel/deref, first-full:offset
  full:address:integer/deref<span class="Special"> &lt;- </span>copy 0:literal
  <span class="Comment"># result.first-free = 0</span>
  free:address:integer<span class="Special"> &lt;- </span>get-address result:address:channel/deref, first-free:offset
  free:address:integer/deref<span class="Special"> &lt;- </span>copy 0:literal
  <span class="Comment"># result.data = new location[ingredient+1]</span>
  capacity:integer<span class="Special"> &lt;- </span>next-ingredient
  capacity:integer<span class="Special"> &lt;- </span>add capacity:integer, 1:literal  <span class="Comment"># unused slot for 'full?' below</span>
  dest:address:address:array:location<span class="Special"> &lt;- </span>get-address result:address:channel/deref, data:offset
  dest:address:address:array:location/deref<span class="Special"> &lt;- </span>new location:type, capacity:integer
  <span class="Identifier">reply</span> result:address:channel
<span class="Delimiter">]</span>

<span class="Comment"># chan:address:channel &lt;- write chan:address:channel, val:location</span>
recipe write <span class="Delimiter">[</span>
  default-space:address:array:location<span class="Special"> &lt;- </span>new location:type, 30:literal
  chan:address:channel<span class="Special"> &lt;- </span>next-ingredient
  val:location<span class="Special"> &lt;- </span>next-ingredient
  <span class="Delimiter">{</span>
    <span class="Comment"># block if chan is full</span>
    full:boolean<span class="Special"> &lt;- </span>channel-full? chan:address:channel
    <span class="Identifier">break-unless</span> full:boolean
    full-address:address:integer<span class="Special"> &lt;- </span>get-address chan:address:channel/deref, first-full:offset
    wait-for-location full-address:address:integer/deref
  <span class="Delimiter">}</span>
  <span class="Comment"># store val</span>
  circular-buffer:address:array:location<span class="Special"> &lt;- </span>get chan:address:channel/deref, data:offset
  free:address:integer<span class="Special"> &lt;- </span>get-address chan:address:channel/deref, first-free:offset
  dest:address:location<span class="Special"> &lt;- </span>index-address circular-buffer:address:array:location/deref, free:address:integer/deref
  dest:address:location/deref<span class="Special"> &lt;- </span>copy val:location
  <span class="Comment"># increment free</span>
  free:address:integer/deref<span class="Special"> &lt;- </span>add free:address:integer/deref, 1:literal
  <span class="Delimiter">{</span>
    <span class="Comment"># wrap free around to 0 if necessary</span>
    len:integer<span class="Special"> &lt;- </span>length circular-buffer:address:array:location/deref
    at-end?:boolean<span class="Special"> &lt;- </span>greater-or-equal free:address:integer/deref, len:integer
    <span class="Identifier">break-unless</span> at-end?:boolean
    free:address:integer/deref<span class="Special"> &lt;- </span>copy 0:literal
  <span class="Delimiter">}</span>
  <span class="Identifier">reply</span> chan:address:channel/same-as-ingredient:0
<span class="Delimiter">]</span>

<span class="Comment"># result:location, chan:address:channel &lt;- read chan:address:channel</span>
recipe read <span class="Delimiter">[</span>
  default-space:address:array:location<span class="Special"> &lt;- </span>new location:type, 30:literal
  chan:address:channel<span class="Special"> &lt;- </span>next-ingredient
  <span class="Delimiter">{</span>
    <span class="Comment"># block if chan is empty</span>
    empty:boolean<span class="Special"> &lt;- </span>channel-empty? chan:address:channel
    <span class="Identifier">break-unless</span> empty:boolean
    free-address:address:integer<span class="Special"> &lt;- </span>get-address chan:address:channel/deref, first-free:offset
    wait-for-location free-address:address:integer/deref
  <span class="Delimiter">}</span>
  <span class="Comment"># read result</span>
  full:address:integer<span class="Special"> &lt;- </span>get-address chan:address:channel/deref, first-full:offset
  circular-buffer:address:array:location<span class="Special"> &lt;- </span>get chan:address:channel/deref, data:offset
  result:location<span class="Special"> &lt;- </span>index circular-buffer:address:array:location/deref, full:address:integer/deref
  <span class="Comment"># increment full</span>
  full:address:integer/deref<span class="Special"> &lt;- </span>add full:address:integer/deref, 1:literal
  <span class="Delimiter">{</span>
    <span class="Comment"># wrap full around to 0 if necessary</span>
    len:integer<span class="Special"> &lt;- </span>length circular-buffer:address:array:location/deref
    at-end?:boolean<span class="Special"> &lt;- </span>greater-or-equal full:address:integer/deref, len:integer
    <span class="Identifier">break-unless</span> at-end?:boolean
    full:address:integer/deref<span class="Special"> &lt;- </span>copy 0:literal
  <span class="Delimiter">}</span>
  <span class="Identifier">reply</span> result:location, chan:address:channel/same-as-ingredient:0
<span class="Delimiter">]</span>

scenario channel-initialization <span class="Delimiter">[</span>
  run <span class="Delimiter">[</span>
    1:address:channel<span class="Special"> &lt;- </span>init-channel 3:literal/capacity
    2:integer<span class="Special"> &lt;- </span>get 1:address:channel/deref, first-full:offset
    3:integer<span class="Special"> &lt;- </span>get 1:address:channel/deref, first-free:offset
  <span class="Delimiter">]</span>
  memory-should-contain <span class="Delimiter">[</span>
    2<span class="Special"> &lt;- </span>0  <span class="Comment"># first-full</span>
    3<span class="Special"> &lt;- </span>0  <span class="Comment"># first-free</span>
  <span class="Delimiter">]</span>
<span class="Delimiter">]</span>

scenario channel-write-increments-free <span class="Delimiter">[</span>
  run <span class="Delimiter">[</span>
    1:address:channel<span class="Special"> &lt;- </span>init-channel 3:literal/capacity
    1:address:channel<span class="Special"> &lt;- </span>write 1:address:channel, 34:literal
    2:integer<span class="Special"> &lt;- </span>get 1:address:channel/deref, first-full:offset
    3:integer<span class="Special"> &lt;- </span>get 1:address:channel/deref, first-free:offset
  <span class="Delimiter">]</span>
  memory-should-contain <span class="Delimiter">[</span>
    2<span class="Special"> &lt;- </span>0  <span class="Comment"># first-full</span>
    3<span class="Special"> &lt;- </span>1  <span class="Comment"># first-free</span>
  <span class="Delimiter">]</span>
<span class="Delimiter">]</span>

scenario channel-read-increments-full <span class="Delimiter">[</span>
  run <span class="Delimiter">[</span>
    1:address:channel<span class="Special"> &lt;- </span>init-channel 3:literal/capacity
    1:address:channel<span class="Special"> &lt;- </span>write 1:address:channel, 34:literal
    _, 1:address:channel<span class="Special"> &lt;- </span>read 1:address:channel
    2:integer<span class="Special"> &lt;- </span>get 1:address:channel/deref, first-full:offset
    3:integer<span class="Special"> &lt;- </span>get 1:address:channel/deref, first-free:offset
  <span class="Delimiter">]</span>
  memory-should-contain <span class="Delimiter">[</span>
    2<span class="Special"> &lt;- </span>1  <span class="Comment"># first-full</span>
    3<span class="Special"> &lt;- </span>1  <span class="Comment"># first-free</span>
  <span class="Delimiter">]</span>
<span class="Delimiter">]</span>

scenario channel-wrap <span class="Delimiter">[</span>
  run <span class="Delimiter">[</span>
    <span class="Comment"># channel with just 1 slot</span>
    1:address:channel<span class="Special"> &lt;- </span>init-channel 1:literal/capacity
    <span class="Comment"># write and read a value</span>
    1:address:channel<span class="Special"> &lt;- </span>write 1:address:channel, 34:literal
    _, 1:address:channel<span class="Special"> &lt;- </span>read 1:address:channel
    <span class="Comment"># first-free will now be 1</span>
    2:integer<span class="Special"> &lt;- </span>get 1:address:channel/deref, first-free:offset
    3:integer<span class="Special"> &lt;- </span>get 1:address:channel/deref, first-free:offset
    <span class="Comment"># write second value, verify that first-free wraps</span>
    1:address:channel<span class="Special"> &lt;- </span>write 1:address:channel, 34:literal
    4:integer<span class="Special"> &lt;- </span>get 1:address:channel/deref, first-free:offset
    <span class="Comment"># read second value, verify that first-full wraps</span>
    _, 1:address:channel<span class="Special"> &lt;- </span>read 1:address:channel
    5:integer<span class="Special"> &lt;- </span>get 1:address:channel/deref, first-full:offset
  <span class="Delimiter">]</span>
  memory-should-contain <span class="Delimiter">[</span>
    2<span class="Special"> &lt;- </span>1  <span class="Comment"># first-free after first write</span>
    3<span class="Special"> &lt;- </span>1  <span class="Comment"># first-full after first read</span>
    4<span class="Special"> &lt;- </span>0  <span class="Comment"># first-free after second write, wrapped</span>
    5<span class="Special"> &lt;- </span>0  <span class="Comment"># first-full after second read, wrapped</span>
  <span class="Delimiter">]</span>
<span class="Delimiter">]</span>

<span class="SalientComment">## helpers</span>

<span class="Comment"># An empty channel has first-empty and first-full both at the same value.</span>
recipe channel-empty? <span class="Delimiter">[</span>
  default-space:address:array:location<span class="Special"> &lt;- </span>new location:type, 30:literal
  chan:address:channel<span class="Special"> &lt;- </span>next-ingredient
  <span class="Comment"># return chan.first-full == chan.first-free</span>
  full:integer<span class="Special"> &lt;- </span>get chan:address:channel/deref, first-full:offset
  free:integer<span class="Special"> &lt;- </span>get chan:address:channel/deref, first-free:offset
  result:boolean<span class="Special"> &lt;- </span>equal full:integer, free:integer
  <span class="Identifier">reply</span> result:boolean
<span class="Delimiter">]</span>

<span class="Comment"># A full channel has first-empty just before first-full, wasting one slot.</span>
<span class="Comment"># (Other alternatives: <a href="https://en.wikipedia.org/wiki/Circular_buffer#Full_.2F_Empty_Buffer_Distinction)">https://en.wikipedia.org/wiki/Circular_buffer#Full_.2F_Empty_Buffer_Distinction)</a></span>
recipe channel-full? <span class="Delimiter">[</span>
  default-space:address:array:location<span class="Special"> &lt;- </span>new location:type, 30:literal
  chan:address:channel<span class="Special"> &lt;- </span>next-ingredient
  <span class="Comment"># tmp = chan.first-free + 1</span>
  tmp:integer<span class="Special"> &lt;- </span>get chan:address:channel/deref, first-free:offset
  tmp:integer<span class="Special"> &lt;- </span>add tmp:integer, 1:literal
  <span class="Delimiter">{</span>
    <span class="Comment"># if tmp == chan.capacity, tmp = 0</span>
    len:integer<span class="Special"> &lt;- </span>channel-capacity chan:address:channel
    at-end?:boolean<span class="Special"> &lt;- </span>greater-or-equal tmp:integer, len:integer
    <span class="Identifier">break-unless</span> at-end?:boolean
    tmp:integer<span class="Special"> &lt;- </span>copy 0:literal
  <span class="Delimiter">}</span>
  <span class="Comment"># return chan.first-full == tmp</span>
  full:integer<span class="Special"> &lt;- </span>get chan:address:channel/deref, first-full:offset
  result:boolean<span class="Special"> &lt;- </span>equal full:integer, tmp:integer
  <span class="Identifier">reply</span> result:boolean
<span class="Delimiter">]</span>

<span class="Comment"># result:integer &lt;- channel-capacity chan:address:channel</span>
recipe channel-capacity <span class="Delimiter">[</span>
  default-space:address:array:location<span class="Special"> &lt;- </span>new location:type, 30:literal
  chan:address:channel<span class="Special"> &lt;- </span>next-ingredient
  q:address:array:location<span class="Special"> &lt;- </span>get chan:address:channel/deref, data:offset
  result:integer<span class="Special"> &lt;- </span>length q:address:array:location/deref
  <span class="Identifier">reply</span> result:integer
<span class="Delimiter">]</span>

scenario channel-new-empty-not-full <span class="Delimiter">[</span>
  run <span class="Delimiter">[</span>
    1:address:channel<span class="Special"> &lt;- </span>init-channel 3:literal/capacity
    2:integer<span class="Special"> &lt;- </span>channel-empty? 1:address:channel
    3:integer<span class="Special"> &lt;- </span>channel-full? 1:address:channel
  <span class="Delimiter">]</span>
  memory-should-contain <span class="Delimiter">[</span>
    2<span class="Special"> &lt;- </span>1  <span class="Comment"># empty?</span>
    3<span class="Special"> &lt;- </span>0  <span class="Comment"># full?</span>
  <span class="Delimiter">]</span>
<span class="Delimiter">]</span>

scenario channel-write-not-empty <span class="Delimiter">[</span>
  run <span class="Delimiter">[</span>
    1:address:channel<span class="Special"> &lt;- </span>init-channel 3:literal/capacity
    1:address:channel<span class="Special"> &lt;- </span>write 1:address:channel, 34:literal
    2:integer<span class="Special"> &lt;- </span>channel-empty? 1:address:channel
    3:integer<span class="Special"> &lt;- </span>channel-full? 1:address:channel
  <span class="Delimiter">]</span>
  memory-should-contain <span class="Delimiter">[</span>
    2<span class="Special"> &lt;- </span>0  <span class="Comment"># empty?</span>
    3<span class="Special"> &lt;- </span>0  <span class="Comment"># full?</span>
  <span class="Delimiter">]</span>
<span class="Delimiter">]</span>

scenario channel-write-full <span class="Delimiter">[</span>
  run <span class="Delimiter">[</span>
    1:address:channel<span class="Special"> &lt;- </span>init-channel 1:literal/capacity
    1:address:channel<span class="Special"> &lt;- </span>write 1:address:channel, 34:literal
    2:integer<span class="Special"> &lt;- </span>channel-empty? 1:address:channel
    3:integer<span class="Special"> &lt;- </span>channel-full? 1:address:channel
  <span class="Delimiter">]</span>
  memory-should-contain <span class="Delimiter">[</span>
    2<span class="Special"> &lt;- </span>0  <span class="Comment"># empty?</span>
    3<span class="Special"> &lt;- </span>1  <span class="Comment"># full?</span>
  <span class="Delimiter">]</span>
<span class="Delimiter">]</span>

scenario channel-read-not-full <span class="Delimiter">[</span>
  run <span class="Delimiter">[</span>
    1:address:channel<span class="Special"> &lt;- </span>init-channel 1:literal/capacity
    1:address:channel<span class="Special"> &lt;- </span>write 1:address:channel, 34:literal
    _, 1:address:channel<span class="Special"> &lt;- </span>read 1:address:channel
    2:integer<span class="Special"> &lt;- </span>channel-empty? 1:address:channel
    3:integer<span class="Special"> &lt;- </span>channel-full? 1:address:channel
  <span class="Delimiter">]</span>
  memory-should-contain <span class="Delimiter">[</span>
    2<span class="Special"> &lt;- </span>1  <span class="Comment"># empty?</span>
    3<span class="Special"> &lt;- </span>0  <span class="Comment"># full?</span>
  <span class="Delimiter">]</span>
<span class="Delimiter">]</span>
</pre>
</body>
</html>
<!-- vim: set foldmethod=manual : -->