Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Mutex, Atomics & Beyond

Avatar for Nicolás García Nicolás García
April 25, 2025
6

Mutex, Atomics & Beyond

Mutex, Atomics & Beyond
24/04/2025
CocoaHeads Nantes

Avatar for Nicolás García

Nicolás García

April 25, 2025
Tweet

Transcript

  1. class SomeClass { private var queue = DispatchQueue(…) private var

    _someVariable = true var someVariable: Bool { get { queue.sync { _someVariable } } set { queue.sync { self._someVariable = newValue } } } }
  2. class SomeClass { // serialisation but at the cost of

    a new thread dispatch private var queue = DispatchQueue(…) private var _someVariable = true var someVariable: Bool { get { queue.sync { _someVariable } } set { queue.sync { self._someVariable = newValue } } } }
  3. // Swift Concurrency error: SomeClass is not Sendable class SomeClass

    { private var queue = DispatchQueue(…) private var _someVariable = true var someVariable: Bool { get { queue.sync { _someVariable } } set { queue.sync { self._someVariable = newValue } } } }
  4. fi nal class SomeClass: @unchecked Sendable { private var queue

    = DispatchQueue(…) private var _someVariable = true var someVariable: Bool { get { queue.sync { _someVariable } } set { queue.sync { self._someVariable = newValue } } } }
  5. fi nal class SomeClass: @unchecked Sendable { // trust me

    bro, its safe… private var queue = DispatchQueue(…) private var _someVariable = true var someVariable: Bool { get { queue.sync { _someVariable } } set { queue.sync { self._someVariable = newValue } } } }
  6. actor SomeActor { private var someVariable = true
 func getSomeVariable()

    -> Bool { someVariable } func setSomeVariable(_ newValue: Bool) { someVariable = newValue } }
  7. actor SomeActor { private var someVariable = true
 func getSomeVariable()

    -> Bool { someVariable } func setSomeVariable(_ newValue: Bool) { someVariable = newValue } }
 
 Serialization + isolation + Sendable !
  8. let actor = SomeActor() Task { let current = await

    actor.getSomeVariable() await actor.setSomeVariable(false) } 
 
 But a bit awkward for quick UI toggles or sync-style logic…
  9. fi nal class SomeClass { private var _someVariable = true

    private let lock = NSLock() var someVariable: Bool { get { lock.lock(); defer { lock.unlock() } // Don’t forget to unlock or you’ll risk a deadlock, otherwise -> NSRecursiveLock return _someVariable } set { lock.lock(); _someVariable = newValue lock.unlock() // unlocking in a di ff erent thread may cause unde fi ned behaviour } } }
  10. import OSLock fi nal class SomeClass { private var _someVariable

    = true private let lock = OSAllocatedUnfairLock() var someVariable: Bool { get { lock.withLock { _someVariable } } set { lock.withLock { _someVariable = newValue } } } }
  11. WWDC 2024 New Synchronization module announced at the end of

    “What’s new in Swift” (around 28:45) • https://developer.apple.com/videos/ play/wwdc2024/10136 • Low level primitives: Mutex and Atomics • SE-0433 https://github.com/swiftlang/ swift-evolution/blob/main/proposals/ 0433-mutex.md
  12. import Synchronization Key points • Mutex and Atomics low level

    primitives • iOS 18 + • New keywords: ~Copyable, consuming, borrowing…
  13. Non copyable types Ownership • Available since Swift 5.9 •

    It removes the copying capability of a type • “~” its called a negative constraint • Enforces strict ownership and move semantics • Borrowing • Temporarily gives access to a non copyable resource without transferring ownership • Consuming • Transfers ownership, leaving the original reference invalid • Bene fi ts • Prevention: common errors and unnecessary duplication • Performance: reducing the overhead of eventual copies • Expressiveness: more control over low memory management
  14. Tool Era Concurrency Ready? Sendable? Swift Integration Global var 2000s

    ❌ ❌ ❌ Class ❌ ❌ ✅ DispatchQueue GCD ✅ (kind of) ❌ ✅ @unchecked Sendable Hack? ✅ ✅ (ish) 😬 Actor 2021 ✅ ✅ ✅✅ NSLock Legacy ✅ ❌ ❌ os_unfair_lock C-level ✅ ❌ ❌ OSAllocatedUnfairLock iOS 16+ ✅ ❌ ✅ Synchronization.Mutex iOS 18+ ✅ ✅ ✅✅✅ Thread-safety in Swift
  15. Who am I Freelance iOS & macOS Developer • Back

    to early 2013 • A fi rst experience with Eclipse IDE and Android SDK • Xcode 4.6 and an iPhone 4 • I’m sorry for those of you coming from the pre-ARC era 🦕 • Worked on IoT, video, banking and Cloud B2B and B2C apps