File: AtomicBool.swift

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (248 lines) | stat: -rw-r--r-- 7,554 bytes parent folder | download
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2023 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 Builtin

//===----------------------------------------------------------------------===//
// Bool AtomicRepresentable conformance
//===----------------------------------------------------------------------===//

@available(SwiftStdlib 6.0, *)
extension Bool: AtomicRepresentable {
  /// The storage representation type that `Self` encodes to and decodes from
  /// which is a suitable type when used in atomic operations.
  @available(SwiftStdlib 6.0, *)
  public typealias AtomicRepresentation = UInt8.AtomicRepresentation

  /// Destroys a value of `Self` and prepares an `AtomicRepresentation` storage
  /// type to be used for atomic operations.
  ///
  /// - Note: This is not an atomic operation. This simply encodes the logical
  ///   type `Self` into its storage representation suitable for atomic
  ///   operations, `AtomicRepresentation`.
  ///
  /// - Parameter value: A valid instance of `Self` that's about to be destroyed
  ///   to encode an instance of its `AtomicRepresentation`.
  /// - Returns: The newly encoded `AtomicRepresentation` storage.
  @available(SwiftStdlib 6.0, *)
  @_alwaysEmitIntoClient
  @_transparent
  public static func encodeAtomicRepresentation(
    _ value: borrowing Bool
  ) -> AtomicRepresentation {
    UInt8.encodeAtomicRepresentation(
      UInt8(Builtin.zext_Int1_Int8(value._value))
    )
  }

  /// Recovers the logical atomic type `Self` by destroying some
  /// `AtomicRepresentation` storage instance returned from an atomic operation.
  ///
  /// - Note: This is not an atomic operation. This simply decodes the storage
  ///   representation used in atomic operations back into the logical type for
  ///   normal use, `Self`.
  ///
  /// - Parameter storage: The storage representation for `Self` that's used
  ///   within atomic operations.
  /// - Returns: The newly decoded logical type `Self`.
  @available(SwiftStdlib 6.0, *)
  @_alwaysEmitIntoClient
  @_transparent
  public static func decodeAtomicRepresentation(
    _ representation: consuming AtomicRepresentation
  ) -> Bool {
    Bool(Builtin.trunc_Int8_Int1(
      UInt8.decodeAtomicRepresentation(representation)._value)
    )
  }
}

//===----------------------------------------------------------------------===//
// Bool atomic operations
//===----------------------------------------------------------------------===//

@available(SwiftStdlib 6.0, *)
extension Atomic where Value == Bool {
  /// Perform an atomic logical AND operation and return the old and new value,
  /// applying the specified memory ordering.
  ///
  /// - Parameter operand: A boolean value.
  /// - Parameter ordering: The memory ordering to apply on this operation.
  /// - Returns: A tuple with the old value before the operation a the new value
  ///   after the operation.
  @available(SwiftStdlib 6.0, *)
  @discardableResult
  @_semantics("atomics.requires_constant_orderings")
  @_alwaysEmitIntoClient
  @_transparent
  public func logicalAnd(
    _ operand: Bool,
    ordering: AtomicUpdateOrdering
  ) -> (oldValue: Bool, newValue: Bool) {
    let builtinOperand = Bool.encodeAtomicRepresentation(operand)._storage

    let original = switch ordering {
    case .relaxed:
      Builtin.atomicrmw_and_monotonic_Int8(
        _rawAddress,
        builtinOperand
      )

    case .acquiring:
      Builtin.atomicrmw_and_acquire_Int8(
        _rawAddress,
        builtinOperand
      )

    case .releasing:
      Builtin.atomicrmw_and_release_Int8(
        _rawAddress,
        builtinOperand
      )

    case .acquiringAndReleasing:
      Builtin.atomicrmw_and_acqrel_Int8(
        _rawAddress,
        builtinOperand
      )

    case .sequentiallyConsistent:
      Builtin.atomicrmw_and_seqcst_Int8(
        _rawAddress,
        builtinOperand
      )

    default:
      Builtin.unreachable()
    }

    let old = Bool.decodeAtomicRepresentation(UInt8.AtomicRepresentation(original))

    return (oldValue: old, newValue: old && operand)
  }

  /// Perform an atomic logical OR operation and return the old and new value,
  /// applying the specified memory ordering.
  ///
  /// - Parameter operand: A boolean value.
  /// - Parameter ordering: The memory ordering to apply on this operation.
  /// - Returns: A tuple with the old value before the operation a the new value
  ///   after the operation.
  @available(SwiftStdlib 6.0, *)
  @discardableResult
  @_semantics("atomics.requires_constant_orderings")
  @_alwaysEmitIntoClient
  @_transparent
  public func logicalOr(
    _ operand: Bool,
    ordering: AtomicUpdateOrdering
  ) -> (oldValue: Bool, newValue: Bool) {
    let builtinOperand = Bool.encodeAtomicRepresentation(operand)._storage

    let original = switch ordering {
    case .relaxed:
      Builtin.atomicrmw_or_monotonic_Int8(
        _rawAddress,
        builtinOperand
      )

    case .acquiring:
      Builtin.atomicrmw_or_acquire_Int8(
        _rawAddress,
        builtinOperand
      )

    case .releasing:
      Builtin.atomicrmw_or_release_Int8(
        _rawAddress,
        builtinOperand
      )

    case .acquiringAndReleasing:
      Builtin.atomicrmw_or_acqrel_Int8(
        _rawAddress,
        builtinOperand
      )

    case .sequentiallyConsistent:
      Builtin.atomicrmw_or_seqcst_Int8(
        _rawAddress,
        builtinOperand
      )

    default:
      Builtin.unreachable()
    }

    let old = Bool.decodeAtomicRepresentation(UInt8.AtomicRepresentation(original))

    return (oldValue: old, newValue: old || operand)
  }

  /// Perform an atomic logical XOR operation and return the old and new value,
  /// applying the specified memory ordering.
  ///
  /// - Parameter operand: A boolean value.
  /// - Parameter ordering: The memory ordering to apply on this operation.
  /// - Returns: A tuple with the old value before the operation a the new value
  ///   after the operation.
  @available(SwiftStdlib 6.0, *)
  @discardableResult
  @_semantics("atomics.requires_constant_orderings")
  @_alwaysEmitIntoClient
  @_transparent
  public func logicalXor(
    _ operand: Bool,
    ordering: AtomicUpdateOrdering
  ) -> (oldValue: Bool, newValue: Bool) {
    let builtinOperand = Bool.encodeAtomicRepresentation(operand)._storage

    let original = switch ordering {
    case .relaxed:
      Builtin.atomicrmw_xor_monotonic_Int8(
        _rawAddress,
        builtinOperand
      )

    case .acquiring:
      Builtin.atomicrmw_xor_acquire_Int8(
        _rawAddress,
        builtinOperand
      )

    case .releasing:
      Builtin.atomicrmw_xor_release_Int8(
        _rawAddress,
        builtinOperand
      )

    case .acquiringAndReleasing:
      Builtin.atomicrmw_xor_acqrel_Int8(
        _rawAddress,
        builtinOperand
      )

    case .sequentiallyConsistent:
      Builtin.atomicrmw_xor_seqcst_Int8(
        _rawAddress,
        builtinOperand
      )

    default:
      Builtin.unreachable()
    }

    let old = Bool.decodeAtomicRepresentation(UInt8.AtomicRepresentation(original))

    return (oldValue: old, newValue: old != operand)
  }
}