Coverage Report

Created: 2026-10-13 06:43

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/swift-nio/Sources/NIOConcurrencyHelpers/NIOAtomic.swift
Line
Count
Source
1
//===----------------------------------------------------------------------===//
2
//
3
// This source file is part of the SwiftNIO open source project
4
//
5
// Copyright (c) 2017-2018 Apple Inc. and the SwiftNIO project authors
6
// Licensed under Apache License v2.0
7
//
8
// See LICENSE.txt for license information
9
// See CONTRIBUTORS.txt for the list of SwiftNIO project authors
10
//
11
// SPDX-License-Identifier: Apache-2.0
12
//
13
//===----------------------------------------------------------------------===//
14
15
import CNIOAtomics
16
17
/// The protocol that all types that can be made atomic must conform to.
18
///
19
/// **Do not add conformance to this protocol for arbitrary types**. Only a small range
20
/// of types have appropriate atomic operations supported by the CPU, and those types
21
/// already have conformances implemented.
22
@preconcurrency
23
public protocol NIOAtomicPrimitive {
24
    associatedtype AtomicWrapper
25
    static var nio_atomic_create_with_existing_storage: @Sendable (UnsafeMutablePointer<AtomicWrapper>, Self) -> Void {
26
        get
27
    }
28
    static var nio_atomic_compare_and_exchange: @Sendable (UnsafeMutablePointer<AtomicWrapper>, Self, Self) -> Bool {
29
        get
30
    }
31
    static var nio_atomic_add: @Sendable (UnsafeMutablePointer<AtomicWrapper>, Self) -> Self { get }
32
    static var nio_atomic_sub: @Sendable (UnsafeMutablePointer<AtomicWrapper>, Self) -> Self { get }
33
    static var nio_atomic_exchange: @Sendable (UnsafeMutablePointer<AtomicWrapper>, Self) -> Self { get }
34
    static var nio_atomic_load: @Sendable (UnsafeMutablePointer<AtomicWrapper>) -> Self { get }
35
    static var nio_atomic_store: @Sendable (UnsafeMutablePointer<AtomicWrapper>, Self) -> Void { get }
36
}
37
38
extension Bool: NIOAtomicPrimitive {
39
    public typealias AtomicWrapper = catmc_nio_atomic__Bool
40
    public static let nio_atomic_create_with_existing_storage = catmc_nio_atomic__Bool_create_with_existing_storage
41
    public static let nio_atomic_compare_and_exchange = catmc_nio_atomic__Bool_compare_and_exchange
42
    public static let nio_atomic_add = catmc_nio_atomic__Bool_add
43
    public static let nio_atomic_sub = catmc_nio_atomic__Bool_sub
44
    public static let nio_atomic_exchange = catmc_nio_atomic__Bool_exchange
45
    public static let nio_atomic_load = catmc_nio_atomic__Bool_load
46
    public static let nio_atomic_store = catmc_nio_atomic__Bool_store
47
}
48
49
extension Int8: NIOAtomicPrimitive {
50
    public typealias AtomicWrapper = catmc_nio_atomic_int_least8_t
51
    public static let nio_atomic_create_with_existing_storage =
52
        catmc_nio_atomic_int_least8_t_create_with_existing_storage
53
    public static let nio_atomic_compare_and_exchange = catmc_nio_atomic_int_least8_t_compare_and_exchange
54
    public static let nio_atomic_add = catmc_nio_atomic_int_least8_t_add
55
    public static let nio_atomic_sub = catmc_nio_atomic_int_least8_t_sub
56
    public static let nio_atomic_exchange = catmc_nio_atomic_int_least8_t_exchange
57
    public static let nio_atomic_load = catmc_nio_atomic_int_least8_t_load
58
    public static let nio_atomic_store = catmc_nio_atomic_int_least8_t_store
59
}
60
61
extension UInt8: NIOAtomicPrimitive {
62
    public typealias AtomicWrapper = catmc_nio_atomic_uint_least8_t
63
    public static let nio_atomic_create_with_existing_storage =
64
        catmc_nio_atomic_uint_least8_t_create_with_existing_storage
65
    public static let nio_atomic_compare_and_exchange = catmc_nio_atomic_uint_least8_t_compare_and_exchange
66
    public static let nio_atomic_add = catmc_nio_atomic_uint_least8_t_add
67
    public static let nio_atomic_sub = catmc_nio_atomic_uint_least8_t_sub
68
    public static let nio_atomic_exchange = catmc_nio_atomic_uint_least8_t_exchange
69
    public static let nio_atomic_load = catmc_nio_atomic_uint_least8_t_load
70
    public static let nio_atomic_store = catmc_nio_atomic_uint_least8_t_store
71
}
72
73
extension Int16: NIOAtomicPrimitive {
74
    public typealias AtomicWrapper = catmc_nio_atomic_int_least16_t
75
    public static let nio_atomic_create_with_existing_storage =
76
        catmc_nio_atomic_int_least16_t_create_with_existing_storage
77
    public static let nio_atomic_compare_and_exchange = catmc_nio_atomic_int_least16_t_compare_and_exchange
78
    public static let nio_atomic_add = catmc_nio_atomic_int_least16_t_add
79
    public static let nio_atomic_sub = catmc_nio_atomic_int_least16_t_sub
80
    public static let nio_atomic_exchange = catmc_nio_atomic_int_least16_t_exchange
81
    public static let nio_atomic_load = catmc_nio_atomic_int_least16_t_load
82
    public static let nio_atomic_store = catmc_nio_atomic_int_least16_t_store
83
}
84
85
extension UInt16: NIOAtomicPrimitive {
86
    public typealias AtomicWrapper = catmc_nio_atomic_uint_least16_t
87
    public static let nio_atomic_create_with_existing_storage =
88
        catmc_nio_atomic_uint_least16_t_create_with_existing_storage
89
    public static let nio_atomic_compare_and_exchange = catmc_nio_atomic_uint_least16_t_compare_and_exchange
90
    public static let nio_atomic_add = catmc_nio_atomic_uint_least16_t_add
91
    public static let nio_atomic_sub = catmc_nio_atomic_uint_least16_t_sub
92
    public static let nio_atomic_exchange = catmc_nio_atomic_uint_least16_t_exchange
93
    public static let nio_atomic_load = catmc_nio_atomic_uint_least16_t_load
94
    public static let nio_atomic_store = catmc_nio_atomic_uint_least16_t_store
95
}
96
97
extension Int32: NIOAtomicPrimitive {
98
    public typealias AtomicWrapper = catmc_nio_atomic_int_least32_t
99
    public static let nio_atomic_create_with_existing_storage =
100
        catmc_nio_atomic_int_least32_t_create_with_existing_storage
101
    public static let nio_atomic_compare_and_exchange = catmc_nio_atomic_int_least32_t_compare_and_exchange
102
    public static let nio_atomic_add = catmc_nio_atomic_int_least32_t_add
103
    public static let nio_atomic_sub = catmc_nio_atomic_int_least32_t_sub
104
    public static let nio_atomic_exchange = catmc_nio_atomic_int_least32_t_exchange
105
    public static let nio_atomic_load = catmc_nio_atomic_int_least32_t_load
106
    public static let nio_atomic_store = catmc_nio_atomic_int_least32_t_store
107
}
108
109
extension UInt32: NIOAtomicPrimitive {
110
    public typealias AtomicWrapper = catmc_nio_atomic_uint_least32_t
111
    public static let nio_atomic_create_with_existing_storage =
112
        catmc_nio_atomic_uint_least32_t_create_with_existing_storage
113
    public static let nio_atomic_compare_and_exchange = catmc_nio_atomic_uint_least32_t_compare_and_exchange
114
    public static let nio_atomic_add = catmc_nio_atomic_uint_least32_t_add
115
    public static let nio_atomic_sub = catmc_nio_atomic_uint_least32_t_sub
116
    public static let nio_atomic_exchange = catmc_nio_atomic_uint_least32_t_exchange
117
    public static let nio_atomic_load = catmc_nio_atomic_uint_least32_t_load
118
    public static let nio_atomic_store = catmc_nio_atomic_uint_least32_t_store
119
}
120
121
extension Int64: NIOAtomicPrimitive {
122
    public typealias AtomicWrapper = catmc_nio_atomic_long_long
123
    public static let nio_atomic_create_with_existing_storage = catmc_nio_atomic_long_long_create_with_existing_storage
124
    public static let nio_atomic_compare_and_exchange = catmc_nio_atomic_long_long_compare_and_exchange
125
    public static let nio_atomic_add = catmc_nio_atomic_long_long_add
126
    public static let nio_atomic_sub = catmc_nio_atomic_long_long_sub
127
    public static let nio_atomic_exchange = catmc_nio_atomic_long_long_exchange
128
    public static let nio_atomic_load = catmc_nio_atomic_long_long_load
129
    public static let nio_atomic_store = catmc_nio_atomic_long_long_store
130
}
131
132
extension UInt64: NIOAtomicPrimitive {
133
    public typealias AtomicWrapper = catmc_nio_atomic_unsigned_long_long
134
    public static let nio_atomic_create_with_existing_storage =
135
        catmc_nio_atomic_unsigned_long_long_create_with_existing_storage
136
    public static let nio_atomic_compare_and_exchange = catmc_nio_atomic_unsigned_long_long_compare_and_exchange
137
    public static let nio_atomic_add = catmc_nio_atomic_unsigned_long_long_add
138
    public static let nio_atomic_sub = catmc_nio_atomic_unsigned_long_long_sub
139
    public static let nio_atomic_exchange = catmc_nio_atomic_unsigned_long_long_exchange
140
    public static let nio_atomic_load = catmc_nio_atomic_unsigned_long_long_load
141
    public static let nio_atomic_store = catmc_nio_atomic_unsigned_long_long_store
142
}
143
144
#if os(Windows)
145
extension Int: NIOAtomicPrimitive {
146
    public typealias AtomicWrapper = catmc_nio_atomic_intptr_t
147
    public static let nio_atomic_create_with_existing_storage = catmc_nio_atomic_intptr_t_create_with_existing_storage
148
    public static let nio_atomic_compare_and_exchange = catmc_nio_atomic_intptr_t_compare_and_exchange
149
    public static let nio_atomic_add = catmc_nio_atomic_intptr_t_add
150
    public static let nio_atomic_sub = catmc_nio_atomic_intptr_t_sub
151
    public static let nio_atomic_exchange = catmc_nio_atomic_intptr_t_exchange
152
    public static let nio_atomic_load = catmc_nio_atomic_intptr_t_load
153
    public static let nio_atomic_store = catmc_nio_atomic_intptr_t_store
154
}
155
156
extension UInt: NIOAtomicPrimitive {
157
    public typealias AtomicWrapper = catmc_nio_atomic_uintptr_t
158
    public static let nio_atomic_create_with_existing_storage = catmc_nio_atomic_uintptr_t_create_with_existing_storage
159
    public static let nio_atomic_compare_and_exchange = catmc_nio_atomic_uintptr_t_compare_and_exchange
160
    public static let nio_atomic_add = catmc_nio_atomic_uintptr_t_add
161
    public static let nio_atomic_sub = catmc_nio_atomic_uintptr_t_sub
162
    public static let nio_atomic_exchange = catmc_nio_atomic_uintptr_t_exchange
163
    public static let nio_atomic_load = catmc_nio_atomic_uintptr_t_load
164
    public static let nio_atomic_store = catmc_nio_atomic_uintptr_t_store
165
}
166
#else
167
extension Int: NIOAtomicPrimitive {
168
    public typealias AtomicWrapper = catmc_nio_atomic_long
169
    public static let nio_atomic_create_with_existing_storage = catmc_nio_atomic_long_create_with_existing_storage
170
    public static let nio_atomic_compare_and_exchange = catmc_nio_atomic_long_compare_and_exchange
171
    public static let nio_atomic_add = catmc_nio_atomic_long_add
172
    public static let nio_atomic_sub = catmc_nio_atomic_long_sub
173
    public static let nio_atomic_exchange = catmc_nio_atomic_long_exchange
174
    public static let nio_atomic_load = catmc_nio_atomic_long_load
175
    public static let nio_atomic_store = catmc_nio_atomic_long_store
176
}
177
178
extension UInt: NIOAtomicPrimitive {
179
    public typealias AtomicWrapper = catmc_nio_atomic_unsigned_long
180
    public static let nio_atomic_create_with_existing_storage =
181
        catmc_nio_atomic_unsigned_long_create_with_existing_storage
182
    public static let nio_atomic_compare_and_exchange = catmc_nio_atomic_unsigned_long_compare_and_exchange
183
    public static let nio_atomic_add = catmc_nio_atomic_unsigned_long_add
184
    public static let nio_atomic_sub = catmc_nio_atomic_unsigned_long_sub
185
    public static let nio_atomic_exchange = catmc_nio_atomic_unsigned_long_exchange
186
    public static let nio_atomic_load = catmc_nio_atomic_unsigned_long_load
187
    public static let nio_atomic_store = catmc_nio_atomic_unsigned_long_store
188
}
189
#endif
190
191
/// An encapsulation of an atomic primitive object.
192
///
193
/// Atomic objects support a wide range of atomic operations:
194
///
195
/// - Compare and swap
196
/// - Add
197
/// - Subtract
198
/// - Exchange
199
/// - Load current value
200
/// - Store current value
201
///
202
/// Atomic primitives are useful when building constructs that need to
203
/// communicate or cooperate across multiple threads. In the case of
204
/// SwiftNIO this usually involves communicating across multiple event loops.
205
///
206
/// By necessity, all atomic values are references: after all, it makes no
207
/// sense to talk about managing an atomic value when each time it's modified
208
/// the thread that modified it gets a local copy!
209
@available(*, deprecated, message: "please use ManagedAtomic from https://github.com/apple/swift-atomics instead")
210
public final class NIOAtomic<T: NIOAtomicPrimitive> {
211
    @usableFromInline
212
    typealias Manager = ManagedBufferPointer<Void, T.AtomicWrapper>
213
214
    /// Create an atomic object with `value`
215
    @inlinable
216
    @available(OpenBSD, unavailable, message: "malloc_size is unavailable.")
217
0
    public static func makeAtomic(value: T) -> NIOAtomic {
218
0
        let manager = Manager(bufferClass: self, minimumCapacity: 1) { _, _ in }
219
0
        manager.withUnsafeMutablePointerToElements {
220
0
            T.nio_atomic_create_with_existing_storage($0, value)
221
0
        }
222
0
        return manager.buffer as! NIOAtomic<T>
223
0
    }
224
225
    /// Atomically compares the value against `expected` and, if they are equal,
226
    /// replaces the value with `desired`.
227
    ///
228
    /// This implementation conforms to C11's `atomic_compare_exchange_strong`. This
229
    /// means that the compare-and-swap will always succeed if `expected` is equal to
230
    /// value. Additionally, it uses a *sequentially consistent ordering*. For more
231
    /// details on atomic memory models, check the documentation for C11's
232
    /// `stdatomic.h`.
233
    ///
234
    /// - Parameter expected: The value that this object must currently hold for the
235
    ///     compare-and-swap to succeed.
236
    /// - Parameter desired: The new value that this object will hold if the compare
237
    ///     succeeds.
238
    /// - Returns: `True` if the exchange occurred, or `False` if `expected` did not
239
    ///     match the current value and so no exchange occurred.
240
    @inlinable
241
0
    public func compareAndExchange(expected: T, desired: T) -> Bool {
242
0
        Manager(unsafeBufferObject: self).withUnsafeMutablePointerToElements {
243
0
            T.nio_atomic_compare_and_exchange($0, expected, desired)
244
0
        }
245
0
    }
246
247
    /// Atomically adds `rhs` to this object.
248
    ///
249
    /// This implementation uses a *relaxed* memory ordering. This guarantees nothing
250
    /// more than that this operation is atomic: there is no guarantee that any other
251
    /// event will be ordered before or after this one.
252
    ///
253
    /// - Parameter rhs: The value to add to this object.
254
    /// - Returns: The previous value of this object, before the addition occurred.
255
    @inlinable
256
    @discardableResult
257
0
    public func add(_ rhs: T) -> T {
258
0
        Manager(unsafeBufferObject: self).withUnsafeMutablePointerToElements {
259
0
            T.nio_atomic_add($0, rhs)
260
0
        }
261
0
    }
262
263
    /// Atomically subtracts `rhs` from this object.
264
    ///
265
    /// This implementation uses a *relaxed* memory ordering. This guarantees nothing
266
    /// more than that this operation is atomic: there is no guarantee that any other
267
    /// event will be ordered before or after this one.
268
    ///
269
    /// - Parameter rhs: The value to subtract from this object.
270
    /// - Returns: The previous value of this object, before the subtraction occurred.
271
    @inlinable
272
    @discardableResult
273
0
    public func sub(_ rhs: T) -> T {
274
0
        Manager(unsafeBufferObject: self).withUnsafeMutablePointerToElements {
275
0
            T.nio_atomic_sub($0, rhs)
276
0
        }
277
0
    }
278
279
    /// Atomically exchanges `value` for the current value of this object.
280
    ///
281
    /// This implementation uses a *relaxed* memory ordering. This guarantees nothing
282
    /// more than that this operation is atomic: there is no guarantee that any other
283
    /// event will be ordered before or after this one.
284
    ///
285
    /// - Parameter value: The new value to set this object to.
286
    /// - Returns: The value previously held by this object.
287
    @inlinable
288
0
    public func exchange(with value: T) -> T {
289
0
        Manager(unsafeBufferObject: self).withUnsafeMutablePointerToElements {
290
0
            T.nio_atomic_exchange($0, value)
291
0
        }
292
0
    }
293
294
    /// Atomically loads and returns the value of this object.
295
    ///
296
    /// This implementation uses a *relaxed* memory ordering. This guarantees nothing
297
    /// more than that this operation is atomic: there is no guarantee that any other
298
    /// event will be ordered before or after this one.
299
    ///
300
    /// - Returns: The value of this object
301
    @inlinable
302
0
    public func load() -> T {
303
0
        Manager(unsafeBufferObject: self).withUnsafeMutablePointerToElements {
304
0
            T.nio_atomic_load($0)
305
0
        }
306
0
    }
307
308
    /// Atomically replaces the value of this object with `value`.
309
    ///
310
    /// This implementation uses a *relaxed* memory ordering. This guarantees nothing
311
    /// more than that this operation is atomic: there is no guarantee that any other
312
    /// event will be ordered before or after this one.
313
    ///
314
    /// - Parameter value: The new value to set the object to.
315
    @inlinable
316
0
    public func store(_ value: T) {
317
0
        Manager(unsafeBufferObject: self).withUnsafeMutablePointerToElements {
318
0
            T.nio_atomic_store($0, value)
319
0
        }
320
0
    }
321
322
0
    deinit {
323
0
        Manager(unsafeBufferObject: self).withUnsafeMutablePointers { headerPtr, elementsPtr in
324
0
            elementsPtr.deinitialize(count: 1)
325
0
            headerPtr.deinitialize(count: 1)
326
0
        }
327
0
    }
328
}
329
330
@available(*, deprecated)
331
extension NIOAtomic: Sendable {}