fixes for Swift 6 and threading concerns
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
// MainActorMutationBridge.swift
|
||||
// Lyrebird
|
||||
//
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import Foundation
|
||||
|
||||
/// Run a `@MainActor` node-tree mutation from a language-side dispatch adapter that may
|
||||
/// execute either on the main thread or inline on a background NRT driver thread.
|
||||
///
|
||||
/// Phase 201 made the engine's add / schedule path and `Synthesizer.build`/`release`/`free`
|
||||
/// `@MainActor`. Most language-side adapters dispatch those mutations through
|
||||
/// `DispatchQueue.main.async`, so they already run on main. Two adapters can run a mutation
|
||||
/// **inline on the calling thread**:
|
||||
///
|
||||
/// - `LyrebirdMessageContext(nrtEngine:)` — the NRT inline dispatcher (`_dispatch = { $0() }`)
|
||||
/// used by `TriggerListener._drainSync`. An NRT render driven from a background thread
|
||||
/// would invoke the mutation off-main.
|
||||
/// - `LyrebirdAudioUnit.allocateRenderResources()` — overrides a nonisolated `AUAudioUnit`
|
||||
/// method; AU hosts *usually* call it on main, but the contract does not guarantee it.
|
||||
///
|
||||
/// A bare `MainActor.assumeIsolated` would **trap** (`dispatch_assert_queue_fail`) on those
|
||||
/// off-main paths. This bridge instead:
|
||||
///
|
||||
/// - runs `body` inline via `assumeIsolated` when already on the main thread (the common
|
||||
/// case — zero hop), and
|
||||
/// - hops synchronously to main otherwise. NRT is offline (not the RT audio thread), so a
|
||||
/// synchronous main hop is acceptable; it never runs on the real-time render callback,
|
||||
/// which uses the default `DispatchQueue.main.async` dispatcher and never reaches here.
|
||||
///
|
||||
/// `body` is intentionally **not** `@Sendable` so it can capture the non-`Sendable` engine /
|
||||
/// synthesizer it mutates; `DispatchQueue.sync` does not escape the closure, so this is safe.
|
||||
/// `body` returns `Void`: a non-`Sendable` result would have to cross back from the main
|
||||
/// thread on the off-main hop, which `DispatchQueue.sync` rejects. All node-tree mutations
|
||||
/// here are fire-and-forget, so `Void` is sufficient and keeps the signature simple.
|
||||
@inline(__always)
|
||||
internal func lyrebirdRunMainActorMutation(_ body: @MainActor () throws -> Void) rethrows {
|
||||
// NOTE: no `_lyrebirdAssertNotOnRTThread()` here. `RTThreadIdentity` is a thread-local
|
||||
// marker that persists on pooled/reused threads (and AU drivers may mark the allocate
|
||||
// thread), so an assert false-positives on legitimate off-main callers like AUv3
|
||||
// `allocateRenderResources`. RT-path purity is instead enforced statically by the
|
||||
// `generatorSourcesContainNoActorHopMachinery` source lint (Phase201IsolationGuardTests).
|
||||
if Thread.isMainThread {
|
||||
try MainActor.assumeIsolated(body)
|
||||
return
|
||||
}
|
||||
try DispatchQueue.main.sync { try MainActor.assumeIsolated(body) }
|
||||
}
|
||||
Reference in New Issue
Block a user