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

Nesting Task scopes #4343

Closed
wants to merge 8 commits into from
Closed

Conversation

MiniaczQ
Copy link
Contributor

@MiniaczQ MiniaczQ commented Mar 27, 2022

Objective

attempt to
Allow for nested task scopes as in #4301.
Possibly not use a scope argument for further nesting.

Progress

I wrapped Tasks results field in Arc<Mutex<>> to make it internally mutable,
then changed functions signatures to no longer use &mut but &.
Currently stuck on LocalExecutor which is !Send + !Sync, hence prevents nesting inside Scope::spawn.
full error message
(nesting spawn_local works, but is stopped by another problem, I wanna focus on fixing the original)

@github-actions github-actions bot added the S-Needs-Triage This issue needs to be labelled label Mar 27, 2022
@MiniaczQ MiniaczQ changed the title put a mutex on spawned vec (non-wasm) Nesting Task scopes Mar 27, 2022
@alice-i-cecile alice-i-cecile added C-Usability A targeted quality-of-life change that makes Bevy easier to use A-Tasks Tools for parallel and async work and removed S-Needs-Triage This issue needs to be labelled labels Mar 30, 2022
@@ -68,19 +68,22 @@ impl TaskPool {

let mut scope = Scope {
executor,
results: Vec::new(),
results: Arc::new(Mutex::new(Vec::new())),
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we want to avoid the use of Mutexes on the single threaded executor. The primary use case for the single threaded executor is for wasm and wasm doesn't support Atomic wait on the main thread which is what mutexes use when you build with atomics for wasm.

I think without enabling atomics, Mutexes become a no op, so this might work with a default wasm build config today.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Right
I didn't implement it, but Joy came up with the idea of splitting scope into a local variant and non-local variant.
Although originally we didn't decide on results storage (I was pro-shared), if we split them up we can make a thread safe LocalScope
Signatures would look something like this:

LocalScope::spawn_local(FnOnce(&LocalScope, &Scope));     // keep both scopes
Scope::spawn(FnOnce(&Scope));     // keep scope, lose local scope

scopes like this:

struct LocalScope<T> {
  local_executor: ...,
  results: RefCell<Vec<T>>,
  scope: &Scope,
}
struct Scope<T> {
  executor: ...,
  results: Mutex<Vec<T>>,
}

It's also worth mentioning that there were talks about moving wasm to multithreading although I'm not seeing any progress (nor contributing much :/)

@@ -265,7 +266,7 @@ impl Default for TaskPool {
pub struct Scope<'scope, T> {
executor: &'scope async_executor::Executor<'scope>,
local_executor: &'scope async_executor::LocalExecutor<'scope>,
spawned: Vec<async_executor::Task<T>>,
spawned: Arc<Mutex<Vec<async_executor::Task<T>>>>,
Copy link
Member

@TheRawMeatball TheRawMeatball Apr 6, 2022

Choose a reason for hiding this comment

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

Replacing this Arc with a reference to a Mutex<Vec<T>> stored on the stack should be possible. Alternatively, a queue might also be better to have less locking overhead.

Copy link
Contributor Author

@MiniaczQ MiniaczQ Apr 7, 2022

Choose a reason for hiding this comment

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

Stack of which thread?
I've never seen a solution like that so I'm slightly confused

Copy link
Member

Choose a reason for hiding this comment

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

It'd be similar to how the code currently makes a fresh Executor on the stack when scope is called. We'd just also make a Mutex<Vec<Task<T>>> on the stack, and store a regular rust reference to it inside Scope

@hymm hymm mentioned this pull request Apr 11, 2022
hymm added a commit to hymm/bevy that referenced this pull request Apr 11, 2022
Co-authored-by: MiniaczQ<MiniaczQ@gmail.com>
hymm added a commit to hymm/bevy that referenced this pull request Apr 11, 2022
Co-authored-by: MiniaczQ<MiniaczQ@gmail.com>
@MiniaczQ MiniaczQ closed this Apr 11, 2022
hymm pushed a commit to hymm/bevy that referenced this pull request Apr 11, 2022
Co-authored-by: MiniaczQ<MiniaczQ@gmail.com>
hymm pushed a commit to hymm/bevy that referenced this pull request Apr 14, 2022
Co-authored-by: MiniaczQ <MiniaczQ@gmail.com>
hymm pushed a commit to hymm/bevy that referenced this pull request Apr 23, 2022
Co-authored-by: MiniaczQ <MiniaczQ@gmail.com>
hymm pushed a commit to hymm/bevy that referenced this pull request Apr 25, 2022
Co-authored-by: MiniaczQ <MiniaczQ@gmail.com>
hymm pushed a commit to hymm/bevy that referenced this pull request Jun 4, 2022
Co-authored-by: MiniaczQ <MiniaczQ@gmail.com>
hymm pushed a commit to hymm/bevy that referenced this pull request Jun 10, 2022
Co-authored-by: MiniaczQ <MiniaczQ@gmail.com>
hymm pushed a commit to hymm/bevy that referenced this pull request Jun 14, 2022
Co-authored-by: MiniaczQ <MiniaczQ@gmail.com>
@MiniaczQ MiniaczQ deleted the nested-task-scopes branch June 16, 2022 16:15
hymm pushed a commit to hymm/bevy that referenced this pull request Jul 12, 2022
Co-authored-by: MiniaczQ <MiniaczQ@gmail.com>
maniwani pushed a commit to maniwani/bevy that referenced this pull request Aug 1, 2022
Co-authored-by: MiniaczQ <MiniaczQ@gmail.com>
maniwani pushed a commit to maniwani/bevy that referenced this pull request Aug 5, 2022
Co-authored-by: MiniaczQ <MiniaczQ@gmail.com>
hymm pushed a commit to hymm/bevy that referenced this pull request Aug 11, 2022
Co-authored-by: MiniaczQ <MiniaczQ@gmail.com>
hymm pushed a commit to hymm/bevy that referenced this pull request Sep 14, 2022
Co-authored-by: MiniaczQ <MiniaczQ@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Tasks Tools for parallel and async work C-Usability A targeted quality-of-life change that makes Bevy easier to use
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants