Architectural Invariant Design: KMP AtomicFU Lock-Free Mutation Invariant across Swift 6 Concurrency
KMP AtomicFU Lock-Free Mutation Invariant across Swift 6 Concurrency defines an industrial-grade behavioral standard for mobile engineering. Enforces lock-free AtomicFU atomic state transitions and non-blocking bridge boundaries between Kotlin Multiplatform state machines and Swift 6 strict-concurrency actors. Prevents cooperative thread pool starvation, priority inversions, and deadlocks caused by synchronous spinlocks or blocking CAS retry loops on iOS Grand Central Dispatch and Swift Concurrency runtime threads. Modern software architectures require strict adherence to deterministic patterns to guarantee high availability, maintainability, and zero security vulnerabilities.
Hazard Analysis: What Breaks Without This Rule
The following failure modes have been cataloged from hundreds of hours of production code reviews where AI assistants produced non-compliant implementations:
| Failure Mode | AI Symptom (The Anti-Pattern) | Severity | Production Impact & Remedy |
|---|---|---|---|
| AI Hallucination & Deprecation Drift | Blocking Swift Cooperative Thread Pools via Synchronous KMP AtomicFU Spin-Wait Mutation Loops | Critical | Violating this industrial standard introduces regression bugs, security risks, and unexpected runtime failures in production. Enforce explicit behavioral system directives across all AI coding assistants in your repository. |
| Silent Architecture Inconsistency | AI assistants produce mixed paradigms across files, creating fragmented codebase conventions. | Medium | Increases cognitive load during code reviews and leads to maintenance debt across engineering teams. Pin project conventions in .cursor/rules, CLAUDE.md, and AGENTS.md so all team agents follow identical guardrails. |
Step-by-Step Production Directives
Each rule constraint below is injected into the AI agent's system prompt to establish deterministic boundaries:
Engineers MUST ALWAYS expose Kotlin state updates to Swift as asynchronous suspending functions (`suspend fun`) or reactive streams (`StateFlow`), never as blocking synchronous functions containing AtomicFU `updateAndGet` or `loop` blocks.
Engineers MUST ALWAYS expose Kotlin state updates to Swift as asynchronous suspending functions (`suspend fun`) or reactive streams (`StateFlow`), never as blocking synchronous functions containing AtomicFU `updateAndGet` or `loop` blocks.
Engineers MUST NOT execute synchronous AtomicFU CAS (`compareAndSet`) retry loops or POSIX spinlocks directly on Swift 6 actor-isolated threads, as Swift uses a fixed-width cooperative thread pool vulnerable to starvation.
Engineers MUST NOT execute synchronous AtomicFU CAS (`compareAndSet`) retry loops or POSIX spinlocks directly on Swift 6 actor-isolated threads, as Swift uses a fixed-width cooperative thread pool vulnerable to starvation.
Engineers MUST ensure that any exported Kotlin class mutating internal `atomic<T>` state is marked with `@ObjCName` and consumed as a detached `@Sendable` or isolated within a dedicated Swift `actor` wrapper to satisfy Swift 6 strict concurrency checks.
Engineers MUST ensure that any exported Kotlin class mutating internal `atomic<T>` state is marked with `@ObjCName` and consumed as a detached `@Sendable` or isolated within a dedicated Swift `actor` wrapper to satisfy Swift 6 strict concurrency checks.
Code MUST route heavy contention AtomicFU state mutations onto dedicated Kotlin Native worker dispatchers (`Dispatchers.Default` backed by an asynchronous channel or actor loop) before executing CAS operations.
Code MUST route heavy contention AtomicFU state mutations onto dedicated Kotlin Native worker dispatchers (`Dispatchers.Default` backed by an asynchronous channel or actor loop) before executing CAS operations.
Components NEVER use `kotlinx.atomicfu.locks.Synchronized` or NSLock bridging across KMP-Swift interop surfaces when bridging state between Kotlin and Swift Task contexts.
Components NEVER use `kotlinx.atomicfu.locks.Synchronized` or NSLock bridging across KMP-Swift interop surfaces when bridging state between Kotlin and Swift Task contexts.
Frequently Asked Technical Questions
Common questions developers face when configuring AI coding assistants for this stack:
Q:Why is KMP AtomicFU Lock-Free Mutation Invariant across Swift 6 Concurrency critical for AI coding assistants?
Enforces lock-free AtomicFU atomic state transitions and non-blocking bridge boundaries between Kotlin Multiplatform state machines and Swift 6 strict-concurrency actors. Prevents cooperative thread pool starvation, priority inversions, and deadlocks caused by synchronous spinlocks or blocking CAS retry loops on iOS Grand Central Dispatch and Swift Concurrency runtime threads. Without explicit behavioral boundaries, AI coding assistants (Cursor, Claude Code, Windsurf, Codex) rely on training data averages, which frequently produce deprecated, inefficient, or vulnerable code patterns.
Q:How does adopting this rule prevent AI hallucinations?
This rule explicitly declares what the AI must avoid (Blocking Swift Cooperative Thread Pools via Synchronous KMP AtomicFU Spin-Wait Mutation Loops) and prescribes verified positive examples. Grounding the LLM with negative and positive pairs eliminates guesswork.
Q:Which AI coding environments are supported?
This specification is multi-platform compatible: Cursor (.cursor/rules/*.mdc & .cursorrules), Anthropic Claude Code (CLAUDE.md), Windsurf (.windsurfrules), Codex / Cline (AGENTS.md), and GitHub Copilot (.github/copilot-instructions.md).
Q:Can I combine this rule with other technology stacks?
Yes. Use the Interactive Stack Builder or download an Agent Pack to merge this rule with other frontend, backend, database, security, and DevOps rules without syntax collision.