mirror of
https://github.com/apple/swift.git
synced 2025-12-14 20:36:38 +01:00
Turns out some people used this type despite it being prefixed with `_stdlib_`, so we have to keep it, with an obsoletion message this time. Second copy of the same type is kept available past Swift 5 in SwiftPrivate for use in tests.
61 lines
1.8 KiB
Swift
61 lines
1.8 KiB
Swift
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This source file is part of the Swift.org open source project
|
|
//
|
|
// Copyright (c) 2014 - 2018 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
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
import Swift
|
|
|
|
// This type intentionally shadows the stdlib one
|
|
@available(swift, introduced: 5.0)
|
|
public final class _stdlib_AtomicInt {
|
|
internal var _value: Int
|
|
|
|
internal var _valuePtr: UnsafeMutablePointer<Int> {
|
|
return _getUnsafePointerToStoredProperties(self).assumingMemoryBound(
|
|
to: Int.self)
|
|
}
|
|
|
|
public init(_ value: Int = 0) {
|
|
_value = value
|
|
}
|
|
|
|
public func store(_ desired: Int) {
|
|
return _swift_stdlib_atomicStoreInt(object: _valuePtr, desired: desired)
|
|
}
|
|
|
|
public func load() -> Int {
|
|
return _swift_stdlib_atomicLoadInt(object: _valuePtr)
|
|
}
|
|
|
|
% for operation_name, operation in [ ('Add', '+'), ('And', '&'), ('Or', '|'), ('Xor', '^') ]:
|
|
@discardableResult
|
|
public func fetchAnd${operation_name}(_ operand: Int) -> Int {
|
|
return _swift_stdlib_atomicFetch${operation_name}Int(
|
|
object: _valuePtr,
|
|
operand: operand)
|
|
}
|
|
|
|
public func ${operation_name.lower()}AndFetch(_ operand: Int) -> Int {
|
|
return fetchAnd${operation_name}(operand) ${operation} operand
|
|
}
|
|
% end
|
|
|
|
public func compareExchange(expected: inout Int, desired: Int) -> Bool {
|
|
var expectedVar = expected
|
|
let result = _swift_stdlib_atomicCompareExchangeStrongInt(
|
|
object: _valuePtr,
|
|
expected: &expectedVar,
|
|
desired: desired)
|
|
expected = expectedVar
|
|
return result
|
|
}
|
|
}
|
|
|