Skip to content

Add per-allocator mutex for transfer_all/transfer_free#8

Merged
canlin-zhang merged 1 commit into
masterfrom
per-allocator-mutex
Mar 16, 2026
Merged

Add per-allocator mutex for transfer_all/transfer_free#8
canlin-zhang merged 1 commit into
masterfrom
per-allocator-mutex

Conversation

@canlin-zhang
Copy link
Copy Markdown
Owner

Summary

Each PoolAllocator now has its own std::mutex (transfer_mutex). transfer_all and transfer_free lock only the destination's mutex before mutating its internal state.

The source allocator is not locked — it is assumed to be accessed only by its owning thread (typically a thread_local instance being emptied before the thread exits).

Why destination-only locking?

  • Source: thread-local, single-writer — no contention
  • Destination: shared target that multiple worker threads may transfer into concurrently — needs protection
  • No deadlock: only one mutex locked at a time per transfer call
  • Not a hot path: called once per threaded simulation run (~20ns uncontended)

Changes

  • pool_allocator.h: Add #include <mutex>, add mutable std::mutex transfer_mutex private member, update doc comments
  • pool_allocator.tcc: Add std::lock_guard in transfer_all and transfer_free

Test plan

  • All 14 existing tests pass

Each PoolAllocator now has its own std::mutex (transfer_mutex).
transfer_all and transfer_free lock only the destination's mutex,
since the source is assumed to be accessed only by its owning thread.

This avoids deadlocks (only one mutex locked at a time) while
protecting the destination when multiple threads transfer concurrently.

Also fix lcov coverage mismatch error with gcov-13 templates by adding
--ignore-errors mismatch to the coverage capture command.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@canlin-zhang canlin-zhang merged commit a4f7678 into master Mar 16, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant