@@ -40,14 +40,15 @@ public final class SourceKitDImpl: SourceKitD {
40
40
public let values : sourcekitd_values
41
41
42
42
/// Lock protecting private state.
43
- let lock : NSLock = NSLock ( )
43
+ let lock : Lock = Lock ( )
44
44
45
45
/// List of notification handlers that will be called for each notification.
46
46
private var _notificationHandlers : [ WeakSKDNotificationHandler ] = [ ]
47
47
48
48
var notificationHandlers : [ SKDNotificationHandler ] {
49
- lock. lock ( ) ; defer { lock. unlock ( ) }
50
- return _notificationHandlers. compactMap { $0. value }
49
+ lock. withLock {
50
+ _notificationHandlers. compactMap { $0. value }
51
+ }
51
52
}
52
53
53
54
public init ( dylib path: AbsolutePath ) throws {
@@ -65,9 +66,7 @@ public final class SourceKitDImpl: SourceKitD {
65
66
self . api. initialize ( )
66
67
self . api. set_notification_handler { [ weak self] rawResponse in
67
68
guard let self = self else { return }
68
- self . lock. lock ( )
69
- let handlers = self . _notificationHandlers. compactMap ( \. value)
70
- self . lock. unlock ( )
69
+ let handlers = self . lock. withLock { self . _notificationHandlers. compactMap ( \. value) }
71
70
72
71
let response = SKDResponse ( rawResponse, sourcekitd: self )
73
72
for handler in handlers {
@@ -85,15 +84,17 @@ public final class SourceKitDImpl: SourceKitD {
85
84
86
85
/// Adds a new notification handler (referenced weakly).
87
86
public func addNotificationHandler( _ handler: SKDNotificationHandler ) {
88
- lock. lock ( ) ; defer { lock. unlock ( ) }
89
- _notificationHandlers. removeAll ( where: { $0. value == nil } )
90
- _notificationHandlers. append ( . init( handler) )
87
+ lock. withLock {
88
+ _notificationHandlers. removeAll ( where: { $0. value == nil } )
89
+ _notificationHandlers. append ( . init( handler) )
90
+ }
91
91
}
92
92
93
93
/// Removes a previously registered notification handler.
94
94
public func removeNotificationHandler( _ handler: SKDNotificationHandler ) {
95
- lock. lock ( ) ; defer { lock. unlock ( ) }
96
- _notificationHandlers. removeAll ( where: { $0. value == nil || $0. value === handler} )
95
+ lock. withLock {
96
+ _notificationHandlers. removeAll ( where: { $0. value == nil || $0. value === handler} )
97
+ }
97
98
}
98
99
}
99
100
0 commit comments