|
10 | 10 | //
|
11 | 11 | //===----------------------------------------------------------------------===//
|
12 | 12 |
|
13 |
| -import CAtomics |
| 13 | +import Synchronization |
14 | 14 |
|
15 |
| -// TODO: Use atomic types from the standard library (https://github.com/swiftlang/sourcekit-lsp/issues/1949) |
16 | 15 | package final class AtomicBool: Sendable {
|
17 |
| - private nonisolated(unsafe) let atomic: UnsafeMutablePointer<CAtomicUInt32> |
| 16 | + private let atomic: Atomic<Bool> |
18 | 17 |
|
19 | 18 | 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) |
25 | 20 | }
|
26 | 21 |
|
27 | 22 | package var value: Bool {
|
28 | 23 | get {
|
29 |
| - atomic_uint32_get(atomic) != 0 |
| 24 | + atomic.load(ordering: .sequentiallyConsistent) |
30 | 25 | }
|
31 | 26 | set {
|
32 |
| - atomic_uint32_set(atomic, newValue ? 1 : 0) |
| 27 | + atomic.store(newValue, ordering: .sequentiallyConsistent) |
33 | 28 | }
|
34 | 29 | }
|
35 | 30 | }
|
36 | 31 |
|
37 | 32 | package final class AtomicUInt8: Sendable {
|
38 |
| - private nonisolated(unsafe) let atomic: UnsafeMutablePointer<CAtomicUInt32> |
| 33 | + private let atomic: Atomic<UInt8> |
39 | 34 |
|
40 | 35 | 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) |
46 | 37 | }
|
47 | 38 |
|
48 | 39 | package var value: UInt8 {
|
49 | 40 | get {
|
50 |
| - UInt8(atomic_uint32_get(atomic)) |
| 41 | + atomic.load(ordering: .sequentiallyConsistent) |
51 | 42 | }
|
52 | 43 | set {
|
53 |
| - atomic_uint32_set(atomic, UInt32(newValue)) |
| 44 | + atomic.store(newValue, ordering: .sequentiallyConsistent) |
54 | 45 | }
|
55 | 46 | }
|
56 | 47 | }
|
57 | 48 |
|
58 | 49 | package final class AtomicUInt32: Sendable {
|
59 |
| - private nonisolated(unsafe) let atomic: UnsafeMutablePointer<CAtomicUInt32> |
| 50 | + private let atomic: Atomic<UInt32> |
60 | 51 |
|
61 | 52 | package init(initialValue: UInt32) {
|
62 |
| - self.atomic = atomic_uint32_create(initialValue) |
| 53 | + self.atomic = Atomic(initialValue) |
63 | 54 | }
|
64 | 55 |
|
65 | 56 | package var value: UInt32 {
|
66 | 57 | get {
|
67 |
| - atomic_uint32_get(atomic) |
| 58 | + atomic.load(ordering: .sequentiallyConsistent) |
68 | 59 | }
|
69 | 60 | set {
|
70 |
| - atomic_uint32_set(atomic, newValue) |
| 61 | + atomic.store(newValue, ordering: .sequentiallyConsistent) |
71 | 62 | }
|
72 | 63 | }
|
73 | 64 |
|
74 |
| - deinit { |
75 |
| - atomic_uint32_destroy(atomic) |
76 |
| - } |
77 |
| - |
78 | 65 | package func fetchAndIncrement() -> UInt32 {
|
79 |
| - return atomic_uint32_fetch_and_increment(atomic) |
| 66 | + atomic.add(1, ordering: .sequentiallyConsistent).oldValue |
80 | 67 | }
|
81 | 68 | }
|
82 | 69 |
|
83 | 70 | package final class AtomicInt32: Sendable {
|
84 |
| - private nonisolated(unsafe) let atomic: UnsafeMutablePointer<CAtomicInt32> |
| 71 | + private let atomic: Atomic<Int32> |
85 | 72 |
|
86 | 73 | package init(initialValue: Int32) {
|
87 |
| - self.atomic = atomic_int32_create(initialValue) |
| 74 | + self.atomic = Atomic(initialValue) |
88 | 75 | }
|
89 | 76 |
|
90 | 77 | package var value: Int32 {
|
91 | 78 | get {
|
92 |
| - atomic_int32_get(atomic) |
| 79 | + atomic.load(ordering: .sequentiallyConsistent) |
93 | 80 | }
|
94 | 81 | set {
|
95 |
| - atomic_int32_set(atomic, newValue) |
| 82 | + atomic.store(newValue, ordering: .sequentiallyConsistent) |
96 | 83 | }
|
97 | 84 | }
|
98 | 85 |
|
99 |
| - deinit { |
100 |
| - atomic_int32_destroy(atomic) |
101 |
| - } |
102 |
| - |
103 | 86 | package func fetchAndIncrement() -> Int32 {
|
104 |
| - return atomic_int32_fetch_and_increment(atomic) |
| 87 | + atomic.add(1, ordering: .sequentiallyConsistent).oldValue |
105 | 88 | }
|
106 | 89 | }
|
0 commit comments