-
Notifications
You must be signed in to change notification settings - Fork 507
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
switch to crossbeam::scope, create task-runner scope
- Loading branch information
Showing
5 changed files
with
146 additions
and
23 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
use crate::RunResult; | ||
use crossbeam::thread; | ||
|
||
type ScopeResult<'src> = RunResult<'src, ()>; | ||
|
||
pub(crate) struct TaskScope<'env, 'src, 'inner_scope> { | ||
inner: &'inner_scope thread::Scope<'env>, | ||
join_handles: Vec<thread::ScopedJoinHandle<'inner_scope, ScopeResult<'src>>>, | ||
} | ||
|
||
impl<'env, 'src, 'inner_scope> TaskScope<'env, 'src, 'inner_scope> { | ||
pub(crate) fn spawn<'scope, F>(&'scope mut self, f: F) | ||
where | ||
'src: 'env, | ||
F: FnOnce() -> ScopeResult<'src>, | ||
F: Send + 'env, | ||
{ | ||
self.join_handles.push(self.inner.spawn(|_scope| f())); | ||
} | ||
} | ||
|
||
/// task runner scope, based on `crossbeam::thread::scope`. | ||
/// | ||
/// The `scope` object can be used to `.spawn` new tasks to be | ||
/// run. The first error will be returned as result of this `task_scope`. | ||
/// | ||
/// Only works for tasks with an `RunResult<'src, ()>` result type. | ||
pub(crate) fn task_scope<'env, 'src, F>(f: F) -> ScopeResult<'src> | ||
where | ||
F: for<'inner_scope> FnOnce(&mut TaskScope<'env, 'src, 'inner_scope>) -> ScopeResult<'src>, | ||
{ | ||
thread::scope(|scope| { | ||
let mut task_scope = TaskScope { | ||
inner: scope, | ||
join_handles: Vec::new(), | ||
}; | ||
|
||
f(&mut task_scope)?; | ||
|
||
for handle in task_scope.join_handles { | ||
handle.join().expect("could not join thread")?; | ||
} | ||
Ok(()) | ||
}) | ||
.expect("could not join thread") | ||
} |