Skip to main content

Module concurrent_manager

Module concurrent_manager 

Source
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 capacity at construction and stores slots/next pointers in Vec so the size is dynamic when alloc is available.
  • Ergonomic concurrent API: store_shared, read_shared, etc. take &self and are safe for concurrent use; trait methods required by MemoryManager delegate 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 RwLock ensures 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_count is an AtomicUsize for cheap diagnostics without locking and is kept consistent with successful freelist operations.

§Safety notes

  • free_shared uses 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 accessing message.
  • pop_free / push_free are 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 RwLock for the actual store/free.

Tests in this module exercise correctness under concurrent usage and allocation reuse.

Structs§

ConcurrentHeaderGuard
Header guard — holds a read lock on the slot and derefs to MessageHeader.
ConcurrentMemoryManager
Public concurrent manager handle.
ConcurrentReadGuard
Read guard — holds a read lock and dereferences to Message<P>.
ConcurrentWriteGuard
Write guard — holds a write lock and dereferences (mutably) to Message<P>.