Skip to content

Commit

Permalink
auto merge of #6639 : osaut/rust/arc-clean, r=brson
Browse files Browse the repository at this point in the history
* Add ARC::get method and implements the function from it.
* Add an example showing a simple use of ARC.

Update PR #6622 to avoid git noise.

I will remove the function get later.
  • Loading branch information
bors committed May 20, 2013
2 parents 26babaa + 3f232bc commit 54eafc0
Showing 1 changed file with 34 additions and 4 deletions.
38 changes: 34 additions & 4 deletions src/libstd/arc.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
Expand All @@ -8,9 +8,33 @@
// 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.
*
* # Example
*
* In this example, a large vector of floats is shared between several tasks.
* With simple pipes, without ARC, a copy would have to be made for each task.
*
* ~~~
* extern mod std;
* use std::arc;
* let numbers=vec::from_fn(100, |ind| (ind as float)*rand::random());
* let shared_numbers=arc::ARC(numbers);
*
* for 10.times {
* let (port, chan) = stream();
* chan.send(shared_numbers.clone());
*
* do spawn {
* let shared_numbers=port.recv();
* let local_numbers=shared_numbers.get();
*
* // Work with the local numbers
* }
* }
* ~~~
*/

use sync;
Expand All @@ -21,7 +45,7 @@ use core::unstable::sync::UnsafeAtomicRcBox;
use core::ptr;
use core::task;

/// As sync::condvar, a mechanism for unlock-and-descheduling and signalling.
/// As sync::condvar, a mechanism for unlock-and-descheduling and signaling.
pub struct Condvar<'self> {
is_mutex: bool,
failed: &'self mut bool,
Expand Down Expand Up @@ -93,9 +117,14 @@ pub fn ARC<T:Const + Owned>(data: T) -> ARC<T> {
* wrapper.
*/
pub fn get<'a, T:Const + Owned>(rc: &'a ARC<T>) -> &'a T {
unsafe { &*rc.x.get_immut() }
rc.get()
}

impl<T:Const+Owned> ARC<T> {
pub fn get<'a>(&'a self) -> &'a T {
unsafe { &*self.x.get_immut() }
}
}
/**
* Duplicate an atomically reference counted wrapper.
*
Expand Down Expand Up @@ -508,6 +537,7 @@ mod tests {
c.send(arc::clone(&arc_v));

assert_eq!((*arc::get(&arc_v))[2], 3);
assert_eq!(arc_v.get()[4], 5);

info!(arc_v);
}
Expand Down

0 comments on commit 54eafc0

Please sign in to comment.