-
Notifications
You must be signed in to change notification settings - Fork 10.5k
/
Copy pathAtomicInt.swift.gyb
60 lines (50 loc) · 1.81 KB
/
AtomicInt.swift.gyb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
//===----------------------------------------------------------------------===//
//
// 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
}
}