@@ -19,60 +19,48 @@ import SwiftExtensions
19
19
20
20
/// `IndexDelegate` for the SourceKit workspace.
21
21
actor SourceKitIndexDelegate : IndexDelegate {
22
-
23
- let queue = AsyncQueue < Serial > ( )
24
-
25
22
/// Registered `MainFilesDelegate`s to notify when main files change.
26
23
var mainFilesChangedCallbacks : [ @Sendable ( ) async -> Void ] = [ ]
27
24
28
25
/// The count of pending unit events. Whenever this transitions to 0, it represents a time where
29
26
/// the index finished processing known events. Of course, that may have already changed by the
30
27
/// time we are notified.
31
- var pendingUnitCount : Int = 0
28
+ let pendingUnitCount = AtomicInt32 ( initialValue : 0 )
32
29
33
30
public init ( ) { }
34
31
35
32
nonisolated public func processingAddedPending( _ count: Int ) {
36
- queue. async {
37
- await self . addPending ( count)
38
- }
39
- }
40
-
41
- private func addPending( _ count: Int ) {
42
- pendingUnitCount += count
33
+ pendingUnitCount. value += Int32 ( count)
43
34
}
44
35
45
36
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 ( )
37
+ pendingUnitCount. value -= Int32 ( count)
38
+ if pendingUnitCount. value == 0 {
39
+ Task {
40
+ await indexChanged ( )
41
+ }
55
42
}
56
43
57
- if pendingUnitCount < 0 {
58
- assertionFailure ( " pendingUnitCount = \( pendingUnitCount) < 0 " )
59
- pendingUnitCount = 0
60
- indexChanged ( )
44
+ if pendingUnitCount. value < 0 {
45
+ // Technically this is not data race safe because `pendingUnitCount` might change between the check and us setting
46
+ // it to 0. But then, this should never happen anyway, so it's fine.
47
+ logger. fault ( " pendingUnitCount dropped below zero: \( self . pendingUnitCount. value) " )
48
+ pendingUnitCount. value = 0
49
+ Task {
50
+ await indexChanged ( )
51
+ }
61
52
}
62
53
}
63
54
64
- private func indexChanged( ) {
55
+ private func indexChanged( ) async {
65
56
logger. debug ( " IndexStoreDB changed " )
66
57
for callback in mainFilesChangedCallbacks {
67
- queue. async {
68
- await callback ( )
69
- }
58
+ await callback ( )
70
59
}
71
60
}
72
61
73
62
/// Register a delegate to receive notifications when main files change.
74
63
public func addMainFileChangedCallback( _ callback: @escaping @Sendable ( ) async -> Void ) {
75
64
mainFilesChangedCallbacks. append ( callback)
76
65
}
77
-
78
66
}
0 commit comments