std/atomics: lock-free for any 1/2/4/8/16-byte type, not just Trivial #25363
+783
−684
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Changes
std/atomicsto determine lock-free eligibility by size,supportsCopyMem, architecture, and memory management rather than by type.Previously, only
Trivialtypes (SomeNumber | bool | enum | ptr | pointer) could use lock-free atomics. Any other type (small objects, char, etc) would fall back to spin-lock even though it could fit in a register.Now, any type with
sizeof(T)of 1, 2, 4, 8, or 16 bytes (with architecture support) and no managed memory (under ARC/ORC) uses lock-free atomics.Dependencies
This PR depends on #25374 which fixes a compiler bug that blocks using
when isLockFree(T)in theAtomic[T]body.Changes
Trivialtype constraint with publicisLockFree(T)templatehasLockFree8constant for 8-byte atomic supporthasLockFree16constant for 16-byte atomic supportInt128) for 16-byte lock-free operations-d:nimEnforceLockFreeAtomicsto error instead of silently falling back to spinlock-d:nimNoLockFree16to disable 16-byte lock-free for old x86-64 CPUsArchitecture Support
8-byte atomics (
hasLockFree8)16-byte atomics (
hasLockFree16)Note: Use
-d:nimNoLockFree16if targeting very old x86-64 CPUs (pre-2005) that lack CMPXCHG16B.Lock-free eligibility by type and memory manager
* Requires
hasLockFree8** Requires
hasLockFree16