Skip to content

Commit 6e76922

Browse files
committed
Uses Native swift Atomics
Replaces the usage of CAtomics with the native Atomics from the swift library and bumps the development platform to macOS 15
1 parent 825c17e commit 6e76922

File tree

2 files changed

+20
-37
lines changed

2 files changed

+20
-37
lines changed

Package.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -694,7 +694,7 @@ if buildOnlyTests {
694694

695695
let package = Package(
696696
name: "SourceKitLSP",
697-
platforms: [.macOS(.v13)],
697+
platforms: [.macOS("15")],
698698
products: products,
699699
dependencies: dependencies,
700700
targets: targets,

Sources/SwiftExtensions/Atomics.swift

Lines changed: 19 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -10,97 +10,80 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13-
import CAtomics
13+
import Synchronization
1414

15-
// TODO: Use atomic types from the standard library (https://github.com/swiftlang/sourcekit-lsp/issues/1949)
1615
package final class AtomicBool: Sendable {
17-
private nonisolated(unsafe) let atomic: UnsafeMutablePointer<CAtomicUInt32>
16+
private let atomic: Atomic<Bool>
1817

1918
package init(initialValue: Bool) {
20-
self.atomic = atomic_uint32_create(initialValue ? 1 : 0)
21-
}
22-
23-
deinit {
24-
atomic_uint32_destroy(atomic)
19+
self.atomic = Atomic(initialValue)
2520
}
2621

2722
package var value: Bool {
2823
get {
29-
atomic_uint32_get(atomic) != 0
24+
atomic.load(ordering: .sequentiallyConsistent)
3025
}
3126
set {
32-
atomic_uint32_set(atomic, newValue ? 1 : 0)
27+
atomic.store(newValue, ordering: .sequentiallyConsistent)
3328
}
3429
}
3530
}
3631

3732
package final class AtomicUInt8: Sendable {
38-
private nonisolated(unsafe) let atomic: UnsafeMutablePointer<CAtomicUInt32>
33+
private let atomic: Atomic<UInt8>
3934

4035
package init(initialValue: UInt8) {
41-
self.atomic = atomic_uint32_create(UInt32(initialValue))
42-
}
43-
44-
deinit {
45-
atomic_uint32_destroy(atomic)
36+
self.atomic = Atomic(initialValue)
4637
}
4738

4839
package var value: UInt8 {
4940
get {
50-
UInt8(atomic_uint32_get(atomic))
41+
atomic.load(ordering: .sequentiallyConsistent)
5142
}
5243
set {
53-
atomic_uint32_set(atomic, UInt32(newValue))
44+
atomic.store(newValue, ordering: .sequentiallyConsistent)
5445
}
5546
}
5647
}
5748

5849
package final class AtomicUInt32: Sendable {
59-
private nonisolated(unsafe) let atomic: UnsafeMutablePointer<CAtomicUInt32>
50+
private let atomic: Atomic<UInt32>
6051

6152
package init(initialValue: UInt32) {
62-
self.atomic = atomic_uint32_create(initialValue)
53+
self.atomic = Atomic(initialValue)
6354
}
6455

6556
package var value: UInt32 {
6657
get {
67-
atomic_uint32_get(atomic)
58+
atomic.load(ordering: .sequentiallyConsistent)
6859
}
6960
set {
70-
atomic_uint32_set(atomic, newValue)
61+
atomic.store(newValue, ordering: .sequentiallyConsistent)
7162
}
7263
}
7364

74-
deinit {
75-
atomic_uint32_destroy(atomic)
76-
}
77-
7865
package func fetchAndIncrement() -> UInt32 {
79-
return atomic_uint32_fetch_and_increment(atomic)
66+
atomic.add(1, ordering: .sequentiallyConsistent).oldValue
8067
}
8168
}
8269

8370
package final class AtomicInt32: Sendable {
84-
private nonisolated(unsafe) let atomic: UnsafeMutablePointer<CAtomicInt32>
71+
private let atomic: Atomic<Int32>
8572

8673
package init(initialValue: Int32) {
87-
self.atomic = atomic_int32_create(initialValue)
74+
self.atomic = Atomic(initialValue)
8875
}
8976

9077
package var value: Int32 {
9178
get {
92-
atomic_int32_get(atomic)
79+
atomic.load(ordering: .sequentiallyConsistent)
9380
}
9481
set {
95-
atomic_int32_set(atomic, newValue)
82+
atomic.store(newValue, ordering: .sequentiallyConsistent)
9683
}
9784
}
9885

99-
deinit {
100-
atomic_int32_destroy(atomic)
101-
}
102-
10386
package func fetchAndIncrement() -> Int32 {
104-
return atomic_int32_fetch_and_increment(atomic)
87+
atomic.add(1, ordering: .sequentiallyConsistent).oldValue
10588
}
10689
}

0 commit comments

Comments
 (0)