mirror of
https://github.com/apple/swift-atomics
synced 2026-11-25 04:37:47 +00:00
Linux on x86_64 unfortunately disables support for cmpxchg16 by default, making double-wide atomic operations fall back to using locks. We cannot currently override this on the package level, so stop providing double-wide atomics in this configuration by default. To enable codegen support for double-wide atomics on Linux/x86_64, compile the package with `-Xcc -mcx16 -Xswiftc -DENABLE_DOUBLEWIDE_ATOMICS`.
231 lines
7.1 KiB
Plaintext
231 lines
7.1 KiB
Plaintext
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This source file is part of the Swift Atomics open source project
|
|
//
|
|
// Copyright (c) 2020 Apple Inc. and the Swift project authors
|
|
// Licensed under Apache License v2.0 with Runtime Library Exception
|
|
//
|
|
// See https://swift.org/LICENSE.txt for license information
|
|
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
%{
|
|
from gyb_utils import (
|
|
autogenerated_warning, loadOrderings, storeOrderings, updateOrderings,
|
|
integerOperations, lowerFirst, argLabel, shimOrders)
|
|
|
|
def atomicTypes():
|
|
return ["Int", "Int64", "Int32", "Int16", "Int8",
|
|
"UInt", "UInt64", "UInt32", "UInt16", "UInt8",
|
|
"DoubleWord"]
|
|
}%
|
|
${autogenerated_warning()}
|
|
|
|
import _AtomicsShims
|
|
|
|
% for swiftType in atomicTypes():
|
|
% if swiftType == "DoubleWord":
|
|
// Double-wide atomic primitives on x86_64 CPUs aren't available by default
|
|
// on Linux distributions, and we cannot currently enable them automatically.
|
|
#if !(os(Linux) && arch(x86_64)) || ENABLE_DOUBLEWIDE_ATOMICS
|
|
% end
|
|
extension ${swiftType}: AtomicValue {
|
|
public struct AtomicRepresentation {
|
|
public typealias Value = ${swiftType}
|
|
|
|
@usableFromInline
|
|
var _storage: _Atomic${swiftType}Storage
|
|
|
|
@inline(__always) @_alwaysEmitIntoClient
|
|
public init(_ value: Value) {
|
|
self._storage = _sa_prepare_${swiftType}(value)
|
|
}
|
|
|
|
@inline(__always) @_alwaysEmitIntoClient
|
|
public func dispose() -> Value {
|
|
_sa_dispose_${swiftType}(_storage)
|
|
}
|
|
}
|
|
}
|
|
|
|
extension UnsafeMutablePointer
|
|
where Pointee == ${swiftType}.AtomicRepresentation {
|
|
@inlinable @inline(__always)
|
|
internal var _extract: UnsafeMutablePointer<_Atomic${swiftType}Storage> {
|
|
// `${swiftType}` is layout-compatible with its only stored property.
|
|
return UnsafeMutableRawPointer(self)
|
|
.assumingMemoryBound(to: _Atomic${swiftType}Storage.self)
|
|
}
|
|
}
|
|
|
|
extension ${swiftType}.AtomicRepresentation: AtomicStorage {
|
|
@_semantics("atomics.requires_constant_orderings")
|
|
@_transparent @_alwaysEmitIntoClient
|
|
public static func atomicLoad(
|
|
at pointer: UnsafeMutablePointer<Self>,
|
|
ordering: AtomicLoadOrdering
|
|
) -> Value {
|
|
switch ordering {
|
|
% for (swiftOrder, shimOrder) in loadOrderings:
|
|
case .${swiftOrder}:
|
|
return _sa_load_${shimOrder}_${swiftType}(pointer._extract)
|
|
% end
|
|
default:
|
|
fatalError("Unsupported ordering")
|
|
}
|
|
}
|
|
|
|
@_semantics("atomics.requires_constant_orderings")
|
|
@_transparent @_alwaysEmitIntoClient
|
|
public static func atomicStore(
|
|
_ desired: Value,
|
|
at pointer: UnsafeMutablePointer<Self>,
|
|
ordering: AtomicStoreOrdering
|
|
) {
|
|
switch ordering {
|
|
% for (swiftOrder, shimOrder) in storeOrderings:
|
|
case .${swiftOrder}:
|
|
_sa_store_${shimOrder}_${swiftType}(pointer._extract, desired)
|
|
% end
|
|
default:
|
|
fatalError("Unsupported ordering")
|
|
}
|
|
}
|
|
|
|
@_semantics("atomics.requires_constant_orderings")
|
|
@_transparent @_alwaysEmitIntoClient
|
|
public static func atomicExchange(
|
|
_ desired: Value,
|
|
at pointer: UnsafeMutablePointer<Self>,
|
|
ordering: AtomicUpdateOrdering
|
|
) -> Value {
|
|
switch ordering {
|
|
% for (swiftOrder, shimOrder, _) in updateOrderings:
|
|
case .${swiftOrder}:
|
|
return _sa_exchange_${shimOrder}_${swiftType}(pointer._extract, desired)
|
|
% end
|
|
default:
|
|
fatalError("Unsupported ordering")
|
|
}
|
|
}
|
|
|
|
@_semantics("atomics.requires_constant_orderings")
|
|
@_transparent @_alwaysEmitIntoClient
|
|
public static func atomicCompareExchange(
|
|
expected: Value,
|
|
desired: Value,
|
|
at pointer: UnsafeMutablePointer<Self>,
|
|
ordering: AtomicUpdateOrdering
|
|
) -> (exchanged: Bool, original: Value) {
|
|
var expected = expected
|
|
let exchanged: Bool
|
|
switch ordering {
|
|
% for (swiftOrder, shimOrder, failOrder) in updateOrderings:
|
|
case .${swiftOrder}:
|
|
exchanged = _sa_cmpxchg_strong_${shimOrder}_${failOrder}_${swiftType}(
|
|
pointer._extract,
|
|
&expected, desired)
|
|
% end
|
|
default:
|
|
fatalError("Unsupported ordering")
|
|
}
|
|
return (exchanged, expected)
|
|
}
|
|
|
|
@_semantics("atomics.requires_constant_orderings")
|
|
@_transparent @_alwaysEmitIntoClient
|
|
public static func atomicCompareExchange(
|
|
expected: Value,
|
|
desired: Value,
|
|
at pointer: UnsafeMutablePointer<Self>,
|
|
successOrdering: AtomicUpdateOrdering,
|
|
failureOrdering: AtomicLoadOrdering
|
|
) -> (exchanged: Bool, original: Value) {
|
|
var expected = expected
|
|
let exchanged: Bool
|
|
// FIXME: stdatomic.h (and LLVM underneath) doesn't support
|
|
// arbitrary ordering combinations yet, so upgrade the success
|
|
// ordering when necessary so that it is at least as "strong" as
|
|
// the failure case.
|
|
switch (successOrdering, failureOrdering) {
|
|
% for (swiftSuccess, shimSuccess, _) in updateOrderings:
|
|
% for (swiftFailure, shimFailure) in loadOrderings:
|
|
case (.${swiftSuccess}, .${swiftFailure}):
|
|
exchanged = _sa_cmpxchg_strong_${shimOrders(shimSuccess, shimFailure)}_${swiftType}(
|
|
pointer._extract,
|
|
&expected,
|
|
desired)
|
|
% end
|
|
% end
|
|
default:
|
|
fatalError("Unsupported ordering")
|
|
}
|
|
return (exchanged, expected)
|
|
}
|
|
|
|
@_semantics("atomics.requires_constant_orderings")
|
|
@_transparent @_alwaysEmitIntoClient
|
|
public static func atomicWeakCompareExchange(
|
|
expected: Value,
|
|
desired: Value,
|
|
at pointer: UnsafeMutablePointer<Self>,
|
|
successOrdering: AtomicUpdateOrdering,
|
|
failureOrdering: AtomicLoadOrdering
|
|
) -> (exchanged: Bool, original: Value) {
|
|
var expected = expected
|
|
let exchanged: Bool
|
|
// FIXME: stdatomic.h (and LLVM underneath) doesn't support
|
|
// arbitrary ordering combinations yet, so upgrade the success
|
|
// ordering when necessary so that it is at least as "strong" as
|
|
// the failure case.
|
|
switch (successOrdering, failureOrdering) {
|
|
% for (swiftSuccess, shimSuccess, _) in updateOrderings:
|
|
% for (swiftFailure, shimFailure) in loadOrderings:
|
|
case (.${swiftSuccess}, .${swiftFailure}):
|
|
exchanged = _sa_cmpxchg_weak_${shimOrders(shimSuccess, shimFailure)}_${swiftType}(
|
|
pointer._extract,
|
|
&expected,
|
|
desired)
|
|
% end
|
|
% end
|
|
default:
|
|
fatalError("Unsupported ordering")
|
|
}
|
|
return (exchanged, expected)
|
|
}
|
|
}
|
|
|
|
% if swiftType <> "DoubleWord":
|
|
extension ${swiftType}: AtomicInteger {}
|
|
|
|
extension ${swiftType}.AtomicRepresentation: AtomicIntegerStorage {
|
|
% for (name, cname, op, label, doc) in integerOperations:
|
|
% defaultValue = " = 1" if label <> "" else ""
|
|
@_semantics("atomics.requires_constant_orderings")
|
|
@_transparent @_alwaysEmitIntoClient
|
|
@discardableResult
|
|
public static func atomicLoadThen${name}(
|
|
${label} operand: Value${defaultValue},
|
|
at pointer: UnsafeMutablePointer<Self>,
|
|
ordering: AtomicUpdateOrdering
|
|
) -> Value {
|
|
switch ordering {
|
|
% for (swiftOrder, shimOrder, _) in updateOrderings:
|
|
case .${swiftOrder}:
|
|
return _sa_fetch_${cname}_${shimOrder}_${swiftType}(
|
|
pointer._extract,
|
|
operand)
|
|
% end
|
|
default:
|
|
fatalError("Unsupported ordering")
|
|
}
|
|
}
|
|
% end
|
|
}
|
|
% end
|
|
|
|
% if swiftType == "DoubleWord":
|
|
#endif // ENABLE_DOUBLEWIDE_ATOMICS
|
|
% end
|
|
% end
|