apple / swift-migration-guide

Apache License 2.0
290 stars 21 forks source link

Manual Synchronization should show modern locking patterns with Sendable #62

Open ktoso opened 3 months ago

ktoso commented 3 months ago

We have developed some patterns to limit the "just sprinkle @unchecked Sendable on it" on codebases.

We should showcase some of our recent locks:


Maybe we should take the chance to show good lock patterns here.

Like for example https://github.com/apple/swift-nio/blob/main/Sources/NIOConcurrencyHelpers/NIOLockedValueBox.swift which is just:


public struct NIOLockedValueBox<Value> {

    public init(_ value: Value)

    public func withLockedValue<T>(_ mutate: (inout Value) throws -> T) rethrows -> T
}

extension NIOLockedValueBox: Sendable where Value: Sendable {}

Or also the new Mutex type -- we can ask https://github.com/swiftlang/swift-evolution/blob/main/proposals/0433-mutex.md @Azoy to provide a small snippet

FranzBusch commented 3 months ago

I agree. If using new new Mutex most types don't even have to use @unchecked Sendable since Mutex is unconditionally Sendable. @unchecked Sendable is required with atomics though.

mattmassicotte commented 3 months ago

I think this would be excellent content, and might have a nice home in some kind of "Design Guidelines" article. That's come up many times.

ktoso commented 3 months ago

And we can mention I think we mention OSAllocatedUnfairLock as well