Skip to content

Commit 929579c

Browse files
committed
Restore TLV when needed
1 parent 42f3f9e commit 929579c

File tree

4 files changed

+26
-4
lines changed

4 files changed

+26
-4
lines changed

rayon-core/src/job.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,12 @@ where
8585
F: FnOnce(bool) -> R + Send,
8686
R: Send,
8787
{
88-
pub fn new(func: F, latch: L) -> StackJob<L, F, R> {
88+
pub fn new(tlv: usize, func: F, latch: L) -> StackJob<L, F, R> {
8989
StackJob {
9090
latch: latch,
9191
func: UnsafeCell::new(Some(func)),
9292
result: UnsafeCell::new(JobResult::None),
93-
tlv: tlv::get(),
93+
tlv,
9494
}
9595
}
9696

rayon-core/src/join/mod.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use log::Event::*;
44
use registry::{self, WorkerThread};
55
use std::any::Any;
66
use unwind;
7+
use tlv;
78

89
use FnContext;
910

@@ -120,10 +121,12 @@ where
120121
worker: worker_thread.index()
121122
});
122123

124+
let tlv = tlv::get();
123125
// Create virtual wrapper for task b; this all has to be
124126
// done here so that the stack frame can keep it all live
125127
// long enough.
126128
let job_b = StackJob::new(
129+
tlv,
127130
|migrated| oper_b(FnContext::new(migrated)),
128131
SpinLatch::new(),
129132
);
@@ -134,7 +137,7 @@ where
134137
let status_a = unwind::halt_unwinding(move || oper_a(FnContext::new(injected)));
135138
let result_a = match status_a {
136139
Ok(v) => v,
137-
Err(err) => join_recover_from_panic(worker_thread, &job_b.latch, err),
140+
Err(err) => join_recover_from_panic(worker_thread, &job_b.latch, err, tlv),
138141
};
139142

140143
// Now that task A has finished, try to pop job B from the
@@ -152,6 +155,10 @@ where
152155
worker: worker_thread.index()
153156
});
154157
let result_b = job_b.run_inline(injected);
158+
159+
// Restore the TLV since we might have run some jobs overwriting it when waiting for job b.
160+
tlv::set(tlv);
161+
155162
return (result_a, result_b);
156163
} else {
157164
log!(PoppedJob {
@@ -171,6 +178,9 @@ where
171178
}
172179
}
173180

181+
// Restore the TLV since we might have run some jobs overwriting it when waiting for job b.
182+
tlv::set(tlv);
183+
174184
return (result_a, job_b.into_result());
175185
})
176186
}
@@ -183,7 +193,12 @@ unsafe fn join_recover_from_panic(
183193
worker_thread: &WorkerThread,
184194
job_b_latch: &SpinLatch,
185195
err: Box<Any + Send>,
196+
tlv: usize,
186197
) -> ! {
187198
worker_thread.wait_until(job_b_latch);
199+
200+
// Restore the TLV since we might have run some jobs overwriting it when waiting for job b.
201+
tlv::set(tlv);
202+
188203
unwind::resume_unwinding(err)
189204
}

rayon-core/src/registry.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,7 @@ impl Registry {
387387
{
388388
// This thread isn't a member of *any* thread pool, so just block.
389389
debug_assert!(WorkerThread::current().is_null());
390-
let job = StackJob::new(|injected| {
390+
let job = StackJob::new(0, |injected| {
391391
let worker_thread = WorkerThread::current();
392392
assert!(injected && !worker_thread.is_null());
393393
op(&*worker_thread, true)
@@ -410,6 +410,7 @@ impl Registry {
410410
debug_assert!(current_thread.registry().id() != self.id());
411411
let latch = TickleLatch::new(SpinLatch::new(), &current_thread.registry().sleep);
412412
let job = StackJob::new(
413+
0,
413414
|injected| {
414415
let worker_thread = WorkerThread::current();
415416
assert!(injected && !worker_thread.is_null());

rayon-core/src/scope/mod.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ impl<'scope> ScopeBuilder<'scope> {
5151
in_worker(move |owner_thread, _| {
5252
scope.steal_till_jobs_complete(owner_thread);
5353
});
54+
// Restore the TLV if we ran some jobs while waiting
55+
tlv::set(scope.tlv);
5456
result.unwrap() // only None if `op` panicked, and that would have been propagated
5557
}
5658
}
@@ -311,6 +313,8 @@ where
311313
};
312314
let result = scope.execute_job_closure(op);
313315
scope.steal_till_jobs_complete(owner_thread);
316+
// Restore the TLV if we ran some jobs while waiting
317+
tlv::set(scope.tlv);
314318
result.unwrap() // only None if `op` panicked, and that would have been propagated
315319
}
316320
})
@@ -460,6 +464,8 @@ impl<'scope> Scope<'scope> {
460464
log!(ScopeCompletePanicked {
461465
owner_thread: owner_thread.index()
462466
});
467+
// Restore the TLV if we ran some jobs while waiting
468+
tlv::set(self.tlv);
463469
let value: Box<Box<Any + Send + 'static>> = mem::transmute(panic);
464470
unwind::resume_unwinding(*value);
465471
} else {

0 commit comments

Comments
 (0)