Skip to content

Commit e5d46a3

Browse files
committed
Use an atomic for notificationIDForLogging
This allows us to get rid of the lock.
1 parent 765a049 commit e5d46a3

File tree

3 files changed

+32
-14
lines changed

3 files changed

+32
-14
lines changed

Package.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,7 @@ let package = Package(
312312
name: "SourceKitLSP",
313313
dependencies: [
314314
"BuildServerProtocol",
315+
"CAtomics",
315316
"LanguageServerProtocol",
316317
"LanguageServerProtocolJSONRPC",
317318
"LSPLogging",

Sources/CAtomics/include/CAtomics.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,32 @@ static inline void atomic_uint8_set(AtomicUInt8 *atomic, uint8_t newValue) {
6363
atomic->value = newValue;
6464
}
6565

66+
// MARK: AtomicInt
67+
68+
typedef struct {
69+
_Atomic(int) value;
70+
} AtomicUInt32;
71+
72+
__attribute__((swift_name("AtomicUInt32.init(initialValue:)")))
73+
static inline AtomicUInt32 atomic_int_create(uint8_t initialValue) {
74+
AtomicUInt32 atomic;
75+
atomic.value = initialValue;
76+
return atomic;
77+
}
78+
79+
__attribute__((swift_name("getter:AtomicUInt32.value(self:)")))
80+
static inline uint32_t atomic_int_get(AtomicUInt32 *atomic) {
81+
return atomic->value;
82+
}
83+
84+
__attribute__((swift_name("setter:AtomicUInt32.value(self:_:)")))
85+
static inline void atomic_uint32_set(AtomicUInt32 *atomic, uint32_t newValue) {
86+
atomic->value = newValue;
87+
}
88+
89+
__attribute__((swift_name("AtomicUInt32.fetchAndIncrement(self:)")))
90+
static inline uint32_t atomic_uint32_fetch_and_increment(AtomicUInt32 *atomic) {
91+
return atomic->value++;
92+
}
93+
6694
#endif // SOURCEKITLSP_CATOMICS_H

Sources/SourceKitLSP/SourceKitLSPServer.swift

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
//===----------------------------------------------------------------------===//
1212

1313
import BuildServerProtocol
14+
import CAtomics
1415
import Dispatch
1516
import Foundation
1617
import IndexStoreDB
@@ -812,19 +813,7 @@ public actor SourceKitLSPServer {
812813

813814
// MARK: - MessageHandler
814815

815-
private let notificationIDForLoggingLock = NSLock()
816-
private var notificationIDForLogging: Int = 0
817-
818-
/// On every call, returns a new unique number that can be used to identify a notification.
819-
///
820-
/// This is needed so we can consistently refer to a notification using the `category` of the logger.
821-
/// Requests don't need this since they already have a unique ID in the LSP protocol.
822-
private func getNextNotificationIDForLogging() -> Int {
823-
return notificationIDForLoggingLock.withLock {
824-
notificationIDForLogging += 1
825-
return notificationIDForLogging
826-
}
827-
}
816+
private var notificationIDForLogging = AtomicUInt32(initialValue: 1)
828817

829818
extension SourceKitLSPServer: MessageHandler {
830819
public nonisolated func handle(_ params: some NotificationType) {
@@ -835,7 +824,7 @@ extension SourceKitLSPServer: MessageHandler {
835824
self.cancelRequest(params)
836825
}
837826

838-
let notificationID = getNextNotificationIDForLogging()
827+
let notificationID = notificationIDForLogging.fetchAndIncrement()
839828

840829
let signposter = Logger(subsystem: subsystem, category: "notification-\(notificationID)").makeSignposter()
841830
let signpostID = signposter.makeSignpostID()

0 commit comments

Comments
 (0)