Expand description
A lock-free, append-only atomic pool.
This library implements an atomic, append-only collection of items, where individual items can be acquired and relased so they are always associated with at most one owner. Thus, each item is at all times either free or acquired, and the library presents operations for acquiring a free item or for releasing an already acquired one.
The implementation is inspired by the one used in folly’s hazard pointer implementation,
originally ported into haphazard
. It consists of two linked lists implemented using a
single shared node type, where each node holds both a pointer to the next node in the overall
list, and a “skip pointer” to the next _available (non-acquired) node in the list. This enables
acquiring to be efficient and atomic.
Examples
Basic operation
use sento::Pool;
type Value = i32;
let pool = Pool::<Value>::new();
let v1 = pool.acquire(); // this will allocate a new Value with Value::default()
let v2 = pool.acquire(); // so will this
// we can get a long-lived shared reference to the value
let v1_ref = v1.into_ref();
// by releasing v1, it is now "free" and will be used rather than allocating a new Value.
// note, however, that v1 will not be deallocated until pool is dropped!
pool.release(v1);
let v1_again = pool.acquire();
// note that the semantics of acquire and release are up to you.
// .release does not require that you surrender the released reference,
// since the referenced value still lives in the same place.
assert_eq!(v1_ref as *const _, &*v1_again as *const _);
// when the Pool is dropped, it also frees all the nodes,
// so at that point you can't access the values any more.
drop(pool);