55

GitHub - glessard/swift-atomics: Atomic operations bridged from Clang to Swift

 6 years ago
source link: https://github.com/glessard/swift-atomics
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

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.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK