]> git.proxmox.com Git - cargo.git/blobdiff - vendor/thread_local/src/cached.rs
New upstream version 0.52.0
[cargo.git] / vendor / thread_local / src / cached.rs
index ab43c86da59fc595863341d430fdfdcd342b9ec8..16f6516de709e6b219235e1d58841dfcdbd55b22 100644 (file)
@@ -1,25 +1,19 @@
+#![allow(deprecated)]
+
 use super::{IntoIter, IterMut, ThreadLocal};
-use std::cell::UnsafeCell;
 use std::fmt;
 use std::panic::UnwindSafe;
-use std::sync::atomic::{AtomicUsize, Ordering};
-use thread_id;
-use unreachable::{UncheckedOptionExt, UncheckedResultExt};
+use std::usize;
 
-/// Wrapper around `ThreadLocal` which adds a fast path for a single thread.
+/// Wrapper around [`ThreadLocal`].
 ///
-/// This has the same API as `ThreadLocal`, but will register the first thread
-/// that sets a value as its owner. All accesses by the owner will go through
-/// a special fast path which is much faster than the normal `ThreadLocal` path.
+/// This used to add a fast path for a single thread, however that has been
+/// obsoleted by performance improvements to [`ThreadLocal`] itself.
+#[deprecated(since = "1.1.0", note = "Use `ThreadLocal` instead")]
 pub struct CachedThreadLocal<T: Send> {
-    owner: AtomicUsize,
-    local: UnsafeCell<Option<Box<T>>>,
-    global: ThreadLocal<T>,
+    inner: ThreadLocal<T>,
 }
 
-// CachedThreadLocal is always Sync, even if T isn't
-unsafe impl<T: Send> Sync for CachedThreadLocal<T> {}
-
 impl<T: Send> Default for CachedThreadLocal<T> {
     fn default() -> CachedThreadLocal<T> {
         CachedThreadLocal::new()
@@ -28,71 +22,38 @@ impl<T: Send> Default for CachedThreadLocal<T> {
 
 impl<T: Send> CachedThreadLocal<T> {
     /// Creates a new empty `CachedThreadLocal`.
+    #[inline]
     pub fn new() -> CachedThreadLocal<T> {
         CachedThreadLocal {
-            owner: AtomicUsize::new(0),
-            local: UnsafeCell::new(None),
-            global: ThreadLocal::new(),
+            inner: ThreadLocal::new(),
         }
     }
 
     /// Returns the element for the current thread, if it exists.
+    #[inline]
     pub fn get(&self) -> Option<&T> {
-        let id = thread_id::get();
-        let owner = self.owner.load(Ordering::Relaxed);
-        if owner == id {
-            return unsafe { Some((*self.local.get()).as_ref().unchecked_unwrap()) };
-        }
-        if owner == 0 {
-            return None;
-        }
-        self.global.get_fast(id)
+        self.inner.get()
     }
 
     /// Returns the element for the current thread, or creates it if it doesn't
     /// exist.
-    #[inline(always)]
+    #[inline]
     pub fn get_or<F>(&self, create: F) -> &T
     where
         F: FnOnce() -> T,
     {
-        unsafe {
-            self.get_or_try(|| Ok::<T, ()>(create()))
-                .unchecked_unwrap_ok()
-        }
+        self.inner.get_or(create)
     }
 
     /// Returns the element for the current thread, or creates it if it doesn't
     /// exist. If `create` fails, that error is returned and no element is
     /// added.
+    #[inline]
     pub fn get_or_try<F, E>(&self, create: F) -> Result<&T, E>
     where
         F: FnOnce() -> Result<T, E>,
     {
-        let id = thread_id::get();
-        let owner = self.owner.load(Ordering::Relaxed);
-        if owner == id {
-            return Ok(unsafe { (*self.local.get()).as_ref().unchecked_unwrap() });
-        }
-        self.get_or_try_slow(id, owner, create)
-    }
-
-    #[cold]
-    #[inline(never)]
-    fn get_or_try_slow<F, E>(&self, id: usize, owner: usize, create: F) -> Result<&T, E>
-    where
-        F: FnOnce() -> Result<T, E>,
-    {
-        if owner == 0 && self.owner.compare_and_swap(0, id, Ordering::Relaxed) == 0 {
-            unsafe {
-                (*self.local.get()) = Some(Box::new(create()?));
-                return Ok((*self.local.get()).as_ref().unchecked_unwrap());
-            }
-        }
-        match self.global.get_fast(id) {
-            Some(x) => Ok(x),
-            None => Ok(self.global.insert(id, Box::new(create()?), true)),
-        }
+        self.inner.get_or_try(create)
     }
 
     /// Returns a mutable iterator over the local values of all threads.
@@ -100,10 +61,10 @@ impl<T: Send> CachedThreadLocal<T> {
     /// Since this call borrows the `ThreadLocal` mutably, this operation can
     /// be done safely---the mutable borrow statically guarantees no other
     /// threads are currently accessing their associated values.
+    #[inline]
     pub fn iter_mut(&mut self) -> CachedIterMut<T> {
         CachedIterMut {
-            local: unsafe { (*self.local.get()).as_mut().map(|x| &mut **x) },
-            global: self.global.iter_mut(),
+            inner: self.inner.iter_mut(),
         }
     }
 
@@ -113,8 +74,9 @@ impl<T: Send> CachedThreadLocal<T> {
     /// Since this call borrows the `ThreadLocal` mutably, this operation can
     /// be done safely---the mutable borrow statically guarantees no other
     /// threads are currently accessing their associated values.
+    #[inline]
     pub fn clear(&mut self) {
-        *self = CachedThreadLocal::new();
+        self.inner.clear();
     }
 }
 
@@ -124,8 +86,7 @@ impl<T: Send> IntoIterator for CachedThreadLocal<T> {
 
     fn into_iter(self) -> CachedIntoIter<T> {
         CachedIntoIter {
-            local: unsafe { (*self.local.get()).take().map(|x| *x) },
-            global: self.global.into_iter(),
+            inner: self.inner.into_iter(),
         }
     }
 }
@@ -156,42 +117,44 @@ impl<T: Send + fmt::Debug> fmt::Debug for CachedThreadLocal<T> {
 impl<T: Send + UnwindSafe> UnwindSafe for CachedThreadLocal<T> {}
 
 /// Mutable iterator over the contents of a `CachedThreadLocal`.
+#[deprecated(since = "1.1.0", note = "Use `IterMut` instead")]
 pub struct CachedIterMut<'a, T: Send + 'a> {
-    local: Option<&'a mut T>,
-    global: IterMut<'a, T>,
+    inner: IterMut<'a, T>,
 }
 
 impl<'a, T: Send + 'a> Iterator for CachedIterMut<'a, T> {
     type Item = &'a mut T;
 
+    #[inline]
     fn next(&mut self) -> Option<&'a mut T> {
-        self.local.take().or_else(|| self.global.next())
+        self.inner.next()
     }
 
+    #[inline]
     fn size_hint(&self) -> (usize, Option<usize>) {
-        let len = self.global.size_hint().0 + self.local.is_some() as usize;
-        (len, Some(len))
+        self.inner.size_hint()
     }
 }
 
 impl<'a, T: Send + 'a> ExactSizeIterator for CachedIterMut<'a, T> {}
 
 /// An iterator that moves out of a `CachedThreadLocal`.
+#[deprecated(since = "1.1.0", note = "Use `IntoIter` instead")]
 pub struct CachedIntoIter<T: Send> {
-    local: Option<T>,
-    global: IntoIter<T>,
+    inner: IntoIter<T>,
 }
 
 impl<T: Send> Iterator for CachedIntoIter<T> {
     type Item = T;
 
+    #[inline]
     fn next(&mut self) -> Option<T> {
-        self.local.take().or_else(|| self.global.next())
+        self.inner.next()
     }
 
+    #[inline]
     fn size_hint(&self) -> (usize, Option<usize>) {
-        let len = self.global.size_hint().0 + self.local.is_some() as usize;
-        (len, Some(len))
+        self.inner.size_hint()
     }
 }