Skip to content

Commit 369231b

Browse files
committed
core: Rename SharedMutableState to UnsafeAtomicRcBox
1 parent fa1d047 commit 369231b

File tree

3 files changed

+131
-131
lines changed

3 files changed

+131
-131
lines changed

src/libcore/unstable/global.rs

+8-13
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,7 @@ use unstable::intrinsics::atomic_cxchg;
3737
use hashmap::HashMap;
3838
use sys::Closure;
3939

40-
#[cfg(test)] use unstable::sync::{SharedMutableState, shared_mutable_state};
41-
#[cfg(test)] use unstable::sync::get_shared_immutable_state;
40+
#[cfg(test)] use unstable::sync::{UnsafeAtomicRcBox};
4241
#[cfg(test)] use task::spawn;
4342
#[cfg(test)] use uint;
4443

@@ -234,35 +233,31 @@ extern {
234233

235234
#[test]
236235
fn test_clone_rc() {
237-
type MyType = SharedMutableState<int>;
238-
239-
fn key(_v: SharedMutableState<int>) { }
236+
fn key(_v: UnsafeAtomicRcBox<int>) { }
240237

241238
for uint::range(0, 100) |_| {
242239
do spawn {
243240
unsafe {
244241
let val = do global_data_clone_create(key) {
245-
~shared_mutable_state(10)
242+
~UnsafeAtomicRcBox::new(10)
246243
};
247244

248-
assert!(get_shared_immutable_state(&val) == &10);
245+
assert!(val.get() == &10);
249246
}
250247
}
251248
}
252249
}
253250

254251
#[test]
255252
fn test_modify() {
256-
type MyType = SharedMutableState<int>;
257-
258-
fn key(_v: SharedMutableState<int>) { }
253+
fn key(_v: UnsafeAtomicRcBox<int>) { }
259254

260255
unsafe {
261256
do global_data_modify(key) |v| {
262257
match v {
263258
None => {
264259
unsafe {
265-
Some(~shared_mutable_state(10))
260+
Some(~UnsafeAtomicRcBox::new(10))
266261
}
267262
}
268263
_ => fail!()
@@ -272,7 +267,7 @@ fn test_modify() {
272267
do global_data_modify(key) |v| {
273268
match v {
274269
Some(sms) => {
275-
let v = get_shared_immutable_state(sms);
270+
let v = sms.get();
276271
assert!(*v == 10);
277272
None
278273
},
@@ -284,7 +279,7 @@ fn test_modify() {
284279
match v {
285280
None => {
286281
unsafe {
287-
Some(~shared_mutable_state(10))
282+
Some(~UnsafeAtomicRcBox::new(10))
288283
}
289284
}
290285
_ => fail!()

src/libcore/unstable/sync.rs

+80-77
Original file line numberDiff line numberDiff line change
@@ -19,98 +19,103 @@ use ops::Drop;
1919
use clone::Clone;
2020
use kinds::Owned;
2121

22-
/****************************************************************************
23-
* Shared state & exclusive ARC
24-
****************************************************************************/
25-
26-
struct ArcData<T> {
27-
count: libc::intptr_t,
28-
// FIXME(#3224) should be able to make this non-option to save memory
29-
data: Option<T>,
22+
/// An atomically reference counted pointer.
23+
///
24+
/// Enforces no shared-memory safety.
25+
pub struct UnsafeAtomicRcBox<T> {
26+
data: *mut libc::c_void,
3027
}
3128

32-
struct ArcDestruct<T> {
33-
data: *libc::c_void,
29+
struct AtomicRcBoxData<T> {
30+
count: int,
31+
data: Option<T>,
3432
}
3533

36-
#[unsafe_destructor]
37-
impl<T> Drop for ArcDestruct<T>{
38-
fn finalize(&self) {
34+
impl<T: Owned> UnsafeAtomicRcBox<T> {
35+
pub fn new(data: T) -> UnsafeAtomicRcBox<T> {
3936
unsafe {
40-
do task::unkillable {
41-
let mut data: ~ArcData<T> = cast::transmute(self.data);
42-
let new_count =
43-
intrinsics::atomic_xsub(&mut data.count, 1) - 1;
44-
assert!(new_count >= 0);
45-
if new_count == 0 {
46-
// drop glue takes over.
47-
} else {
48-
cast::forget(data);
49-
}
50-
}
37+
let data = ~AtomicRcBoxData { count: 1, data: Some(data) };
38+
let ptr = cast::transmute(data);
39+
return UnsafeAtomicRcBox { data: ptr };
5140
}
5241
}
53-
}
5442

55-
fn ArcDestruct<T>(data: *libc::c_void) -> ArcDestruct<T> {
56-
ArcDestruct {
57-
data: data
43+
#[inline(always)]
44+
#[cfg(stage0)]
45+
pub unsafe fn get(&self) -> *mut T
46+
{
47+
let mut data: ~AtomicRcBoxData<T> = cast::transmute(self.data);
48+
assert!(data.count > 0);
49+
let r: *mut T = cast::transmute(data.data.get_mut_ref());
50+
cast::forget(data);
51+
return r;
5852
}
59-
}
6053

61-
/**
62-
* COMPLETELY UNSAFE. Used as a primitive for the safe versions in std::arc.
63-
*
64-
* Data races between tasks can result in crashes and, with sufficient
65-
* cleverness, arbitrary type coercion.
66-
*/
67-
pub type SharedMutableState<T> = ArcDestruct<T>;
54+
#[inline(always)]
55+
#[cfg(not(stage0))]
56+
pub unsafe fn get(&self) -> *mut T
57+
{
58+
let mut data: ~AtomicRcBoxData<T> = cast::transmute(self.data);
59+
assert!(data.count > 0);
60+
let r: *mut T = data.data.get_mut_ref();
61+
cast::forget(data);
62+
return r;
63+
}
6864

69-
pub unsafe fn shared_mutable_state<T:Owned>(data: T) ->
70-
SharedMutableState<T> {
71-
let data = ~ArcData { count: 1, data: Some(data) };
72-
let ptr = cast::transmute(data);
73-
ArcDestruct(ptr)
74-
}
65+
#[inline(always)]
66+
#[cfg(stage0)]
67+
pub unsafe fn get_immut(&self) -> *T
68+
{
69+
let mut data: ~AtomicRcBoxData<T> = cast::transmute(self.data);
70+
assert!(data.count > 0);
71+
let r: *T = cast::transmute(data.data.get_mut_ref());
72+
cast::forget(data);
73+
return r;
74+
}
7575

76-
#[inline(always)]
77-
pub unsafe fn get_shared_mutable_state<T:Owned>(
78-
rc: *SharedMutableState<T>) -> *mut T
79-
{
80-
let ptr: ~ArcData<T> = cast::transmute((*rc).data);
81-
assert!(ptr.count > 0);
82-
let r = cast::transmute(ptr.data.get_ref());
83-
cast::forget(ptr);
84-
return r;
85-
}
86-
#[inline(always)]
87-
pub unsafe fn get_shared_immutable_state<'a,T:Owned>(
88-
rc: &'a SharedMutableState<T>) -> &'a T {
89-
let ptr: ~ArcData<T> = cast::transmute((*rc).data);
90-
assert!(ptr.count > 0);
91-
// Cast us back into the correct region
92-
let r = cast::transmute_region(ptr.data.get_ref());
93-
cast::forget(ptr);
94-
return r;
76+
#[inline(always)]
77+
#[cfg(not(stage0))]
78+
pub unsafe fn get_immut(&self) -> *T
79+
{
80+
let mut data: ~AtomicRcBoxData<T> = cast::transmute(self.data);
81+
assert!(data.count > 0);
82+
let r: *T = cast::transmute_immut(data.data.get_mut_ref());
83+
cast::forget(data);
84+
return r;
85+
}
9586
}
9687

97-
pub unsafe fn clone_shared_mutable_state<T:Owned>(rc: &SharedMutableState<T>)
98-
-> SharedMutableState<T> {
99-
let mut ptr: ~ArcData<T> = cast::transmute((*rc).data);
100-
let new_count = intrinsics::atomic_xadd(&mut ptr.count, 1) + 1;
101-
assert!(new_count >= 2);
102-
cast::forget(ptr);
103-
ArcDestruct((*rc).data)
88+
impl<T: Owned> Clone for UnsafeAtomicRcBox<T> {
89+
fn clone(&self) -> UnsafeAtomicRcBox<T> {
90+
unsafe {
91+
let mut data: ~AtomicRcBoxData<T> = cast::transmute(self.data);
92+
let new_count = intrinsics::atomic_xadd(&mut data.count, 1) + 1;
93+
assert!(new_count >= 2);
94+
cast::forget(data);
95+
return UnsafeAtomicRcBox { data: self.data };
96+
}
97+
}
10498
}
10599

106-
impl<T:Owned> Clone for SharedMutableState<T> {
107-
fn clone(&self) -> SharedMutableState<T> {
100+
#[unsafe_destructor]
101+
impl<T> Drop for UnsafeAtomicRcBox<T>{
102+
fn finalize(&self) {
108103
unsafe {
109-
clone_shared_mutable_state(self)
104+
do task::unkillable {
105+
let mut data: ~AtomicRcBoxData<T> = cast::transmute(self.data);
106+
let new_count = intrinsics::atomic_xsub(&mut data.count, 1) - 1;
107+
assert!(new_count >= 0);
108+
if new_count == 0 {
109+
// drop glue takes over.
110+
} else {
111+
cast::forget(data);
112+
}
113+
}
110114
}
111115
}
112116
}
113117

118+
114119
/****************************************************************************/
115120

116121
#[allow(non_camel_case_types)] // runtime type
@@ -160,7 +165,7 @@ struct ExData<T> {
160165
* An arc over mutable data that is protected by a lock. For library use only.
161166
*/
162167
pub struct Exclusive<T> {
163-
x: SharedMutableState<ExData<T>>
168+
x: UnsafeAtomicRcBox<ExData<T>>
164169
}
165170

166171
pub fn exclusive<T:Owned>(user_data: T) -> Exclusive<T> {
@@ -170,16 +175,14 @@ pub fn exclusive<T:Owned>(user_data: T) -> Exclusive<T> {
170175
data: user_data
171176
};
172177
Exclusive {
173-
x: unsafe {
174-
shared_mutable_state(data)
175-
}
178+
x: UnsafeAtomicRcBox::new(data)
176179
}
177180
}
178181

179182
impl<T:Owned> Clone for Exclusive<T> {
180183
// Duplicate an exclusive ARC, as std::arc::clone.
181184
fn clone(&self) -> Exclusive<T> {
182-
Exclusive { x: unsafe { clone_shared_mutable_state(&self.x) } }
185+
Exclusive { x: self.x.clone() }
183186
}
184187
}
185188

@@ -192,7 +195,7 @@ pub impl<T:Owned> Exclusive<T> {
192195
// the exclusive. Supporting that is a work in progress.
193196
#[inline(always)]
194197
unsafe fn with<U>(&self, f: &fn(x: &mut T) -> U) -> U {
195-
let rec = get_shared_mutable_state(&self.x);
198+
let rec = self.x.get();
196199
do (*rec).lock.lock {
197200
if (*rec).failed {
198201
fail!(

0 commit comments

Comments
 (0)