Skip to content

Commit 25a1b45

Browse files
Convert SourceKitIndexDelegate to be an actor (#1003)
1 parent b381811 commit 25a1b45

File tree

2 files changed

+29
-26
lines changed

2 files changed

+29
-26
lines changed

Sources/SourceKitLSP/SourceKitIndexDelegate.swift

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@
1313
import Dispatch
1414
import IndexStoreDB
1515
import SKCore
16+
import SKSupport
1617

1718
/// `IndexDelegate` for the SourceKit workspace.
1819
///
1920
/// *Public for testing*.
20-
public final class SourceKitIndexDelegate: IndexDelegate {
21+
public actor SourceKitIndexDelegate: IndexDelegate {
2122

22-
/// Provides mutual exclusion for other members.
23-
let queue: DispatchQueue = DispatchQueue(label: "\(SourceKitIndexDelegate.self)-queue")
23+
let queue = AsyncQueue<Serial>()
2424

2525
/// Registered `MainFilesDelegate`s to notify when main files change.
2626
var mainFilesDelegates: [MainFilesDelegate] = []
@@ -32,47 +32,50 @@ public final class SourceKitIndexDelegate: IndexDelegate {
3232

3333
public init() {}
3434

35-
public func processingAddedPending(_ count: Int) {
36-
queue.sync {
37-
pendingUnitCount += count
35+
nonisolated public func processingAddedPending(_ count: Int) {
36+
queue.async {
37+
await self.addPending(count)
3838
}
3939
}
4040

41-
public func processingCompleted(_ count: Int) {
42-
queue.sync {
43-
pendingUnitCount -= count
44-
if pendingUnitCount == 0 {
45-
_indexChanged()
46-
}
41+
private func addPending(_ count: Int) {
42+
pendingUnitCount += count
43+
}
4744

48-
if pendingUnitCount < 0 {
49-
assertionFailure("pendingUnitCount = \(pendingUnitCount) < 0")
50-
pendingUnitCount = 0
51-
_indexChanged()
52-
}
45+
nonisolated public func processingCompleted(_ count: Int) {
46+
queue.async {
47+
await self.processCompleted(count)
48+
}
49+
}
50+
51+
private func processCompleted(_ count: Int) {
52+
pendingUnitCount -= count
53+
if pendingUnitCount == 0 {
54+
_indexChanged()
55+
}
56+
57+
if pendingUnitCount < 0 {
58+
assertionFailure("pendingUnitCount = \(pendingUnitCount) < 0")
59+
pendingUnitCount = 0
60+
_indexChanged()
5361
}
5462
}
5563

56-
/// *Must be called on queue*.
5764
func _indexChanged() {
5865
for delegate in mainFilesDelegates {
59-
Task {
66+
queue.async {
6067
await delegate.mainFilesChanged()
6168
}
6269
}
6370
}
6471

6572
/// Register a delegate to receive notifications when main files change.
6673
public func registerMainFileChanged(_ delegate: MainFilesDelegate) {
67-
queue.sync {
68-
mainFilesDelegates.append(delegate)
69-
}
74+
mainFilesDelegates.append(delegate)
7075
}
7176

7277
/// Un-register a delegate to receive notifications when main files change.
7378
public func unregisterMainFileChanged(_ delegate: MainFilesDelegate) {
74-
queue.sync {
75-
mainFilesDelegates.removeAll(where: { $0 === delegate })
76-
}
79+
mainFilesDelegates.removeAll(where: { $0 === delegate })
7780
}
7881
}

Sources/SourceKitLSP/Workspace.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public final class Workspace {
8888
fallbackBuildSystem: FallbackBuildSystem(buildSetup: buildSetup),
8989
mainFilesProvider: index
9090
)
91-
indexDelegate?.registerMainFileChanged(buildSystemManager)
91+
await indexDelegate?.registerMainFileChanged(buildSystemManager)
9292
}
9393

9494
/// Creates a workspace for a given root `URL`, inferring the `ExternalWorkspace` if possible.

0 commit comments

Comments
 (0)