mirror of
https://github.com/apple/swift-nio
synced 2026-11-26 04:57:49 +00:00
stop atomics from allocating on every operation
This commit is contained in:
1 parent
1fffa95ad1
commit
0853dc3911
1 file changed
+49
@@ -36,6 +36,13 @@ public final class Atomic<T: AtomicPrimitive> {
|
||||
private let value: OpaquePointer
|
||||
|
||||
/// Create an atomic object with `value`.
|
||||
@_specialize(where T == Int)
|
||||
@_specialize(where T == Bool)
|
||||
@_specialize(where T == UInt64)
|
||||
// in Swift 4.0 (fixed in 4.0.2), there was a crash that only allowed three specialisations otherwise the compiler would crash
|
||||
// FIXME: Bring back when we require Swift >= 4.0.2
|
||||
// @_specialize(where T == UInt)
|
||||
// @_specialize(where T == Int64)
|
||||
public init(value: T) {
|
||||
self.value = T.atomic_create(value)
|
||||
}
|
||||
@@ -55,6 +62,13 @@ public final class Atomic<T: AtomicPrimitive> {
|
||||
/// succeeds.
|
||||
/// - Returns: `True` if the exchange occurred, or `False` if `expected` did not
|
||||
/// match the current value and so no exchange occurred.
|
||||
@_specialize(where T == Int)
|
||||
@_specialize(where T == Bool)
|
||||
@_specialize(where T == UInt64)
|
||||
// in Swift 4.0 (fixed in 4.0.2), there was a crash that only allowed three specialisations otherwise the compiler would crash
|
||||
// FIXME: Bring back when we require Swift >= 4.0.2
|
||||
// @_specialize(where T == UInt)
|
||||
// @_specialize(where T == Int64)
|
||||
public func compareAndExchange(expected: T, desired: T) -> Bool {
|
||||
return T.atomic_compare_and_exchange(self.value, expected, desired)
|
||||
}
|
||||
@@ -67,6 +81,13 @@ public final class Atomic<T: AtomicPrimitive> {
|
||||
///
|
||||
/// - Parameter rhs: The value to add to this object.
|
||||
/// - Returns: The previous value of this object, before the addition occurred.
|
||||
@_specialize(where T == Int)
|
||||
@_specialize(where T == Bool)
|
||||
@_specialize(where T == UInt64)
|
||||
// in Swift 4.0 (fixed in 4.0.2), there was a crash that only allowed three specialisations otherwise the compiler would crash
|
||||
// FIXME: Bring back when we require Swift >= 4.0.2
|
||||
// @_specialize(where T == UInt)
|
||||
// @_specialize(where T == Int64)
|
||||
public func add(_ rhs: T) -> T {
|
||||
return T.atomic_add(self.value, rhs)
|
||||
}
|
||||
@@ -79,6 +100,13 @@ public final class Atomic<T: AtomicPrimitive> {
|
||||
///
|
||||
/// - Parameter rhs: The value to subtract from this object.
|
||||
/// - Returns: The previous value of this object, before the subtraction occurred.
|
||||
@_specialize(where T == Int)
|
||||
@_specialize(where T == Bool)
|
||||
@_specialize(where T == UInt64)
|
||||
// in Swift 4.0 (fixed in 4.0.2), there was a crash that only allowed three specialisations otherwise the compiler would crash
|
||||
// FIXME: Bring back when we require Swift >= 4.0.2
|
||||
// @_specialize(where T == UInt)
|
||||
// @_specialize(where T == Int64)
|
||||
public func sub(_ rhs: T) -> T {
|
||||
return T.atomic_sub(self.value, rhs)
|
||||
}
|
||||
@@ -91,6 +119,13 @@ public final class Atomic<T: AtomicPrimitive> {
|
||||
///
|
||||
/// - Parameter value: The new value to set this object to.
|
||||
/// - Returns: The value previously held by this object.
|
||||
@_specialize(where T == Int)
|
||||
@_specialize(where T == Bool)
|
||||
@_specialize(where T == UInt64)
|
||||
// in Swift 4.0 (fixed in 4.0.2), there was a crash that only allowed three specialisations otherwise the compiler would crash
|
||||
// FIXME: Bring back when we require Swift >= 4.0.2
|
||||
// @_specialize(where T == UInt)
|
||||
// @_specialize(where T == Int64)
|
||||
public func exchange(with value: T) -> T {
|
||||
return T.atomic_exchange(self.value, value)
|
||||
}
|
||||
@@ -102,6 +137,13 @@ public final class Atomic<T: AtomicPrimitive> {
|
||||
/// event will be ordered before or after this one.
|
||||
///
|
||||
/// - Returns: The value of this object
|
||||
@_specialize(where T == Int)
|
||||
@_specialize(where T == Bool)
|
||||
@_specialize(where T == UInt64)
|
||||
// in Swift 4.0 (fixed in 4.0.2), there was a crash that only allowed three specialisations otherwise the compiler would crash
|
||||
// FIXME: Bring back when we require Swift >= 4.0.2
|
||||
// @_specialize(where T == UInt)
|
||||
// @_specialize(where T == Int64)
|
||||
public func load() -> T {
|
||||
return T.atomic_load(self.value)
|
||||
}
|
||||
@@ -113,6 +155,13 @@ public final class Atomic<T: AtomicPrimitive> {
|
||||
/// event will be ordered before or after this one.
|
||||
///
|
||||
/// - Parameter value: The new value to set the object to.
|
||||
@_specialize(where T == Int)
|
||||
@_specialize(where T == Bool)
|
||||
@_specialize(where T == UInt64)
|
||||
// in Swift 4.0 (fixed in 4.0.2), there was a crash that only allowed three specialisations otherwise the compiler would crash
|
||||
// FIXME: Bring back when we require Swift >= 4.0.2
|
||||
// @_specialize(where T == UInt)
|
||||
// @_specialize(where T == Int64)
|
||||
public func store(_ value: T) -> Void {
|
||||
T.atomic_store(self.value, value)
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user