Skip to content

Commit 095ca6c

Browse files
committed
[sourcekitd] Switch to TSCBasic's Lock for improved block API
1 parent d1a997f commit 095ca6c

File tree

1 file changed

+12
-11
lines changed

1 file changed

+12
-11
lines changed

Sources/SourceKitD/SourceKitDImpl.swift

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,15 @@ public final class SourceKitDImpl: SourceKitD {
4040
public let values: sourcekitd_values
4141

4242
/// Lock protecting private state.
43-
let lock: NSLock = NSLock()
43+
let lock: Lock = Lock()
4444

4545
/// List of notification handlers that will be called for each notification.
4646
private var _notificationHandlers: [WeakSKDNotificationHandler] = []
4747

4848
var notificationHandlers: [SKDNotificationHandler] {
49-
lock.lock(); defer { lock.unlock() }
50-
return _notificationHandlers.compactMap { $0.value }
49+
lock.withLock {
50+
_notificationHandlers.compactMap { $0.value }
51+
}
5152
}
5253

5354
public init(dylib path: AbsolutePath) throws {
@@ -65,9 +66,7 @@ public final class SourceKitDImpl: SourceKitD {
6566
self.api.initialize()
6667
self.api.set_notification_handler { [weak self] rawResponse in
6768
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) }
7170

7271
let response = SKDResponse(rawResponse, sourcekitd: self)
7372
for handler in handlers {
@@ -85,15 +84,17 @@ public final class SourceKitDImpl: SourceKitD {
8584

8685
/// Adds a new notification handler (referenced weakly).
8786
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+
}
9191
}
9292

9393
/// Removes a previously registered notification handler.
9494
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+
}
9798
}
9899
}
99100

0 commit comments

Comments
 (0)