// 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() {
/// }
/// ```
#[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
/// 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
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
}
#[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
/// 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);
}
}
+#[unstable = "waiting on stability of Clone"]
impl<T: Share + Send> Clone for Arc<T> {
/// Duplicate an atomically reference counted wrapper.
///
}
}
+#[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
}
}
/// data is cloned if the reference count is greater than one.
#[inline]
#[experimental]
- pub fn make_unique<'a>(&'a mut self) -> &'a mut T {
+ 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.
}
#[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
// 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],
}
}
+#[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.
///
}
#[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> {
}
#[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
#[allow(experimental)]
mod tests {
use std::clone::Clone;
+ use std::collections::MutableSeq;
use std::comm::channel;
use std::mem::drop;
use std::ops::Drop;
use std::task;
use std::vec::Vec;
use super::{Arc, Weak};
- use sync::Mutex;
+ use std::sync::Mutex;
struct Canary(*mut atomics::AtomicUint);
#[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());