Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wasmroutines #1321

Open
mfateev opened this issue Dec 30, 2019 · 5 comments
Open

Wasmroutines #1321

mfateev opened this issue Dec 30, 2019 · 5 comments

Comments

@mfateev
Copy link

mfateev commented Dec 30, 2019

Existing WebAssembly threads proposal focuses on enabling wasm compiled program to utilize multiple system threads. This is an extremely useful feature for CPU intensive tasks. But there is a large class of problems which is IO intensive and is better solved by much lighter weight coroutines than system threads. The recent addition of Rust async-wait to a language that already supports multithreading is a good example of such a need. The problem with async-await is that it requires a completely different way to write application code as well as different compilation techniques. From the other side, one of the major reasons for the Go popularity is goroutines which look like real threads to the developer while executed as coroutines by the runtime.
I believe that WebAssembly is uniquely positioned to bring the advantage of Go threading model to practically any multithreaded application that can be compiled to wasm. The idea is that the compiled program doesn’t need to change to leverage the green threads, this becomes the host choice on how to execute it.
Another important feature of WebAssembly is deterministic execution. With green threads it should be straightforward to implement deterministic execution mode for multithreaded targets.
The strawman name for the feature is Wasmroutines.

I found a few issues related to "native wasm threads" like #126 and #1252 but it looks like they are considered as something that wasm might get eventually, but pretty theoretical at this point.

I believe that the single system thread implementation can be done with relatively small effort (for example it could use the normal linear memory) and would provide a lot of value (like executing any multithreaded program without modification) that is worth having as a separate design proposal.

@tlively
Copy link
Member

tlively commented Jan 2, 2020

You may be interested in Asyncify, which is a code transformation tool that can be used to pause and resume execution or switch stacks in WebAssembly without requiring any new WebAssembly features. This solution has overhead, and I believe there are plans to propose a new mechanism for stack switching using algebraic effect handlers, which are essentially just resumable exceptions. The exceptions proposal has been designed with this future direction in mind.

@mfateev
Copy link
Author

mfateev commented Jan 2, 2020

Thanks @tlively. Asyncify looks like cool stopgap solution for my problem. I'll try to prototype on top of it. Ideally I would like to execute any multithreaded WebAssembly program without any modification using only the features that the host provides.

@lukewagner
Copy link
Member

@mfateev I think you're right; there would be significant value in supporting coroutines. There've already been some preliminary discussions about unifying exception handling and coroutines within the framework of algebraic effects. This is discussed in the old exception handling proposal, and I think @rossberg has some newer design thinking compatible with the new exception handling proposal.

@chicoxyzzy
Copy link
Member

chicoxyzzy commented Jan 4, 2020

There is a video where @rossberg presents proposed design for continuations (mentioned by @lukewagner).

https://youtu.be/pq-Pa2Fj4nE?t=3231

@mfateev
Copy link
Author

mfateev commented Jan 6, 2020

Thanks for the video. The "stack switching" is a very powerful abstraction that would enable the large number of use cases. Looking forward for it being supported.

My angle is somehow different in proposing no new features in the wasm bytecode. I'm looking into providing the deterministic execution for any multithreaded code that uses atomic.wait/notify and other atomic operations. I believe that we can specify a special host execution mode that is essentially coroutine based, but looks like multhreaded runtime to a Web Assembly application.

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

No branches or pull requests

4 participants