|
| 1 | +// |
| 2 | +// This source file is part of the Swift.org open source project |
| 3 | +// |
| 4 | +// Copyright (c) 2026 Apple Inc. and the Swift project authors |
| 5 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 6 | +// |
| 7 | +// See https://swift.org/LICENSE.txt for license information |
| 8 | +// See https://swift.org/CONTRIBUTORS.txt for Swift project authors |
| 9 | +// |
| 10 | + |
| 11 | +internal import _TestingInternals |
| 12 | + |
| 13 | +#if canImport(Synchronization) |
| 14 | +internal import Synchronization |
| 15 | +#endif |
| 16 | + |
| 17 | +#if SWT_TARGET_OS_APPLE |
| 18 | +/// A type that replicates the interface of ``Synchronization/Atomic``. |
| 19 | +/// |
| 20 | +/// This type is used on Apple platforms because our deployment target there is |
| 21 | +/// earlier than the availability of the ``Synchronization/Atomic`` type. It |
| 22 | +/// replicates the interface of that type but is implemented differently (using |
| 23 | +/// a heap-allocated value) with support for only those atomic operations that |
| 24 | +/// we need to use in the testing library. |
| 25 | +/// |
| 26 | +/// Since we don't try to implement the complete ``Synchronization/AtomicRepresentable`` |
| 27 | +/// protocol, this implementation only supports using a few types that are |
| 28 | +/// actually in use in the testing library. |
| 29 | +struct Atomic<Value>: ~Copyable { |
| 30 | + /// Storage for the underlying atomic value. |
| 31 | + private nonisolated(unsafe) var _address: UnsafeMutablePointer<Value> |
| 32 | + |
| 33 | + init(_ value: consuming sending Value) { |
| 34 | + _address = .allocate(capacity: 1) |
| 35 | + _address.initialize(to: value) |
| 36 | + } |
| 37 | + |
| 38 | + deinit { |
| 39 | + _address.deinitialize(count: 1) |
| 40 | + _address.deallocate() |
| 41 | + } |
| 42 | + |
| 43 | + /// The orderings supported by this type. |
| 44 | + /// |
| 45 | + /// At this time, we only implement sequentially consistent ordering. For more |
| 46 | + /// information about atomic operation ordering, see [`AtomicUpdateOrdering`](https://developer.apple.com/documentation/synchronization/atomicupdateordering). |
| 47 | + enum Ordering { |
| 48 | + case sequentiallyConsistent |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +extension Atomic: Sendable where Value: Sendable {} |
| 53 | + |
| 54 | +// MARK: - Atomic<Bool> |
| 55 | + |
| 56 | +extension Atomic where Value == Bool { |
| 57 | + func load(ordering: Ordering) -> Value { |
| 58 | + swt_atomicLoad(_address) |
| 59 | + } |
| 60 | + |
| 61 | + func store(_ desired: consuming Value, ordering: Ordering) { |
| 62 | + swt_atomicStore(_address, desired) |
| 63 | + } |
| 64 | + |
| 65 | + func compareExchange(expected: consuming Value, desired: consuming Value) -> (exchanged: Bool, original: Value) { |
| 66 | + var expected = expected |
| 67 | + let exchanged = swt_atomicCompareExchange(_address, &expected, desired) |
| 68 | + return (exchanged, expected) |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +// MARK: - Atomic<CInt> |
| 73 | + |
| 74 | +extension Atomic where Value == CInt { |
| 75 | + func load(ordering: Ordering) -> Value { |
| 76 | + return swt_atomicLoad(_address) |
| 77 | + } |
| 78 | + |
| 79 | + func store(_ desired: consuming Value, ordering: Ordering) { |
| 80 | + swt_atomicStore(_address, desired) |
| 81 | + } |
| 82 | + |
| 83 | + func compareExchange(expected: consuming Value, desired: consuming Value, ordering: Ordering) -> (exchanged: Bool, original: Value) { |
| 84 | + var expected = expected |
| 85 | + let exchanged = swt_atomicCompareExchange(_address, &expected, desired) |
| 86 | + return (exchanged, expected) |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +// MARK: - Atomic<Int> |
| 91 | + |
| 92 | +extension Atomic where Value == Int { |
| 93 | + func load(ordering: Ordering) -> Value { |
| 94 | + swt_atomicLoad(_address) |
| 95 | + } |
| 96 | + |
| 97 | + func store(_ desired: consuming Value, ordering: Ordering) { |
| 98 | + swt_atomicStore(_address, desired) |
| 99 | + } |
| 100 | + |
| 101 | + func compareExchange(expected: consuming Value, desired: consuming Value, ordering: Ordering) -> (exchanged: Bool, original: Value) { |
| 102 | + var expected = expected |
| 103 | + let exchanged = swt_atomicCompareExchange(_address, &expected, desired) |
| 104 | + return (exchanged, expected) |
| 105 | + } |
| 106 | + |
| 107 | + @discardableResult |
| 108 | + func add(_ operand: Value, ordering: Ordering) -> (oldValue: Value, newValue: Value) { |
| 109 | + while true { |
| 110 | + let oldValue = load(ordering: ordering) |
| 111 | + let newValue = oldValue + operand |
| 112 | + if compareExchange(expected: oldValue, desired: newValue, ordering: ordering).exchanged { |
| 113 | + return (oldValue, newValue) |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + @discardableResult |
| 119 | + func subtract(_ operand: Value, ordering: Ordering) -> (oldValue: Value, newValue: Value) { |
| 120 | + add(-operand, ordering: ordering) |
| 121 | + } |
| 122 | +} |
| 123 | +#endif |
0 commit comments