Skip to content

Commit

Permalink
Fix concurrency warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
wtholliday committed May 27, 2023
1 parent f6df347 commit 78fe8e8
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 25 deletions.
2 changes: 1 addition & 1 deletion Sources/Audio/Internals/Engine/EngineAudioUnit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ public class EngineAudioUnit: AUAudioUnit {
}

// Add render jobs for taps.
for tap in Tap.getTapsFor(node: node) {
for tap in TapRegistry.shared.getTapsFor(node: node) {

// We don't actually care about this output buffer. Perhaps
// there's a better way to express this?
Expand Down
25 changes: 1 addition & 24 deletions Sources/Audio/Internals/Engine/Tap.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,6 @@ public class Tap {

var task: Task<Void, Error>? = nil

struct WeakTap {
weak var tap: Tap?

init(tap: Tap?) {
self.tap = tap
}
}

static var tapRegistryLock = NSLock()
static var tapRegistry: [ObjectIdentifier: [WeakTap]] = [:]

static func getTapsFor(node: Node) -> [Tap] {
tapRegistryLock.withLock {
(Self.tapRegistry[ObjectIdentifier(node)] ?? []).compactMap { $0.tap }
}
}

public init(_ input: Node, bufferSize: Int = 1024, tapBlock: @escaping ([Float], [Float]) async -> Void) {

let componentDescription = AudioComponentDescription(effect: "tap2")
Expand Down Expand Up @@ -91,13 +74,7 @@ public class Tap {
}
}

Self.tapRegistryLock.withLock {
if Self.tapRegistry.keys.contains(ObjectIdentifier(input)) {
Self.tapRegistry[ObjectIdentifier(input)]?.append(WeakTap(tap: self))
} else {
Self.tapRegistry[ObjectIdentifier(input)] = [WeakTap(tap: self)]
}
}
TapRegistry.shared.add(tap: self, for: input)

// Trigger a recompile if input already has an associated engine.
if let engineAU = NodeEnginesManager.shared.getEngine(for: input) {
Expand Down
36 changes: 36 additions & 0 deletions Sources/Audio/Internals/Engine/TapRegistry.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright AudioKit. All Rights Reserved. Revision History at http://github.com/AudioKit/AudioKit/

import Foundation

class TapRegistry: @unchecked Sendable {

struct WeakTap {
weak var tap: Tap?

init(tap: Tap?) {
self.tap = tap
}
}

let tapRegistryLock = NSLock()

var tapRegistry: [ObjectIdentifier: [WeakTap]] = [:]

func getTapsFor(node: Node) -> [Tap] {
tapRegistryLock.withLock {
(tapRegistry[ObjectIdentifier(node)] ?? []).compactMap { $0.tap }
}
}

func add(tap: Tap, for node: Node) {
tapRegistryLock.withLock {
if tapRegistry.keys.contains(ObjectIdentifier(node)) {
tapRegistry[ObjectIdentifier(node)]?.append(WeakTap(tap: tap))
} else {
tapRegistry[ObjectIdentifier(node)] = [WeakTap(tap: tap)]
}
}
}

static let shared = TapRegistry()
}

0 comments on commit 78fe8e8

Please sign in to comment.