Skip to content

Instantly share code, notes, and snippets.

@eholk
Created May 23, 2012 19:06
Show Gist options
  • Select an option

  • Save eholk/2777126 to your computer and use it in GitHub Desktop.

Select an option

Save eholk/2777126 to your computer and use it in GitHub Desktop.
resource arc_destruct<T>(data: swappable<~arc_data<T>>) {
let data = unwrap(data);
let ptr = ptr::mut_addr_of((*data).count);
let new_count = rustrt::rust_atomic_decrement(ptr);
assert new_count >= 0;
if new_count > 0 {
unsafe {
unsafe::forget(data);
}
}
// Otherwise, we are the last one, so we use the normal
// shutdown proccess.
}
@nikomatsakis

Copy link
Copy Markdown

To create the resource:

type arc_rec<T> = {rc: uint,  data: T};

fn mk_arc<T>(-data: T) -> arc<T> {
    let data = ~{rc: 1u, data: data};
    let ptr: *arc_rec<T> = unsafe::reinterpret_cast(data);
    unsafe::forget(data);
    ret arc(ptr);
}

resource arc<T>(r: *arc_rec<T>) {
    if ref count is zero {
        let ptr: ~arc_rec<T> = unsafe::reinterpret_cast(r);
        // let drop glue take over here
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment