@@ -19,60 +19,48 @@ import SwiftExtensions
1919
2020/// `IndexDelegate` for the SourceKit workspace.
2121actor  SourceKitIndexDelegate :  IndexDelegate  { 
22- 
23-   let  queue =  AsyncQueue < Serial > ( ) 
24- 
2522  /// Registered `MainFilesDelegate`s to notify when main files change.
2623  var  mainFilesChangedCallbacks :  [ @Sendable   ( )  async  ->  Void ]  =  [ ] 
2724
2825  /// The count of pending unit events. Whenever this transitions to 0, it represents a time where
2926  /// the index finished processing known events. Of course, that may have already changed by the
3027  /// time we are notified.
31-   var  pendingUnitCount :   Int   =   0 
28+   let  pendingUnitCount   =   AtomicInt32 ( initialValue :   0 ) 
3229
3330  public  init ( )  { } 
3431
3532  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) 
4334  } 
4435
4536  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+       } 
5542    } 
5643
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+       } 
6152    } 
6253  } 
6354
64-   private  func  indexChanged( )  { 
55+   private  func  indexChanged( )  async   { 
6556    logger. debug ( " IndexStoreDB changed " ) 
6657    for  callback  in  mainFilesChangedCallbacks { 
67-       queue. async { 
68-         await  callback ( ) 
69-       } 
58+       await  callback ( ) 
7059    } 
7160  } 
7261
7362  /// Register a delegate to receive notifications when main files change.
7463  public  func  addMainFileChangedCallback( _ callback:  @escaping  @Sendable   ( )  async  ->  Void )  { 
7564    mainFilesChangedCallbacks. append ( callback) 
7665  } 
77- 
7866} 
0 commit comments