1 // Copyright 2013 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 //! Atomically reference counted data
13 //! This modules contains the implementation of an atomically reference counted
14 //! pointer for the purpose of sharing data between tasks. This is obviously a
15 //! very unsafe primitive to use, but it has its use cases when implementing
16 //! concurrent data structures and similar tasks.
18 //! Great care must be taken to ensure that data races do not arise through the
19 //! usage of `UnsafeArc`, and this often requires some form of external
20 //! synchronization. The only guarantee provided to you by this class is that
21 //! the underlying data will remain valid (not free'd) so long as the reference
22 //! count is greater than one.
29 use sync::atomics::{AtomicUint, SeqCst, Relaxed, Acquire};
32 /// An atomically reference counted pointer.
34 /// Enforces no shared-memory safety.
35 //#[unsafe_no_drop_flag] FIXME: #9758
36 pub struct UnsafeArc<T> {
37 priv data: *mut ArcData<T>,
45 unsafe fn new_inner<T: Send>(data: T, refcount: uint) -> *mut ArcData<T> {
46 let data = ~ArcData { count: AtomicUint::new(refcount), data: data };
50 impl<T: Send> UnsafeArc<T> {
51 /// Creates a new `UnsafeArc` which wraps the given data.
52 pub fn new(data: T) -> UnsafeArc<T> {
53 unsafe { UnsafeArc { data: new_inner(data, 1) } }
56 /// As new(), but returns an extra pre-cloned handle.
57 pub fn new2(data: T) -> (UnsafeArc<T>, UnsafeArc<T>) {
59 let ptr = new_inner(data, 2);
60 (UnsafeArc { data: ptr }, UnsafeArc { data: ptr })
64 /// As new(), but returns a vector of as many pre-cloned handles as
66 pub fn newN(data: T, num_handles: uint) -> ~[UnsafeArc<T>] {
69 ~[] // need to free data here
71 let ptr = new_inner(data, num_handles);
72 vec::from_fn(num_handles, |_| UnsafeArc { data: ptr })
77 /// Gets a pointer to the inner shared data. Note that care must be taken to
78 /// ensure that the outer `UnsafeArc` does not fall out of scope while this
79 /// pointer is in use, otherwise it could possibly contain a use-after-free.
81 pub fn get(&self) -> *mut T {
83 assert!((*self.data).count.load(Relaxed) > 0);
84 return &mut (*self.data).data as *mut T;
88 /// Gets an immutable pointer to the inner shared data. This has the same
89 /// caveats as the `get` method.
91 pub fn get_immut(&self) -> *T {
93 assert!((*self.data).count.load(Relaxed) > 0);
94 return &(*self.data).data as *T;
99 impl<T: Send> Clone for UnsafeArc<T> {
100 fn clone(&self) -> UnsafeArc<T> {
102 // This barrier might be unnecessary, but I'm not sure...
103 let old_count = (*self.data).count.fetch_add(1, Acquire);
104 assert!(old_count >= 1);
105 return UnsafeArc { data: self.data };
111 impl<T> Drop for UnsafeArc<T>{
114 // Happens when destructing an unwrapper's handle and from
115 // `#[unsafe_no_drop_flag]`
116 if self.data.is_null() {
119 // Must be acquire+release, not just release, to make sure this
120 // doesn't get reordered to after the unwrapper pointer load.
121 let old_count = (*self.data).count.fetch_sub(1, SeqCst);
122 assert!(old_count >= 1);
124 let _: ~ArcData<T> = cast::transmute(self.data);
133 use super::UnsafeArc;
138 assert_eq!(size_of::<UnsafeArc<[int, ..10]>>(), size_of::<*[int, ..10]>());
143 // Tests that the many-refcounts-at-once constructors don't leak.
144 let _ = UnsafeArc::new2(~~"hello");
145 let x = UnsafeArc::newN(~~"hello", 0);
146 assert_eq!(x.len(), 0)
147 let x = UnsafeArc::newN(~~"hello", 1);
148 assert_eq!(x.len(), 1)
149 let x = UnsafeArc::newN(~~"hello", 10);
150 assert_eq!(x.len(), 10)