Skip to content

Commit

Permalink
Reduce genericity in join wrappers
Browse files Browse the repository at this point in the history
  • Loading branch information
cuviper committed Jul 11, 2019
1 parent 71d424b commit 885d932
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
6 changes: 5 additions & 1 deletion rayon-core/src/job.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,14 @@ where
R: Send,
{
unsafe fn execute(this: *const Self) {
fn call<R>(func: impl FnOnce(bool) -> R) -> impl FnOnce() -> R {
move || func(true)
}

let this = &*this;
let abort = unwind::AbortIfPanic;
let func = (*this.func.get()).take().unwrap();
(*this.result.get()) = match unwind::halt_unwinding(|| func(true)) {
(*this.result.get()) = match unwind::halt_unwinding(call(func)) {
Ok(x) => JobResult::Ok(x),
Err(x) => JobResult::Panic(x),
};
Expand Down
24 changes: 18 additions & 6 deletions rayon-core/src/join/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,12 @@ where
RA: Send,
RB: Send,
{
join_context(|_| oper_a(), |_| oper_b())
#[inline]
fn call<R>(f: impl FnOnce() -> R) -> impl FnOnce(FnContext) -> R {
move |_| f()
}

join_context(call(oper_a), call(oper_b))
}

/// Identical to `join`, except that the closures have a parameter
Expand All @@ -115,6 +120,16 @@ where
RA: Send,
RB: Send,
{
#[inline]
fn call_a<R>(f: impl FnOnce(FnContext) -> R, injected: bool) -> impl FnOnce() -> R {
move || f(FnContext::new(injected))
}

#[inline]
fn call_b<R>(f: impl FnOnce(FnContext) -> R) -> impl FnOnce(bool) -> R {
move |migrated| f(FnContext::new(migrated))
}

registry::in_worker(|worker_thread, injected| unsafe {
log!(Join {
worker: worker_thread.index()
Expand All @@ -123,15 +138,12 @@ where
// Create virtual wrapper for task b; this all has to be
// done here so that the stack frame can keep it all live
// long enough.
let job_b = StackJob::new(
|migrated| oper_b(FnContext::new(migrated)),
SpinLatch::new(),
);
let job_b = StackJob::new(call_b(oper_b), SpinLatch::new());
let job_b_ref = job_b.as_job_ref();
worker_thread.push(job_b_ref);

// Execute task a; hopefully b gets stolen in the meantime.
let status_a = unwind::halt_unwinding(move || oper_a(FnContext::new(injected)));
let status_a = unwind::halt_unwinding(call_a(oper_a, injected));
let result_a = match status_a {
Ok(v) => v,
Err(err) => join_recover_from_panic(worker_thread, &job_b.latch, err),
Expand Down

0 comments on commit 885d932

Please sign in to comment.