Skip to content

Commit ff3d221

Browse files
authored
Merge pull request #60 from lorentey/add-doc-bundle
Add a DocC bundle for the Atomics module
2 parents 088df27 + 3366a36 commit ff3d221

21 files changed

+446
-1
lines changed

Diff for: .spi.yml

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
version: 1
2+
builder:
3+
configs:
4+
- documentation_targets: [Atomics]

Diff for: Package.swift

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
//
44
// This source file is part of the Swift.org open source project
55
//
6-
// Copyright (c) 2020-2021 Apple Inc. and the Swift project authors
6+
// Copyright (c) 2020-2022 Apple Inc. and the Swift project authors
77
// Licensed under Apache License v2.0 with Runtime Library Exception
88
//
99
// See https://swift.org/LICENSE.txt for license information
@@ -32,6 +32,7 @@ let package = Package(
3232
dependencies: ["_AtomicsShims"],
3333
exclude: [
3434
"CMakeLists.txt",
35+
"Atomics.docc",
3536
"HighLevelTypes.swift.gyb",
3637
"PointerConformances.swift.gyb",
3738
"IntegerConformances.swift.gyb",

Diff for: [email protected]

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// swift-tools-version:5.5
2+
//===----------------------------------------------------------------------===//
3+
//
4+
// This source file is part of the Swift.org open source project
5+
//
6+
// Copyright (c) 2020-2022 Apple Inc. and the Swift project authors
7+
// Licensed under Apache License v2.0 with Runtime Library Exception
8+
//
9+
// See https://swift.org/LICENSE.txt for license information
10+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
11+
//
12+
//===----------------------------------------------------------------------===//
13+
14+
import PackageDescription
15+
16+
let package = Package(
17+
name: "swift-atomics",
18+
products: [
19+
.library(
20+
name: "Atomics",
21+
targets: ["Atomics"]),
22+
],
23+
targets: [
24+
.target(
25+
name: "_AtomicsShims",
26+
exclude: [
27+
"CMakeLists.txt"
28+
]
29+
),
30+
.target(
31+
name: "Atomics",
32+
dependencies: ["_AtomicsShims"],
33+
exclude: [
34+
"CMakeLists.txt",
35+
"HighLevelTypes.swift.gyb",
36+
"PointerConformances.swift.gyb",
37+
"IntegerConformances.swift.gyb",
38+
"AtomicBool.swift.gyb",
39+
"AtomicLazyReference.swift.gyb",
40+
]
41+
),
42+
.testTarget(
43+
name: "AtomicsTests",
44+
dependencies: ["Atomics"],
45+
exclude: [
46+
"main.swift",
47+
"Basics.swift.gyb"
48+
]
49+
),
50+
]
51+
)

Diff for: Sources/Atomics/AtomicValue.swift

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
/// A type that supports atomic operations through a separate atomic storage
1414
/// representation.
1515
public protocol AtomicValue {
16+
/// The atomic storage representation for this value.
1617
associatedtype AtomicRepresentation: AtomicStorage
1718
where AtomicRepresentation.Value == Self
1819
}

Diff for: Sources/Atomics/Atomics.docc/Atomics.md

+134
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
# ``Atomics``
2+
3+
An atomics library for Swift.
4+
5+
## Overview
6+
7+
This package implements an atomics library for Swift, providing atomic operations for a variety of Swift types, including integers and pointer values. The goal is to enable intrepid developers to start building synchronization constructs directly in Swift.
8+
9+
Atomic operations aren't subject to the usual exclusivity rules. The same memory location may be safely read and updated from multiple concurrent threads of execution, as long as all such access is done through atomic operations. For example, here is a trivial atomic counter:
10+
11+
``` swift
12+
import Atomics
13+
import Dispatch
14+
15+
let counter = ManagedAtomic<Int>(0)
16+
17+
DispatchQueue.concurrentPerform(iterations: 10) { _ in
18+
for _ in 0 ..< 1_000_000 {
19+
counter.wrappingIncrement(ordering: .relaxed)
20+
}
21+
}
22+
counter.load(ordering: .relaxed) // ⟹ 10_000_000
23+
```
24+
25+
The only way to access the counter value is to use one of the methods provided by `ManagedAtomic`, each of which implement a particular atomic operation, and each of which require an explicit ordering value. (Swift supports a subset of the C/C++ memory orderings.)
26+
27+
## Features
28+
29+
The package implements atomic operations for the following Swift constructs, all of which conform to the public `AtomicValue` protocol:
30+
31+
- Standard signed integer types (`Int`, `Int64`, `Int32`, `Int16`, `Int8`)
32+
- Standard unsigned integer types (`UInt`, `UInt64`, `UInt32`, `UInt16`, `UInt8`)
33+
- Booleans (`Bool`)
34+
- Standard pointer types (`UnsafeRawPointer`, `UnsafeMutableRawPointer`, `UnsafePointer<T>`, `UnsafeMutablePointer<T>`), along with their optional-wrapped forms (such as `Optional<UnsafePointer<T>>`)
35+
- Unmanaged references (`Unmanaged<T>`, `Optional<Unmanaged<T>>`)
36+
- A special `DoubleWord` type that consists of two `UInt` values, `low` and `high`, providing double-wide atomic primitives
37+
- Any `RawRepresentable` type whose `RawValue` is in turn an atomic type (such as simple custom enum types)
38+
- Strong references to class instances that opted into atomic use (by conforming to the `AtomicReference` protocol)
39+
40+
Of particular note is full support for atomic strong references. This provides a convenient memory reclamation solution for concurrent data structures that fits perfectly with Swift's reference counting memory management model. (Atomic strong references are implemented in terms of `DoubleWord` operations.) However, accessing an atomic strong reference is (relatively) expensive, so we also provide a separate set of efficient constructs (`ManagedAtomicLazyReference` and `UnsafeAtomicLazyReference`) for the common case of a lazily initialized (but otherwise constant) atomic strong reference.
41+
42+
### Lock-Free vs Wait-Free Operations
43+
44+
All atomic operations exposed by this package are guaranteed to have lock-free implementations. However, we do not guarantee wait-free operation -- depending on the capabilities of the target platform, some of the exposed operations may be implemented by compare-and-exchange loops. That said, all atomic operations map directly to dedicated CPU instructions where available -- to the extent supported by llvm & Clang.
45+
46+
### Memory Management
47+
48+
Atomic access is implemented in terms of dedicated atomic storage representations that are kept distinct from the corresponding regular (non-atomic) type. (E.g., the actual integer value underlying the counter above isn't directly accessible.) This has several advantages:
49+
50+
- it helps prevent accidental non-atomic access to atomic variables,
51+
- it enables custom storage representations (such as the one used by atomic strong references), and
52+
- it is a better fit with the standard C atomics library that we use to implement the actual operations (as enabled by [SE-0282]).
53+
54+
[SE-0282]: https://github.com/apple/swift-evolution/blob/master/proposals/0282-atomics.md
55+
56+
While the underlying pointer-based atomic operations are exposed as static methods on the corresponding `AtomicStorage` types, we strongly recommend the use of higher-level atomic wrappers to manage the details of preparing/disposing atomic storage. This version of the library provides two wrapper types:
57+
58+
- an easy to use, memory-safe `ManagedAtomic<T>` generic class and
59+
- a less convenient, but more flexible `UnsafeAtomic<T>` generic struct.
60+
61+
Both constructs provide the following operations on all `AtomicValue` types:
62+
63+
```swift
64+
func load(ordering: AtomicLoadOrdering) -> Value
65+
func store(_ desired: Value, ordering: AtomicStoreOrdering)
66+
func exchange(_ desired: Value, ordering: AtomicUpdateOrdering) -> Value
67+
68+
func compareExchange(
69+
expected: Value,
70+
desired: Value,
71+
ordering: AtomicUpdateOrdering
72+
) -> (exchanged: Bool, original: Value)
73+
74+
func compareExchange(
75+
expected: Value,
76+
desired: Value,
77+
successOrdering: AtomicUpdateOrdering,
78+
failureOrdering: AtomicLoadOrdering
79+
) -> (exchanged: Bool, original: Value)
80+
81+
func weakCompareExchange(
82+
expected: Value,
83+
desired: Value,
84+
successOrdering: AtomicUpdateOrdering,
85+
failureOrdering: AtomicLoadOrdering
86+
) -> (exchanged: Bool, original: Value)
87+
```
88+
89+
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.
90+
91+
For an introduction to the APIs provided by this package, for now please see the [first version of SE-0282][SE-0282r0].
92+
93+
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`.
94+
95+
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.)
96+
97+
98+
## Topics
99+
100+
### Atomic Container Types
101+
102+
- ``ManagedAtomic``
103+
- ``UnsafeAtomic``
104+
- ``ManagedAtomicLazyReference``
105+
- ``UnsafeAtomicLazyReference``
106+
107+
### Memory Orderings
108+
109+
- ``AtomicLoadOrdering``
110+
- ``AtomicStoreOrdering``
111+
- ``AtomicUpdateOrdering``
112+
113+
### Atomic Value Protocols
114+
115+
- ``AtomicValue``
116+
- ``AtomicInteger``
117+
- ``AtomicReference``
118+
- ``AtomicOptionalWrappable``
119+
120+
### Atomic Storage Representations
121+
122+
- ``AtomicStorage``
123+
- ``AtomicIntegerStorage``
124+
- ``AtomicRawRepresentableStorage``
125+
- ``AtomicReferenceStorage``
126+
- ``AtomicOptionalReferenceStorage``
127+
128+
### Fences
129+
130+
- ``atomicMemoryFence(ordering:)``
131+
132+
### Value Types
133+
134+
- ``DoubleWord``
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# ``Atomics/AtomicInteger``
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# ``Atomics/AtomicIntegerStorage``
2+
3+
## Topics
4+
5+
### Atomic Integer Operations
6+
7+
- ``atomicLoadThenWrappingIncrement(by:at:ordering:)``
8+
- ``atomicLoadThenWrappingDecrement(by:at:ordering:)``
9+
- ``atomicLoadThenBitwiseOr(with:at:ordering:)``
10+
- ``atomicLoadThenBitwiseAnd(with:at:ordering:)``
11+
- ``atomicLoadThenBitwiseXor(with:at:ordering:)``
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# ``Atomics/AtomicLoadOrdering``
2+
3+
## Topics
4+
5+
### Ordering Values
6+
7+
- ``relaxed``
8+
- ``acquiring``
9+
- ``sequentiallyConsistent``
10+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# ``Atomics/AtomicOptionalReferenceStorage``
2+
3+
## Topics
4+
5+
### Creating and Disposing Atomic Storage
6+
7+
- ``init(_:)``
8+
- ``dispose()``
9+
10+
### Atomic Operations
11+
12+
- ``atomicLoad(at:ordering:)``
13+
- ``atomicStore(_:at:ordering:)``
14+
- ``atomicExchange(_:at:ordering:)``
15+
- ``atomicCompareExchange(expected:desired:at:ordering:)``
16+
- ``atomicCompareExchange(expected:desired:at:successOrdering:failureOrdering:)``
17+
- ``atomicWeakCompareExchange(expected:desired:at:successOrdering:failureOrdering:)``
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# ``Atomics/AtomicOptionalWrappable``
2+
3+
## Topics
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# ``Atomics/AtomicRawRepresentableStorage``
2+
3+
## Topics
4+
5+
### Creating and Disposing Atomic Storage
6+
7+
- ``init(_:)``
8+
- ``dispose()``
9+
10+
### Atomic Operations
11+
12+
- ``atomicLoad(at:ordering:)``
13+
- ``atomicStore(_:at:ordering:)``
14+
- ``atomicExchange(_:at:ordering:)``
15+
- ``atomicCompareExchange(expected:desired:at:ordering:)``
16+
- ``atomicCompareExchange(expected:desired:at:successOrdering:failureOrdering:)``
17+
- ``atomicWeakCompareExchange(expected:desired:at:successOrdering:failureOrdering:)``
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# ``Atomics/AtomicReferenceStorage``
2+
3+
## Topics
4+
5+
### Creating and Disposing Atomic Storage
6+
7+
- ``init(_:)``
8+
- ``dispose()``
9+
10+
### Atomic Operations
11+
12+
- ``atomicLoad(at:ordering:)``
13+
- ``atomicStore(_:at:ordering:)``
14+
- ``atomicExchange(_:at:ordering:)``
15+
- ``atomicCompareExchange(expected:desired:at:ordering:)``
16+
- ``atomicCompareExchange(expected:desired:at:successOrdering:failureOrdering:)``
17+
- ``atomicWeakCompareExchange(expected:desired:at:successOrdering:failureOrdering:)``
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# ``Atomics/AtomicReference``
2+
3+
## Topics
4+
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# ``Atomics/AtomicStorage``
2+
3+
## Topics
4+
5+
### Associated Types
6+
7+
- ``Value``
8+
9+
### Creating and Disposing Atomic Storage
10+
11+
- ``init(_:)``
12+
- ``dispose()``
13+
14+
### Atomic Operations
15+
16+
- ``atomicLoad(at:ordering:)``
17+
- ``atomicStore(_:at:ordering:)``
18+
- ``atomicExchange(_:at:ordering:)``
19+
- ``atomicCompareExchange(expected:desired:at:ordering:)``
20+
- ``atomicCompareExchange(expected:desired:at:successOrdering:failureOrdering:)``
21+
- ``atomicWeakCompareExchange(expected:desired:at:successOrdering:failureOrdering:)``
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# ``Atomics/AtomicStoreOrdering``
2+
3+
## Topics
4+
5+
### Ordering Values
6+
7+
- ``relaxed``
8+
- ``releasing``
9+
- ``sequentiallyConsistent``
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# ``Atomics/AtomicUpdateOrdering``
2+
3+
## Topics
4+
5+
### Ordering Values
6+
7+
- ``relaxed``
8+
- ``acquiring``
9+
- ``releasing``
10+
- ``acquiringAndReleasing``
11+
- ``sequentiallyConsistent``
+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# ``Atomics/AtomicValue``
2+
3+
## Topics
4+
5+
### Associated Types
6+
7+
- ``AtomicRepresentation``
+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# ``Atomics/ManagedAtomic``
2+
3+
## Topics
4+
5+
### Initializers
6+
7+
- ``init(_:)``
8+
9+
### Atomic Operations
10+
11+
- ``load(ordering:)``
12+
- ``store(_:ordering:)``
13+
- ``exchange(_:ordering:)``
14+
- ``compareExchange(expected:desired:ordering:)``
15+
- ``compareExchange(expected:desired:successOrdering:failureOrdering:)``
16+
- ``weakCompareExchange(expected:desired:successOrdering:failureOrdering:)``
17+
18+
### Atomic Integer Operations
19+
20+
- ``wrappingIncrement(by:ordering:)``
21+
- ``wrappingDecrement(by:ordering:)``
22+
- ``loadThenWrappingIncrement(by:ordering:)``
23+
- ``loadThenWrappingDecrement(by:ordering:)``
24+
- ``loadThenBitwiseOr(with:ordering:)``
25+
- ``loadThenBitwiseAnd(with:ordering:)``
26+
- ``loadThenBitwiseXor(with:ordering:)``
27+
- ``wrappingIncrementThenLoad(by:ordering:)``
28+
- ``wrappingDecrementThenLoad(by:ordering:)``
29+
- ``bitwiseOrThenLoad(with:ordering:)``
30+
- ``bitwiseAndThenLoad(with:ordering:)``
31+
- ``bitwiseXorThenLoad(with:ordering:)``
32+
33+
### Atomic Boolean Operations
34+
35+
- ``logicalOrThenLoad(with:ordering:)``
36+
- ``logicalAndThenLoad(with:ordering:)``
37+
- ``logicalXorThenLoad(with:ordering:)``
38+
- ``loadThenLogicalOr(with:ordering:)``
39+
- ``loadThenLogicalAnd(with:ordering:)``
40+
- ``loadThenLogicalXor(with:ordering:)``

0 commit comments

Comments
 (0)