Skip to content

Commit

Permalink
Add a AtomicOnce type
Browse files Browse the repository at this point in the history
  • Loading branch information
Zoxc committed Jul 8, 2019
1 parent 0ca5730 commit d5fbdd2
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 14 deletions.
20 changes: 6 additions & 14 deletions src/librustc/ty/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,8 @@ use rustc_data_structures::stable_hasher::{HashStable, hash_stable_hashmap,
StableHasher, StableHasherResult,
StableVec};
use arena::SyncDroplessArena;
use rustc_data_structures::cold_path;
use rustc_data_structures::indexed_vec::{Idx, IndexVec};
use rustc_data_structures::sync::{Lrc, Lock, WorkerLocal, AtomicCell};
use rustc_data_structures::sync::{Lrc, Lock, WorkerLocal, AtomicOnce};
use std::any::Any;
use std::borrow::Borrow;
use std::cmp::Ordering;
Expand Down Expand Up @@ -1022,7 +1021,7 @@ pub struct GlobalCtxt<'tcx> {

pub hir_defs: hir::map::Definitions,

hir_map: AtomicCell<Option<&'tcx hir_map::Map<'tcx>>>,
hir_map: AtomicOnce<&'tcx hir_map::Map<'tcx>>,

/// A map from DefPathHash -> DefId. Includes DefIds from the local crate
/// as well as all upstream crates. Only populated in incremental mode.
Expand Down Expand Up @@ -1089,17 +1088,10 @@ impl<'tcx> TyCtxt<'tcx> {

#[inline(always)]
pub fn hir(self) -> &'tcx hir_map::Map<'tcx> {
let value = self.hir_map.load();
if unlikely!(value.is_none()) {
self.hir_map.get_or_init(|| {
// We can use `with_ignore` here because the hir map does its own tracking
cold_path(|| self.dep_graph.with_ignore(|| {
let map = self.hir_map(LOCAL_CRATE);
self.hir_map.store(Some(map));
map
}))
} else {
value.unwrap()
}
self.dep_graph.with_ignore(|| self.hir_map(LOCAL_CRATE))
})
}

pub fn alloc_steal_mir(self, mir: Body<'tcx>) -> &'tcx Steal<Body<'tcx>> {
Expand Down Expand Up @@ -1281,7 +1273,7 @@ impl<'tcx> TyCtxt<'tcx> {
extern_prelude: resolutions.extern_prelude,
hir_forest,
hir_defs,
hir_map: AtomicCell::new(None),
hir_map: AtomicOnce::new(),
def_path_hash_to_def_id,
queries: query::Queries::new(
providers,
Expand Down
29 changes: 29 additions & 0 deletions src/librustc_data_structures/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use std::collections::HashMap;
use std::hash::{Hash, BuildHasher};
use std::marker::PhantomData;
use std::ops::{Deref, DerefMut};
use crate::cold_path;
use crate::owning_ref::{Erased, OwningRef};

pub fn serial_join<A, B, RA, RB>(oper_a: A, oper_b: B) -> (RA, RB)
Expand Down Expand Up @@ -573,6 +574,34 @@ impl<T> Once<T> {
}
}

/// A type whose inner value can be written once and then will stay read-only
pub struct AtomicOnce<T: Copy>(AtomicCell<Option<T>>);

impl<T: Copy> AtomicOnce<T> {
/// Creates an AtomicOnce value which is uninitialized
#[inline]
pub fn new() -> Self {
AtomicOnce(AtomicCell::new(None))
}

/// Gets the inner value. If the value is not already initalized.
/// It will be initalized by the closure. The closure may be called
/// concurrently by many threads.
#[inline(always)]
pub fn get_or_init(&self, f: impl FnOnce() -> T) -> T {
let value = self.0.load();
if unlikely!(value.is_none()) {
cold_path(|| {
let value = f();
self.0.store(Some(value));
value
})
} else {
value.unwrap()
}
}
}

#[derive(Debug)]
pub struct Lock<T>(InnerLock<T>);

Expand Down

0 comments on commit d5fbdd2

Please sign in to comment.