Skip to content

Instantly share code, notes, and snippets.

@swhitty
swhitty / Mutex.swift
Last active June 3, 2025 03:23
Backports the Swift 6 type Mutex<Value> to all Apple platforms that support OSAllocatedUnfairLock
// Backports the Swift 6 type Mutex<Value> to all Darwin platforms via OSAllocatedUnfairLock.
// Lightweight version of https://github.com/swhitty/swift-mutex
// Feel free to use any part of this gist.
import struct os.OSAllocatedUnfairLock
// Backports the Swift 6.0 Mutex API
@available(iOS, introduced: 16.0, deprecated: 18.0, message: "use Mutex from Synchronization module")
@available(macOS, introduced: 13.0, deprecated: 15.0, message: "use Mutex from Synchronization module")
public struct Mutex<Value: ~Copyable>: @unchecked Sendable, ~Copyable {