Skip to content

A lightweight WebAssembly runtime that is fast, secure, and standards-compliant

Make threading promote behavior more spec-compliant (#14635)

Alex Crichton55c37f87b891
diff --git a/crates/wasmtime/src/runtime/component/concurrent.rs b/crates/wasmtime/src/runtime/component/concurrent.rs
index 30661548f9..eb22762eb3 100644
--- a/crates/wasmtime/src/runtime/component/concurrent.rs
+++ b/crates/wasmtime/src/runtime/component/concurrent.rs
@@ -4175,27 +4175,59 @@ impl Instance {
bail!(Trap::CannotResumeThread);
}
- let state = store.concurrent_state_mut()?;
- let thread = state.get_mut(guest_thread.thread)?;
let priority = match how {
- ResumeThread::Promote | ResumeThread::Resume => Priority::Switch,
+ ResumeThread::Resume => Priority::Switch,
ResumeThread::ResumeLater => Priority::Low,
- };
- match (&how, &thread.state) {
- // Promotion is a noop unless the thread is in a ready state.
- (ResumeThread::Promote, GuestThreadState::Ready { .. }) => {}
- (ResumeThread::Promote, _) => return Ok(false),
+ // Promotion is a noop unless the thread is ready, so only search
+ // through pending work items to see if something matches.
+ ResumeThread::Promote => {
+ let instance = self.runtime_instance(runtime_instance);
+ let do_not_enter = store
+ .instance_state(instance)
+ .concurrent_state()
+ .do_not_enter;
+ return store.concurrent_state_mut()?.promote_work_item_matching(
+ |item: &WorkItem| match item {
+ WorkItem::ResumeThread { thread, .. }
+ | WorkItem::ResumeFiber { thread, .. } => *thread == guest_thread,
+
+ WorkItem::GuestCall {
+ call: GuestCall { thread, kind },
+ ..
+ } => {
+ *thread == guest_thread
+ && match kind {
+ // Events can only be delivered if this
+ // instance's exclusive lock isn't held, so
+ // gate on that.
+ GuestCallKind::DeliverEvent { .. } => !do_not_enter,
+
+ // A not-yet-started thread made ready via
+ // `thread.resume-later`.
+ GuestCallKind::StartExplicit(_) => true,
+
+ // Technically shouldn't be possible since
+ // if this hasn't started nothing could get
+ // its id...
+ GuestCallKind::StartImplicit(_) => false,
+ }
+ }
- // When resuming a thread it must be in a suspended state otherwise
- // this operation is a trap.
- (
- ResumeThread::Resume | ResumeThread::ResumeLater,
- GuestThreadState::NotStartedExplicit(_) | GuestThreadState::Suspended(_),
- ) => {}
- (ResumeThread::Resume | ResumeThread::ResumeLater, _) => {
- bail!(Trap::CannotResumeThread)
+ WorkItem::PushFuture(_) | WorkItem::WorkerFunction(_) => false,
+ },
+ );
}
+ };
+
+ let state = store.concurrent_state_mut()?;
+ let thread = state.get_mut(guest_thread.thread)?;
+
+ // When resuming a thread it must be in a suspended state otherwise
+ // this operation is a trap.
+ match &thread.state {
+ GuestThreadState::NotStartedExplicit(_) | GuestThreadState::Suspended(_) => {}
+ _ => bail!(Trap::CannotResumeThread),
}
match mem::replace(&mut thread.state, GuestThreadState::Running) {
@@ -4225,17 +4257,9 @@ impl Instance {
priority,
)?;
}
- GuestThreadState::Ready { fiber } => {
- log::trace!("resuming thread {thread_id:?} that was ready");
- thread.state = GuestThreadState::Ready { fiber };
- store
- .concurrent_state_mut()?
- .promote_thread_work_item(guest_thread)?;
- }
- other @ (GuestThreadState::NotStartedImplicit
- | GuestThreadState::Running
- | GuestThreadState::Completed) => {
+ other => {
thread.state = other;
+ bail_bug!("thread state checked to be resumable above");
}
}
Ok(true)
diff --git a/tests/misc_testsuite/component-model-threading/promote-ready-threads.wast b/tests/misc_testsuite/component-model-threading/promote-ready-threads.wast
new file mode 100644
index 0000000000..1d0a2fd955
--- /dev/null
+++ b/tests/misc_testsuite/component-model-threading/promote-ready-threads.wast
@@ -0,0 +1,478 @@
+;;! component_model_async = true
+;;! component_model_threading = true
+
+;; `thread.{yield,suspend}-then-promote(T)` must switch to T immediately if T
+;; is ready, per the spec's `Thread.ready`, regardless of how T became ready.
+;; Each case below makes two threads ready, the promoted one second, and
+;; records the order in which they run.
+
+;; T2 was made ready with `thread.resume-later` but hasn't started yet.
+(component
+ (core module $libc (table (export "t") 1 funcref))
+ (core instance $libc (instantiate $libc))
+ (core type $start-ty (func (param i32)))
+ (core func $thread.new-indirect (canon thread.new-indirect $start-ty (core table $libc "t")))
+ (core func $thread.resume-later (canon thread.resume-later))
+ (core func $thread.yield-then-promote (canon thread.yield-then-promote))
+ (core func $task.return (canon task.return (result u32)))
+ (core module $m
+ (import "" "thread.new-indirect" (func $thread.new-indirect (param i32 i32) (result i32)))
+ (import "" "thread.resume-later" (func $thread.resume-later (param i32)))
+ (import "" "thread.yield-then-promote" (func $thread.yield-then-promote (param i32) (result i32)))
+ (import "" "task.return" (func $task.return (param i32)))
+ (import "libc" "t" (table 1 funcref))
+ (global $g (mut i32) (i32.const 0))
+ (global $done (mut i32) (i32.const 0))
+ (func $f (param i32)
+ (global.set $g (i32.add (i32.mul (global.get $g) (i32.const 10)) (local.get 0)))
+ (global.set $done (i32.add (global.get $done) (i32.const 1))))
+ (elem (i32.const 0) func $f)
+ (func (export "run") (result i32)
+ (local $t1 i32) (local $t2 i32)
+ (local.set $t1 (call $thread.new-indirect (i32.const 0) (i32.const 1)))
+ (local.set $t2 (call $thread.new-indirect (i32.const 0) (i32.const 2)))
+ (call $thread.resume-later (local.get $t1))
+ (call $thread.resume-later (local.get $t2))
+ (drop (call $thread.yield-then-promote (local.get $t2)))
+ (i32.const 1 (; YIELD ;)))
+ (func (export "cb") (param i32 i32 i32) (result i32)
+ (if (i32.ne (global.get $done) (i32.const 2))
+ (then (return (i32.const 1 (; YIELD ;)))))
+ (call $task.return (global.get $g))
+ (i32.const 0 (; EXIT ;))))
+ (core instance $i (instantiate $m
+ (with "libc" (instance $libc))
+ (with "" (instance
+ (export "thread.new-indirect" (func $thread.new-indirect))
+ (export "thread.resume-later" (func $thread.resume-later))
+ (export "thread.yield-then-promote" (func $thread.yield-then-promote))
+ (export "task.return" (func $task.return))))))
+ (func (export "run") async (result u32)
+ (canon lift (core func $i "run") async (callback (core func $i "cb"))))
+)
+(assert_return (invoke "run") (u32.const 21))
+
+;; T2 started, suspended itself, and was then made ready again with
+;; `thread.resume-later`.
+(component
+ (core module $libc (table (export "t") 2 funcref))
+ (core instance $libc (instantiate $libc))
+ (core type $start-ty (func (param i32)))
+ (core func $thread.new-indirect (canon thread.new-indirect $start-ty (core table $libc "t")))
+ (core func $thread.resume-later (canon thread.resume-later))
+ (core func $thread.yield-then-resume (canon thread.yield-then-resume))
+ (core func $thread.yield-then-promote (canon thread.yield-then-promote))
+ (core func $thread.suspend (canon thread.suspend))
+ (core func $task.return (canon task.return (result u32)))
+ (core module $m
+ (import "" "thread.new-indirect" (func $thread.new-indirect (param i32 i32) (result i32)))
+ (import "" "thread.resume-later" (func $thread.resume-later (param i32)))
+ (import "" "thread.yield-then-resume" (func $thread.yield-then-resume (param i32) (result i32)))
+ (import "" "thread.yield-then-promote" (func $thread.yield-then-promote (param i32) (result i32)))
+ (import "" "thread.suspend" (func $thread.suspend (result i32)))
+ (import "" "task.return" (func $task.return (param i32)))
+ (import "libc" "t" (table 2 funcref))
+ (global $g (mut i32) (i32.const 0))
+ (global $done (mut i32) (i32.const 0))
+ (func $log (param i32)
+ (global.set $g (i32.add (i32.mul (global.get $g) (i32.const 10)) (local.get 0)))
+ (global.set $done (i32.add (global.get $done) (i32.const 1))))
+ (func $f (param i32)
+ (call $log (local.get 0)))
+ (func $suspend-first (param i32)
+ (drop (call $thread.suspend))
+ (call $log (local.get 0)))
+ (elem (i32.const 0) func $f $suspend-first)
+ (func (export "run") (result i32)
+ (local $t1 i32) (local $t2 i32)
+ (local.set $t2 (call $thread.new-indirect (i32.const 1) (i32.const 2)))
+ ;; let T2 run until it suspends itself
+ (drop (call $thread.yield-then-resume (local.get $t2)))
+ (local.set $t1 (call $thread.new-indirect (i32.const 0) (i32.const 1)))
+ (call $thread.resume-later (local.get $t1))
+ (call $thread.resume-later (local.get $t2))
+ (drop (call $thread.yield-then-promote (local.get $t2)))
+ (i32.const 1 (; YIELD ;)))
+ (func (export "cb") (param i32 i32 i32) (result i32)
+ (if (i32.ne (global.get $done) (i32.const 2))
+ (then (return (i32.const 1 (; YIELD ;)))))
+ (call $task.return (global.get $g))
+ (i32.const 0 (; EXIT ;))))
+ (core instance $i (instantiate $m
+ (with "libc" (instance $libc))
+ (with "" (instance
+ (export "thread.new-indirect" (func $thread.new-indirect))
+ (export "thread.resume-later" (func $thread.resume-later))
+ (export "thread.yield-then-resume" (func $thread.yield-then-resume))
+ (export "thread.yield-then-promote" (func $thread.yield-then-promote))
+ (export "thread.suspend" (func $thread.suspend))
+ (export "task.return" (func $task.return))))))
+ (func (export "run") async (result u32)
+ (canon lift (core func $i "run") async (callback (core func $i "cb"))))
+)
+(assert_return (invoke "run") (u32.const 21))
+
+;; The promoted thread is the implicit thread of a callback-lifted task which
+;; returned `WAIT` and whose event has since arrived. Explicit thread T1 is
+;; blocked in `waitable-set.wait` and is woken up first, and then explicit
+;; thread T3 wakes up the implicit thread and promotes it.
+(component
+ (core module $libc
+ (table (export "t") 3 funcref)
+ (memory (export "m") 1))
+ (core instance $libc (instantiate $libc))
+ (core type $start-ty (func (param i32)))
+
+ (type $f (future))
+ (core func $future.new (canon future.new $f))
+ (core func $future.read (canon future.read $f async))
+ (core func $future.write (canon future.write $f async))
+ (core func $waitable-set.new (canon waitable-set.new))
+ (core func $waitable-set.wait (canon waitable-set.wait (memory (core memory $libc "m"))))
+ (core func $waitable.join (canon waitable.join))
+ (core func $thread.new-indirect (canon thread.new-indirect $start-ty (core table $libc "t")))
+ (core func $thread.resume-later (canon thread.resume-later))
+ (core func $thread.index (canon thread.index))
+ (core func $thread.yield-then-promote (canon thread.yield-then-promote))
+ (core func $task.return (canon task.return (result u32)))
+
+ (core module $m
+ (import "" "future.new" (func $future.new (result i64)))
+ (import "" "future.read" (func $future.read (param i32 i32) (result i32)))
+ (import "" "future.write" (func $future.write (param i32 i32) (result i32)))
+ (import "" "waitable-set.new" (func $waitable-set.new (result i32)))
+ (import "" "waitable-set.wait" (func $waitable-set.wait (param i32 i32) (result i32)))
+ (import "" "waitable.join" (func $waitable.join (param i32 i32)))
+ (import "" "thread.new-indirect" (func $thread.new-indirect (param i32 i32) (result i32)))
+ (import "" "thread.resume-later" (func $thread.resume-later (param i32)))
+ (import "" "thread.index" (func $thread.index (result i32)))
+ (import "" "thread.yield-then-promote" (func $thread.yield-then-promote (param i32) (result i32)))
+ (import "" "task.return" (func $task.return (param i32)))
+ (import "libc" "t" (table 3 funcref))
+ (global $g (mut i32) (i32.const 0))
+ (global $done (mut i32) (i32.const 0))
+ (global $w1 (mut i32) (i32.const 0))
+ (global $w2 (mut i32) (i32.const 0))
+ (global $s1 (mut i32) (i32.const 0))
+ (global $main (mut i32) (i32.const 0))
+ (func $log (param i32)
+ (global.set $g (i32.add (i32.mul (global.get $g) (i32.const 10)) (local.get 0)))
+ (global.set $done (i32.add (global.get $done) (i32.const 1))))
+ ;; start a read on a new future, joining it to `set` and returning the
+ ;; writable end
+ (func $start-read (param $set i32) (result i32)
+ (local $tmp i64) (local $r i32)
+ (local.set $tmp (call $future.new))
+ (local.set $r (i32.wrap_i64 (local.get $tmp)))
+ (if (i32.ne (call $future.read (local.get $r) (i32.const 0)) (i32.const -1 (; BLOCKED ;)))
+ (then unreachable))
+ (call $waitable.join (local.get $r) (local.get $set))
+ (i32.wrap_i64 (i64.shr_u (local.get $tmp) (i64.const 32))))
+ (func $waiter (param i32)
+ (if (i32.ne (call $waitable-set.wait (global.get $s1) (i32.const 0)) (i32.const 4 (; FUTURE_READ ;)))
+ (then unreachable))
+ (call $log (local.get 0)))
+ (func $wake-both (param i32)
+ ;; wake up T1 and then the implicit thread, and then promote the latter
+ (if (i32.ne (call $future.write (global.get $w1) (i32.const 0)) (i32.const 0 (; COMPLETED ;)))
+ (then unreachable))
+ (if (i32.ne (call $future.write (global.get $w2) (i32.const 0)) (i32.const 0 (; COMPLETED ;)))
+ (then unreachable))
+ (drop (call $thread.yield-then-promote (global.get $main))))
+ (elem (i32.const 0) func $waiter $wake-both)
+ (func (export "run") (result i32)
+ (local $s2 i32) (local $t1 i32) (local $t3 i32)
+ (global.set $main (call $thread.index))
+ (global.set $s1 (call $waitable-set.new))
+ (global.set $w1 (call $start-read (global.get $s1)))
+ (local.set $s2 (call $waitable-set.new))
+ (global.set $w2 (call $start-read (local.get $s2)))
+ (local.set $t1 (call $thread.new-indirect (i32.const 0) (i32.const 1)))
+ (local.set $t3 (call $thread.new-indirect (i32.const 1) (i32.const 0)))
+ (call $thread.resume-later (local.get $t1))
+ (call $thread.resume-later (local.get $t3))
+ (i32.or (i32.const 2 (; WAIT ;)) (i32.shl (local.get $s2) (i32.const 4))))
+ (func (export "cb") (param $event i32) (param i32 i32) (result i32)
+ (if (i32.eq (local.get $event) (i32.const 4 (; FUTURE_READ ;)))
+ (then (call $log (i32.const 2))))
+ (if (i32.ne (global.get $done) (i32.const 2))
+ (then (return (i32.const 1 (; YIELD ;)))))
+ (call $task.return (global.get $g))
+ (i32.const 0 (; EXIT ;))))
+ (core instance $i (instantiate $m
+ (with "libc" (instance $libc))
+ (with "" (instance
+ (export "future.new" (func $future.new))
+ (export "future.read" (func $future.read))
+ (export "future.write" (func $future.write))
+ (export "waitable-set.new" (func $waitable-set.new))
+ (export "waitable-set.wait" (func $waitable-set.wait))
+ (export "waitable.join" (func $waitable.join))
+ (export "thread.new-indirect" (func $thread.new-indirect))
+ (export "thread.resume-later" (func $thread.resume-later))
+ (export "thread.index" (func $thread.index))
+ (export "thread.yield-then-promote" (func $thread.yield-then-promote))
+ (export "task.return" (func $task.return))))))
+ (func (export "run") async (result u32)
+ (canon lift (core func $i "run") async (callback (core func $i "cb"))))
+)
+(assert_return (invoke "run") (u32.const 21))
+
+;; The promoted thread T1 is a stackful explicit thread blocked in
+;; `waitable-set.wait` whose event has arrived. T1 and T2 each wait on their
+;; own future; both futures are written, T2's first, and then T1 is promoted.
+;; The result is `100 * <promote's return value> + <run order>`, so this also
+;; checks that a successful promotion returns 0.
+(component
+ (core module $libc
+ (table (export "t") 1 funcref)
+ (memory (export "m") 1))
+ (core instance $libc (instantiate $libc))
+ (core type $start-ty (func (param i32)))
+ (type $f (future))
+ (core func $future.new (canon future.new $f))
+ (core func $future.read (canon future.read $f async))
+ (core func $future.write (canon future.write $f async))
+ (core func $waitable-set.new (canon waitable-set.new))
+ (core func $waitable-set.wait (canon waitable-set.wait (memory (core memory $libc "m"))))
+ (core func $waitable.join (canon waitable.join))
+ (core func $thread.new-indirect (canon thread.new-indirect $start-ty (core table $libc "t")))
+ (core func $thread.yield-then-resume (canon thread.yield-then-resume))
+ (core func $thread.yield-then-promote (canon thread.yield-then-promote))
+ (core func $task.return (canon task.return (result u32)))
+ (core module $m
+ (import "" "future.new" (func $future.new (result i64)))
+ (import "" "future.read" (func $future.read (param i32 i32) (result i32)))
+ (import "" "future.write" (func $future.write (param i32 i32) (result i32)))
+ (import "" "waitable-set.new" (func $waitable-set.new (result i32)))
+ (import "" "waitable-set.wait" (func $waitable-set.wait (param i32 i32) (result i32)))
+ (import "" "waitable.join" (func $waitable.join (param i32 i32)))
+ (import "" "thread.new-indirect" (func $thread.new-indirect (param i32 i32) (result i32)))
+ (import "" "thread.yield-then-resume" (func $thread.yield-then-resume (param i32) (result i32)))
+ (import "" "thread.yield-then-promote" (func $thread.yield-then-promote (param i32) (result i32)))
+ (import "" "task.return" (func $task.return (param i32)))
+ (import "libc" "t" (table 1 funcref))
+ (global $g (mut i32) (i32.const 0))
+ (global $done (mut i32) (i32.const 0))
+ (global $s1 (mut i32) (i32.const 0))
+ (global $s2 (mut i32) (i32.const 0))
+ (global $ret (mut i32) (i32.const -1))
+ (func $log (param i32)
+ (global.set $g (i32.add (i32.mul (global.get $g) (i32.const 10)) (local.get 0)))
+ (global.set $done (i32.add (global.get $done) (i32.const 1))))
+ (func $start-read (param $set i32) (result i32)
+ (local $tmp i64) (local $r i32)
+ (local.set $tmp (call $future.new))
+ (local.set $r (i32.wrap_i64 (local.get $tmp)))
+ (if (i32.ne (call $future.read (local.get $r) (i32.const 0)) (i32.const -1))
+ (then unreachable))
+ (call $waitable.join (local.get $r) (local.get $set))
+ (i32.wrap_i64 (i64.shr_u (local.get $tmp) (i64.const 32))))
+ (func $waiter (param $n i32)
+ (if (i32.ne (call $waitable-set.wait
+ (select (global.get $s1) (global.get $s2) (i32.eq (local.get $n) (i32.const 1)))
+ (i32.const 0))
+ (i32.const 4))
+ (then unreachable))
+ (call $log (local.get $n)))
+ (elem (i32.const 0) func $waiter)
+ (func (export "run") (result i32)
+ (local $t1 i32) (local $t2 i32) (local $w1 i32) (local $w2 i32)
+ (global.set $s1 (call $waitable-set.new))
+ (global.set $s2 (call $waitable-set.new))
+ (local.set $w1 (call $start-read (global.get $s1)))
+ (local.set $w2 (call $start-read (global.get $s2)))
+ (local.set $t1 (call $thread.new-indirect (i32.const 0) (i32.const 1)))
+ (local.set $t2 (call $thread.new-indirect (i32.const 0) (i32.const 2)))
+ ;; run each until it blocks in wait
+ (drop (call $thread.yield-then-resume (local.get $t1)))
+ (drop (call $thread.yield-then-resume (local.get $t2)))
+ (if (global.get $done) (then unreachable))
+ ;; wake T2 first, then T1
+ (if (i32.ne (call $future.write (local.get $w2) (i32.const 0)) (i32.const 0)) (then unreachable))
+ (if (i32.ne (call $future.write (local.get $w1) (i32.const 0)) (i32.const 0)) (then unreachable))
+ (global.set $ret (call $thread.yield-then-promote (local.get $t1)))
+ (i32.const 1 (; YIELD ;)))
+ (func (export "cb") (param i32 i32 i32) (result i32)
+ (if (i32.ne (global.get $done) (i32.const 2))
+ (then (return (i32.const 1 (; YIELD ;)))))
+ (call $task.return (i32.add (i32.mul (global.get $ret) (i32.const 100)) (global.get $g)))
+ (i32.const 0 (; EXIT ;))))
+ (core instance $i (instantiate $m
+ (with "libc" (instance $libc))
+ (with "" (instance
+ (export "future.new" (func $future.new))
+ (export "future.read" (func $future.read))
+ (export "future.write" (func $future.write))
+ (export "waitable-set.new" (func $waitable-set.new))
+ (export "waitable-set.wait" (func $waitable-set.wait))
+ (export "waitable.join" (func $waitable.join))
+ (export "thread.new-indirect" (func $thread.new-indirect))
+ (export "thread.yield-then-resume" (func $thread.yield-then-resume))
+ (export "thread.yield-then-promote" (func $thread.yield-then-promote))
+ (export "task.return" (func $task.return))))))
+ (func (export "run") async (result u32)
+ (canon lift (core func $i "run") async (callback (core func $i "cb"))))
+)
+(assert_return (invoke "run") (u32.const 12))
+
+;; Same as the previous case, but with `thread.suspend-then-promote`. The main
+;; thread stays suspended until T1 resumes it with `thread.resume-later`.
+(component
+ (core module $libc
+ (table (export "t") 1 funcref)
+ (memory (export "m") 1))
+ (core instance $libc (instantiate $libc))
+ (core type $start-ty (func (param i32)))
+ (type $f (future))
+ (core func $future.new (canon future.new $f))
+ (core func $future.read (canon future.read $f async))
+ (core func $future.write (canon future.write $f async))
+ (core func $waitable-set.new (canon waitable-set.new))
+ (core func $waitable-set.wait (canon waitable-set.wait (memory (core memory $libc "m"))))
+ (core func $waitable.join (canon waitable.join))
+ (core func $thread.new-indirect (canon thread.new-indirect $start-ty (core table $libc "t")))
+ (core func $thread.yield-then-resume (canon thread.yield-then-resume))
+ (core func $thread.suspend-then-promote (canon thread.suspend-then-promote))
+ (core func $task.return (canon task.return (result u32)))
+ (core func $thread.index (canon thread.index))
+ (core func $thread.resume-later (canon thread.resume-later))
+ (core module $m
+ (import "" "future.new" (func $future.new (result i64)))
+ (import "" "future.read" (func $future.read (param i32 i32) (result i32)))
+ (import "" "future.write" (func $future.write (param i32 i32) (result i32)))
+ (import "" "waitable-set.new" (func $waitable-set.new (result i32)))
+ (import "" "waitable-set.wait" (func $waitable-set.wait (param i32 i32) (result i32)))
+ (import "" "waitable.join" (func $waitable.join (param i32 i32)))
+ (import "" "thread.new-indirect" (func $thread.new-indirect (param i32 i32) (result i32)))
+ (import "" "thread.yield-then-resume" (func $thread.yield-then-resume (param i32) (result i32)))
+ (import "" "thread.suspend-then-promote" (func $thread.suspend-then-promote (param i32) (result i32)))
+ (import "" "task.return" (func $task.return (param i32)))
+ (import "libc" "t" (table 1 funcref))
+ (import "" "thread.index" (func $thread.index (result i32)))
+ (import "" "thread.resume-later" (func $thread.resume-later (param i32)))
+ (global $main (mut i32) (i32.const 0))
+ (global $g (mut i32) (i32.const 0))
+ (global $done (mut i32) (i32.const 0))
+ (global $s1 (mut i32) (i32.const 0))
+ (global $s2 (mut i32) (i32.const 0))
+ (global $ret (mut i32) (i32.const -1))
+ (func $log (param i32)
+ (global.set $g (i32.add (i32.mul (global.get $g) (i32.const 10)) (local.get 0)))
+ (global.set $done (i32.add (global.get $done) (i32.const 1))))
+ (func $start-read (param $set i32) (result i32)
+ (local $tmp i64) (local $r i32)
+ (local.set $tmp (call $future.new))
+ (local.set $r (i32.wrap_i64 (local.get $tmp)))
+ (if (i32.ne (call $future.read (local.get $r) (i32.const 0)) (i32.const -1))
+ (then unreachable))
+ (call $waitable.join (local.get $r) (local.get $set))
+ (i32.wrap_i64 (i64.shr_u (local.get $tmp) (i64.const 32))))
+ (func $waiter (param $n i32)
+ (if (i32.ne (call $waitable-set.wait
+ (select (global.get $s1) (global.get $s2) (i32.eq (local.get $n) (i32.const 1)))
+ (i32.const 0))
+ (i32.const 4))
+ (then unreachable))
+ (call $log (local.get $n))
+ ;; resume the suspended main thread
+ (if (i32.eq (local.get $n) (i32.const 1)) (then (call $thread.resume-later (global.get $main)))))
+ (elem (i32.const 0) func $waiter)
+ (func (export "run") (result i32)
+ (local $t1 i32) (local $t2 i32) (local $w1 i32) (local $w2 i32)
+ (global.set $main (call $thread.index))
+ (global.set $s1 (call $waitable-set.new))
+ (global.set $s2 (call $waitable-set.new))
+ (local.set $w1 (call $start-read (global.get $s1)))
+ (local.set $w2 (call $start-read (global.get $s2)))
+ (local.set $t1 (call $thread.new-indirect (i32.const 0) (i32.const 1)))
+ (local.set $t2 (call $thread.new-indirect (i32.const 0) (i32.const 2)))
+ ;; run each until it blocks in wait
+ (drop (call $thread.yield-then-resume (local.get $t1)))
+ (drop (call $thread.yield-then-resume (local.get $t2)))
+ (if (global.get $done) (then unreachable))
+ ;; wake T2 first, then T1
+ (if (i32.ne (call $future.write (local.get $w2) (i32.const 0)) (i32.const 0)) (then unreachable))
+ (if (i32.ne (call $future.write (local.get $w1) (i32.const 0)) (i32.const 0)) (then unreachable))
+ (global.set $ret (call $thread.suspend-then-promote (local.get $t1)))
+ (i32.const 1 (; YIELD ;)))
+ (func (export "cb") (param i32 i32 i32) (result i32)
+ (if (i32.ne (global.get $done) (i32.const 2))
+ (then (return (i32.const 1 (; YIELD ;)))))
+ (call $task.return (i32.add (i32.mul (global.get $ret) (i32.const 100)) (global.get $g)))
+ (i32.const 0 (; EXIT ;))))
+ (core instance $i (instantiate $m
+ (with "libc" (instance $libc))
+ (with "" (instance
+ (export "future.new" (func $future.new))
+ (export "future.read" (func $future.read))
+ (export "future.write" (func $future.write))
+ (export "waitable-set.new" (func $waitable-set.new))
+ (export "waitable-set.wait" (func $waitable-set.wait))
+ (export "waitable.join" (func $waitable.join))
+ (export "thread.new-indirect" (func $thread.new-indirect))
+ (export "thread.yield-then-resume" (func $thread.yield-then-resume))
+ (export "thread.suspend-then-promote" (func $thread.suspend-then-promote))
+ (export "task.return" (func $task.return))
+ (export "thread.index" (func $thread.index))
+ (export "thread.resume-later" (func $thread.resume-later))))))
+ (func (export "run") async (result u32)
+ (canon lift (core func $i "run") async (callback (core func $i "cb"))))
+)
+(assert_return (invoke "run") (u32.const 12))
+
+;; The promoted thread is the implicit thread of a callback-lifted task which
+;; returned `YIELD`. Explicit thread T1 is made ready first, and then explicit
+;; thread T3 promotes the implicit thread, whose callback logs 2.
+(component
+ (core module $libc (table (export "t") 2 funcref))
+ (core instance $libc (instantiate $libc))
+ (core type $start-ty (func (param i32)))
+ (core func $thread.new-indirect (canon thread.new-indirect $start-ty (core table $libc "t")))
+ (core func $thread.resume-later (canon thread.resume-later))
+ (core func $thread.yield-then-promote (canon thread.yield-then-promote))
+ (core func $thread.index (canon thread.index))
+ (core func $task.return (canon task.return (result u32)))
+ (core module $m
+ (import "" "thread.new-indirect" (func $thread.new-indirect (param i32 i32) (result i32)))
+ (import "" "thread.resume-later" (func $thread.resume-later (param i32)))
+ (import "" "thread.yield-then-promote" (func $thread.yield-then-promote (param i32) (result i32)))
+ (import "" "thread.index" (func $thread.index (result i32)))
+ (import "" "task.return" (func $task.return (param i32)))
+ (import "libc" "t" (table 2 funcref))
+ (global $g (mut i32) (i32.const 0))
+ (global $done (mut i32) (i32.const 0))
+ (global $main (mut i32) (i32.const 0))
+ (global $first (mut i32) (i32.const 1))
+ (func $log (param i32)
+ (global.set $g (i32.add (i32.mul (global.get $g) (i32.const 10)) (local.get 0)))
+ (global.set $done (i32.add (global.get $done) (i32.const 1))))
+ (func $f (param i32) (call $log (local.get 0)))
+ (func $promoter (param i32) (drop (call $thread.yield-then-promote (global.get $main))))
+ (elem (i32.const 0) func $f $promoter)
+ (func (export "run") (result i32)
+ (local $t1 i32) (local $t3 i32)
+ (global.set $main (call $thread.index))
+ (local.set $t1 (call $thread.new-indirect (i32.const 0) (i32.const 1)))
+ (local.set $t3 (call $thread.new-indirect (i32.const 1) (i32.const 0)))
+ (call $thread.resume-later (local.get $t3))
+ (call $thread.resume-later (local.get $t1))
+ (i32.const 1 (; YIELD ;)))
+ (func (export "cb") (param i32 i32 i32) (result i32)
+ (if (global.get $first) (then (global.set $first (i32.const 0)) (call $log (i32.const 2))))
+ (if (i32.ne (global.get $done) (i32.const 2))
+ (then (return (i32.const 1 (; YIELD ;)))))
+ (call $task.return (global.get $g))
+ (i32.const 0 (; EXIT ;))))
+ (core instance $i (instantiate $m
+ (with "libc" (instance $libc))
+ (with "" (instance
+ (export "thread.new-indirect" (func $thread.new-indirect))
+ (export "thread.resume-later" (func $thread.resume-later))
+ (export "thread.yield-then-promote" (func $thread.yield-then-promote))
+ (export "thread.index" (func $thread.index))
+ (export "task.return" (func $task.return))))))
+ (func (export "run") async (result u32)
+ (canon lift (core func $i "run") async (callback (core func $i "cb"))))
+)
+(assert_return (invoke "run") (u32.const 21))