fixes for Swift 6 and threading concerns
This commit is contained in:
@@ -22,7 +22,20 @@ import LyrebirdSynth
|
||||
/// compile snippets, and the tracking lists needed for re-evaluation cleanup.
|
||||
/// Snippets receive a `SnippetContext` bound to this session through
|
||||
/// `compileAndRun(_:additive:)`.
|
||||
public final class LiveSession {
|
||||
///
|
||||
/// `@unchecked Sendable` (Phase 203): a `LiveSession` is shared between its `@MainActor`
|
||||
/// owner (`LyrebirdScratchModel`) and the `nonisolated` `compileAndRunAsync` it calls, so
|
||||
/// Swift 6 requires it to be `Sendable`. The conformance is sound by construction:
|
||||
///
|
||||
/// - `engine` and all `-I` / `-L` / `-l` / `rpath` configuration are immutable `let`.
|
||||
/// - The only mutable graph state — `trackedSynths` and `loadedSnippets` — is accessed
|
||||
/// exclusively under `trackingLock` (`track`, `clearTracked`, `trackedSynthCount`,
|
||||
/// `runEntry`).
|
||||
/// - `reportedSampleRate` / `lastSnippetMessage` are test-only side channels (`@testable`),
|
||||
/// written by the single in-flight snippet entry under the documented
|
||||
/// one-invocation-at-a-time precondition (see `compileAndRunAsync`) and read by tests
|
||||
/// after it completes — no concurrent access in practice.
|
||||
public final class LiveSession: @unchecked Sendable {
|
||||
|
||||
public let engine: SynthesisEngine
|
||||
|
||||
@@ -78,7 +91,12 @@ public final class LiveSession {
|
||||
/// (⌘⇧↩ semantics in the future `LyrebirdScratchView` editor).
|
||||
/// - Returns: The compiled snippet handle. Retained internally so the dylib
|
||||
/// stays mapped; callers can ignore it.
|
||||
/// `@MainActor` (Phase 203): runs the snippet entry — and thus the user body's
|
||||
/// `MainActor.assumeIsolated { … }` and any `context.play(_:)` → `engine.add` — on the
|
||||
/// main thread, synchronously (swiftc compile included). The async variant
|
||||
/// ``compileAndRunAsync(_:additive:)`` keeps the compile off-main.
|
||||
@discardableResult
|
||||
@MainActor
|
||||
public func compileAndRun(_ userBody: String, additive: Bool = false) throws -> CompiledSnippet {
|
||||
if !additive {
|
||||
clearTracked()
|
||||
@@ -89,11 +107,16 @@ public final class LiveSession {
|
||||
return snippet
|
||||
}
|
||||
|
||||
/// Asynchronous variant of ``compileAndRun(_:additive:)``. The swiftc
|
||||
/// compile runs on a background `Task.detached`; the snippet entry then
|
||||
/// executes on the caller's actor (typically the main actor, so snippets
|
||||
/// can touch UI-side state). Lets `LyrebirdScratchView`'s editor stay
|
||||
/// responsive during the compile.
|
||||
/// Asynchronous variant of ``compileAndRun(_:additive:)``. The swiftc compile runs on a
|
||||
/// background `Task.detached` so `LyrebirdScratchView`'s editor stays responsive; the
|
||||
/// engine-touching steps (`clearTracked()` and the snippet entry via `runEntry`) then run
|
||||
/// on the **main actor**.
|
||||
///
|
||||
/// Running the entry on the main actor matters (Phase 203): the snippet's
|
||||
/// `context.play(_:)` calls `engine.add(_:)`, which is `@MainActor` (Phase 201) and
|
||||
/// mutates `engine.nodeTreeMirror` — state observed by SwiftUI on the main thread. A
|
||||
/// `nonisolated async` method resumes on the generic executor, NOT the caller's actor, so
|
||||
/// the hop is explicit here rather than assumed.
|
||||
///
|
||||
/// - Precondition: only one in-flight invocation at a time per `LiveSession`.
|
||||
/// Concurrent calls can interleave `clearTracked()` with the prior call's
|
||||
@@ -108,14 +131,16 @@ public final class LiveSession {
|
||||
@discardableResult
|
||||
public func compileAndRunAsync(_ userBody: String, additive: Bool = false) async throws -> CompiledSnippet {
|
||||
if !additive {
|
||||
clearTracked()
|
||||
// `clearTracked()` calls `engine.remove` (touches `nodeTreeMirror`) — main only.
|
||||
await MainActor.run { clearTracked() }
|
||||
}
|
||||
let wrapped: WrappedSnippet = SnippetHarness.wrap(userBody: userBody)
|
||||
let args: [String] = makeSwiftcArgs()
|
||||
let snippet: CompiledSnippet = try await Task.detached(priority: .userInitiated) {
|
||||
try LiveSession.compileBlocking(wrapped: wrapped, extraArgs: args)
|
||||
}.value
|
||||
runEntry(of: snippet)
|
||||
// Snippet entry runs the user's `context.play(_:)` → `engine.add` (@MainActor).
|
||||
await MainActor.run { runEntry(of: snippet) }
|
||||
return snippet
|
||||
}
|
||||
|
||||
|
||||
@@ -37,7 +37,7 @@ public final class SnippetContext {
|
||||
/// - Returns: `true` if the engine accepted the enqueue; `false` if
|
||||
/// `engine.add` threw (e.g. cross-engine binding, double-add, queue full).
|
||||
@discardableResult
|
||||
public func play(_ synth: any Synthesizer) -> Bool {
|
||||
@MainActor public func play(_ synth: any Synthesizer) -> Bool {
|
||||
do {
|
||||
try session.engine.add(synth)
|
||||
session.track(synth)
|
||||
|
||||
@@ -17,6 +17,14 @@
|
||||
public enum SnippetHarness {
|
||||
|
||||
/// Wraps `userBody` in the standard preamble.
|
||||
///
|
||||
/// The user body runs inside `MainActor.assumeIsolated { … }` (Phase 203): snippets call
|
||||
/// `@MainActor` API — `context.play(_:)` and `SynthesizerBase` subclass construction
|
||||
/// (both `@MainActor` since Phase 201) — which would not compile from the nonisolated
|
||||
/// `@_cdecl` entry under Swift 6. The `assumeIsolated` closure gives the body main-actor
|
||||
/// isolation at compile time; it is safe at runtime because `LiveSession` always invokes
|
||||
/// the entry on the main actor (`compileAndRunAsync` hops via `MainActor.run`;
|
||||
/// `compileAndRun` is `@MainActor`).
|
||||
public static func wrap(userBody: String) -> WrappedSnippet {
|
||||
let preamble: String = """
|
||||
import LyrebirdSynth
|
||||
@@ -27,14 +35,18 @@ public enum SnippetHarness {
|
||||
guard let ctx else { return }
|
||||
let context = Unmanaged<SnippetContext>.fromOpaque(ctx).takeUnretainedValue()
|
||||
_ = context
|
||||
MainActor.assumeIsolated {
|
||||
|
||||
"""
|
||||
// Count newlines in the preamble: the user's body starts on the next line.
|
||||
// `components(separatedBy: "\n")` over a string ending in "\n" yields N+1
|
||||
// elements (the last is empty); offset = count of those elements = lines
|
||||
// before the user body + 1 (the line on which the body starts).
|
||||
// before the user body + 1 (the line on which the body starts). The
|
||||
// `MainActor.assumeIsolated {` line is part of the preamble, so the offset
|
||||
// already accounts for it and diagnostic line-mapping stays correct.
|
||||
let lineOffset: Int = preamble.components(separatedBy: "\n").count
|
||||
let suffix: String = "\n}\n"
|
||||
// Close the `assumeIsolated` closure, then the entry function.
|
||||
let suffix: String = "\n }\n}\n"
|
||||
let source: String = preamble + userBody + suffix
|
||||
return WrappedSnippet(source: source, userBodyLineOffset: lineOffset)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user