Skip to content

Commit

Permalink
yet another wat
Browse files Browse the repository at this point in the history
  • Loading branch information
yamt committed Jun 1, 2023
1 parent 4c15914 commit d785f0f
Showing 1 changed file with 95 additions and 0 deletions.
95 changes: 95 additions & 0 deletions wat/wasi-threads/many_spawn.wat.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
;; m4 many_spawn.wat.in|wat2wasm --enable-threads -o many_spawn.wasm -

ifdef(`NUM',,`define(`NUM',100000)')

define(`_repeat',`ifelse(eval($1<=$2),1,`$3`'$0(incr($1),$2,`$3')')')dnl
define(`repeat',`_repeat(1,$1,``$2'')')dnl

define(`EXPECTED',`syscmd(echo "(NUM * (NUM + 1) / 2) % 4294967296" | bc)')

(module
(memory (export "memory") (import "env" "memory") 1 1 shared)
(func $thread_spawn (import "wasi" "thread-spawn") (param i32) (result i32))
(func (export "wasi_thread_start") (param $tid i32) (param $user_arg i32)
local.get $user_arg
call $notify
)
(func $notify (param $v i32)
i32.const 4
local.get $v
i32.atomic.rmw.add
drop
i32.const 0
i32.const 1
i32.atomic.rmw.sub
i32.const 1
i32.ne
br_if 0
i32.const 0
i32.const 1
memory.atomic.notify
drop
)
(func $spawn (param i32)
;; spawn a thread
local.get 0
call $thread_spawn
;; check error
i32.const 0
i32.le_s
if
unreachable
end
)
(func $test (export "test") (param $num i32) (result i32) (local $tmp i32)
i32.const 0
local.get $num
i32.store
;; spawn threads
local.get $num
loop (param i32)
local.tee $tmp
local.get $tmp
call $spawn
i32.const 1
i32.sub
local.tee $tmp
local.get $tmp
br_if 0
drop
end
;; wait for the threads to finish
block
loop
i32.const 0
i32.atomic.load
local.tee $tmp
i32.eqz
br_if 1
i32.const 0
local.get $tmp
i64.const -1
memory.atomic.wait32
;; assert it was not a timeout
i32.const 2
i32.eq
if
unreachable
end
br 0
end
end
i32.const 4
i32.load
)
(func (export "_start")
i32.const NUM
call $test
i32.const EXPECTED
i32.eq
block (param i32)
br_if 0
unreachable
end
)
)

0 comments on commit d785f0f

Please sign in to comment.