镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

Soften language for notify on unshared memories - #191

Merged
conrad-watt merged 1 commit into
mainfrom
tlively-patch-2
Sep 13, 2022
Merged

conrad-watt merged 1 commit into
mainfrom
tlively-patch-2

Conversation

@tlively

@tlively tlively commented Sep 12, 2022

Copy link
Copy Markdown
Member

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 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 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.

cc @syg

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.
@tlively
tlively requested a review from conrad-watt September 12, 2022 23:52

@conrad-watt conrad-watt left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense, thanks!

@conrad-watt
conrad-watt merged commit 7fb4187 into main Sep 13, 2022
@conrad-watt

Copy link
Copy Markdown
Collaborator

(I'd also be interested in @syg's take on how likely this is on the JS side)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants