Skip to content

Commit 2e6ed71

Browse files
authored
Merge pull request #21434 from moiseev/atomic-int-reshuffle-5
[5.0][stdlib] tee _stdlib_AtomicInt > SwiftPrivate
2 parents cde5369 + ea1b962 commit 2e6ed71

File tree

9 files changed

+151
-139
lines changed

9 files changed

+151
-139
lines changed

stdlib/private/StdlibUnittest/RaceTest.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,7 @@ class _RaceTestSharedState<RT : RaceTestWithPerTrialData> {
395395
var stopNow = _stdlib_AtomicInt(0)
396396

397397
var trialBarrier: _stdlib_Barrier
398-
var trialSpinBarrier: _stdlib_AtomicInt = _stdlib_AtomicInt()
398+
var trialSpinBarrier = _stdlib_AtomicInt()
399399

400400
var raceData: [RT.RaceData] = []
401401
var workerStates: [_RaceTestWorkerState<RT>] = []

stdlib/private/SwiftPrivate/AtomicInt.swift.gyb

Lines changed: 3 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212

1313
import Swift
1414

15+
// This type intentionally shadows the stdlib one
16+
@available(swift, introduced: 5.0)
1517
public final class _stdlib_AtomicInt {
1618
internal var _value: Int
1719

@@ -47,7 +49,7 @@ public final class _stdlib_AtomicInt {
4749

4850
public func compareExchange(expected: inout Int, desired: Int) -> Bool {
4951
var expectedVar = expected
50-
let result = _stdlib_atomicCompareExchangeStrongInt(
52+
let result = _swift_stdlib_atomicCompareExchangeStrongInt(
5153
object: _valuePtr,
5254
expected: &expectedVar,
5355
desired: desired)
@@ -56,74 +58,3 @@ public final class _stdlib_AtomicInt {
5658
}
5759
}
5860

59-
public func _swift_stdlib_atomicLoadInt(
60-
object target: UnsafeMutablePointer<Int>) -> Int {
61-
#if arch(i386) || arch(arm)
62-
let value = Builtin.atomicload_seqcst_Int32(target._rawValue)
63-
return Int(value)
64-
#elseif arch(x86_64) || arch(arm64) || arch(powerpc64) || arch(powerpc64le) || arch(s390x)
65-
let value = Builtin.atomicload_seqcst_Int64(target._rawValue)
66-
return Int(value)
67-
#endif
68-
}
69-
70-
public func _swift_stdlib_atomicStoreInt(
71-
object target: UnsafeMutablePointer<Int>,
72-
desired: Int) {
73-
#if arch(i386) || arch(arm)
74-
Builtin.atomicstore_seqcst_Int32(target._rawValue, desired._value)
75-
#elseif arch(x86_64) || arch(arm64) || arch(powerpc64) || arch(powerpc64le) || arch(s390x)
76-
Builtin.atomicstore_seqcst_Int64(target._rawValue, desired._value)
77-
#endif
78-
}
79-
80-
public func _stdlib_atomicCompareExchangeStrongInt(
81-
object target: UnsafeMutablePointer<Int>,
82-
expected: UnsafeMutablePointer<Int>,
83-
desired: Int) -> Bool {
84-
#if arch(i386) || arch(arm)
85-
let (oldValue, won) = Builtin.cmpxchg_seqcst_seqcst_Int32(
86-
target._rawValue, expected.pointee._value, desired._value)
87-
#elseif arch(x86_64) || arch(arm64) || arch(powerpc64) || arch(powerpc64le) || arch(s390x)
88-
let (oldValue, won) = Builtin.cmpxchg_seqcst_seqcst_Int64(
89-
target._rawValue, expected.pointee._value, desired._value)
90-
#endif
91-
expected.pointee._value = oldValue
92-
return Bool(won)
93-
}
94-
95-
% for operation in ['Add', 'And', 'Or', 'Xor']:
96-
// Warning: no overflow checking.
97-
public func _swift_stdlib_atomicFetch${operation}Int(
98-
object target: UnsafeMutablePointer<Int>,
99-
operand: Int) -> Int {
100-
let rawTarget = UnsafeMutableRawPointer(target)
101-
#if arch(i386) || arch(arm)
102-
let value = _swift_stdlib_atomicFetch${operation}Int32(
103-
object: rawTarget.assumingMemoryBound(to: Int32.self),
104-
operand: Int32(operand))
105-
#elseif arch(x86_64) || arch(arm64) || arch(powerpc64) || arch(powerpc64le) || arch(s390x)
106-
let value = _swift_stdlib_atomicFetch${operation}Int64(
107-
object: rawTarget.assumingMemoryBound(to: Int64.self),
108-
operand: Int64(operand))
109-
#endif
110-
return Int(value)
111-
}
112-
113-
% for bits in [ 32, 64 ]:
114-
115-
// Warning: no overflow checking.
116-
public func _swift_stdlib_atomicFetch${operation}Int${bits}(
117-
object target: UnsafeMutablePointer<Int${bits}>,
118-
operand: Int${bits}) -> Int${bits} {
119-
120-
let value = Builtin.atomicrmw_${operation.lower()}_seqcst_Int${bits}(
121-
target._rawValue, operand._value)
122-
123-
return Int${bits}(value)
124-
}
125-
126-
% end
127-
128-
% end
129-
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2018 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
@available(swift, deprecated: 4.2, obsoleted: 5.0)
14+
public final class _stdlib_AtomicInt {
15+
internal var _value: Int
16+
17+
internal var _valuePtr: UnsafeMutablePointer<Int> {
18+
return _getUnsafePointerToStoredProperties(self).assumingMemoryBound(
19+
to: Int.self)
20+
}
21+
22+
public init(_ value: Int = 0) {
23+
_value = value
24+
}
25+
26+
public func store(_ desired: Int) {
27+
return _swift_stdlib_atomicStoreInt(object: _valuePtr, desired: desired)
28+
}
29+
30+
public func load() -> Int {
31+
return _swift_stdlib_atomicLoadInt(object: _valuePtr)
32+
}
33+
34+
% for operation_name, operation in [ ('Add', '+'), ('And', '&'), ('Or', '|'), ('Xor', '^') ]:
35+
@discardableResult
36+
public func fetchAnd${operation_name}(_ operand: Int) -> Int {
37+
return _swift_stdlib_atomicFetch${operation_name}Int(
38+
object: _valuePtr,
39+
operand: operand)
40+
}
41+
42+
public func ${operation_name.lower()}AndFetch(_ operand: Int) -> Int {
43+
return fetchAnd${operation_name}(operand) ${operation} operand
44+
}
45+
% end
46+
47+
public func compareExchange(expected: inout Int, desired: Int) -> Bool {
48+
var expectedVar = expected
49+
let result = _swift_stdlib_atomicCompareExchangeStrongInt(
50+
object: _valuePtr,
51+
expected: &expectedVar,
52+
desired: desired)
53+
expected = expectedVar
54+
return result
55+
}
56+
}
57+
58+
@usableFromInline // used by SwiftPrivate._stdlib_AtomicInt
59+
internal func _swift_stdlib_atomicCompareExchangeStrongInt(
60+
object target: UnsafeMutablePointer<Int>,
61+
expected: UnsafeMutablePointer<Int>,
62+
desired: Int) -> Bool {
63+
#if arch(i386) || arch(arm)
64+
let (oldValue, won) = Builtin.cmpxchg_seqcst_seqcst_Int32(
65+
target._rawValue, expected.pointee._value, desired._value)
66+
#elseif arch(x86_64) || arch(arm64) || arch(powerpc64) || arch(powerpc64le) || arch(s390x)
67+
let (oldValue, won) = Builtin.cmpxchg_seqcst_seqcst_Int64(
68+
target._rawValue, expected.pointee._value, desired._value)
69+
#endif
70+
expected.pointee._value = oldValue
71+
return Bool(won)
72+
}
73+
74+
75+
// FIXME: ideally it should not be here, at the very least not public, but
76+
// @usableFromInline internal to be used by SwiftPrivate._stdlib_AtomicInt
77+
public // Existing uses outside stdlib
78+
func _swift_stdlib_atomicLoadInt(
79+
object target: UnsafeMutablePointer<Int>) -> Int {
80+
#if arch(i386) || arch(arm)
81+
let value = Builtin.atomicload_seqcst_Int32(target._rawValue)
82+
return Int(value)
83+
#elseif arch(x86_64) || arch(arm64) || arch(powerpc64) || arch(powerpc64le) || arch(s390x)
84+
let value = Builtin.atomicload_seqcst_Int64(target._rawValue)
85+
return Int(value)
86+
#endif
87+
}
88+
89+
@usableFromInline // used by SwiftPrivate._stdlib_AtomicInt
90+
internal func _swift_stdlib_atomicStoreInt(
91+
object target: UnsafeMutablePointer<Int>,
92+
desired: Int) {
93+
#if arch(i386) || arch(arm)
94+
Builtin.atomicstore_seqcst_Int32(target._rawValue, desired._value)
95+
#elseif arch(x86_64) || arch(arm64) || arch(powerpc64) || arch(powerpc64le) || arch(s390x)
96+
Builtin.atomicstore_seqcst_Int64(target._rawValue, desired._value)
97+
#endif
98+
}
99+
100+
% for operation in ['Add', 'And', 'Or', 'Xor']:
101+
// Warning: no overflow checking.
102+
// FIXME: ideally it should not be here, at the very least not public, but
103+
// @usableFromInline internal to be used by SwiftPrivate._stdlib_AtomicInt
104+
public // Existing uses outside stdlib
105+
func _swift_stdlib_atomicFetch${operation}Int(
106+
object target: UnsafeMutablePointer<Int>,
107+
operand: Int) -> Int {
108+
let rawTarget = UnsafeMutableRawPointer(target)
109+
#if arch(i386) || arch(arm)
110+
let value = _swift_stdlib_atomicFetch${operation}Int32(
111+
object: rawTarget.assumingMemoryBound(to: Int32.self),
112+
operand: Int32(operand))
113+
#elseif arch(x86_64) || arch(arm64) || arch(powerpc64) || arch(powerpc64le) || arch(s390x)
114+
let value = _swift_stdlib_atomicFetch${operation}Int64(
115+
object: rawTarget.assumingMemoryBound(to: Int64.self),
116+
operand: Int64(operand))
117+
#endif
118+
return Int(value)
119+
}
120+
121+
% for bits in [ 32, 64 ]:
122+
123+
// Warning: no overflow checking.
124+
@usableFromInline // used by SwiftPrivate._stdlib_AtomicInt
125+
internal func _swift_stdlib_atomicFetch${operation}Int${bits}(
126+
object target: UnsafeMutablePointer<Int${bits}>,
127+
operand: Int${bits}) -> Int${bits} {
128+
129+
let value = Builtin.atomicrmw_${operation.lower()}_seqcst_Int${bits}(
130+
target._rawValue, operand._value)
131+
132+
return Int${bits}(value)
133+
}
134+
135+
% end
136+
137+
% end
138+

stdlib/public/core/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ set(SWIFTLIB_ESSENTIAL
3030
ASCII.swift
3131
Assert.swift
3232
AssertCommon.swift
33+
AtomicInt.swift.gyb
3334
BidirectionalCollection.swift
3435
Bitset.swift
3536
Bool.swift

stdlib/public/core/GroupInfo.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@
197197
"Bitset.swift"
198198
],
199199
"Misc": [
200+
"AtomicInt.swift",
200201
"Interval.swift",
201202
"ErrorType.swift",
202203
"InputStream.swift",

stdlib/public/core/Runtime.swift.gyb

Lines changed: 0 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -114,54 +114,6 @@ func _stdlib_atomicLoadARCRef(
114114
return nil
115115
}
116116

117-
//===----------------------------------------------------------------------===//
118-
// These pieces are used in ThreadLocalStorage.swift in debug builds.
119-
// For tests, see similar functions from SwiftPrivate/AtomicInt.swift.gyb
120-
//===----------------------------------------------------------------------===//
121-
internal func _swift_stdlib_atomicLoadInt(
122-
object target: UnsafeMutablePointer<Int>) -> Int {
123-
#if arch(i386) || arch(arm)
124-
let value = Builtin.atomicload_seqcst_Int32(target._rawValue)
125-
return Int(value)
126-
#elseif arch(x86_64) || arch(arm64) || arch(powerpc64) || arch(powerpc64le) || arch(s390x)
127-
let value = Builtin.atomicload_seqcst_Int64(target._rawValue)
128-
return Int(value)
129-
#endif
130-
}
131-
132-
% for bits in [ 32, 64 ]:
133-
134-
// Warning: no overflow checking.
135-
internal func _swift_stdlib_atomicFetchAddInt${bits}(
136-
object target: UnsafeMutablePointer<Int${bits}>,
137-
operand: Int${bits}) -> Int${bits} {
138-
139-
let value = Builtin.atomicrmw_add_seqcst_Int${bits}(
140-
target._rawValue, operand._value)
141-
142-
return Int${bits}(value)
143-
}
144-
145-
% end
146-
147-
// Warning: no overflow checking.
148-
internal func _swift_stdlib_atomicFetchAddInt(
149-
object target: UnsafeMutablePointer<Int>,
150-
operand: Int) -> Int {
151-
let rawTarget = UnsafeMutableRawPointer(target)
152-
#if arch(i386) || arch(arm)
153-
let value = _swift_stdlib_atomicFetchAddInt32(
154-
object: rawTarget.assumingMemoryBound(to: Int32.self),
155-
operand: Int32(operand))
156-
#elseif arch(x86_64) || arch(arm64) || arch(powerpc64) || arch(powerpc64le) || arch(s390x)
157-
let value = _swift_stdlib_atomicFetchAddInt64(
158-
object: rawTarget.assumingMemoryBound(to: Int64.self),
159-
operand: Int64(operand))
160-
#endif
161-
return Int(value)
162-
}
163-
//===----------------------------------------------------------------------===//
164-
165117
//===----------------------------------------------------------------------===//
166118
// Conversion of primitive types to `String`
167119
//===----------------------------------------------------------------------===//

test/Prototypes/CollectionTransformers.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -641,7 +641,7 @@ final public class ForkJoinPool {
641641
internal let _maxThreads: Int
642642
/// Total number of threads: number of running threads plus the number of
643643
/// threads that are preparing to start).
644-
internal let _totalThreads: _stdlib_AtomicInt = _stdlib_AtomicInt(0)
644+
internal let _totalThreads = _stdlib_AtomicInt(0)
645645

646646
internal var _runningThreads: [_ForkJoinWorkerThread] = []
647647
internal var _runningThreadsMutex: _ForkJoinMutex = _ForkJoinMutex()

test/api-digester/Outputs/stability-stdlib-abi.swift.expected

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -120,39 +120,28 @@ Var _ValidUTF8Buffer.encodedReplacementCharacter has declared type change from _
120120
Var __swift_stdlib_UErrorCode.isFailure has been removed
121121
Var __swift_stdlib_UErrorCode.isSuccess has been removed
122122
Var __swift_stdlib_UErrorCode.isWarning has been removed
123+
Var _stdlib_AtomicInt._value has been removed
124+
Var _stdlib_AtomicInt._valuePtr has been removed
123125

124-
Class _stdlib_AtomicInt has been removed
125-
Func _stdlib_atomicCompareExchangeStrongInt(object:expected:desired:) has been removed
126+
127+
Class _stdlib_AtomicInt is now without @_fixed_layout
128+
Func _stdlib_atomicCompareExchangeStrongInt(object:expected:desired:) has been renamed to Func _swift_stdlib_atomicCompareExchangeStrongInt(object:expected:desired:)
126129
Func _stdlib_atomicCompareExchangeStrongInt32(object:expected:desired:) has been removed
127130
Func _stdlib_atomicCompareExchangeStrongInt64(object:expected:desired:) has been removed
128131
Func _stdlib_atomicCompareExchangeStrongUInt32(object:expected:desired:) has been removed
129132
Func _stdlib_atomicCompareExchangeStrongUInt64(object:expected:desired:) has been removed
130-
Func _swift_stdlib_atomicFetchAddInt(object:operand:) has been removed
131-
Func _swift_stdlib_atomicFetchAddInt32(object:operand:) has been removed
132-
Func _swift_stdlib_atomicFetchAddInt64(object:operand:) has been removed
133133
Func _swift_stdlib_atomicFetchAddUInt32(object:operand:) has been removed
134134
Func _swift_stdlib_atomicFetchAddUInt64(object:operand:) has been removed
135-
Func _swift_stdlib_atomicFetchAndInt(object:operand:) has been removed
136-
Func _swift_stdlib_atomicFetchAndInt32(object:operand:) has been removed
137-
Func _swift_stdlib_atomicFetchAndInt64(object:operand:) has been removed
138135
Func _swift_stdlib_atomicFetchAndUInt32(object:operand:) has been removed
139136
Func _swift_stdlib_atomicFetchAndUInt64(object:operand:) has been removed
140-
Func _swift_stdlib_atomicFetchOrInt(object:operand:) has been removed
141-
Func _swift_stdlib_atomicFetchOrInt32(object:operand:) has been removed
142-
Func _swift_stdlib_atomicFetchOrInt64(object:operand:) has been removed
143137
Func _swift_stdlib_atomicFetchOrUInt32(object:operand:) has been removed
144138
Func _swift_stdlib_atomicFetchOrUInt64(object:operand:) has been removed
145-
Func _swift_stdlib_atomicFetchXorInt(object:operand:) has been removed
146-
Func _swift_stdlib_atomicFetchXorInt32(object:operand:) has been removed
147-
Func _swift_stdlib_atomicFetchXorInt64(object:operand:) has been removed
148139
Func _swift_stdlib_atomicFetchXorUInt32(object:operand:) has been removed
149140
Func _swift_stdlib_atomicFetchXorUInt64(object:operand:) has been removed
150-
Func _swift_stdlib_atomicLoadInt(object:) has been removed
151141
Func _swift_stdlib_atomicLoadInt32(object:) has been removed
152142
Func _swift_stdlib_atomicLoadInt64(object:) has been removed
153143
Func _swift_stdlib_atomicLoadUInt32(object:) has been removed
154144
Func _swift_stdlib_atomicLoadUInt64(object:) has been removed
155-
Func _swift_stdlib_atomicStoreInt(object:desired:) has been removed
156145
Func _swift_stdlib_atomicStoreInt32(object:desired:) has been removed
157146
Func _swift_stdlib_atomicStoreInt64(object:desired:) has been removed
158147
Func _swift_stdlib_atomicStoreUInt32(object:desired:) has been removed

validation-test/stdlib/Prototypes/PersistentVector.swift.gyb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// RUN: %empty-directory(%t)
22
// RUN: %gyb %s -o %t/PersistentVector.swift
3-
// RUN: %line-directive %t/PersistentVector.swift -- %target-build-swift -parse-stdlib %t/PersistentVector.swift -o %t/a.out
3+
// RUN: %line-directive %t/PersistentVector.swift -- %target-build-swift -parse-stdlib -Xfrontend -disable-access-control %t/PersistentVector.swift -o %t/a.out
44
// RUN: %target-codesign %t/a.out
55
// RUN: %line-directive %t/PersistentVector.swift -- %target-run %t/a.out
66
// REQUIRES: executable_test

0 commit comments

Comments
 (0)