Atomic operations bridged from Clang to Swift
Swift C Shell Objective-C
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
Sources
Tests
Utilities
Xcode
.gitignore
.travis.yml
LICENSE Create LICENSE Aug 31, 2017
Package.swift make CAtomics externally available Aug 25, 2017
README.md

README.md

swift-atomics Build Status

Some atomic functions from Clang in Swift

The atomic functions available in /usr/include/libkern/OSAtomic.h are quite limiting in Swift, due to the type system. Furthermore, some simple things such as a synchronized load or a synchronized store are not immediately available. On top of that, they have now been deprecated.

Clang, on the other hand, has an implementation of the C11 atomic functions built-in. They're also available on Linux.

This project bridges a subset of Clang's C11 atomics support to Swift.

The following Swift types are implemented:

  • AtomicPointer, AtomicMutablePointer, AtomicRawPointer, AtomicMutableRawPointer and AtomicOpaquePointer;
  • AtomicInt and AtomicUInt, AtomicInt32 and AtomicUInt32, AtomicInt64 and AtomicUInt64;
  • AtomicBool

The pointer types have the following methods:

  • load, store, swap and CAS

The integer types have the following methods:

  • load, store, swap, CAS, add, subtract, increment, decrement, bitwiseAnd, bitwiseOr and bitwiseXor

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

These types should be used as members of reference types, or captured by closures. They are implemented as structs so that using them does not automatically incur additional memory allocation. They should not be used members of another struct, since that would result in violations the law-of-exclusivity.

This library requires Swift 3.0 or later. On Linux, it also requires Clang 3.6 or later.