]> git.lizzy.rs Git - rust.git/blobdiff - src/liballoc/arc.rs
auto merge of #15999 : Kimundi/rust/fix_folder, r=nikomatsakis
[rust.git] / src / liballoc / arc.rs
index 546e4e5269979c27e773823d76beb39fd064a0ff..bf477781aabf6a8207d61956fc5682013783bb46 100644 (file)
@@ -8,10 +8,10 @@
 // option. This file may not be copied, modified, or distributed
 // except according to those terms.
 
-/*!
- * Concurrency-enabled mechanisms for sharing mutable and/or immutable state
- * between tasks.
- */
+#![stable]
+
+//! Concurrency-enabled mechanisms for sharing mutable and/or immutable state
+//! between tasks.
 
 use core::atomics;
 use core::clone::Clone;
 /// task.
 ///
 /// ```rust
-/// extern crate sync;
-///
-/// use sync::Arc;
+/// use std::sync::Arc;
 ///
 /// fn main() {
 ///     let numbers = Vec::from_fn(100, |i| i as f32);
 ///     let shared_numbers = Arc::new(numbers);
 ///
-///     for _ in range(0, 10) {
+///     for _ in range(0u, 10) {
 ///         let child_numbers = shared_numbers.clone();
 ///
 ///         spawn(proc() {
@@ -53,6 +51,7 @@
 /// }
 /// ```
 #[unsafe_no_drop_flag]
+#[stable]
 pub struct Arc<T> {
     // FIXME #12808: strange name to try to avoid interfering with
     // field accesses of the contained type via Deref
@@ -64,6 +63,7 @@ pub struct Arc<T> {
 /// Weak pointers will not keep the data inside of the `Arc` alive, and can be
 /// used to break cycles between `Arc` pointers.
 #[unsafe_no_drop_flag]
+#[experimental = "Weak pointers may not belong in this module."]
 pub struct Weak<T> {
     // FIXME #12808: strange name to try to avoid interfering with
     // field accesses of the contained type via Deref
@@ -79,6 +79,7 @@ struct ArcInner<T> {
 impl<T: Share + Send> Arc<T> {
     /// Create an atomically reference counted wrapper.
     #[inline]
+    #[stable]
     pub fn new(data: T) -> Arc<T> {
         // Start the weak pointer count as 1 which is the weak pointer that's
         // held by all the strong pointers (kinda), see std/rc.rs for more info
@@ -91,7 +92,7 @@ pub fn new(data: T) -> Arc<T> {
     }
 
     #[inline]
-    fn inner<'a>(&'a self) -> &'a ArcInner<T> {
+    fn inner(&self) -> &ArcInner<T> {
         // This unsafety is ok because while this arc is alive we're guaranteed
         // that the inner pointer is valid. Furthermore, we know that the
         // `ArcInner` structure itself is `Share` because the inner data is
@@ -105,6 +106,7 @@ fn inner<'a>(&'a self) -> &'a ArcInner<T> {
     /// Weak pointers will not keep the data alive. Once all strong references
     /// to the underlying data have been dropped, the data itself will be
     /// destroyed.
+    #[experimental = "Weak pointers may not belong in this module."]
     pub fn downgrade(&self) -> Weak<T> {
         // See the clone() impl for why this is relaxed
         self.inner().weak.fetch_add(1, atomics::Relaxed);
@@ -112,6 +114,7 @@ pub fn downgrade(&self) -> Weak<T> {
     }
 }
 
+#[unstable = "waiting on stability of Clone"]
 impl<T: Share + Send> Clone for Arc<T> {
     /// Duplicate an atomically reference counted wrapper.
     ///
@@ -136,9 +139,10 @@ fn clone(&self) -> Arc<T> {
     }
 }
 
+#[experimental = "Deref is experimental."]
 impl<T: Send + Share> Deref<T> for Arc<T> {
     #[inline]
-    fn deref<'a>(&'a self) -> &'a T {
+    fn deref(&self) -> &T {
         &self.inner().data
     }
 }
@@ -151,8 +155,12 @@ impl<T: Send + Share + Clone> Arc<T> {
     /// data is cloned if the reference count is greater than one.
     #[inline]
     #[experimental]
-    pub fn make_unique<'a>(&'a mut self) -> &'a mut T {
-        if self.inner().strong.load(atomics::SeqCst) != 1 {
+    pub fn make_unique(&mut self) -> &mut T {
+        // Note that we hold a strong reference, which also counts as
+        // a weak reference, so we only clone if there is an
+        // additional reference of either kind.
+        if self.inner().strong.load(atomics::SeqCst) != 1 ||
+           self.inner().weak.load(atomics::SeqCst) != 1 {
             *self = Arc::new(self.deref().clone())
         }
         // This unsafety is ok because we're guaranteed that the pointer
@@ -166,6 +174,7 @@ pub fn make_unique<'a>(&'a mut self) -> &'a mut T {
 }
 
 #[unsafe_destructor]
+#[experimental = "waiting on stability of Drop"]
 impl<T: Share + Send> Drop for Arc<T> {
     fn drop(&mut self) {
         // This structure has #[unsafe_no_drop_flag], so this drop glue may run
@@ -180,9 +189,9 @@ fn drop(&mut self) {
 
         // This fence is needed to prevent reordering of use of the data and
         // deletion of the data. Because it is marked `Release`, the
-        // decreasing of the reference count sychronizes with this `Acquire`
+        // decreasing of the reference count synchronizes with this `Acquire`
         // fence. This means that use of the data happens before decreasing
-        // the refernce count, which happens before this fence, which
+        // the reference count, which happens before this fence, which
         // happens before the deletion of the data.
         //
         // As explained in the [Boost documentation][1],
@@ -209,6 +218,7 @@ fn drop(&mut self) {
     }
 }
 
+#[experimental = "Weak pointers may not belong in this module."]
 impl<T: Share + Send> Weak<T> {
     /// Attempts to upgrade this weak reference to a strong reference.
     ///
@@ -228,12 +238,13 @@ pub fn upgrade(&self) -> Option<Arc<T>> {
     }
 
     #[inline]
-    fn inner<'a>(&'a self) -> &'a ArcInner<T> {
+    fn inner(&self) -> &ArcInner<T> {
         // See comments above for why this is "safe"
         unsafe { &*self._ptr }
     }
 }
 
+#[experimental = "Weak pointers may not belong in this module."]
 impl<T: Share + Send> Clone for Weak<T> {
     #[inline]
     fn clone(&self) -> Weak<T> {
@@ -244,6 +255,7 @@ fn clone(&self) -> Weak<T> {
 }
 
 #[unsafe_destructor]
+#[experimental = "Weak pointers may not belong in this module."]
 impl<T: Share + Send> Drop for Weak<T> {
     fn drop(&mut self) {
         // see comments above for why this check is here
@@ -264,6 +276,7 @@ fn drop(&mut self) {
 #[allow(experimental)]
 mod tests {
     use std::clone::Clone;
+    use std::collections::MutableSeq;
     use std::comm::channel;
     use std::mem::drop;
     use std::ops::Drop;
@@ -272,7 +285,7 @@ mod tests {
     use std::task;
     use std::vec::Vec;
     use super::{Arc, Weak};
-    use sync::Mutex;
+    use std::sync::Mutex;
 
     struct Canary(*mut atomics::AtomicUint);
 
@@ -356,16 +369,30 @@ fn test_cowarc_clone_unique2() {
         assert!(*cow1 == *cow2);
     }
 
+    #[test]
+    fn test_cowarc_clone_weak() {
+        let mut cow0 = Arc::new(75u);
+        let cow1_weak = cow0.downgrade();
+
+        assert!(75 == *cow0);
+        assert!(75 == *cow1_weak.upgrade().unwrap());
+
+        *cow0.make_unique() += 1;
+
+        assert!(76 == *cow0);
+        assert!(cow1_weak.upgrade().is_none());
+    }
+
     #[test]
     fn test_live() {
-        let x = Arc::new(5);
+        let x = Arc::new(5i);
         let y = x.downgrade();
         assert!(y.upgrade().is_some());
     }
 
     #[test]
     fn test_dead() {
-        let x = Arc::new(5);
+        let x = Arc::new(5i);
         let y = x.downgrade();
         drop(x);
         assert!(y.upgrade().is_none());