You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -25,6 +25,7 @@ The only way to access the counter value is to use one of the methods provided b
25
25
26
26
## Table of Contents
27
27
28
+
*[Word of Warning](#word-of-warning)
28
29
*[Getting Started](#getting-started)
29
30
*[Features](#features)
30
31
*[Lock\-Free vs Wait\-Free Operations](#lock-free-vs-wait-free-operations)
@@ -34,6 +35,21 @@ The only way to access the counter value is to use one of the methods provided b
34
35
*[Contributing to Swift Atomics](#contributing-to-swift-atomics)
35
36
*[Development](#development)
36
37
38
+
## Word of Warning
39
+
40
+
Atomic values are fundamental to managing concurrency. However, they are far too low level to be used lightly. These things are full of traps. They are extremely difficult to use correctly -- far trickier than, say, unsafe pointers.
41
+
42
+
The best way to deal with atomics is to avoid directly using them. It's always better to rely on higher-level constructs, whenever possible.
43
+
44
+
This package exists to support the few cases where the use of atomics is unavoidable -- such as when implementing those high-level synchronization/concurrency constructs.
45
+
46
+
The primary focus is to provide systems programmers access to atomic operations with an API design that emphasizes clarity over superficial convenience:
47
+
48
+
- Each atomic operation is invoked in client code using a clear, unabbreviated name that directly specifies what that operation does. Atomic operations are never implicit -- they are always clearly spelled out.
49
+
50
+
- There is no default memory ordering, to avoid accidental (and costly) use of sequential consistency. (This is surprisingly common issue in C/C++.)
51
+
52
+
- Operations such as compare/exchange prefer to keep input values cleanly separated from results. There are no `inout` parameters.
.upToNextMajor(from: "1.0.0") // or `.upToNextMinor
67
+
.upToNextMajor(from: "1.1.0") // or `.upToNextMinor
52
68
)
53
69
],
54
70
targets: [
@@ -71,7 +87,7 @@ The package implements atomic operations for the following Swift constructs, all
71
87
- Booleans (`Bool`)
72
88
- Standard pointer types (`UnsafeRawPointer`, `UnsafeMutableRawPointer`, `UnsafePointer<T>`, `UnsafeMutablePointer<T>`), along with their optional-wrapped forms (such as `Optional<UnsafePointer<T>>`)
- A special `DoubleWord` type that consists of two `UInt` values, `low` and `high`, providing double-wide atomic primitives
90
+
- A special `DoubleWord` type that consists of two `UInt` values, `first` and `second`, providing double-wide atomic primitives
75
91
- Any `RawRepresentable` type whose `RawValue` is in turn an atomic type (such as simple custom enum types)
76
92
- Strong references to class instances that opted into atomic use (by conforming to the `AtomicReference` protocol)
77
93
@@ -116,6 +132,12 @@ func compareExchange(
116
132
failureOrdering: AtomicLoadOrdering
117
133
) -> (exchanged: Bool, original: Value)
118
134
135
+
funcweakCompareExchange(
136
+
expected: Value,
137
+
desired: Value,
138
+
ordering: AtomicUpdateOrdering
139
+
) -> (exchanged: Bool, original: Value)
140
+
119
141
funcweakCompareExchange(
120
142
expected: Value,
121
143
desired: Value,
@@ -124,11 +146,106 @@ func weakCompareExchange(
124
146
) -> (exchanged: Bool, original: Value)
125
147
```
126
148
127
-
Integer types come with additional atomic operations for incrementing or decrementing values and bitwise logical operations. `Bool` provides select additional boolean operations along the same vein.
149
+
Integer types come with additional atomic operations for incrementing or decrementing values and bitwise logical operations.
128
150
129
-
For an introduction to the APIs provided by this package, for now please see the [first version of SE-0282][SE-0282r0].
151
+
```swift
152
+
funcloadThenWrappingIncrement(
153
+
byoperand: Value=1,
154
+
ordering: AtomicUpdateOrdering
155
+
) ->Value
156
+
157
+
funcloadThenWrappingDecrement(
158
+
byoperand: Value=1,
159
+
ordering: AtomicUpdateOrdering
160
+
) ->Value
161
+
162
+
funcloadThenBitwiseAnd(
163
+
withoperand: Value,
164
+
ordering: AtomicUpdateOrdering
165
+
) ->Value
166
+
167
+
funcloadThenBitwiseOr(
168
+
withoperand: Value,
169
+
ordering: AtomicUpdateOrdering
170
+
) ->Value
130
171
131
-
Note that when/if Swift gains support for non-copiable types, we expect to replace both `ManagedAtomic` and `UnsafeAtomic` with a single move-only atomic struct that combines the performance and versatility of `UnsafeAtomic` with the ease-of-use and memory safety of `ManagedAtomic`.
172
+
funcloadThenBitwiseXor(
173
+
withoperand: Value,
174
+
ordering: AtomicUpdateOrdering
175
+
) ->Value
176
+
177
+
funcwrappingIncrementThenLoad(
178
+
byoperand: Value=1,
179
+
ordering: AtomicUpdateOrdering
180
+
) ->Value
181
+
182
+
funcwrappingDecrementThenLoad(
183
+
byoperand: Value=1,
184
+
ordering: AtomicUpdateOrdering
185
+
) ->Value
186
+
187
+
funcbitwiseAndThenLoad(
188
+
withoperand: Value,
189
+
ordering: AtomicUpdateOrdering
190
+
) ->Value
191
+
192
+
funcbitwiseOrThenLoad(
193
+
withoperand: Value,
194
+
ordering: AtomicUpdateOrdering
195
+
) ->Value
196
+
197
+
funcbitwiseXorThenLoad(
198
+
withoperand: Value,
199
+
ordering: AtomicUpdateOrdering
200
+
) ->Value
201
+
202
+
203
+
funcwrappingIncrement(
204
+
byoperand: Value=1,
205
+
ordering: AtomicUpdateOrdering
206
+
)
207
+
208
+
funcwrappingDecrement(
209
+
byoperand: Value=1,
210
+
ordering: AtomicUpdateOrdering
211
+
)
212
+
```
213
+
214
+
`Bool` provides select additional boolean operations along the same vein.
215
+
216
+
```swift
217
+
funcloadThenLogicalAnd(
218
+
withoperand: Value,
219
+
ordering: AtomicUpdateOrdering
220
+
) ->Value
221
+
222
+
funcloadThenLogicalOr(
223
+
withoperand: Value,
224
+
ordering: AtomicUpdateOrdering
225
+
) ->Value
226
+
227
+
funcloadThenLogicalXor(
228
+
withoperand: Value,
229
+
ordering: AtomicUpdateOrdering
230
+
) ->Value
231
+
232
+
funclogicalAndThenLoad(
233
+
withoperand: Value,
234
+
ordering: AtomicUpdateOrdering
235
+
) ->Value
236
+
237
+
funclogicalOrThenLoad(
238
+
withoperand: Value,
239
+
ordering: AtomicUpdateOrdering
240
+
) ->Value
241
+
242
+
funclogicalXorThenLoad(
243
+
withoperand: Value,
244
+
ordering: AtomicUpdateOrdering
245
+
) ->Value
246
+
```
247
+
248
+
For an introduction to the APIs provided by this package, for now please see the [first version of SE-0282][SE-0282r0].
132
249
133
250
The current version of the `Atomics` module does not implement APIs for tagged atomics (see [issue #1](https://github.com/apple/swift-atomics/issues/1)), although it does expose a `DoubleWord` type that can be used to implement them. (Atomic strong references are already implemented in terms of `DoubleWord`, although in their current form they do not expose any user-customizable bits.)
134
251
@@ -138,7 +255,7 @@ The Swift Atomics package is source stable. The version numbers follow [Semantic
138
255
139
256
[semver]: https://semver.org
140
257
141
-
The public API of version 1.0 of the `swift-atomics` package consists of non-underscored declarations that are marked `public` in the `Atomics` module.
258
+
The public API of version 1.1 of the `swift-atomics` package consists of non-underscored declarations that are marked `public` in the `Atomics` module.
142
259
143
260
By "underscored declarations" we mean declarations that have a leading underscore anywhere in their fully qualified name. For instance, here are some names that wouldn't be considered part of the public API, even if they were technically marked public:
144
261
@@ -149,13 +266,13 @@ By "underscored declarations" we mean declarations that have a leading underscor
149
266
150
267
Interfaces that aren't part of the public API may continue to change in any release, including patch releases.
151
268
152
-
Note that contents of the `_AtomicsShims` module explicitly aren't public API. (As implied by its underscored module name.) The definitions therein may therefore change at whim, and the entire module may be removed in any new release -- do not import this module directly. We also don't make any source compatibility promises about the contents of the `Utilities`and `Tests` subdirectories.
269
+
Note that contents of the `_AtomicsShims` module explicitly aren't public API. (As implied by its underscored module name.) The definitions therein may therefore change at whim, and the entire module may be removed in any new release -- do not import this module directly. We also don't make any source compatibility promises about the contents of the `Utilities`, `Tests`, `Xcode`and `cmake` subdirectories.
153
270
154
271
If you have a use case that requires using underscored APIs, please [submit a Feature Request][enhancement] describing it! We'd like the public interface to be as useful as possible -- although preferably without compromising safety or limiting future evolution.
155
272
156
273
Future minor versions of the package may introduce changes to these rules as needed.
157
274
158
-
We'd like this package to quickly embrace Swift language and toolchain improvements that are relevant to its mandate. Accordingly, from time to time, we expect that new versions of this package will require clients to upgrade to a more recent Swift toolchain release. (This allows the package to make use of new language/stdlib features, build on compiler bug fixes, and adopt new package manager functionality as soon as they are available.)
275
+
We'd like this package to quickly embrace Swift language and toolchain improvements that are relevant to its mandate. Accordingly, from time to time, new versions of this package will require clients to upgrade to a more recent Swift toolchain release. (This allows the package to make use of new language/stdlib features, build on compiler bug fixes, and adopt new package manager functionality as soon as they are available.)
159
276
160
277
Requiring a new Swift release will only require a minor version bump.
161
278
@@ -196,8 +313,6 @@ A number of [source files](./Sources/Atomics) have a `.swift.gyb` extension. The
196
313
197
314
To regenerate sources (and to update the inventory of XCTest tests), you need to manually run the script [`generate-sources.sh`](./Utilities/generate-sources.sh) in the Utilities folder of this repository. This needs to be done every time you modify one of the template files.
198
315
199
-
The same script also runs `swift test --generate-linuxmain` to register any newly added unit tests.
200
-
201
316
In addition to gyb, the [`_AtomicsShims.h`](./Sources/_AtomicsShims/include/_AtomicsShims.h) header file uses the C preprocessor to define trivial wrapper functions for every supported atomic operation -- memory ordering pairing.
0 commit comments