Skip to content

Commit

Permalink
Update README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
glessard authored May 7, 2019
1 parent 2ede65e commit c88d92b
Showing 1 changed file with 11 additions and 10 deletions.
21 changes: 11 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,38 +24,39 @@ The integer types have the following methods:
`AtomicBool` has the following methods:
- `load`, `store`, `swap`, `CAS`, `and`, `or`, and `xor`.

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

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

### Module CAtomics

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

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

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

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.

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

```swift
import CAtomics

class Example {
private var state = UnsafeMutablePointer<AtomicInt>.allocate(capacity: 1)
private var counter = UnsafeMutablePointer<AtomicInt>.allocate(capacity: 1)
init() {
CAtomicsInitialize(state, 0)
CAtomicsInitialize(counter, 0)
}

deinit {
state.deallocate()
counter.deallocate()
}

func increment(by value: Int = 1) {
CAtomicsAdd(state, value, .relaxed)
CAtomicsAdd(counter, value, .relaxed)
}
}
```
Expand Down

0 comments on commit c88d92b

Please sign in to comment.