about summary refs log tree commit diff
path: root/compiler/rustc_data_structures/src
diff options
context:
space:
mode:
authorJoshua Nelson <jyn514@gmail.com>2021-03-29 13:50:40 -0400
committerJoshua Nelson <jyn514@gmail.com>2021-03-29 13:50:40 -0400
commit526bb10701af099f6812077a57bf64b53e85dd4c (patch)
treed6fb4972cfc9be7a4aed3df4755d539ee18ea2fa /compiler/rustc_data_structures/src
parentf3523544f114a4a4cc2ced3f1a53f5f3bc158751 (diff)
downloadrust-526bb10701af099f6812077a57bf64b53e85dd4c.tar.gz
rust-526bb10701af099f6812077a57bf64b53e85dd4c.zip
Revert changes to sync data structures
There isn't currently a good reviewer for these, and I don't want to
remove things that will just be added again. I plan to make a separate
PR for these changes so the rest of the cleanup can land.
Diffstat (limited to 'compiler/rustc_data_structures/src')
-rw-r--r--compiler/rustc_data_structures/src/sync.rs110
1 files changed, 110 insertions, 0 deletions
diff --git a/compiler/rustc_data_structures/src/sync.rs b/compiler/rustc_data_structures/src/sync.rs
index 357686342be..26706cd2b1b 100644
--- a/compiler/rustc_data_structures/src/sync.rs
+++ b/compiler/rustc_data_structures/src/sync.rs
@@ -43,6 +43,46 @@ cfg_if! {
         use std::ops::Add;
         use std::panic::{resume_unwind, catch_unwind, AssertUnwindSafe};
 
+        /// This is a single threaded variant of AtomicCell provided by crossbeam.
+        /// Unlike `Atomic` this is intended for all `Copy` types,
+        /// but it lacks the explicit ordering arguments.
+        #[derive(Debug)]
+        pub struct AtomicCell<T: Copy>(Cell<T>);
+
+        impl<T: Copy> AtomicCell<T> {
+            #[inline]
+            pub fn new(v: T) -> Self {
+                AtomicCell(Cell::new(v))
+            }
+
+            #[inline]
+            pub fn get_mut(&mut self) -> &mut T {
+                self.0.get_mut()
+            }
+        }
+
+        impl<T: Copy> AtomicCell<T> {
+            #[inline]
+            pub fn into_inner(self) -> T {
+                self.0.into_inner()
+            }
+
+            #[inline]
+            pub fn load(&self) -> T {
+                self.0.get()
+            }
+
+            #[inline]
+            pub fn store(&self, val: T) {
+                self.0.set(val)
+            }
+
+            #[inline]
+            pub fn swap(&self, val: T) -> T {
+                self.0.replace(val)
+            }
+        }
+
         /// 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.
@@ -60,6 +100,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 +113,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> {
@@ -109,6 +159,22 @@ cfg_if! {
             (oper_a(), oper_b())
         }
 
+        pub struct SerialScope;
+
+        impl SerialScope {
+            pub fn spawn<F>(&self, f: F)
+                where F: FnOnce(&SerialScope)
+            {
+                f(self)
+            }
+        }
+
+        pub fn scope<F, R>(f: F) -> R
+            where F: FnOnce(&SerialScope) -> R
+        {
+            f(&SerialScope)
+        }
+
         #[macro_export]
         macro_rules! parallel {
             ($($blocks:tt),*) => {
@@ -180,6 +246,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 +280,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
             }
@@ -236,6 +318,8 @@ cfg_if! {
 
         pub use std::sync::atomic::{AtomicBool, AtomicUsize, AtomicU32, AtomicU64};
 
+        pub use crossbeam_utils::atomic::AtomicCell;
+
         pub use std::sync::Arc as Lrc;
         pub use std::sync::Weak as Weak;
 
@@ -437,6 +521,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 +547,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 +581,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 +633,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> {