Skip to content

Commit 9f82785

Browse files
Replace rustc_data_structures::sync::Once with OnceCell
1 parent 215f2d3 commit 9f82785

File tree

4 files changed

+7
-129
lines changed

4 files changed

+7
-129
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3791,6 +3791,7 @@ dependencies = [
37913791
"libc",
37923792
"log",
37933793
"measureme",
3794+
"once_cell",
37943795
"parking_lot 0.10.2",
37953796
"rustc-hash",
37963797
"rustc-rayon",

src/librustc_data_structures/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ indexmap = "1"
1515
log = "0.4"
1616
jobserver_crate = { version = "0.1.13", package = "jobserver" }
1717
lazy_static = "1"
18+
once_cell = { version = "1", features = ["parking_lot"] }
1819
rustc_serialize = { path = "../libserialize", package = "serialize" }
1920
graphviz = { path = "../libgraphviz" }
2021
cfg-if = "0.1.2"

src/librustc_data_structures/sync.rs

Lines changed: 4 additions & 129 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
use crate::owning_ref::{Erased, OwningRef};
2121
use std::collections::HashMap;
2222
use std::hash::{BuildHasher, Hash};
23-
use std::marker::PhantomData;
2423
use std::ops::{Deref, DerefMut};
2524

2625
pub use std::sync::atomic::Ordering;
@@ -230,6 +229,8 @@ cfg_if! {
230229
pub use std::cell::RefMut as LockGuard;
231230
pub use std::cell::RefMut as MappedLockGuard;
232231

232+
pub use once_cell::unsync::OnceCell;
233+
233234
use std::cell::RefCell as InnerRwLock;
234235
use std::cell::RefCell as InnerLock;
235236

@@ -313,6 +314,8 @@ cfg_if! {
313314
pub use parking_lot::MutexGuard as LockGuard;
314315
pub use parking_lot::MappedMutexGuard as MappedLockGuard;
315316

317+
pub use once_cell::sync::OnceCell;
318+
316319
pub use std::sync::atomic::{AtomicBool, AtomicUsize, AtomicU32, AtomicU64};
317320

318321
pub use crossbeam_utils::atomic::AtomicCell;
@@ -432,134 +435,6 @@ impl<K: Eq + Hash, V: Eq, S: BuildHasher> HashMapExt<K, V> for HashMap<K, V, S>
432435
}
433436
}
434437

435-
/// A type whose inner value can be written once and then will stay read-only
436-
// This contains a PhantomData<T> since this type conceptually owns a T outside the Mutex once
437-
// initialized. This ensures that Once<T> is Sync only if T is. If we did not have PhantomData<T>
438-
// we could send a &Once<Cell<bool>> to multiple threads and call `get` on it to get access
439-
// to &Cell<bool> on those threads.
440-
pub struct Once<T>(Lock<Option<T>>, PhantomData<T>);
441-
442-
impl<T> Once<T> {
443-
/// Creates an Once value which is uninitialized
444-
#[inline(always)]
445-
pub fn new() -> Self {
446-
Once(Lock::new(None), PhantomData)
447-
}
448-
449-
/// Consumes the value and returns Some(T) if it was initialized
450-
#[inline(always)]
451-
pub fn into_inner(self) -> Option<T> {
452-
self.0.into_inner()
453-
}
454-
455-
/// Tries to initialize the inner value to `value`.
456-
/// Returns `None` if the inner value was uninitialized and `value` was consumed setting it
457-
/// otherwise if the inner value was already set it returns `value` back to the caller
458-
#[inline]
459-
pub fn try_set(&self, value: T) -> Option<T> {
460-
let mut lock = self.0.lock();
461-
if lock.is_some() {
462-
return Some(value);
463-
}
464-
*lock = Some(value);
465-
None
466-
}
467-
468-
/// Tries to initialize the inner value to `value`.
469-
/// Returns `None` if the inner value was uninitialized and `value` was consumed setting it
470-
/// otherwise if the inner value was already set it asserts that `value` is equal to the inner
471-
/// value and then returns `value` back to the caller
472-
#[inline]
473-
pub fn try_set_same(&self, value: T) -> Option<T>
474-
where
475-
T: Eq,
476-
{
477-
let mut lock = self.0.lock();
478-
if let Some(ref inner) = *lock {
479-
assert!(*inner == value);
480-
return Some(value);
481-
}
482-
*lock = Some(value);
483-
None
484-
}
485-
486-
/// Tries to initialize the inner value to `value` and panics if it was already initialized
487-
#[inline]
488-
pub fn set(&self, value: T) {
489-
assert!(self.try_set(value).is_none());
490-
}
491-
492-
/// Initializes the inner value if it wasn't already done by calling the provided closure. It
493-
/// ensures that no-one else can access the value in the mean time by holding a lock for the
494-
/// duration of the closure.
495-
/// A reference to the inner value is returned.
496-
#[inline]
497-
pub fn init_locking<F: FnOnce() -> T>(&self, f: F) -> &T {
498-
{
499-
let mut lock = self.0.lock();
500-
if lock.is_none() {
501-
*lock = Some(f());
502-
}
503-
}
504-
505-
self.borrow()
506-
}
507-
508-
/// Tries to initialize the inner value by calling the closure without ensuring that no-one
509-
/// else can access it. This mean when this is called from multiple threads, multiple
510-
/// closures may concurrently be computing a value which the inner value should take.
511-
/// Only one of these closures are used to actually initialize the value.
512-
/// If some other closure already set the value,
513-
/// we return the value our closure computed wrapped in a `Option`.
514-
/// If our closure set the value, `None` is returned.
515-
/// If the value is already initialized, the closure is not called and `None` is returned.
516-
#[inline]
517-
pub fn init_nonlocking<F: FnOnce() -> T>(&self, f: F) -> Option<T> {
518-
if self.0.lock().is_some() { None } else { self.try_set(f()) }
519-
}
520-
521-
/// Tries to initialize the inner value by calling the closure without ensuring that no-one
522-
/// else can access it. This mean when this is called from multiple threads, multiple
523-
/// closures may concurrently be computing a value which the inner value should take.
524-
/// Only one of these closures are used to actually initialize the value.
525-
/// If some other closure already set the value, we assert that it our closure computed
526-
/// a value equal to the value already set and then
527-
/// we return the value our closure computed wrapped in a `Option`.
528-
/// If our closure set the value, `None` is returned.
529-
/// If the value is already initialized, the closure is not called and `None` is returned.
530-
#[inline]
531-
pub fn init_nonlocking_same<F: FnOnce() -> T>(&self, f: F) -> Option<T>
532-
where
533-
T: Eq,
534-
{
535-
if self.0.lock().is_some() { None } else { self.try_set_same(f()) }
536-
}
537-
538-
/// Tries to get a reference to the inner value, returns `None` if it is not yet initialized
539-
#[inline(always)]
540-
pub fn try_get(&self) -> Option<&T> {
541-
let lock = &*self.0.lock();
542-
if let Some(ref inner) = *lock {
543-
// This is safe since we won't mutate the inner value
544-
unsafe { Some(&*(inner as *const T)) }
545-
} else {
546-
None
547-
}
548-
}
549-
550-
/// Gets reference to the inner value, panics if it is not yet initialized
551-
#[inline(always)]
552-
pub fn get(&self) -> &T {
553-
self.try_get().expect("value was not set")
554-
}
555-
556-
/// Gets reference to the inner value, panics if it is not yet initialized
557-
#[inline(always)]
558-
pub fn borrow(&self) -> &T {
559-
self.get()
560-
}
561-
}
562-
563438
#[derive(Debug)]
564439
pub struct Lock<T>(InnerLock<T>);
565440

src/tools/tidy/src/deps.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ const WHITELIST: &[&str] = &[
128128
"miniz_oxide",
129129
"nodrop",
130130
"num_cpus",
131+
"once_cell",
131132
"opaque-debug",
132133
"parking_lot",
133134
"parking_lot_core",

0 commit comments

Comments
 (0)