-
Notifications
You must be signed in to change notification settings - Fork 12.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make CrateMetadata and CStore thread-safe #48936
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,8 +22,7 @@ use rustc_back::PanicStrategy; | |
use rustc_data_structures::indexed_vec::IndexVec; | ||
use rustc::util::nodemap::{FxHashMap, FxHashSet, NodeMap}; | ||
|
||
use std::cell::{RefCell, Cell}; | ||
use rustc_data_structures::sync::Lrc; | ||
use rustc_data_structures::sync::{Lrc, RwLock, Lock}; | ||
use syntax::{ast, attr}; | ||
use syntax::ext::base::SyntaxExtension; | ||
use syntax::symbol::Symbol; | ||
|
@@ -62,13 +61,13 @@ pub struct CrateMetadata { | |
/// Information about the extern crate that caused this crate to | ||
/// be loaded. If this is `None`, then the crate was injected | ||
/// (e.g., by the allocator) | ||
pub extern_crate: Cell<Option<ExternCrate>>, | ||
pub extern_crate: Lock<Option<ExternCrate>>, | ||
|
||
pub blob: MetadataBlob, | ||
pub cnum_map: RefCell<CrateNumMap>, | ||
pub cnum_map: Lock<CrateNumMap>, | ||
pub cnum: CrateNum, | ||
pub codemap_import_info: RefCell<Vec<ImportedFileMap>>, | ||
pub attribute_cache: RefCell<[Vec<Option<Lrc<[ast::Attribute]>>>; 2]>, | ||
pub codemap_import_info: RwLock<Vec<ImportedFileMap>>, | ||
pub attribute_cache: Lock<[Vec<Option<Lrc<[ast::Attribute]>>>; 2]>, | ||
|
||
pub root: schema::CrateRoot, | ||
|
||
|
@@ -81,7 +80,7 @@ pub struct CrateMetadata { | |
|
||
pub trait_impls: FxHashMap<(u32, DefIndex), schema::LazySeq<DefIndex>>, | ||
|
||
pub dep_kind: Cell<DepKind>, | ||
pub dep_kind: Lock<DepKind>, | ||
pub source: CrateSource, | ||
|
||
pub proc_macros: Option<Vec<(ast::Name, Lrc<SyntaxExtension>)>>, | ||
|
@@ -90,21 +89,23 @@ pub struct CrateMetadata { | |
} | ||
|
||
pub struct CStore { | ||
metas: RefCell<IndexVec<CrateNum, Option<Lrc<CrateMetadata>>>>, | ||
metas: RwLock<IndexVec<CrateNum, Option<Lrc<CrateMetadata>>>>, | ||
/// Map from NodeId's of local extern crate statements to crate numbers | ||
extern_mod_crate_map: RefCell<NodeMap<CrateNum>>, | ||
pub metadata_loader: Box<MetadataLoader>, | ||
extern_mod_crate_map: Lock<NodeMap<CrateNum>>, | ||
pub metadata_loader: Box<MetadataLoader + Sync>, | ||
} | ||
|
||
impl CStore { | ||
pub fn new(metadata_loader: Box<MetadataLoader>) -> CStore { | ||
pub fn new(metadata_loader: Box<MetadataLoader + Sync>) -> CStore { | ||
CStore { | ||
metas: RefCell::new(IndexVec::new()), | ||
extern_mod_crate_map: RefCell::new(FxHashMap()), | ||
metas: RwLock::new(IndexVec::new()), | ||
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. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I added a comment about this function. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
pub fn next_crate_num(&self) -> CrateNum { | ||
CrateNum::new(self.metas.borrow().len() + 1) | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should probably add a separate primitive for state that is initialized once and then never modified again. There must be more efficient ways to do this than a mutex.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
parking_lot::Once
looks like a good building block for such a thing.