diff options
| author | bors <bors@rust-lang.org> | 2014-11-23 20:26:58 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2014-11-23 20:26:58 +0000 |
| commit | 4e5259503cd8aac9905c7ac6d68d0c4caab1d28c (patch) | |
| tree | 9d93d055fa5aa82480c4b5771aba4cca5efdfc9b /src/liballoc/arc.rs | |
| parent | 220b99b148559e8996a1dbd279e8ca190bf94b2e (diff) | |
| parent | d6b023a46750d6c2df919908bd0f1460d3d9c8a6 (diff) | |
| download | rust-4e5259503cd8aac9905c7ac6d68d0c4caab1d28c.tar.gz rust-4e5259503cd8aac9905c7ac6d68d0c4caab1d28c.zip | |
auto merge of #19242 : jakub-/rust/roll-up, r=jakub-
Diffstat (limited to 'src/liballoc/arc.rs')
| -rw-r--r-- | src/liballoc/arc.rs | 55 |
1 files changed, 54 insertions, 1 deletions
diff --git a/src/liballoc/arc.rs b/src/liballoc/arc.rs index c4f53d74467..4f744b0b2de 100644 --- a/src/liballoc/arc.rs +++ b/src/liballoc/arc.rs @@ -119,6 +119,16 @@ impl<T> Arc<T> { } } +/// Get the number of weak references to this value. +#[inline] +#[experimental] +pub fn weak_count<T>(this: &Arc<T>) -> uint { this.inner().weak.load(atomic::SeqCst) - 1 } + +/// Get the number of strong references to this value. +#[inline] +#[experimental] +pub fn strong_count<T>(this: &Arc<T>) -> uint { this.inner().strong.load(atomic::SeqCst) } + #[unstable = "waiting on stability of Clone"] impl<T> Clone for Arc<T> { /// Duplicate an atomically reference counted wrapper. @@ -321,7 +331,7 @@ mod tests { use std::sync::atomic; use std::task; use std::vec::Vec; - use super::{Arc, Weak}; + use super::{Arc, Weak, weak_count, strong_count}; use std::sync::Mutex; struct Canary(*mut atomic::AtomicUint); @@ -466,6 +476,49 @@ mod tests { } #[test] + fn test_strong_count() { + let a = Arc::new(0u32); + assert!(strong_count(&a) == 1); + let w = a.downgrade(); + assert!(strong_count(&a) == 1); + let b = w.upgrade().expect(""); + assert!(strong_count(&b) == 2); + assert!(strong_count(&a) == 2); + drop(w); + drop(a); + assert!(strong_count(&b) == 1); + let c = b.clone(); + assert!(strong_count(&b) == 2); + assert!(strong_count(&c) == 2); + } + + #[test] + fn test_weak_count() { + let a = Arc::new(0u32); + assert!(strong_count(&a) == 1); + assert!(weak_count(&a) == 0); + let w = a.downgrade(); + assert!(strong_count(&a) == 1); + assert!(weak_count(&a) == 1); + let x = w.clone(); + assert!(weak_count(&a) == 2); + drop(w); + drop(x); + assert!(strong_count(&a) == 1); + assert!(weak_count(&a) == 0); + let c = a.clone(); + assert!(strong_count(&a) == 2); + assert!(weak_count(&a) == 0); + let d = c.downgrade(); + assert!(weak_count(&c) == 1); + assert!(strong_count(&c) == 2); + + drop(a); + drop(c); + drop(d); + } + + #[test] fn show_arc() { let a = Arc::new(5u32); assert!(format!("{}", a).as_slice() == "5") |
