Skip to content

Adopt Atomic.#1599

Merged
grynspan merged 5 commits intomainfrom
jgrynspan/adopt-atomic
Mar 2, 2026
Merged

Adopt Atomic.#1599
grynspan merged 5 commits intomainfrom
jgrynspan/adopt-atomic

Conversation

@grynspan
Copy link
Contributor

@grynspan grynspan commented Mar 1, 2026

This PR moves us from Mutex to Atomic for scalar integer values that don't need a full mutex guarding them.

On Darwin, we cannot use types from the Synchronization module because we need to back-deploy earlier than they are available, so we simulate a subset of the Atomic API using C atomic intrinsics provided by clang and bubbled up through Stubs.h. Unlike the real Atomic type, we heap-allocate our storage, but we're heap-allocating the current mutex so that isn't a performance regression.

I have only implemented the set of functionality we're actually using; adding more functionality isn't hard, but is beyond the scope of this PR. In particular, I have not attempted to implement/use any atomic orderings other than .sequentiallyConsistent. We can revise orderings later on a case-by-case basis.

Checklist:

  • Code and documentation should follow the style of the Style Guide.
  • If public symbols are renamed or modified, DocC references should be updated.

This PR moves us from `Mutex` to `Atomic` for scalar integer values that don't need a full mutex guarding them.

On Darwin, we cannot use types from the `Synchronization` module because we need to back-deploy earlier than they are available, so we simulate a subset of the `Atomic` API using a mutex. This is not a performance regression on Darwin since we're already using a mutex there anyway.

For this initial PR, I have not attempted to use any atomic orderings other than `.sequentiallyConsistent`. We can revise later on a case-by-case basis.
@grynspan grynspan added this to the Swift 6.4.0 (main) milestone Mar 1, 2026
@grynspan grynspan self-assigned this Mar 1, 2026
@grynspan grynspan added enhancement New feature or request concurrency 🔀 Swift concurrency/sendability issues performance 🏎️ Performance issues tech-debt 💾 reduces technical debt labels Mar 1, 2026
@grynspan grynspan merged commit 95d9871 into main Mar 2, 2026
50 of 52 checks passed
@grynspan grynspan deleted the jgrynspan/adopt-atomic branch March 2, 2026 19:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

concurrency 🔀 Swift concurrency/sendability issues enhancement New feature or request performance 🏎️ Performance issues tech-debt 💾 reduces technical debt

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants