Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 16 additions & 29 deletions design/mvp/CanonicalABI.md
Original file line number Diff line number Diff line change
Expand Up @@ -554,7 +554,6 @@ class Task:
state: State
num_borrows: int
implicit_thread: Optional[Thread]
threads: list[Thread]

def __init__(self, ft, opts, inst, on_start, on_resolve):
self.ft = ft
Expand All @@ -565,7 +564,6 @@ class Task:
self.state = Task.State.INITIAL
self.num_borrows = 0
self.implicit_thread = None
self.threads = []
```

The `Task.needs_exclusive` predicate returns whether this task's implicit thread
Expand Down Expand Up @@ -627,14 +625,8 @@ exports.
if self.needs_exclusive():
assert(self.inst.exclusive_thread is None)
self.inst.exclusive_thread = self.implicit_thread
self.register_thread(self.implicit_thread)
self.implicit_thread.index = self.inst.threads.add(self.implicit_thread)
return True

def register_thread(self, thread):
assert(thread not in self.threads and thread.task is self)
self.threads.append(thread)
assert(thread.index is None)
thread.index = self.inst.threads.add(thread)
```
Since the order in which suspended threads are resumed is nondeterministic (see
`Store.tick` below), once `Task.enter_implicit_thread` suspends the task's
Expand All @@ -649,32 +641,26 @@ above definition ensures the following properties:
thundering herd of pending tasks).

Once a task's implicit thread has cleared the backpressure gate, it is added to
the lists of threads running inside the current task and component instance by
`Task.register_thread()` (which is also called by `thread.new-indirect`, below).
the component instance's `threads` table (just like the explicit threads created
by `thread.new-indirect`, below).

Symmetrically, the `Task.exit_implicit_thread` method is called before a task's
implicit thread returns to reverse the effects of `Task.enter_implicit_thread`.
In particular, if the `exclusive_thread` lock was acquired, it is released.
`Task.unregister_thread` (which is also called by `thread.new-indirect`, below)
traps if the task's last thread is unregistered and the task has not yet
returned a value to its caller.
implicit thread returns to reverse the effects of `Task.enter_implicit_thread`,
removing the thread from the `threads` table and, if the `exclusive_thread` lock
was acquired, releasing it.
```python
def exit_implicit_thread(self):
assert(current_thread() is self.implicit_thread)
self.unregister_thread(self.implicit_thread)
self.inst.threads.remove(self.implicit_thread.index)
if self.ft.async_ and self.needs_exclusive():
assert(self.inst.exclusive_thread is self.implicit_thread)
self.inst.exclusive_thread = None

def unregister_thread(self, thread):
assert(thread in self.threads and thread.task is self)
self.threads.remove(thread)
if len(self.threads) == 0:
trap_if(self.state != Task.State.RESOLVED)
assert(self.num_borrows == 0)
assert(thread.index is not None)
self.inst.threads.remove(thread.index)
```
Note that there is no trap when a task's implicit thread (or any other thread)
exits before the task has resolved (by calling `task.return` or `task.cancel`),
even if it is the task's last thread. In that case, the task will never
resolve, which, from the caller's perspective, is indistinguishable from the
case where the task's threads are all blocked forever.

The `Task.request_cancellation` method implements the `OnCancel` callback that
is called by the `subtask.cancel` built-in. If a task's implicit thread is
Expand Down Expand Up @@ -3465,7 +3451,8 @@ In both of the `async` cases below (with or without `callback`), the
`task.return` built-in must be called, providing the return value as core wasm
*parameters* to the `task.return` built-in (rather than as core function
results as in the synchronous case). If `task.return` is *not* called by the
time the `Task`'s last `Thread` exits, there is a trap (in `Task.unregister_thread`).
time the `Task`'s last `Thread` exits, the task never resolves (as described
in `Task.exit_implicit_thread` above).

In the `async` non-`callback` ("stackful async") case, there is a single call
to the core wasm callee which must return empty core results. Waiting for async
Expand Down Expand Up @@ -4621,10 +4608,10 @@ def canon_thread_new_indirect(ft, ftbl: Table[CoreFuncRef], fi, c):
return [-1]
def thread_func():
[] = call_and_trap_on_throw(f.callee, [c])
task.unregister_thread(new_thread)
task.inst.threads.remove(new_thread.index)
new_thread = Thread(task, thread_func)
assert(new_thread.suspended())
task.register_thread(new_thread)
new_thread.index = task.inst.threads.add(new_thread)
return [new_thread.index]
```
The newly-created thread starts out in a "suspended" state and so, to
Expand Down
10 changes: 6 additions & 4 deletions design/mvp/Concurrency.md
Original file line number Diff line number Diff line change
Expand Up @@ -729,10 +729,12 @@ the "started" state.
The way an `async` export returns its value using the async ABI is by calling
[`task.return`], passing the core values that are to be lifted as *parameters*.
When using the async ABI, *any* of the threads contained by a task can call
`task.return`; there is no "main thread" of a task. When the last thread of a
task returns, there is a trap if `task.return` has not been called. Thus, *some*
thread (either the thread created implicitly for the initial export call or some
thread transitively created by that thread) must call `task.return`.
`task.return`; there is no "main thread" of a task. However, *some* thread
(either the thread created implicitly for the initial export call or some
thread transitively created by that thread) must call `task.return` for the
caller to receive a result. If the last thread of a task exits before
`task.return` has been called, the task simply never returns, as if its threads
blocked forever on some never-met condition.

Returning values by calling `task.return` allows a task to continue executing
even after it has passed its initial results to the caller. This is also
Expand Down
4 changes: 2 additions & 2 deletions design/mvp/Text.md
Original file line number Diff line number Diff line change
Expand Up @@ -1712,8 +1712,8 @@ For details, see [Backpressure] in the concurrency explainer and
| Canonical ABI signature | `[lower(FuncT.results)*] -> []` |

The `task.return` built-in takes as parameters the result values of the
[current task]. One of `task.return` or `task.cancel` must be called exactly
once from any of a task's threads.
[current task]. At most one call to `task.return` or `task.cancel` may be made
from any of a task's threads; if neither is called, the task never resolves.

The `canon task.return` definition takes component-level return type and the
list of `canonopt` to be used to lift the return value. When called, the
Expand Down
25 changes: 4 additions & 21 deletions design/mvp/canonical-abi/definitions.py
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,6 @@ class State(Enum):
state: State
num_borrows: int
implicit_thread: Optional[Thread]
threads: list[Thread]

def __init__(self, ft, opts, inst, on_start, on_resolve):
self.ft = ft
Expand All @@ -416,7 +415,6 @@ def __init__(self, ft, opts, inst, on_start, on_resolve):
self.state = Task.State.INITIAL
self.num_borrows = 0
self.implicit_thread = None
self.threads = []

def needs_exclusive(self):
assert(self.ft.async_)
Expand All @@ -439,31 +437,16 @@ def has_backpressure():
if self.needs_exclusive():
assert(self.inst.exclusive_thread is None)
self.inst.exclusive_thread = self.implicit_thread
self.register_thread(self.implicit_thread)
self.implicit_thread.index = self.inst.threads.add(self.implicit_thread)
return True

def register_thread(self, thread):
assert(thread not in self.threads and thread.task is self)
self.threads.append(thread)
assert(thread.index is None)
thread.index = self.inst.threads.add(thread)

def exit_implicit_thread(self):
assert(current_thread() is self.implicit_thread)
self.unregister_thread(self.implicit_thread)
self.inst.threads.remove(self.implicit_thread.index)
if self.ft.async_ and self.needs_exclusive():
assert(self.inst.exclusive_thread is self.implicit_thread)
self.inst.exclusive_thread = None

def unregister_thread(self, thread):
assert(thread in self.threads and thread.task is self)
self.threads.remove(thread)
if len(self.threads) == 0:
trap_if(self.state != Task.State.RESOLVED)
assert(self.num_borrows == 0)
assert(thread.index is not None)
self.inst.threads.remove(thread.index)

def request_cancellation(self):
if self.state == Task.State.INITIAL:
self.state = Task.State.PENDING_CANCEL
Expand Down Expand Up @@ -2625,10 +2608,10 @@ def canon_thread_new_indirect(ft, ftbl: Table[CoreFuncRef], fi, c):
return [-1]
def thread_func():
[] = call_and_trap_on_throw(f.callee, [c])
task.unregister_thread(new_thread)
task.inst.threads.remove(new_thread.index)
new_thread = Thread(task, thread_func)
assert(new_thread.suspended())
task.register_thread(new_thread)
new_thread.index = task.inst.threads.add(new_thread)
return [new_thread.index]

### 🧵 `canon thread.resume-later`
Expand Down
4 changes: 3 additions & 1 deletion test/async/during-sync-call-exclusive-resume.wast
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,9 @@
(assert_trap (invoke "sync-block") "cannot block a synchronous task before returning")

;; Same as above, except the async callback task has *not* resolved when its
;; callback returns EXIT during the sync call, which traps.
;; callback returns EXIT during the sync call. Exiting without having resolved
;; does not itself trap (the task is simply left unresolved), so the outcome is
;; the same as above: once no ready threads remain, the sync call traps.
(component
(component $C
(core module $Core
Expand Down
92 changes: 92 additions & 0 deletions test/async/exit-without-resolving.wast
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
;; When the last thread of an async-typed task exits before the task has
;; resolved (via task.return or task.cancel), there is no trap at the point of
;; exit: the task is simply left unresolved forever, just as if its threads
;; were all blocked forever. Whether this leads to a trap depends on whether
;; anyone waits for the task to resolve.
(component definition $Tester
(component $C
(core module $Core
(func (export "exit") (result i32)
(i32.const 0 (; EXIT ;)))
(func (export "unreachable-cb") (param i32 i32 i32) (result i32)
unreachable)
)
(core instance $core (instantiate $Core))
(func (export "exit") async
(canon lift (core func $core "exit") async (callback (core func $core "unreachable-cb"))))
)
(component $D
(import "c" (instance $c
(export "exit" (func async))
))
(core module $Core
(import "" "sync-exit" (func $sync-exit))
(import "" "async-exit" (func $async-exit (result i32)))
(import "" "waitable-set.new" (func $waitable-set.new (result i32)))
(import "" "waitable.join" (func $waitable.join (param i32 i32)))

;; non-async-typed: synchronously wait for a subtask that will never
;; resolve
(func (export "sync-typed-wait") (result i32)
(call $sync-exit)
unreachable)

;; async-typed: start a subtask that runs until its thread exits, leaving
;; the subtask unresolved, and then WAIT for the subtask to resolve
(func (export "async-typed-wait") (result i32)
(local $ret i32) (local $ws i32)
(local.set $ret (call $async-exit))
(if (i32.ne (i32.and (local.get $ret) (i32.const 0xf)) (i32.const 1 (; STARTED ;)))
(then unreachable))
(local.set $ws (call $waitable-set.new))
(call $waitable.join (i32.shr_u (local.get $ret) (i32.const 4)) (local.get $ws))
(i32.or (i32.const 2 (; WAIT ;)) (i32.shl (local.get $ws) (i32.const 4))))
(func (export "unreachable-cb") (param i32 i32 i32) (result i32)
unreachable)

;; non-async-typed: start a subtask that runs until its thread exits,
;; leaving the subtask unresolved, and then return a value anyway
(func (export "no-wait") (result i32)
(if (i32.ne (i32.and (call $async-exit) (i32.const 0xf)) (i32.const 1 (; STARTED ;)))
(then unreachable))
(i32.const 42))
)
(canon lower (func $c "exit") (core func $sync-exit))
(canon lower (func $c "exit") async (core func $async-exit))
(canon waitable-set.new (core func $waitable-set.new))
(canon waitable.join (core func $waitable.join))
(core instance $core (instantiate $Core (with "" (instance
(export "sync-exit" (func $sync-exit))
(export "async-exit" (func $async-exit))
(export "waitable-set.new" (func $waitable-set.new))
(export "waitable.join" (func $waitable.join))
))))
(func (export "sync-typed-wait") (result u32)
(canon lift (core func $core "sync-typed-wait")))
(func (export "async-typed-wait") async (result u32)
(canon lift (core func $core "async-typed-wait") async (callback (core func $core "unreachable-cb"))))
(func (export "no-wait") (result u32)
(canon lift (core func $core "no-wait")))
)
(instance $c (instantiate $C))
(instance $d (instantiate $D (with "c" (instance $c))))
(func (export "sync-typed-wait") (alias export $d "sync-typed-wait"))
(func (export "async-typed-wait") (alias export $d "async-typed-wait"))
(func (export "no-wait") (alias export $d "no-wait"))
)

;; A non-async-typed caller waiting on a subtask whose threads have all exited
;; traps once no threads can run.
(component instance $i $Tester)
(assert_trap (invoke "sync-typed-wait") "cannot block a synchronous task before returning")

;; An async-typed caller waiting on a subtask whose threads have all exited
;; deadlocks, since nothing can ever wake it.
(component instance $i $Tester)
(assert_trap (invoke "async-typed-wait") "deadlock detected: event loop cannot make further progress")

;; A caller that doesn't wait on the unresolved subtask returns its value
;; without trapping and can even be reentered.
(component instance $i $Tester)
(assert_return (invoke "no-wait") (u32.const 42))
(assert_return (invoke "no-wait") (u32.const 42))
1 change: 1 addition & 0 deletions test/nyi.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
./values/post-return.wast
./async/big-interleaving-test.wast
./async/forward.wast
./async/exit-without-resolving.wast
Loading