#[doc = "An atomically reference counted wrapper that can be used hare immutable data between tasks."] export arc, get, clone; #[abi = "cdecl"] native mod rustrt { #[rust_stack] fn rust_atomic_increment(p: &mut libc::intptr_t) -> libc::intptr_t; #[rust_stack] fn rust_atomic_decrement(p: &mut libc::intptr_t) -> libc::intptr_t; } type arc_data = { mut count: libc::intptr_t, data: T }; resource arc_destruct(data: *arc_data) { unsafe { let ptr = &mut (*data).count; let new_count = rustrt::rust_atomic_decrement(ptr); assert new_count >= 0; if new_count == 0 { let _ptr : ~arc_data = unsafe::reinterpret_cast(data); // drop glue takes over. } } } type arc = arc_destruct; fn arc(-data: T) -> arc { let data = ~{mut count: 1, data: data}; unsafe { let ptr = unsafe::reinterpret_cast(data); unsafe::forget(data); arc_destruct(ptr) } } fn get(rc: &a.arc) -> &a.T { unsafe { &(***rc).data } } fn clone(rc: &arc) -> arc { let data = **rc; unsafe { rustrt::rust_atomic_increment(&mut (*data).count); } arc_destruct(**rc) }