Make Mir::predecessors thread-safe

This commit is contained in:
John Kåre Alsaker 2018-02-15 10:52:26 +01:00
parent 8fd7d49c95
commit 70de2e8ad6
2 changed files with 9 additions and 9 deletions

View file

@ -8,8 +8,8 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.
use std::cell::{Ref, RefCell};
use rustc_data_structures::indexed_vec::IndexVec;
use rustc_data_structures::sync::{RwLock, ReadGuard};
use rustc_data_structures::stable_hasher::{HashStable, StableHasher,
StableHasherResult};
use ich::StableHashingContext;
@ -19,7 +19,7 @@ use rustc_serialize as serialize;
#[derive(Clone, Debug)]
pub struct Cache {
predecessors: RefCell<Option<IndexVec<BasicBlock, Vec<BasicBlock>>>>
predecessors: RwLock<Option<IndexVec<BasicBlock, Vec<BasicBlock>>>>
}
@ -46,7 +46,7 @@ impl<'a> HashStable<StableHashingContext<'a>> for Cache {
impl Cache {
pub fn new() -> Self {
Cache {
predecessors: RefCell::new(None)
predecessors: RwLock::new(None)
}
}
@ -55,12 +55,12 @@ impl Cache {
*self.predecessors.borrow_mut() = None;
}
pub fn predecessors(&self, mir: &Mir) -> Ref<IndexVec<BasicBlock, Vec<BasicBlock>>> {
pub fn predecessors(&self, mir: &Mir) -> ReadGuard<IndexVec<BasicBlock, Vec<BasicBlock>>> {
if self.predecessors.borrow().is_none() {
*self.predecessors.borrow_mut() = Some(calculate_predecessors(mir));
}
Ref::map(self.predecessors.borrow(), |p| p.as_ref().unwrap())
ReadGuard::map(self.predecessors.borrow(), |p| p.as_ref().unwrap())
}
}

View file

@ -34,7 +34,7 @@ use util::ppaux;
use std::slice;
use hir::{self, InlineAsm};
use std::borrow::{Cow};
use std::cell::Ref;
use rustc_data_structures::sync::ReadGuard;
use std::fmt::{self, Debug, Formatter, Write};
use std::{iter, mem, u32};
use std::ops::{Index, IndexMut};
@ -187,13 +187,13 @@ impl<'tcx> Mir<'tcx> {
}
#[inline]
pub fn predecessors(&self) -> Ref<IndexVec<BasicBlock, Vec<BasicBlock>>> {
pub fn predecessors(&self) -> ReadGuard<IndexVec<BasicBlock, Vec<BasicBlock>>> {
self.cache.predecessors(self)
}
#[inline]
pub fn predecessors_for(&self, bb: BasicBlock) -> Ref<Vec<BasicBlock>> {
Ref::map(self.predecessors(), |p| &p[bb])
pub fn predecessors_for(&self, bb: BasicBlock) -> ReadGuard<Vec<BasicBlock>> {
ReadGuard::map(self.predecessors(), |p| &p[bb])
}
#[inline]