Skip to content

Commit 7fb4187

Browse files
authored
Soften language for notify on unshared memories (#191)
Since #144, we've specified that `memory.atomic.notify` is allowed to be executed on unshared memories, but always returns 0 since it is impossible for there to be anything waiting on an unshared memory since all variations of the wait instructions in Wasm and JS trap or throw exceptions on unshared memories. However, I realized recently that there is no technical reason why [`Atomics.waitAsync`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Atomics/waitAsync) shouldn't be able to wait on an unshared memory. Because `Atomics.waitAsync` returns a promise rather than blocking, it can in principle be used to synchronize between multiple asynchronous tasks in flight concurrently in the same JS context, and now that [JSPI](https://github.com/WebAssembly/js-promise-integration/blob/main/proposals/js-promise-integration/Overview.md) is becoming more real, this actually might be a useful thing to do. In particular `Atomics.waitAsync` could be used to build suspending versions of mutexes and condition variables to synchronize between threads running as different asynchronous tasks within a single JS context. Actually realizing this vision of asynchronous threading with JSPI on top of `Atomics.waitAsync` requires changes on the JS side to allow `Atomics.waitAsync` and `Atomics.notify` to be used on unshared ArrayBuffers, but in the meantime we can prepare for that change on the Wasm side by softening the language stating that `memory.atomic.notify` unconditionally returns 0 for unshared memories.
1 parent fa319c9 commit 7fb4187

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

proposals/threads/Overview.md

+4-3
Original file line numberDiff line numberDiff line change
@@ -416,9 +416,10 @@ Similarly, `memory.atomic.wait64` is equivalent in behavior to executing the fol
416416
The notify operator takes two operands: an address operand and a count as an
417417
unsigned `i32`. The operation will notify as many waiters as are waiting on the
418418
same effective address, up to the maximum as specified by `count`. The operator
419-
returns the number of waiters that were woken as an unsigned `i32`. Note that if
420-
the notify operator is used with an unshared linear memory, the number of
421-
waiters will always be zero.
419+
returns the number of waiters that were woken as an unsigned `i32`. Note that
420+
there is no way to create a waiter on unshared linear memory from within Wasm,
421+
so if the notify operator is used with an unshared linear memory, the number of
422+
waiters will always be zero unless the host has created such a waiter.
422423

423424
* `memory.atomic.notify`: notify `count` threads waiting on the given address via `memory.atomic.wait32` or `memory.atomic.wait64`
424425

0 commit comments

Comments
 (0)