From dacf9ba00fb8d6b7202a1ed5f06febe3fd2cf9b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?John=20K=C3=A5re=20Alsaker?= Date: Sun, 1 Apr 2018 08:27:09 +0200 Subject: [PATCH] Make Session.injected_allocator and Session.allocator_kind thread-safe --- src/librustc/session/mod.rs | 8 ++++---- src/librustc_metadata/creader.rs | 8 ++++++++ src/librustc_trans/base.rs | 2 +- 3 files changed, 13 insertions(+), 5 deletions(-) diff --git a/src/librustc/session/mod.rs b/src/librustc/session/mod.rs index 55da5b921af..a92a2c916b2 100644 --- a/src/librustc/session/mod.rs +++ b/src/librustc/session/mod.rs @@ -118,8 +118,8 @@ pub struct Session { /// The metadata::creader module may inject an allocator/panic_runtime /// dependency if it didn't already find one, and this tracks what was /// injected. - pub injected_allocator: Cell>, - pub allocator_kind: Cell>, + pub injected_allocator: Once>, + pub allocator_kind: Once>, pub injected_panic_runtime: Cell>, /// Map from imported macro spans (which consist of @@ -1105,8 +1105,8 @@ pub fn build_session_( const_eval_stack_frame_limit: 100, const_eval_step_limit: 1_000_000, next_node_id: OneThread::new(Cell::new(NodeId::new(1))), - injected_allocator: Cell::new(None), - allocator_kind: Cell::new(None), + injected_allocator: Once::new(), + allocator_kind: Once::new(), injected_panic_runtime: Cell::new(None), imported_macro_spans: OneThread::new(RefCell::new(HashMap::new())), incr_comp_session: OneThread::new(RefCell::new(IncrCompSession::NotInitialized)), diff --git a/src/librustc_metadata/creader.rs b/src/librustc_metadata/creader.rs index 5b54994b9ce..06baea53cd5 100644 --- a/src/librustc_metadata/creader.rs +++ b/src/librustc_metadata/creader.rs @@ -823,6 +823,8 @@ fn inject_allocator_crate(&mut self, krate: &ast::Crate) { needs_allocator = needs_allocator || data.needs_allocator(self.sess); }); if !needs_allocator { + self.sess.injected_allocator.set(None); + self.sess.allocator_kind.set(None); return } @@ -842,6 +844,8 @@ fn inject_allocator_crate(&mut self, krate: &ast::Crate) { } } if !need_lib_alloc && !need_exe_alloc { + self.sess.injected_allocator.set(None); + self.sess.allocator_kind.set(None); return } @@ -879,6 +883,7 @@ fn inject_allocator_crate(&mut self, krate: &ast::Crate) { }); if global_allocator.is_some() { self.sess.allocator_kind.set(Some(AllocatorKind::Global)); + self.sess.injected_allocator.set(None); return } @@ -922,6 +927,9 @@ fn inject_allocator_crate(&mut self, krate: &ast::Crate) { }; let allocation_crate_data = exe_allocation_crate_data.or_else(|| { + // No allocator was injected + self.sess.injected_allocator.set(None); + if attr::contains_name(&krate.attrs, "default_lib_allocator") { // Prefer self as the allocator if there's a collision return None; diff --git a/src/librustc_trans/base.rs b/src/librustc_trans/base.rs index c2d94a17f03..f181275326c 100644 --- a/src/librustc_trans/base.rs +++ b/src/librustc_trans/base.rs @@ -795,7 +795,7 @@ pub fn trans_crate<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, codegen_units.len()); // Translate an allocator shim, if any - let allocator_module = if let Some(kind) = tcx.sess.allocator_kind.get() { + let allocator_module = if let Some(kind) = *tcx.sess.allocator_kind.get() { unsafe { let llmod_id = "allocator"; let (llcx, llmod) = -- GitLab