1(module
2  ;; As we have discussed, it makes sense to make the shared memory an import
3  ;; so that all
4  (import "" "memory" (memory $shmem 1 1 shared))
5  (import "wasi_snapshot_preview1" "fd_write"
6    (func $__wasi_fd_write (param i32 i32 i32 i32) (result i32)))
7  (import "wasi_snapshot_preview1" "proc_exit"
8    (func $__wasi_proc_exit (param i32)))
9  (import "wasi" "thread-spawn"
10    (func $__wasi_thread_spawn (param i32) (result i32)))
11
12  (func (export "_start")
13    (local $i i32)
14
15    ;; Print "Called _start".
16    (call $print (i32.const 32) (i32.const 14))
17
18    ;; Print "Running wasi_thread_start" in several threads.
19    (drop (call $__wasi_thread_spawn (i32.const 0)))
20    (drop (call $__wasi_thread_spawn (i32.const 0)))
21    (drop (call $__wasi_thread_spawn (i32.const 0)))
22
23    ;; Wait for all the threads to notify us that they are done.
24    (local.set $i (i32.const 0))
25    (loop $again
26      ;; Wait for the i32 at address 128 to be incremented by each thread. We
27      ;; maintain a local $i with the atomically loaded value as the expected
28      ;; wait value and to check if all three threads are complete. This wait is
29      ;; for 1ms or until notified, whichever is first.
30      (drop (memory.atomic.wait32 (i32.const 128) (local.get $i) (i64.const 1000000)))
31      (local.set $i (i32.atomic.load (i32.const 128)))
32      (br_if $again (i32.lt_s (local.get $i) (i32.const 3)))
33    )
34
35    ;; Print "Done".
36    (call $print (i32.const 64) (i32.const 5))
37  )
38
39  ;; A threads-enabled module must export this spec-designated entry point.
40  (func (export "wasi_thread_start") (param $tid i32) (param $start_arg i32)
41    (call $print (i32.const 96) (i32.const 26))
42    ;; After printing, we atomically increment the value at address 128 and then
43    ;; wake up the main thread's join loop.
44    (drop (i32.atomic.rmw.add (i32.const 128) (i32.const 1)))
45    (drop (memory.atomic.notify (i32.const 128) (i32.const 1)))
46  )
47
48  ;; A helper function for printing ptr-len strings.
49  (func $print (param $ptr i32) (param $len i32)
50    (i32.store (i32.const 8) (local.get $len))
51    (i32.store (i32.const 4) (local.get $ptr))
52        (drop (call $__wasi_fd_write
53          (i32.const 1)
54          (i32.const 4)
55          (i32.const 1)
56          (i32.const 0)))
57  )
58
59  ;; We still need to export the shared memory for Wiggle's sake.
60  (export "memory" (memory $shmem))
61
62  (data (i32.const 32) "Called _start\0a")
63  (data (i32.const 64) "Done\0a")
64  (data (i32.const 96) "Running wasi_thread_start\0a")
65)
66