While Atomic operations are enabled on Swift types including integers and pointer values, with the APIs for atomic operations provided which follows the design principles for Swift APIs.
Albeit, atomics are difficult to use correctly, unlike other low-level concurrency constructs, but the underlying operations work on a very low level of abstraction. Therefore, approach atomic code with extreme caution.
What are the Supported Atomic Types
Atomics enables the creation of higher-level constructs for managing concurrency without having to resort to importing implementation from any other language.
The atomic operations for the following Swift types, all of which conform to public AtomicValue protocol, are as follows:
- Unmanaged references.
- Strong references to class instances that opted into atomic use (by conforming to the AtomicReference protocol).
- Standard pointer types (UnsafeRawPointer, UnsafeMutableRawPointer, along with their optional-wrapped forms.
- Any RawRepresentable type whose RawValue is in turn an atomic type (such as simple custom enum types).
- Standard unsigned integer types (UInt, UInt64, UInt32, UInt16, UInt8).
Additionally, Atomic strong references are implemented in terms of DoubleWord operations, and atomic operations can be performed from multiple, concurrent threads as long as the value is accessed through atomic operations.
How to Get started with Swift Atomics
If you want to get started with Swift Atomics, start by trying out the Atomics library that is publicly available on GitHub.
And there is also an Atomics forum for discussing the technology, and you can get support for atomic floating point operations when requested.
No comments