Created
July 8, 2024 15:54
-
-
Save samsonjs/e25b6ad7939e4e6b11a2cdb6943fae2f to your computer and use it in GitHub Desktop.
Attempting to send non-Sendable AVFoundation types in a safe way
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public import AVFoundation | |
struct UniqueRef<Value>: ~Copyable, @unchecked Sendable { | |
private let lock = NSLock() | |
private var unsafeValue: Value? | |
init(value: sending Value) { | |
self.unsafeValue = value | |
} | |
mutating func take() -> sending Value? { | |
lock.withLock { | |
defer { unsafeValue = nil } | |
return unsafeValue | |
} | |
} | |
} | |
public struct Settings: ~Copyable, Sendable { | |
private var uniqueMix: UniqueRef<AVAudioMix>? | |
public init(mix: sending AVAudioMix?) { | |
self.uniqueMix = mix.map(UniqueRef.init) | |
} | |
mutating func takeMix() -> sending AVAudioMix? { | |
uniqueMix?.take() | |
} | |
} | |
public final class ExampleExportSession: Sendable { | |
func export(settings: consuming sending Settings) async throws { | |
let writer = ExampleWriter(mix: settings.takeMix()) | |
try await writer.write() | |
} | |
} | |
actor ExampleWriter { | |
let mix: AVAudioMix? | |
init(mix: sending AVAudioMix?) { | |
self.mix = mix | |
} | |
func write() async throws {} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@mattmassicotte That does work but I really do want to pass it in settings to keep the number of parameters down. The real class has a bunch of inputs and I want to make a nicer API for calling it. Here's a bit more of the real code to illustrate what's going on, and you can ignore a lot of
AudioOutputSettings
.I was passing the audio mix around separately instead of shoving it into
AudioOutputSettings
, but it's just a lot of params on the export method. I marked the example with a ⭐️