Skip to content

Commit

Permalink
Make CrateNum allocation more thread-safe.
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelwoerister committed May 9, 2018
1 parent 31d2012 commit b8b957d
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 15 deletions.
5 changes: 1 addition & 4 deletions src/librustc_metadata/creader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ pub struct Library {
pub struct CrateLoader<'a> {
pub sess: &'a Session,
cstore: &'a CStore,
next_crate_num: CrateNum,
local_crate_name: Symbol,
}

Expand Down Expand Up @@ -102,7 +101,6 @@ impl<'a> CrateLoader<'a> {
CrateLoader {
sess,
cstore,
next_crate_num: cstore.next_crate_num(),
local_crate_name: Symbol::intern(local_crate_name),
}
}
Expand Down Expand Up @@ -198,8 +196,7 @@ impl<'a> CrateLoader<'a> {
self.verify_no_symbol_conflicts(span, &crate_root);

// Claim this crate number and cache it
let cnum = self.next_crate_num;
self.next_crate_num = CrateNum::from_u32(cnum.as_u32() + 1);
let cnum = self.cstore.alloc_new_crate_num();

// Stash paths for top-most crate locally if necessary.
let crate_paths = if root.is_none() {
Expand Down
20 changes: 9 additions & 11 deletions src/librustc_metadata/cstore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,29 +96,27 @@ pub struct CStore {
impl CStore {
pub fn new(metadata_loader: Box<MetadataLoader + Sync>) -> CStore {
CStore {
metas: RwLock::new(IndexVec::new()),
metas: RwLock::new(IndexVec::from_elem_n(None, 1)),
extern_mod_crate_map: Lock::new(FxHashMap()),
metadata_loader,
}
}

/// You cannot use this function to allocate a CrateNum in a thread-safe manner.
/// It is currently only used in CrateLoader which is single-threaded code.
pub(super) fn next_crate_num(&self) -> CrateNum {
CrateNum::new(self.metas.borrow().len() + 1)
pub(super) fn alloc_new_crate_num(&self) -> CrateNum {
let mut metas = self.metas.borrow_mut();
let cnum = CrateNum::new(metas.len());
metas.push(None);
cnum
}

pub(super) fn get_crate_data(&self, cnum: CrateNum) -> Lrc<CrateMetadata> {
self.metas.borrow()[cnum].clone().unwrap()
}

pub(super) fn set_crate_data(&self, cnum: CrateNum, data: Lrc<CrateMetadata>) {
use rustc_data_structures::indexed_vec::Idx;
let mut met = self.metas.borrow_mut();
while met.len() <= cnum.index() {
met.push(None);
}
met[cnum] = Some(data);
let mut metas = self.metas.borrow_mut();
assert!(metas[cnum].is_none(), "Overwriting crate metadata entry");
metas[cnum] = Some(data);
}

pub(super) fn iter_crate_data<I>(&self, mut i: I)
Expand Down

0 comments on commit b8b957d

Please sign in to comment.