Skip to content

Add bn::base::shared_mutex, a compact reader/writer lock - #8480

Open
bdash wants to merge 1 commit into
test_compact_mutex_win_linuxfrom
test_compact_shared_mutex_mac_linux
Open

Add bn::base::shared_mutex, a compact reader/writer lock#8480
bdash wants to merge 1 commit into
test_compact_mutex_win_linuxfrom
test_compact_shared_mutex_mac_linux

Conversation

@bdash

@bdash bdash commented Aug 27, 2026

Copy link
Copy Markdown
Contributor

For MSVC this is an alias for std::shared_mutex, which is already an 8-byte wrapper around SRWLOCK.

On macOS and Linux this is implemented via a 4-byte atomic that packs writer, writer-pending, contended, and reader count. Blocking is handled via platform_wait. A queued writer gates new readers so that writers are not starved by heavy read traffic.

std::shared_mutex is 168 bytes on macOS with libc++ and 56 bytes on Linux with libstdc++, so this new implementation is significantly more compact than it. Performance is comparable on both platforms.

For MSVC this is an alias for `std::shared_mutex`, which is already an
8-byte wrapper around `SRWLOCK`.

On macOS and Linux this is implemented via a 4-byte atomic that packs
writer, writer-pending, contended, and reader count. Blocking is handled
via `platform_wait`. A queued writer gates new readers so that writers
are not starved by heavy read traffic.

`std::shared_mutex` is 168 bytes on macOS with libc++ and 56 bytes on
Linux with libstdc++, so this new implementation is significantly more
compact than it. Performance is comparable on both platforms.
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