hawkw / sharded-slab

a lock-free concurrent slab (experimental)
MIT License
273 stars 19 forks source link

APIs for mutable (exclusive) access #39

Open hawkw opened 4 years ago

hawkw commented 4 years ago

In some cases, entries will not be accessed concurrently. It might be nice to have an API for mutably accessing an entry by index if it is not being concurrently accessed. This would need to ensure that the reference count is 0, and (probably) set a bit in the slot's lifecycle to that prevents immutable access while the slot is accessed mutably.

If the slot is already mutably accessed, we would want to return None or an AlreadyAccessed error of some kind (so that users can distinguish between cases where a slot cannot be referenced mutably, and cases where the slot does not exist). We probably don't want to have a method that spins until the slot can be accessed mutably — if users want to block the calling thread, they'd be better off using a read-write lock.

hawkw commented 4 years ago

If we did this, we could consider also having the create API return the mutable guard rather than taking a closure. When the slot has been mutated, the guard can be "downgraded" or dropped to release it for other threads to borrow immutably.