Skip to content

Commit 6c89474

Browse files
authored
Merge pull request #84 from lorentey/finalize-1.1
Update README
2 parents 2a2be81 + c95665d commit 6c89474

File tree

1 file changed

+125
-10
lines changed

1 file changed

+125
-10
lines changed

README.md

Lines changed: 125 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ The only way to access the counter value is to use one of the methods provided b
2525

2626
## Table of Contents
2727

28+
* [Word of Warning](#word-of-warning)
2829
* [Getting Started](#getting-started)
2930
* [Features](#features)
3031
* [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
3435
* [Contributing to Swift Atomics](#contributing-to-swift-atomics)
3536
* [Development](#development)
3637

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.
3753

3854
## Getting Started
3955

@@ -48,7 +64,7 @@ let package = Package(
4864
dependencies: [
4965
.package(
5066
url: "https://github.com/apple/swift-atomics.git",
51-
.upToNextMajor(from: "1.0.0") // or `.upToNextMinor
67+
.upToNextMajor(from: "1.1.0") // or `.upToNextMinor
5268
)
5369
],
5470
targets: [
@@ -71,7 +87,7 @@ The package implements atomic operations for the following Swift constructs, all
7187
- Booleans (`Bool`)
7288
- Standard pointer types (`UnsafeRawPointer`, `UnsafeMutableRawPointer`, `UnsafePointer<T>`, `UnsafeMutablePointer<T>`), along with their optional-wrapped forms (such as `Optional<UnsafePointer<T>>`)
7389
- Unmanaged references (`Unmanaged<T>`, `Optional<Unmanaged<T>>`)
74-
- 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
7591
- Any `RawRepresentable` type whose `RawValue` is in turn an atomic type (such as simple custom enum types)
7692
- Strong references to class instances that opted into atomic use (by conforming to the `AtomicReference` protocol)
7793

@@ -116,6 +132,12 @@ func compareExchange(
116132
failureOrdering: AtomicLoadOrdering
117133
) -> (exchanged: Bool, original: Value)
118134

135+
func weakCompareExchange(
136+
expected: Value,
137+
desired: Value,
138+
ordering: AtomicUpdateOrdering
139+
) -> (exchanged: Bool, original: Value)
140+
119141
func weakCompareExchange(
120142
expected: Value,
121143
desired: Value,
@@ -124,11 +146,106 @@ func weakCompareExchange(
124146
) -> (exchanged: Bool, original: Value)
125147
```
126148

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.
128150

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+
func loadThenWrappingIncrement(
153+
by operand: Value = 1,
154+
ordering: AtomicUpdateOrdering
155+
) -> Value
156+
157+
func loadThenWrappingDecrement(
158+
by operand: Value = 1,
159+
ordering: AtomicUpdateOrdering
160+
) -> Value
161+
162+
func loadThenBitwiseAnd(
163+
with operand: Value,
164+
ordering: AtomicUpdateOrdering
165+
) -> Value
166+
167+
func loadThenBitwiseOr(
168+
with operand: Value,
169+
ordering: AtomicUpdateOrdering
170+
) -> Value
130171

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+
func loadThenBitwiseXor(
173+
with operand: Value,
174+
ordering: AtomicUpdateOrdering
175+
) -> Value
176+
177+
func wrappingIncrementThenLoad(
178+
by operand: Value = 1,
179+
ordering: AtomicUpdateOrdering
180+
) -> Value
181+
182+
func wrappingDecrementThenLoad(
183+
by operand: Value = 1,
184+
ordering: AtomicUpdateOrdering
185+
) -> Value
186+
187+
func bitwiseAndThenLoad(
188+
with operand: Value,
189+
ordering: AtomicUpdateOrdering
190+
) -> Value
191+
192+
func bitwiseOrThenLoad(
193+
with operand: Value,
194+
ordering: AtomicUpdateOrdering
195+
) -> Value
196+
197+
func bitwiseXorThenLoad(
198+
with operand: Value,
199+
ordering: AtomicUpdateOrdering
200+
) -> Value
201+
202+
203+
func wrappingIncrement(
204+
by operand: Value = 1,
205+
ordering: AtomicUpdateOrdering
206+
)
207+
208+
func wrappingDecrement(
209+
by operand: Value = 1,
210+
ordering: AtomicUpdateOrdering
211+
)
212+
```
213+
214+
`Bool` provides select additional boolean operations along the same vein.
215+
216+
```swift
217+
func loadThenLogicalAnd(
218+
with operand: Value,
219+
ordering: AtomicUpdateOrdering
220+
) -> Value
221+
222+
func loadThenLogicalOr(
223+
with operand: Value,
224+
ordering: AtomicUpdateOrdering
225+
) -> Value
226+
227+
func loadThenLogicalXor(
228+
with operand: Value,
229+
ordering: AtomicUpdateOrdering
230+
) -> Value
231+
232+
func logicalAndThenLoad(
233+
with operand: Value,
234+
ordering: AtomicUpdateOrdering
235+
) -> Value
236+
237+
func logicalOrThenLoad(
238+
with operand: Value,
239+
ordering: AtomicUpdateOrdering
240+
) -> Value
241+
242+
func logicalXorThenLoad(
243+
with operand: 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].
132249

133250
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.)
134251

@@ -138,7 +255,7 @@ The Swift Atomics package is source stable. The version numbers follow [Semantic
138255

139256
[semver]: https://semver.org
140257

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.
142259

143260
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:
144261

@@ -149,13 +266,13 @@ By "underscored declarations" we mean declarations that have a leading underscor
149266

150267
Interfaces that aren't part of the public API may continue to change in any release, including patch releases.
151268

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.
153270

154271
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.
155272

156273
Future minor versions of the package may introduce changes to these rules as needed.
157274

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.)
159276

160277
Requiring a new Swift release will only require a minor version bump.
161278

@@ -196,8 +313,6 @@ A number of [source files](./Sources/Atomics) have a `.swift.gyb` extension. The
196313

197314
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.
198315

199-
The same script also runs `swift test --generate-linuxmain` to register any newly added unit tests.
200-
201316
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.
202317

203318
⚛︎︎

0 commit comments

Comments
 (0)