-
Notifications
You must be signed in to change notification settings - Fork 28
Proper runtime "enter" instead of spawn + local blocking #73
Conversation
@stbuehler Hey there! Overall I like this idea a lot! It'd be nice if it the method could be named Also cc/ @stjepang, there's a bunch of atomic operations going on that I don't feel confident in reviewing. Would you mind taking a look? Thanks! |
I'm certainly open to renaming it - my initial choice was indeed If we go with I'll have a look at the atomics again too; the synchronization of |
I took a look at the source of |
Renamed |
@stbuehler dang yeah, the fact that the WASM Still, I kind of wish we could have a WASM futures executor function with the same semantics as the other |
So you want me to rename it back to (In case you didn't notice: I'm also trying to get a |
After reading the PR again, I noticed that the I still don't feel qualified to review any of the atomics code, pinging @stjepang again for a review pass. |
I'd kind of prefer holding off merging this change as it increases complexity quite a bit for comparatively little benefit, which is essentially starting one thread less. I don't feel comfortable saying this is the right step forward as there are many unknowns here. Maybe we can revisit this PR later at some point? |
I think the first commit decreases complexity and improves the API significantly, and have no interest in waiting for it. The second commit increases the complexity of the |
How about this? /// Runtime trait for runtimes supporting blocking.
pub trait BlockingRuntime<F, T>
where
F: Future<Output = T>,
{
/// Runs a future inside the runtime and blocks on the result.
///
/// Needs to call `enter_runtime` or `set_runtime` (only on background threads) in threads
/// running futures.
fn block_on(&self, fut: F) -> T;
} This would remove the need for boxing, support See master...stbuehler:runtime-enter2 for the full idea. |
This: - removes the need to run the runtime in background threads (especially interesting for TokioCurrentThread) - cleans up global state after `enter`/`block_on` is done (i.e. kills runtime including pending tasks if `block_on` finishes) - removes some global state (different runtime contexts shouldn't share state like thread pools anymore) - allows !Send async main if the runtime supports it
Just updated the branch:
|
In the context of #83 (comment) I'm going to go ahead and close this PR. The way our interactions have been going recently has not been great, and I think it's in everyone's best interest if we part ways here. Thanks for your contributions, and good luck! |
Remove the necessity to spawn threads by using an explicit
enter
method in theRuntime
trait which blocks on the passed future.Description, Motivation and Context
Currently all runtimes need to run in background threads / worker pools.
With this PR this is changed: there is an explicit
enter
which is supposed to block on the passed future, so acurrent_thread
runtime doesn't need to spawn new threads (which makes strace way easier to read, and is simply cleaner imho).This also removes various (lazy_statics) globals: if a runtime is
enter
ed again, it will simply have to recreate the runtime environment, and it should shutdown cleanly on leavingenter
. (One might argue whether it should only block on the passed future or on a more generic "runtime shutdown".)The second commit avoids double-boxing with a more generic
JoinHandle
and its type-erasedCompletionHandle
part.This also makes it more explicit that
enter
can't work on wasm; I went for a simple panic though instead of going#[cfg(...)]
-crazy over the API (especially as the existing wasm cfg handling is broken and looks wrong).Types of changes
(Although the breaking change should only be visible for
runtime_raw
users.)