This repository has been archived by the owner on Aug 31, 2023. It is now read-only.
forked from WebAssembly/spec
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlwt.wast
294 lines (263 loc) · 9.79 KB
/
lwt.wast
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
;; dynamic lightweight threads
;; interface to lightweight threads
(module $lwt
(type $func (func)) ;; [] -> []
(type $cont (cont $func)) ;; cont ([] -> [])
(tag $yield (export "yield")) ;; [] -> []
(tag $fork (export "fork") (param (ref $cont))) ;; [cont ([] -> [])] -> []
)
(register "lwt")
(module $example
(type $func (func)) ;; [] -> []
(type $cont (cont $func)) ;; cont ([] -> [])
(tag $yield (import "lwt" "yield")) ;; [] -> []
(tag $fork (import "lwt" "fork") (param (ref $cont))) ;; [cont ([] -> [])] -> []
(func $log (import "spectest" "print_i32") (param i32))
(elem declare func $thread1 $thread2 $thread3)
(func $main (export "main")
(call $log (i32.const 0))
(suspend $fork (cont.new $cont (ref.func $thread1)))
(call $log (i32.const 1))
(suspend $fork (cont.new $cont (ref.func $thread2)))
(call $log (i32.const 2))
(suspend $fork (cont.new $cont (ref.func $thread3)))
(call $log (i32.const 3))
)
(func $thread1
(call $log (i32.const 10))
(suspend $yield)
(call $log (i32.const 11))
(suspend $yield)
(call $log (i32.const 12))
)
(func $thread2
(call $log (i32.const 20))
(suspend $yield)
(call $log (i32.const 21))
(suspend $yield)
(call $log (i32.const 22))
)
(func $thread3
(call $log (i32.const 30))
(suspend $yield)
(call $log (i32.const 31))
(suspend $yield)
(call $log (i32.const 32))
)
)
(register "example")
;; queue of threads
(module $queue
(type $func (func)) ;; [] -> []
(type $cont (cont $func)) ;; cont ([] -> [])
;; Table as simple queue (keeping it simple, no ring buffer)
(table $queue 0 (ref null $cont))
(global $qdelta i32 (i32.const 10))
(global $qback (mut i32) (i32.const 0))
(global $qfront (mut i32) (i32.const 0))
(func $queue-empty (export "queue-empty") (result i32)
(i32.eq (global.get $qfront) (global.get $qback))
)
(func $dequeue (export "dequeue") (result (ref null $cont))
(local $i i32)
(if (call $queue-empty)
(then (return (ref.null $cont)))
)
(local.set $i (global.get $qfront))
(global.set $qfront (i32.add (local.get $i) (i32.const 1)))
(table.get $queue (local.get $i))
)
(func $enqueue (export "enqueue") (param $k (ref null $cont))
;; Check if queue is full
(if (i32.eq (global.get $qback) (table.size $queue))
(then
;; Check if there is enough space in the front to compact
(if (i32.lt_u (global.get $qfront) (global.get $qdelta))
(then
;; Space is below threshold, grow table instead
(drop (table.grow $queue (ref.null $cont) (global.get $qdelta)))
)
(else
;; Enough space, move entries up to head of table
(global.set $qback (i32.sub (global.get $qback) (global.get $qfront)))
(table.copy $queue $queue
(i32.const 0) ;; dest = new front = 0
(global.get $qfront) ;; src = old front
(global.get $qback) ;; len = new back = old back - old front
)
(table.fill $queue ;; null out old entries to avoid leaks
(global.get $qback) ;; start = new back
(ref.null $cont) ;; init value
(global.get $qfront) ;; len = old front = old front - new front
)
(global.set $qfront (i32.const 0))
)
)
)
)
(table.set $queue (global.get $qback) (local.get $k))
(global.set $qback (i32.add (global.get $qback) (i32.const 1)))
)
)
(register "queue")
(module $scheduler
(type $func (func)) ;; [] -> []
(type $cont (cont $func)) ;; cont ([] -> [])
(tag $yield (import "lwt" "yield")) ;; [] -> []
(tag $fork (import "lwt" "fork") (param (ref $cont))) ;; [cont ([] -> [])] -> []
(func $queue-empty (import "queue" "queue-empty") (result i32))
(func $dequeue (import "queue" "dequeue") (result (ref null $cont)))
(func $enqueue (import "queue" "enqueue") (param $k (ref null $cont)))
;; synchronous scheduler (run current thread to completion without
;; yielding)
(func $sync (export "sync") (param $nextk (ref null $cont))
(loop $l
(if (ref.is_null (local.get $nextk)) (then (return)))
(block $on_yield (result (ref $cont))
(block $on_fork (result (ref $cont) (ref $cont))
(resume $cont
(tag $yield $on_yield)
(tag $fork $on_fork)
(local.get $nextk)
)
(local.set $nextk (call $dequeue))
(br $l) ;; thread terminated
) ;; $on_fork (result (ref $cont) (ref $cont))
(local.set $nextk) ;; current thread
(call $enqueue) ;; new thread
(br $l)
) ;; $on_yield (result (ref $cont))
(local.set $nextk) ;; carry on with current thread
(br $l)
)
)
;; four asynchronous schedulers:
;; * kt and tk don't yield on encountering a fork
;; 1) kt runs the continuation, queuing up the new thread for later
;; 2) tk runs the new thread first, queuing up the continuation for later
;; * ykt and ytk do yield on encountering a fork
;; 3) ykt runs the continuation, queuing up the new thread for later
;; 4) ytk runs the new thread first, queuing up the continuation for later
;; no yield on fork, continuation first
(func $kt (export "kt") (param $nextk (ref null $cont))
(loop $l
(if (ref.is_null (local.get $nextk)) (then (return)))
(block $on_yield (result (ref $cont))
(block $on_fork (result (ref $cont) (ref $cont))
(resume $cont
(tag $yield $on_yield)
(tag $fork $on_fork)
(local.get $nextk)
)
(local.set $nextk (call $dequeue))
(br $l) ;; thread terminated
) ;; $on_fork (result (ref $cont) (ref $cont))
(local.set $nextk) ;; current thread
(call $enqueue) ;; new thread
(br $l)
) ;; $on_yield (result (ref $cont))
(call $enqueue) ;; current thread
(local.set $nextk (call $dequeue)) ;; next thread
(br $l)
)
)
;; no yield on fork, new thread first
(func $tk (export "tk") (param $nextk (ref null $cont))
(loop $l
(if (ref.is_null (local.get $nextk)) (then (return)))
(block $on_yield (result (ref $cont))
(block $on_fork (result (ref $cont) (ref $cont))
(resume $cont
(tag $yield $on_yield)
(tag $fork $on_fork)
(local.get $nextk)
)
(local.set $nextk (call $dequeue))
(br $l) ;; thread terminated
) ;; $on_fork (result (ref $cont) (ref $cont))
(call $enqueue) ;; current thread
(local.set $nextk) ;; new thread
(br $l)
) ;; $on_yield (result (ref $cont))
(call $enqueue) ;; current thread
(local.set $nextk (call $dequeue)) ;; next thread
(br $l)
)
)
;; yield on fork, continuation first
(func $ykt (export "ykt") (param $nextk (ref null $cont))
(loop $l
(if (ref.is_null (local.get $nextk)) (then (return)))
(block $on_yield (result (ref $cont))
(block $on_fork (result (ref $cont) (ref $cont))
(resume $cont
(tag $yield $on_yield)
(tag $fork $on_fork)
(local.get $nextk)
)
(local.set $nextk (call $dequeue))
(br $l) ;; thread terminated
) ;; $on_fork (result (ref $cont) (ref $cont))
(call $enqueue) ;; current thread
(call $enqueue) ;; new thread
(local.set $nextk (call $dequeue)) ;; next thread
(br $l)
) ;; $on_yield (result (ref $cont))
(call $enqueue) ;; current thread
(local.set $nextk (call $dequeue)) ;; next thread
(br $l)
)
)
;; yield on fork, new thread first
(func $ytk (export "ytk") (param $nextk (ref null $cont))
(loop $l
(if (ref.is_null (local.get $nextk)) (then (return)))
(block $on_yield (result (ref $cont))
(block $on_fork (result (ref $cont) (ref $cont))
(resume $cont
(tag $yield $on_yield)
(tag $fork $on_fork)
(local.get $nextk)
)
(local.set $nextk (call $dequeue))
(br $l) ;; thread terminated
) ;; $on_fork (result (ref $cont) (ref $cont))
(local.set $nextk)
(call $enqueue) ;; new thread
(call $enqueue (local.get $nextk)) ;; current thread
(local.set $nextk (call $dequeue)) ;; next thread
(br $l)
) ;; $on_yield (result (ref $cont))
(call $enqueue) ;; current thread
(local.set $nextk (call $dequeue)) ;; next thread
(br $l)
)
)
)
(register "scheduler")
(module
(type $func (func)) ;; [] -> []
(type $cont (cont $func)) ;; cont ([] -> [])
(func $scheduler-sync (import "scheduler" "sync") (param $nextk (ref null $cont)))
(func $scheduler-kt (import "scheduler" "kt") (param $nextk (ref null $cont)))
(func $schedule-tk (import "scheduler" "tk") (param $nextk (ref null $cont)))
(func $scheduler-ykt (import "scheduler" "ykt") (param $nextk (ref null $cont)))
(func $scheduler-ytk (import "scheduler" "ytk") (param $nextk (ref null $cont)))
(func $log (import "spectest" "print_i32") (param i32))
(func $main (import "example" "main"))
(elem declare func $main)
(func (export "run")
(call $log (i32.const -1))
(call $scheduler-sync (cont.new $cont (ref.func $main)))
(call $log (i32.const -2))
(call $scheduler-kt (cont.new $cont (ref.func $main)))
(call $log (i32.const -3))
(call $schedule-tk (cont.new $cont (ref.func $main)))
(call $log (i32.const -4))
(call $scheduler-ykt (cont.new $cont (ref.func $main)))
(call $log (i32.const -5))
(call $scheduler-ytk (cont.new $cont (ref.func $main)))
(call $log (i32.const -6))
)
)
(invoke "run")