@@ -18,60 +18,48 @@ import SwiftExtensions
1818
1919/// `IndexDelegate` for the SourceKit workspace.
2020actor SourceKitIndexDelegate : IndexDelegate {
21-
22- let queue = AsyncQueue < Serial > ( )
23-
2421 /// Registered `MainFilesDelegate`s to notify when main files change.
2522 var mainFilesChangedCallbacks : [ @Sendable ( ) async -> Void ] = [ ]
2623
2724 /// The count of pending unit events. Whenever this transitions to 0, it represents a time where
2825 /// the index finished processing known events. Of course, that may have already changed by the
2926 /// time we are notified.
30- var pendingUnitCount : Int = 0
27+ let pendingUnitCount = AtomicInt32 ( initialValue : 0 )
3128
3229 package init ( ) { }
3330
3431 nonisolated package func processingAddedPending( _ count: Int ) {
35- queue. async {
36- await self . addPending ( count)
37- }
38- }
39-
40- private func addPending( _ count: Int ) {
41- pendingUnitCount += count
32+ pendingUnitCount. value += Int32 ( count)
4233 }
4334
4435 nonisolated package func processingCompleted( _ count: Int ) {
45- queue. async {
46- await self . processCompleted ( count)
47- }
48- }
49-
50- private func processCompleted( _ count: Int ) {
51- pendingUnitCount -= count
52- if pendingUnitCount == 0 {
53- indexChanged ( )
36+ pendingUnitCount. value -= Int32 ( count)
37+ if pendingUnitCount. value == 0 {
38+ Task {
39+ await indexChanged ( )
40+ }
5441 }
5542
56- if pendingUnitCount < 0 {
57- assertionFailure ( " pendingUnitCount = \( pendingUnitCount) < 0 " )
58- pendingUnitCount = 0
59- indexChanged ( )
43+ if pendingUnitCount. value < 0 {
44+ // Technically this is not data race safe because `pendingUnitCount` might change between the check and us setting
45+ // it to 0. But then, this should never happen anyway, so it's fine.
46+ logger. fault ( " pendingUnitCount dropped below zero: \( self . pendingUnitCount. value) " )
47+ pendingUnitCount. value = 0
48+ Task {
49+ await indexChanged ( )
50+ }
6051 }
6152 }
6253
63- private func indexChanged( ) {
54+ private func indexChanged( ) async {
6455 logger. debug ( " IndexStoreDB changed " )
6556 for callback in mainFilesChangedCallbacks {
66- queue. async {
67- await callback ( )
68- }
57+ await callback ( )
6958 }
7059 }
7160
7261 /// Register a delegate to receive notifications when main files change.
7362 package func addMainFileChangedCallback( _ callback: @escaping @Sendable ( ) async -> Void ) {
7463 mainFilesChangedCallbacks. append ( callback)
7564 }
76-
7765}
0 commit comments