Skip to content

Commit c101e19

Browse files
committed
Switch to top level storage types
1 parent 5343eba commit c101e19

File tree

5 files changed

+67
-54
lines changed

5 files changed

+67
-54
lines changed

Diff for: stdlib/public/Synchronization/AtomicIntegers.swift.gyb

+4-4
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,16 @@ extension ${intType}: AtomicValue {
3131
% if intType == "Int" or intType == "UInt":
3232
#if _pointerBitWidth(_64)
3333
@available(SwiftStdlib 5.11, *)
34-
public typealias AtomicRepresentation = UInt64.AtomicRepresentation
34+
public typealias AtomicRepresentation = _Atomic64BitStorage
3535
#elseif _pointerBitWidth(_32)
3636
@available(SwiftStdlib 5.11, *)
37-
public typealias AtomicRepresentation = UInt32.AtomicRepresentation
37+
public typealias AtomicRepresentation = _Atomic32BitStorage
3838
#else
3939
#error("Unsupported platform")
4040
#endif
41-
% elif not intType.startswith("U"):
41+
% else:
4242
@available(SwiftStdlib 5.11, *)
43-
public typealias AtomicRepresentation = ${intStorage}.AtomicRepresentation
43+
public typealias AtomicRepresentation = ${intStorage}
4444
% end
4545

4646
/// Destroys a value of `Self` and prepares an `AtomicRepresentation` storage

Diff for: stdlib/public/Synchronization/AtomicStorage.swift.gyb

+31-28
Original file line numberDiff line numberDiff line change
@@ -14,37 +14,32 @@ import Builtin
1414

1515
% from SwiftAtomics import *
1616

17-
% for (type, size, alignment, builtin) in atomicTypes:
17+
% for (type, size, alignment, builtin, swift) in atomicTypes:
1818

1919
//===----------------------------------------------------------------------===//
2020
// ${size} bit Atomic Storage
2121
//===----------------------------------------------------------------------===//
2222

23-
% if type == "WordPair":
2423
@available(SwiftStdlib 5.11, *)
25-
% end
26-
extension ${type} {
27-
/// The storage representation type that `Self` encodes to and decodes from
28-
/// which is a suitable type when used in atomic operations.
24+
@frozen
25+
@_alignment(${alignment})
26+
public struct ${type} {
27+
public var _storage: ${builtin}
28+
2929
@available(SwiftStdlib 5.11, *)
30-
@_alignment(${alignment})
31-
@frozen
32-
public struct AtomicRepresentation {
33-
public var _storage: ${builtin}
34-
35-
@available(SwiftStdlib 5.11, *)
36-
@_alwaysEmitIntoClient
37-
@_transparent
38-
public init(_ _builtin: ${builtin}) {
39-
self._storage = _builtin
40-
}
30+
@_alwaysEmitIntoClient
31+
@_transparent
32+
public init(_ _builtin: ${builtin}) {
33+
self._storage = _builtin
4134
}
4235
}
4336

44-
// ${size} bit atomic loads
37+
//===----------------------------------------------------------------------===//
38+
// ${size} bit atomic load
39+
//===----------------------------------------------------------------------===//
4540

4641
@available(SwiftStdlib 5.11, *)
47-
extension Atomic where Value.AtomicRepresentation == ${type}.AtomicRepresentation {
42+
extension Atomic where Value.AtomicRepresentation == ${type} {
4843
/// Atomically loads and returns the current value, applying the specified
4944
/// memory ordering.
5045
///
@@ -69,10 +64,12 @@ extension Atomic where Value.AtomicRepresentation == ${type}.AtomicRepresentatio
6964
}
7065
}
7166

72-
// ${size} bit atomic stores
67+
//===----------------------------------------------------------------------===//
68+
// ${size} bit atomic store
69+
//===----------------------------------------------------------------------===//
7370

7471
@available(SwiftStdlib 5.11, *)
75-
extension Atomic where Value.AtomicRepresentation == ${type}.AtomicRepresentation {
72+
extension Atomic where Value.AtomicRepresentation == ${type} {
7673
/// Atomically sets the current value to `desired`, applying the specified
7774
/// memory ordering.
7875
///
@@ -101,10 +98,12 @@ extension Atomic where Value.AtomicRepresentation == ${type}.AtomicRepresentatio
10198
}
10299
}
103100

101+
//===----------------------------------------------------------------------===//
104102
// ${size} bit atomic exchanges
103+
//===----------------------------------------------------------------------===//
105104

106105
@available(SwiftStdlib 5.11, *)
107-
extension Atomic where Value.AtomicRepresentation == ${type}.AtomicRepresentation {
106+
extension Atomic where Value.AtomicRepresentation == ${type} {
108107
/// Atomically sets the current value to `desired` and returns the original
109108
/// value, applying the specified memory ordering.
110109
///
@@ -137,10 +136,12 @@ extension Atomic where Value.AtomicRepresentation == ${type}.AtomicRepresentatio
137136
}
138137
}
139138

140-
// ${size} bit atomic compare and exchanges
139+
//===----------------------------------------------------------------------===//
140+
// ${size} bit atomic compare exchanges
141+
//===----------------------------------------------------------------------===//
141142

142143
@available(SwiftStdlib 5.11, *)
143-
extension Atomic where Value.AtomicRepresentation == ${type}.AtomicRepresentation {
144+
extension Atomic where Value.AtomicRepresentation == ${type} {
144145
/// Perform an atomic compare and exchange operation on the current value,
145146
/// applying the specified memory ordering.
146147
///
@@ -181,7 +182,7 @@ extension Atomic where Value.AtomicRepresentation == ${type}.AtomicRepresentatio
181182
}
182183

183184
@available(SwiftStdlib 5.11, *)
184-
extension Atomic where Value.AtomicRepresentation == ${type}.AtomicRepresentation {
185+
extension Atomic where Value.AtomicRepresentation == ${type} {
185186
/// Perform an atomic compare and exchange operation on the current value,
186187
/// applying the specified success/failure memory orderings.
187188
///
@@ -246,10 +247,12 @@ extension Atomic where Value.AtomicRepresentation == ${type}.AtomicRepresentatio
246247
}
247248
}
248249

249-
// ${size} bit atomic weak compare and exchanges
250+
//===----------------------------------------------------------------------===//
251+
// ${size} bit weak compare exchanges
252+
//===----------------------------------------------------------------------===//
250253

251254
@available(SwiftStdlib 5.11, *)
252-
extension Atomic where Value.AtomicRepresentation == ${type}.AtomicRepresentation {
255+
extension Atomic where Value.AtomicRepresentation == ${type} {
253256
/// Perform an atomic weak compare and exchange operation on the current
254257
/// value, applying the memory ordering. This compare-exchange variant is
255258
/// allowed to spuriously fail; it is designed to be called in a loop until
@@ -299,7 +302,7 @@ extension Atomic where Value.AtomicRepresentation == ${type}.AtomicRepresentatio
299302
}
300303

301304
@available(SwiftStdlib 5.11, *)
302-
extension Atomic where Value.AtomicRepresentation == ${type}.AtomicRepresentation {
305+
extension Atomic where Value.AtomicRepresentation == ${type} {
303306
/// Perform an atomic weak compare and exchange operation on the current
304307
/// value, applying the specified success/failure memory orderings. This
305308
/// compare-exchange variant is allowed to spuriously fail; it is designed to

Diff for: stdlib/public/Synchronization/WordPair.swift

+11-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,17 @@ public struct WordPair {
6666

6767
@available(SwiftStdlib 5.11, *)
6868
extension WordPair: AtomicValue {
69-
// WordPair's AtomicRepresentation is defined in AtomicStorage.swift.gyb.
69+
#if _pointerBitWidth(_64)
70+
/// The storage representation type that `Self` encodes to and decodes from
71+
/// which is a suitable type when used in atomic operations.
72+
public typealias AtomicRepresentation = _Atomic128BitStorage
73+
#elseif _pointerBitWidth(_32)
74+
/// The storage representation type that `Self` encodes to and decodes from
75+
/// which is a suitable type when used in atomic operations.
76+
public typealias AtomicRepresentation = _Atomic64BitStorage
77+
#else
78+
#error("Unsupported platform")
79+
#endif
7080

7181
/// Destroys a value of `Self` and prepares an `AtomicRepresentation` storage
7282
/// type to be used for atomic operations.

Diff for: test/stdlib/Atomics/Basics/AtomicWordPair.swift.gyb

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
types = [
88
# Label Type a b
9-
("WordPair", "WordPair", "WordPair(highWord: 100, lowWord: 64)", "WordPair(highWord: 50, lowWord: 32)"),
9+
("WordPair", "WordPair", "WordPair(first: 100, second: 64)", "WordPair(first: 50, second: 32)"),
1010
]
1111
}%
1212
${expand("Tests.gyb-template", line_directive='', types=types)}

Diff for: utils/SwiftAtomics.py

+20-20
Original file line numberDiff line numberDiff line change
@@ -11,31 +11,31 @@
1111
#===-----------------------------------------------------------------------===//
1212

1313
atomicTypes = [
14-
# Swift Size Alignment Builtin
15-
("UInt8", "8", "1", "Builtin.Int8"),
16-
("UInt16", "16", "2", "Builtin.Int16"),
17-
("UInt32", "32", "4", "Builtin.Int32"),
18-
("UInt64", "64", "8", "Builtin.Int64"),
19-
("WordPair", "128", "16", "Builtin.Int128"),
14+
# Name Size Alignment Builtin Swift Type
15+
("_Atomic8BitStorage", "8", "1", "Builtin.Int8", "UInt8"),
16+
("_Atomic16BitStorage", "16", "2", "Builtin.Int16", "UInt16"),
17+
("_Atomic32BitStorage", "32", "4", "Builtin.Int32", "UInt32"),
18+
("_Atomic64BitStorage", "64", "8", "Builtin.Int64", "UInt64"),
19+
20+
# Note: WordPair only on 64 bit platforms.
21+
("_Atomic128BitStorage", "128", "16", "Builtin.Int128", "WordPair"),
2022
]
2123

2224
intTypes = [
23-
# Swift Storage Type Builtin
24-
("Int8", "UInt8", "Int8"),
25-
("Int16", "UInt16", "Int16"),
26-
("Int32", "UInt32", "Int32"),
27-
("Int64", "UInt64", "Int64"),
25+
# Swift Storage Type Builtin
26+
("Int8", "_Atomic8BitStorage", "Int8"),
27+
("Int16", "_Atomic16BitStorage", "Int16"),
28+
("Int32", "_Atomic32BitStorage", "Int32"),
29+
("Int64", "_Atomic64BitStorage", "Int64"),
2830

2931
# We handle the word type's storage in source.
30-
("Int", "", "Word"),
31-
("UInt", "", "Word"),
32-
33-
# We handle the unsigned integer's storage as the canonical storage types in
34-
# AtomicStorage.swift.gyb.
35-
("UInt8", "", "Int8"),
36-
("UInt16", "", "Int16"),
37-
("UInt32", "", "Int32"),
38-
("UInt64", "", "Int64"),
32+
("Int", "", ""),
33+
("UInt", "", ""),
34+
35+
("UInt8", "_Atomic8BitStorage", "Int8"),
36+
("UInt16", "_Atomic16BitStorage", "Int16"),
37+
("UInt32", "_Atomic32BitStorage", "Int32"),
38+
("UInt64", "_Atomic64BitStorage", "Int64"),
3939
]
4040

4141
loadOrderings = [

0 commit comments

Comments
 (0)