Menu
Apple brings low-level atomic operations to Swift language

Apple brings low-level atomic operations to Swift language

Swift Atomics is a new open source package that allows systems programmers to build synchronization constructs in Swift

Credit: Dreamstime

Apple has published Swift Atomics, an open source package that enables systems programmers to build synchronization constructs, such as concurrent data structures, directly in the Swift language.

The Atomics library, introduced October 1 and available on GitHub, enables direct use of low-level atomic operations in Swift. Atomic operations are enabled on a variety of Swift types including integers and pointer values. APIs for atomics operations are provided that follow design principles for Swift APIs.

Apple did offer words of caution: Underlying operations work on a very low level of abstraction. Even more than low-level concurrency constructs, atomics are notoriously difficult to use correctly, said Karoy Larentey, an Apple engineer on the Swift standard library team.

An example of atomic operations was published at swift.org:

import Atomics
import Dispatch
let counter = ManagedAtomic<Int>(0)
DispatchQueue.concurrentPerform(iterations: 10) { _ in
  for _ in 0 ..< 1_000_000 {
    counter.wrappingIncrement(by: 1, ordering: .relaxed)
  }
}
counter.load(ordering: .relaxed) // ⟹ 10_000_000

These operations do not follow normal exclusivity rules for Swift variables, the author noted. Atomic operations can be performed from multiple, concurrent threads of execution as long as the value is only accessed through atomic operations.

Atomics was enabled via a Swift evolution proposal that adopted a C/C++ memory style for Swift and described how regular Swift code interoperates with atomic operations. Most APIs in the new package were derived from previous versions of the evolution proposal.  

Plans for Atomics call for adding more types and improving the current test suite. Tagged atomics is eyed, providing a tool to solve problems with concurrent data structures. An Atomics forum has been set up for discussing the technology. Also, support for atomic floating point operations has been requested.


Follow Us

Join the newsletter!

Or

Sign up to gain exclusive access to email subscriptions, event invitations, competitions, giveaways, and much more.

Membership is free, and your security and privacy remain protected. View our privacy policy before signing up.

Error: Please check your email address.

Events

Why experience is the new battleground for partners

Join us for an exclusive webinar, in association with Hewlett Packard Enterprise and Technology Services Industry Association (TSIA) and learn about the latest industry insights and how technology services continue to evolve to deliver differentiated value, and how partners can be successful in 2021 and beyond.

Featured

Slideshows

Channel kicks 2021 into gear as After Hours returns to Auckland

Channel kicks 2021 into gear as After Hours returns to Auckland

After Hours made a welcome return to the channel social calendar with a bumper crowd of partners, distributors and vendors descending on The Pantry at Park Hyatt in Auckland to kick-start 2021.

Channel kicks 2021 into gear as After Hours returns to Auckland
The Kiwi channel gathers for the 2020 Reseller News Women in ICT Awards

The Kiwi channel gathers for the 2020 Reseller News Women in ICT Awards

Hundreds of leaders from the New Zealand IT industry gathered at the Hilton in Auckland on 17 November to celebrate the finest female talent in the Kiwi channel and recognise the winners of the Reseller News Women in ICT Awards (WIICTA) 2020.

The Kiwi channel gathers for the 2020 Reseller News Women in ICT Awards
Leading female front runners honoured at the 2020 Reseller News Women in ICT Awards

Leading female front runners honoured at the 2020 Reseller News Women in ICT Awards

The leading female front runners of the New Zealand ICT industry joined together for the annual Reseller News Women in ICT Awards event at the Hilton in Auckland, during which hundreds of guests celebrated 13 outstanding individuals who won awards, chosen from more than 50 finalists representing over 30 organisations.

Leading female front runners honoured at the 2020 Reseller News Women in ICT Awards
Show Comments