Skip to content

Commit c88d92b

Browse files
authored
Update README.md
1 parent 2ede65e commit c88d92b

File tree

1 file changed

+11
-10
lines changed

1 file changed

+11
-10
lines changed

README.md

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,38 +24,39 @@ The integer types have the following methods:
2424
`AtomicBool` has the following methods:
2525
- `load`, `store`, `swap`, `CAS`, `and`, `or`, and `xor`.
2626

27-
The memory order (from `<stdatomic.h>`) can be set by using the `order` parameter on each method; the default is `.relaxed` for the integer types (it is sufficient for counter operations, the most common application,) and `.sequential` for pointer types.
27+
The memory order (from `<stdatomic.h>`) can be set by using the `order` parameter on each method; the default is `.relaxed` for the integer types (it is sufficient for counter operations, the most common application,) and `.sequential` for pointer types. Note that `memory_order_consume` has no equivalent in this module, as (as far as I can tell) clang silently upgrades that to `memory_order_acquire`, making it impossible (at the moment) to test whether an algorithm can properly use `memory_order_consume`. This also means that nothing is lost by its absence.
2828

29-
The integer types also have a `value` property, as a convenient way to perform a `.relaxed` load. The pointer types have a `pointer` property for the same purpose.
29+
The integer types also have a `value` property, as a convenient way to perform a `.relaxed` load.
30+
The pointer types have a `pointer` property for the same purpose.
3031

3132
### Module CAtomics
3233

33-
The second module is `CAtomics`, which provides atomics to Swift using a C-style interface. `SwiftAtomics` is built on top of `CAtomics`. The types implemented in `CAtomics` are the same as in `SwiftAtomics`, minus the types which use generics features.
34-
Fuctions defined in `CAtomics` are prefixed with `CAtomics`; they are `Load`, `Store`, `Exchange`, and `CompareAndExchange` for all types. The integer types add `Add`, `Subtract`, `BitwiseAnd`, `BitWiseOr`, and `BitWiseXor`; `AtomicBool` adds `And`, `Or`, and `Xor`.
34+
The second module is `CAtomics`, which provides atomics to Swift using a C-style interface. `SwiftAtomics` is built on top of `CAtomics`. The types implemented in `CAtomics` are the same as in `SwiftAtomics`, minus the types which require swift's generic features.
35+
Functions defined in `CAtomics` are prefixed with `CAtomics`; they are `Load`, `Store`, `Exchange`, and `CompareAndExchange` for all types. The integer types add `Add`, `Subtract`, `BitwiseAnd`, `BitWiseOr`, and `BitWiseXor`; `AtomicBool` adds `And`, `Or`, and `Xor`.
3536

3637
#### Notes on atomics and the law-of-exclusivity:
3738

38-
My experimentation has shown that these types are compatible with Swift 5's run-time exclusivity checking when used as members of class instances, but present difficulties when the thread sanitizer is enabled.
39+
My experimentation has shown that the types defined in `SwiftAtomics` are compatible with Swift 5's run-time exclusivity checking when used as members of class instances, but present difficulties when the thread sanitizer is enabled.
3940

4041
Atomic types are useful as synchronization points between threads, and therefore have an interesting relationship with Swift's exclusivity checking. Firstly, they should be used as members of reference types, or directly captured by closures. They are `struct` types, so as to be not incur additional memory allocation, but that feature means that if you use the thread sanitizer, it will warn about them.
4142

42-
In order to use atomics in a way that is acceptable to the thread sanitizer; you must allocate memory for atomic variables on the heap using `UnsafeMutablePointer`, and then pass that pointer to the `CAtomics` functions as needed. Unfortunately I have not found a way to use the swift-style wrappers in a way that doesn't trigger the thread sanitizer.
43+
In order to use atomics in a way that is acceptable to the thread sanitizer, one must have allocated memory for atomic variables on the heap using `UnsafeMutablePointer`. Then, pass that pointer to the functions defined in the `CAtomics` module, as needed. I haven't found a way to use the swift-style wrappers in a way that doesn't trigger the thread sanitizer.
4344

4445
```swift
4546
import CAtomics
4647

4748
class Example {
48-
private var state = UnsafeMutablePointer<AtomicInt>.allocate(capacity: 1)
49+
private var counter = UnsafeMutablePointer<AtomicInt>.allocate(capacity: 1)
4950
init() {
50-
CAtomicsInitialize(state, 0)
51+
CAtomicsInitialize(counter, 0)
5152
}
5253

5354
deinit {
54-
state.deallocate()
55+
counter.deallocate()
5556
}
5657

5758
func increment(by value: Int = 1) {
58-
CAtomicsAdd(state, value, .relaxed)
59+
CAtomicsAdd(counter, value, .relaxed)
5960
}
6061
}
6162
```

0 commit comments

Comments
 (0)