Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DO NOT MERGE] Atomic experiments #27229

Closed
wants to merge 48 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
3bb6147
[stdlib] Add FieldAccessor
lorentey Feb 18, 2019
3466ca9
[stdlib] Add primitive atomic operations on UnsafeMutableRawPointer
lorentey Feb 18, 2019
a2ae009
[stdlib] Add some high-level unsafe atomic types
lorentey Feb 18, 2019
c72ac9c
[stdlib] Rewrite _stdlib_AtomicInt on top of the new atomic operations
lorentey Feb 18, 2019
75e440b
[SwiftPrivate] Switch to new atomics
lorentey Feb 18, 2019
3033ea0
[stdlib] Split Atomics.swift.gyb
lorentey Sep 20, 2019
f70f1fd
[stdlib] API adjustments
lorentey Sep 20, 2019
368e992
[stdlib] Fix debug performance
lorentey Sep 20, 2019
712e484
[stdlib] AtomicMemoryOrdering → MemoryOrdering
lorentey Sep 20, 2019
536e058
[stdlib] Add atomic operations for wrapping subtraction and unconditi…
lorentey Sep 26, 2019
7b46ac2
[stdlib] Eliminate MemoryOrdering.sequentiallyConsistent
lorentey Sep 26, 2019
72d0a75
[stdlib] Remove UnsafeAtomicBool
lorentey Sep 26, 2019
6862eff
[stdlib] LegacyAtomicInt: Use sequentially consistent ordering for co…
lorentey Oct 8, 2019
2e07f4f
[stdlib] And/Or/Xor → BitwiseAnd/BitwiseOr/BitwiseXor
lorentey Oct 8, 2019
34d0223
[stdlib] UnsafeAtomicUInt: Define views that override memory ordering…
lorentey Oct 8, 2019
eff3dc0
[stlib] Remove MemoryOrdering, replacing it with ordering views
lorentey Oct 8, 2019
cace4d3
[stdlib] Revert to ordering enums (three of them this time) & swiftch…
lorentey Oct 18, 2019
fb33ed8
[stdlib] Remove default values from ordering parameters
lorentey Oct 18, 2019
74e9fea
[stdlib] Rename AtomicUpdateOrdering.acquiringAndReleasing to .barrier
lorentey Oct 25, 2019
cbec4ae
[stdlib] Add AtomicInitializableReference
lorentey Oct 25, 2019
72bcd0a
[stdlib] Tweak doc comments for compareExchange
lorentey Oct 25, 2019
db71e30
[stdlib] AtomicUInt.exchange: actually use the specified ordering
lorentey Oct 25, 2019
91fdae7
[stdlib] Adjust API names
lorentey Nov 1, 2019
5264dd2
[stdlib] Introduce MemoryLayout.unsafeAddress(of:,in:) as public API
lorentey Nov 6, 2019
dd40fc2
Merge branch 'master' into atomics
lorentey Dec 4, 2019
bc80d04
[stdlib] Revert to UnsafeAtomic* with no anchoring
lorentey Dec 10, 2019
b2b6c6d
[stdlib] Underscore MemoryLayout._unsafeAddress(of:)
lorentey Dec 10, 2019
7204a58
[test] Add trivial functional tests for raw atomics
lorentey Dec 11, 2019
5168e9c
Merge branch 'master' into atomics
lorentey Feb 10, 2020
dd779f1
Remove FieldAccessor & friends
lorentey Feb 14, 2020
8ef7b70
Revert “barrier” ordering to “acquiringAndReleasing”
lorentey Feb 18, 2020
8e15417
Re-add sequentially consistent memory ordering
lorentey Feb 18, 2020
1d584a2
Overhaul compare-and-swap interface
lorentey Feb 19, 2020
ecb6f35
[stdlib] Adjust seq_cst raw atomics naming
lorentey Mar 3, 2020
b316a15
Update compareThenExchange docs to reflect new signature
lorentey Mar 3, 2020
2e9e1f5
[stdlib] Add UnsafeAtomicState
lorentey Mar 3, 2020
70a3f40
[stdlib] compareThenExchange ⟹ compareExchange
lorentey Mar 3, 2020
4d9cb72
[stdlib] Add Value typealiases to all atomic types
lorentey Mar 3, 2020
e525096
[stdlib] Add generalized compareExchange
lorentey Mar 3, 2020
207f5d1
[stdlib] Fix warnings
lorentey Mar 3, 2020
d7d180b
[stdlib] Add atomicMemoryFence(ordering:)
lorentey Mar 3, 2020
e899830
Merge branch 'master' into atomics
lorentey Mar 4, 2020
ae9fa88
[stdlib] Move atomics into a separate module
lorentey Mar 4, 2020
cc763d6
[Atomics] Emit ordering factories into clients
lorentey Mar 4, 2020
399ab5b
[SwiftPrivate] Declare dependency on Atomics
lorentey Mar 4, 2020
fb32464
[Atomics] Add @_semantics attributes to constrain constant orderings
lorentey Mar 4, 2020
759926d
[Atomics] Implement 8/16/32/64-bit atomic integers
lorentey Mar 4, 2020
aba6445
[Atmics] UnsafeAtomicState<T> ⟹ UnsafeAtomic<T>
lorentey Mar 5, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
86 changes: 86 additions & 0 deletions stdlib/private/SwiftPrivate/AtomicInt.swift
@@ -0,0 +1,86 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2018 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

import Swift
@_implementationOnly import Atomics

// This type intentionally shadows the stdlib one
@available(swift, introduced: 5.0)
public final class _stdlib_AtomicInt {
internal var _valueStorage: Int

// FIXME: This should be an UnsafeAtomicInt, but we don't want to constrain
// availability.
internal var _valuePtr: UnsafeMutableRawPointer {
return _getUnsafePointerToStoredProperties(self)
}

public init(_ value: Int = 0) {
_valueStorage = value
}

public func store(_ desired: Int) {
_valuePtr._atomicReleasingStoreWord(UInt(bitPattern: desired))
}

public func load() -> Int {
return Int(bitPattern: _valuePtr._atomicAcquiringLoadWord())
}

@discardableResult
public func fetchAndAdd(_ operand: Int) -> Int {
let word = _valuePtr._atomicSerializedLoadThenWrappingIncrementWord(
by: UInt(bitPattern: operand))
return Int(bitPattern: word)
}
@discardableResult
public func fetchAndAnd(_ operand: Int) -> Int {
let word = _valuePtr._atomicSerializedLoadThenBitwiseAndWord(
UInt(bitPattern: operand))
return Int(bitPattern: word)
}
@discardableResult
public func fetchAndOr(_ operand: Int) -> Int {
let word = _valuePtr._atomicSerializedLoadThenBitwiseOrWord(
UInt(bitPattern: operand))
return Int(bitPattern: word)
}
@discardableResult
public func fetchAndXor(_ operand: Int) -> Int {
let word = _valuePtr._atomicSerializedLoadThenBitwiseXorWord(
UInt(bitPattern: operand))
return Int(bitPattern: word)
}

public func addAndFetch(_ operand: Int) -> Int {
return fetchAndAdd(operand) + operand
}
public func andAndFetch(_ operand: Int) -> Int {
return fetchAndAnd(operand) & operand
}
public func orAndFetch(_ operand: Int) -> Int {
return fetchAndOr(operand) | operand
}
public func xorAndFetch(_ operand: Int) -> Int {
return fetchAndXor(operand) ^ operand
}

public func compareExchange(expected: inout Int, desired: Int) -> Bool {
let expectedWord = UInt(bitPattern: expected)
let (success, originalWord) = _valuePtr._atomicSerializedCompareExchangeWord(
expected: expectedWord,
desired: UInt(bitPattern: desired))
expected = Int(bitPattern: originalWord)
return success
}
}

60 changes: 0 additions & 60 deletions stdlib/private/SwiftPrivate/AtomicInt.swift.gyb

This file was deleted.

5 changes: 2 additions & 3 deletions stdlib/private/SwiftPrivate/CMakeLists.txt
Expand Up @@ -9,12 +9,11 @@ add_swift_target_library(swiftSwiftPrivate ${SWIFT_STDLIB_LIBRARY_BUILD_TYPES} I

IO.swift
ShardedAtomicCounter.swift
AtomicInt.swift

"${SWIFT_SOURCE_DIR}/stdlib/linker-support/magic-symbols-for-install-name.c"

GYB_SOURCES
AtomicInt.swift.gyb

SWIFT_MODULE_DEPENDS Atomics
SWIFT_MODULE_DEPENDS_WINDOWS MSVCRT WinSDK
SWIFT_COMPILE_FLAGS ${swift_swiftprivate_compile_flags} ${SWIFT_STANDARD_LIBRARY_SWIFT_FLAGS}
INSTALL_IN_COMPONENT stdlib-experimental
Expand Down
13 changes: 10 additions & 3 deletions stdlib/private/SwiftPrivate/ShardedAtomicCounter.swift
Expand Up @@ -12,6 +12,7 @@

import Swift
import SwiftShims
@_implementationOnly import Atomics

public func _stdlib_getHardwareConcurrency() -> Int {
// This is effectively a re-export of this shims function
Expand Down Expand Up @@ -49,8 +50,11 @@ public struct _stdlib_ShardedAtomicCounter {

public func add(_ operand: Int, randomInt: Int) {
let shardIndex = Int(UInt(bitPattern: randomInt) % UInt(self._shardsCount))
_ = _swift_stdlib_atomicFetchAddInt(
object: self._shardsPtr + shardIndex, operand: operand)
// FIXME: We should use UnsafeAtomicInt here, but we don't want to constrain
// availability.
let raw = UnsafeMutableRawPointer(self._shardsPtr + shardIndex)
_ = raw._atomicRelaxedLoadThenWrappingIncrementWord(
by: UInt(bitPattern: operand))
}

// FIXME: non-atomic as a whole!
Expand All @@ -59,7 +63,10 @@ public struct _stdlib_ShardedAtomicCounter {
let shards = self._shardsPtr
let count = self._shardsCount
for i in 0..<count {
result += _swift_stdlib_atomicLoadInt(object: shards + i)
// FIXME: We should use UnsafeAtomicInt here, but we don't want to constrain
// availability.
let raw = UnsafeMutableRawPointer(shards + i)
result += Int(bitPattern: raw._atomicRelaxedLoadWord())
}
return result
}
Expand Down