Skip to content

Commit

Permalink
Merge pull request rayon-rs#194 from nikomatsakis/stabilize-scope
Browse files Browse the repository at this point in the history
Stabilize scope
  • Loading branch information
cuviper authored Jan 2, 2017
2 parents 276dba0 + cb23bfa commit ec84317
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 11 deletions.
2 changes: 0 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ pub mod prelude;
#[cfg(test)]
mod test;
mod registry;
#[cfg(feature = "unstable")]
mod scope;
mod sleep;
mod thread_pool;
Expand All @@ -33,5 +32,4 @@ pub use configuration::dump_stats;
pub use configuration::initialize;
pub use thread_pool::ThreadPool;
pub use join::join;
#[cfg(feature = "unstable")]
pub use scope::{scope, Scope};
19 changes: 10 additions & 9 deletions src/scope/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,8 +232,8 @@ pub struct Scope<'scope> {
/// execute, even if the spawning task should later panic. `scope()`
/// returns once all spawned jobs have completed, and any panics are
/// propagated at that point.
pub fn scope<'scope, OP>(op: OP)
where OP: for<'s> FnOnce(&'s Scope<'scope>) + 'scope + Send
pub fn scope<'scope, OP, R>(op: OP) -> R
where OP: for<'s> FnOnce(&'s Scope<'scope>) -> R + 'scope + Send, R: Send,
{
in_worker(|owner_thread| {
unsafe {
Expand All @@ -243,10 +243,11 @@ pub fn scope<'scope, OP>(op: OP)
job_completed_latch: CountLatch::new(),
marker: PhantomData,
};
scope.execute_job_closure(op);
let result = scope.execute_job_closure(op);
scope.steal_till_jobs_complete();
result.unwrap() // only None if `op` panicked, and that would have been propagated
}
});
})
}

impl<'scope> Scope<'scope> {
Expand Down Expand Up @@ -280,20 +281,20 @@ impl<'scope> Scope<'scope> {
unsafe fn execute_job<FUNC>(&self, func: FUNC)
where FUNC: FnOnce(&Scope<'scope>) + 'scope
{
self.execute_job_closure(func)
let _: Option<()> = self.execute_job_closure(func);
}

/// Executes `func` as a job in scope. Adjusts the "job completed"
/// counters and also catches any panic and stores it into
/// `scope`.
///
/// Unsafe because this must be executed on a worker thread.
unsafe fn execute_job_closure<FUNC>(&self, func: FUNC)
where FUNC: FnOnce(&Scope<'scope>) + 'scope
unsafe fn execute_job_closure<FUNC, R>(&self, func: FUNC) -> Option<R>
where FUNC: FnOnce(&Scope<'scope>) -> R + 'scope
{
match unwind::halt_unwinding(move || func(self)) {
Ok(()) => self.job_completed_ok(),
Err(err) => self.job_panicked(err),
Ok(r) => { self.job_completed_ok(); Some(r) }
Err(err) => { self.job_panicked(err); None }
}
}

Expand Down
6 changes: 6 additions & 0 deletions src/scope/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ fn scope_empty() {
});
}

#[test]
fn scope_result() {
let x = scope(|_| 22);
assert_eq!(x, 22);
}

#[test]
fn scope_two() {
let counter = &AtomicUsize::new(0);
Expand Down
File renamed without changes.
File renamed without changes.

0 comments on commit ec84317

Please sign in to comment.