Skip to content

Commit

Permalink
Add a new way to create scopes
Browse files Browse the repository at this point in the history
  • Loading branch information
Zoxc committed Jan 2, 2019
1 parent e632a62 commit 6f124ef
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
2 changes: 1 addition & 1 deletion rayon-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ pub mod tlv;
#[cfg(rayon_unstable)]
pub mod internal;
pub use join::{join, join_context};
pub use scope::{scope, Scope};
pub use scope::{scope, Scope, ScopeBuilder};
pub use registry::{Registry, mark_blocked, mark_unblocked, continue_unblocked};
pub use spawn::spawn;
pub use worker_local::WorkerLocal;
Expand Down
34 changes: 34 additions & 0 deletions rayon-core/src/scope/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,40 @@ mod internal;
#[cfg(test)]
mod test;

pub struct ScopeBuilder<'scope> {
scope: Option<Scope<'scope>>,
}

impl<'scope> ScopeBuilder<'scope> {
pub fn new() -> Self {
Self {
scope: None,
}
}

pub fn scope<OP, R>(&'scope mut self, op: OP) -> R
where
OP: FnOnce(&'scope Scope<'scope>) -> R + 'scope + Send,
R: Send,
{
in_worker(move |owner_thread, _| {
unsafe {
self.scope = Some(Scope {
owner_thread_index: owner_thread.index(),
registry: owner_thread.registry().clone(),
panic: AtomicPtr::new(ptr::null_mut()),
job_completed_latch: CountLatch::new(),
marker: PhantomData,
});
let scope = self.scope.as_ref().unwrap();
let result = scope.execute_job_closure(move |_| op(scope));
scope.steal_till_jobs_complete(owner_thread);
result.unwrap() // only None if `op` panicked, and that would have been propagated
}
})
}
}

///Represents a fork-join scope which can be used to spawn any number of tasks. See [`scope()`] for more information.
///
///[`scope()`]: fn.scope.html
Expand Down

0 comments on commit 6f124ef

Please sign in to comment.