Skip to content

Commit 9bfdcde

Browse files
committed
Ensure type checking each function is stealable by other threads
1 parent 2bc0d3f commit 9bfdcde

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

src/librustc/ty/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ use rustc_data_structures::captures::Captures;
3030
use rustc_data_structures::fx::FxHashMap;
3131
use rustc_data_structures::fx::FxIndexMap;
3232
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
33-
use rustc_data_structures::sync::{self, par_for_each, Lrc};
33+
use rustc_data_structures::sync::{self, balance_par_for_each, Lrc};
3434
use rustc_hir as hir;
3535
use rustc_hir::def::{CtorKind, CtorOf, DefKind, Res};
3636
use rustc_hir::def_id::{CrateNum, DefId, DefIdMap, LocalDefId, CRATE_DEF_INDEX, LOCAL_CRATE};
@@ -2642,7 +2642,7 @@ impl<'tcx> TyCtxt<'tcx> {
26422642
}
26432643

26442644
pub fn par_body_owners<F: Fn(DefId) + sync::Sync + sync::Send>(self, f: F) {
2645-
par_for_each(&self.hir().krate().body_ids, |&body_id| {
2645+
balance_par_for_each(&self.hir().krate().body_ids, |&body_id| {
26462646
f(self.hir().body_owner_def_id(body_id))
26472647
});
26482648
}

src/librustc_data_structures/sync.rs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,13 @@ cfg_if! {
227227
resume(panic);
228228
}
229229

230+
pub fn balance_par_for_each<T: IntoIterator>(
231+
t: T,
232+
for_each: impl FnMut(<<T as IntoIterator>::IntoIter as Iterator>::Item),
233+
) {
234+
par_for_each(t, for_each)
235+
}
236+
230237
pub fn par_map<T: IntoIterator, R, C: FromIterator<R>>(
231238
t: T,
232239
mut map: impl FnMut(<<T as IntoIterator>::IntoIter as Iterator>::Item) -> R,
@@ -405,7 +412,9 @@ cfg_if! {
405412

406413
pub use rayon_core::WorkerLocal;
407414

408-
use rayon::iter::{ParallelIterator, FromParallelIterator, IntoParallelIterator};
415+
use rayon::iter::{
416+
ParallelIterator, IndexedParallelIterator, FromParallelIterator, IntoParallelIterator
417+
};
409418

410419
pub fn par_for_each<T: IntoParallelIterator>(
411420
t: T,
@@ -421,6 +430,25 @@ cfg_if! {
421430
resume(panic);
422431
}
423432

433+
/// This does the same as `par_for_each`,
434+
/// but each loop iteration is stealable by other threads.
435+
pub fn balance_par_for_each<T: IntoParallelIterator>(
436+
t: T,
437+
for_each: impl Fn(
438+
<<T as IntoParallelIterator>::Iter as ParallelIterator>::Item
439+
) + Sync + Send
440+
)
441+
where
442+
<T as IntoParallelIterator>::Iter: IndexedParallelIterator
443+
{
444+
// We catch panics here ensuring that all the loop iterations execute.
445+
let panic = Lock::new(None);
446+
t.into_par_iter().with_max_len(1).for_each(|i| {
447+
catch(&panic, || for_each(i));
448+
});
449+
resume(panic);
450+
}
451+
424452
pub fn par_map<
425453
T: IntoParallelIterator,
426454
R: Send,

0 commit comments

Comments
 (0)