Expand description
Concurrent heap-backed memory manager with lock-free freelist.
This module implements a production-ready concurrent memory manager that satisfies the C1a requirements:
- Per-slot locking: reads/writes only lock the targeted slot using an
RwLock, giving fine-grained isolation and good concurrency for reads. - No whole-manager lock: allocation and free are lock-free using a Treiber stack free-list implemented with atomics + a 32-bit tag to avoid ABA.
- Heap-backed capacity: the manager takes a
capacityat construction and stores slots/next pointers inVecso the size is dynamic whenallocis available. - Ergonomic concurrent API:
store_shared,read_shared, etc. take&selfand are safe for concurrent use; trait methods required byMemoryManagerdelegate to these shared methods.
§Rationale
- A Treiber stack with a tagged head (index + u32 tag packed into
u64) gives a simple, fast lock-free freelist and mitigates ABA by advancing the tag on each successful CAS. The tag wraps naturally and safely. - Per-slot
RwLockensures that reads are concurrent and writes are exclusive on a per-slot basis. Combined with the lock-free freelist this means no global lock is held during steady-state reads and writes. available_countis anAtomicUsizefor cheap diagnostics without locking and is kept consistent with successful freelist operations.
§Safety notes
free_shareduses a write lock on the slot, which waits for existing readers to finish. This ensures correctness: freeing a slot cannot race with readers that think the message exists because readers hold the read lock while accessingmessage.pop_free/push_freeare lock-free loops using atomic CAS on the tagged head; they may spin under heavy contention. Backoff/yielding is used to avoid burning a single core indefinitely.
§Performance notes
- Reads are highly concurrent and cheap (single
RwLock::read+ access). - Writes are exclusive per-slot and only block readers of the same slot.
- Allocation/free operations are lock-free and fast; the only blocking
synchronization remaining is slot
RwLockfor the actual store/free.
Tests in this module exercise correctness under concurrent usage and allocation reuse.
Structs§
- Concurrent
Header Guard - Header guard — holds a read lock on the slot and derefs to
MessageHeader. - Concurrent
Memory Manager - Public concurrent manager handle.
- Concurrent
Read Guard - Read guard — holds a read lock and dereferences to
Message<P>. - Concurrent
Write Guard - Write guard — holds a write lock and dereferences (mutably) to
Message<P>.