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.

Featured

Slideshows

Meet the Reseller News 30 Under 30 Tech Awards 2020 winners

Meet the Reseller News 30 Under 30 Tech Awards 2020 winners

This year’s Reseller News 30 Under 30 Tech Awards were held as an integral part of the first entirely virtual Emerging Leaders​ forum, an annual event dedicated to identifying, educating and showcasing the New Zealand technology market’s rising stars. The 30 Under 30 Tech Awards 2020 recognised the outstanding achievements and business excellence of 30 talented individuals​, across both young leaders and those just starting out. In this slideshow, Reseller News honours this year's winners and captures their thoughts about how their ideas of leadership have changed over time.​

Meet the Reseller News 30 Under 30 Tech Awards 2020 winners
Reseller News Exchange Auckland: Beyond the myths — how partners can master cloud security

Reseller News Exchange Auckland: Beyond the myths — how partners can master cloud security

This exclusive Reseller News Exchange event in Auckland explored the challenges facing the partner community on the cloud security frontier, as well as market trends, customer priorities and how the channel can capitalise on the opportunities available. In association with Arrow, Bitdefender, Exclusive Networks, Fortinet and Palo Alto Networks. Photos by Gino Demeer.

Reseller News Exchange Auckland: Beyond the myths — how partners can master cloud security
Reseller News welcomes industry figures at 2020 Hall of Fame lunch

Reseller News welcomes industry figures at 2020 Hall of Fame lunch

Reseller News welcomed 2019 inductees - Leanne Buer, Ross Jenkins and Terry Dunn - to the fourth running of the Reseller News Hall of Fame lunch, held at the French Cafe in Auckland. The inductees discussed the changing face of the IT channel ecosystem in New Zealand and what it means to be a Reseller News Hall of Fame inductee. Photos by Gino Demeer.

Reseller News welcomes industry figures at 2020 Hall of Fame lunch
Show Comments