-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
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
PoC wasm thread implementation #631
Conversation
request_handler.handle_request(&load_request).await; | ||
}) | ||
.detach(); | ||
// self.task_pool |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This for some reason fails with:
error: future cannot be sent between threads safely
--> crates\bevy_asset\src\asset_server.rs:283:22
|
283 | .spawn(async move {
| ^^^^^ future created by async block is not `Send`
|
= help: the trait `Send` is not implemented for `dyn std::future::Future<Output = ()>`
note: future is not `Send` as it awaits another future which is not `Send`
--> crates\bevy_asset\src\asset_server.rs:284:25
|
284 | request_handler.handle_request(&load_request).await;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ await occurs here on type `Pin<Box<dyn std::future::Future<Output = ()>>>`, which is not `Send`
@@ -106,7 +111,8 @@ impl TaskPool { | |||
|
|||
let executor = Arc::new(async_executor::Executor::new()); | |||
|
|||
let num_threads = num_threads.unwrap_or_else(num_cpus::get); | |||
//let num_threads = num_threads.unwrap_or_else(num_cpus::get); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TODO: num_cpus
does not support wasm (just returns 1). Submit a PR implementing https://rustwasm.github.io/wasm-bindgen/api/web_sys/struct.Navigator.html#method.hardware_concurrency
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let num_threads = web_sys::window().unwrap().navigator().hardware_concurrency()
should work. Just needs web-sys
with features = [ "Window", "Navigator" ]
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the snippet. Although what I wanted to say is that it would be better to implement this directly into num_cpus
crate, which aims to be multi-platform.
That issue -- for posterity's sake -- if I recall correctly, is that there is still a synchronous wait in the main thread which waits for the async systems to complete for each frame. Is that correct? And then the solutions are to offload rendering to a wasm thread with an offscreen canvas, or use asynchronous atomics... right? |
That comment was more about This PR, though, ensures that. Well, at least at the basic level. The whole bunch of However, there are still issues that some ECS systems use Synchronous blocking ( Sync primitives ( Now the solutions I see to this:
I see the third option as a most viable long term solution, however, it requires a lot of upfront work and is quite invasive architecturaly. Second option could work now without much effort, but I'm affraid that deadlocking issues could become quite annoying.
Probably answered this above |
Closing because it's outdated. Maybe someone from #88 will want to look at this if needed. |
Opening this as a draft example of implementing wasm threads. This is implemented with the
async
way as discussed on discord.wasm thread implementation is provided by a wasm_thread that I created for this purpose. It abstracts some annoying cases about web workers, namely it does not require bundling additional worker script and can figure out wasm-bindgen shim script URL automatically so it acts as a normal
std::thread
. The main thread blocking issues are still there though.The
headless_wasm
example can be built withbuild_wasm.sh
script. Serveexamples/wasm
over HTTP and openheadless_wasm.html
. This requires nightly rust, because wasm atomics are not yet stable.There is also an issue with
getrandom
which is currently patched inCargo.toml
.