diff options
| author | Joshua Nelson <jyn514@gmail.com> | 2021-03-31 12:29:34 -0400 |
|---|---|---|
| committer | Joshua Nelson <jyn514@gmail.com> | 2021-06-04 15:26:08 -0400 |
| commit | 3412957e7f50a586f8ac2c12d0ffd0384d546534 (patch) | |
| tree | 7652dea67ad54f600ae9d7cdc90b563b33153e94 /compiler/rustc_data_structures/src | |
| parent | 1a5cc2552536eabe8c26a0592e71de5f7e3e0ebd (diff) | |
| download | rust-3412957e7f50a586f8ac2c12d0ffd0384d546534.tar.gz rust-3412957e7f50a586f8ac2c12d0ffd0384d546534.zip | |
Unify parallel and non-parallel APIs
It's confusing for these to be different, even if some of the methods are unused.
Diffstat (limited to 'compiler/rustc_data_structures/src')
| -rw-r--r-- | compiler/rustc_data_structures/src/sync.rs | 56 |
1 files changed, 54 insertions, 2 deletions
diff --git a/compiler/rustc_data_structures/src/sync.rs b/compiler/rustc_data_structures/src/sync.rs index 357686342be..722ce6b6367 100644 --- a/compiler/rustc_data_structures/src/sync.rs +++ b/compiler/rustc_data_structures/src/sync.rs @@ -44,8 +44,8 @@ cfg_if! { use std::panic::{resume_unwind, catch_unwind, AssertUnwindSafe}; /// This is a single threaded variant of `AtomicU64`, `AtomicUsize`, etc. - /// It differs from `AtomicCell` in that it has explicit ordering arguments - /// and is only intended for use with the native atomic types. + /// It has explicit ordering arguments and is only intended for use with + /// the native atomic types. /// You should use this type through the `AtomicU64`, `AtomicUsize`, etc, type aliases /// as it's not intended to be used separately. #[derive(Debug)] @@ -60,6 +60,11 @@ cfg_if! { impl<T: Copy> Atomic<T> { #[inline] + pub fn into_inner(self) -> T { + self.0.into_inner() + } + + #[inline] pub fn load(&self, _: Ordering) -> T { self.0.get() } @@ -68,6 +73,11 @@ cfg_if! { pub fn store(&self, val: T, _: Ordering) { self.0.set(val) } + + #[inline] + pub fn swap(&self, val: T, _: Ordering) -> T { + self.0.replace(val) + } } impl<T: Copy + PartialEq> Atomic<T> { @@ -180,6 +190,12 @@ cfg_if! { pub fn new<F: FnMut(usize) -> T>(mut f: F) -> WorkerLocal<T> { WorkerLocal(OneThread::new(f(0))) } + + /// Returns the worker-local value for each thread + #[inline] + pub fn into_inner(self) -> Vec<T> { + vec![OneThread::into_inner(self.0)] + } } impl<T> Deref for WorkerLocal<T> { @@ -208,6 +224,16 @@ cfg_if! { } #[inline(always)] + pub fn get_mut(&mut self) -> &mut T { + &mut self.0 + } + + #[inline(always)] + pub fn lock(&self) -> &T { + &self.0 + } + + #[inline(always)] pub fn lock_mut(&mut self) -> &mut T { &mut self.0 } @@ -437,6 +463,16 @@ impl<T> RwLock<T> { RwLock(InnerRwLock::new(inner)) } + #[inline(always)] + pub fn into_inner(self) -> T { + self.0.into_inner() + } + + #[inline(always)] + pub fn get_mut(&mut self) -> &mut T { + self.0.get_mut() + } + #[cfg(not(parallel_compiler))] #[inline(always)] pub fn read(&self) -> ReadGuard<'_, T> { @@ -453,6 +489,11 @@ impl<T> RwLock<T> { } } + #[inline(always)] + pub fn with_read_lock<F: FnOnce(&T) -> R, R>(&self, f: F) -> R { + f(&*self.read()) + } + #[cfg(not(parallel_compiler))] #[inline(always)] pub fn try_write(&self) -> Result<WriteGuard<'_, T>, ()> { @@ -482,6 +523,11 @@ impl<T> RwLock<T> { } #[inline(always)] + pub fn with_write_lock<F: FnOnce(&mut T) -> R, R>(&self, f: F) -> R { + f(&mut *self.write()) + } + + #[inline(always)] pub fn borrow(&self) -> ReadGuard<'_, T> { self.read() } @@ -529,6 +575,12 @@ impl<T> OneThread<T> { inner, } } + + #[inline(always)] + pub fn into_inner(value: Self) -> T { + value.check(); + value.inner + } } impl<T> Deref for OneThread<T> { |
