Others / shredder

Garbage collected smart pointers for Rust
MIT License
266 stars 9 forks source link

AtomicGc #36

Closed jrmuizel closed 4 years ago

jrmuizel commented 4 years ago

One convenient use of garbage collectors is implementing concurrent data structures. It would be useful if there was a AtomicGc variant of Gc that allowed atomic modification like https://github.com/vorner/arc-swap does.

Others commented 4 years ago

I agree this would be cool to have! Ive thought about a design like this:

struct AtomicGc { gc_handle: AtomicPtr }

The problem is that it’s unclear when you can invalidate the handle if you overwrite it. Like if I write and replace the handle, how do I know if someone else is trying read/clone the handle at the same time?

Shredders Gc is not just a straight raw ptr due to the root tracking system so I wonder if it’s not fit for purpose :(

I think we could implement this feature using the ArcSwap crate itself but idk if that would be useful/performant.

Others commented 4 years ago

I think I might know how to do this now, but need to play with it locally. Here are my raw notes, probably useless to anyone but me:

Starve collector in busy loop during short critical sections around reads/writes

Read:
set flag, if fails — block and retry later 
read ptr (ptr to arc)
convert to arc read out value
create new gc 
unset flag

Write:
Same basically

Scan:
Only problematic, if scanning outside collector. Thus we just need to make sure it panics if used outside collector 

Tag gc ptrs for CAS
Tag {
    // To compare against what’s in the Atomic
    propper_ptr: *const (),
    // Arc to ensure the memory pointed to by propper_ptr is not deallocated
    holder_arc: Arc<???>
}
Others commented 4 years ago

Resolved by #48